MAGMA  1.2.0
MatrixAlgebraonGPUandMulticoreArchitectures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
testing_ssytrd.cpp
Go to the documentation of this file.
1 /*
2  -- MAGMA (version 1.2.0) --
3  Univ. of Tennessee, Knoxville
4  Univ. of California, Berkeley
5  Univ. of Colorado, Denver
6  May 2012
7 
8  @author Stan Tomov
9 
10  @generated s Thu May 10 22:27:33 2012
11 
12 */
13 
14 // includes, system
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <math.h>
19 #include <cuda.h>
20 #include <cuda_runtime_api.h>
21 #include <cublas.h>
22 
23 // includes, project
24 #include "flops.h"
25 #include "magma.h"
26 #include "magma_lapack.h"
27 #include "testings.h"
28 
29 // Flops formula
30 #define PRECISION_s
31 #if defined(PRECISION_z) || defined(PRECISION_c)
32 #define FLOPS(n) ( 6. * FMULS_HETRD(n) + 2. * FADDS_HETRD(n))
33 #else
34 #define FLOPS(n) ( FMULS_HETRD(n) + FADDS_HETRD(n))
35 #endif
36 
37 /* ////////////////////////////////////////////////////////////////////////////
38  -- Testing ssytrd
39 */
40 int main( int argc, char** argv)
41 {
43 
44  magma_timestr_t start, end;
45  float eps, flops, gpu_perf, cpu_perf;
46  float *h_A, *h_R, *h_Q, *h_work, *work;
47  float *tau;
48  float *diag, *offdiag, *rwork;
49  float result[2] = {0., 0.};
50 
51  /* Matrix size */
52  magma_int_t N = 0, n2, lda, lwork;
53  magma_int_t size[10] = {1024,2048,3072,4032,5184,6016,7040,8064,9088,10112};
54 
55  magma_int_t i, info, nb, checkres, once = 0;
56  magma_int_t ione = 1;
57  magma_int_t itwo = 2;
58  magma_int_t ithree = 3;
59  magma_int_t ISEED[4] = {0,0,0,1};
60  char *uplo = (char *)MagmaLowerStr;
61 
62  if (argc != 1){
63  for(i = 1; i<argc; i++){
64  if (strcmp("-N", argv[i])==0) {
65  N = atoi(argv[++i]);
66  once = 1;
67  }
68  else if (strcmp("-U", argv[i])==0)
69  uplo = (char *)MagmaUpperStr;
70  else if (strcmp("-L", argv[i])==0)
71  uplo = (char *)MagmaLowerStr;
72  }
73  if ( N > 0 )
74  printf(" testing_ssytrd -L|U -N %d\n\n", N);
75  else
76  {
77  printf("\nUsage: \n");
78  printf(" testing_ssytrd -L|U -N %d\n\n", 1024);
79  exit(1);
80  }
81  }
82  else {
83  printf("\nUsage: \n");
84  printf(" testing_ssytrd -L|U -N %d\n\n", 1024);
85  N = size[9];
86  }
87 
88  checkres = getenv("MAGMA_TESTINGS_CHECK") != NULL;
89 
90  eps = lapackf77_slamch( "E" );
91  lda = N;
92  n2 = lda * N;
93  nb = magma_get_ssytrd_nb(N);
94  /* We suppose the magma nb is bigger than lapack nb */
95  lwork = N*nb;
96 
97  /* Allocate host memory for the matrix */
98  TESTING_MALLOC( h_A, float, lda*N );
99  TESTING_HOSTALLOC( h_R, float, lda*N );
100  TESTING_HOSTALLOC( h_work, float, lwork );
101  TESTING_MALLOC( tau, float, N );
102  TESTING_MALLOC( diag, float, N );
103  TESTING_MALLOC( offdiag, float, N-1 );
104 
105  /* To avoid uninitialized variable warning */
106  h_Q = NULL;
107  work = NULL;
108  rwork = NULL;
109 
110  if ( checkres ) {
111  TESTING_MALLOC( h_Q, float, lda*N );
112  TESTING_MALLOC( work, float, 2*N*N );
113 #if defined(PRECISION_z) || defined(PRECISION_c)
114  TESTING_MALLOC( rwork, float, N );
115 #endif
116  }
117 
118  printf("\n\n");
119  printf(" N CPU GFlop/s GPU GFlop/s |A-QHQ'|/N|A| |I-QQ'|/N \n");
120  printf("=============================================================\n");
121  for(i=0; i<10; i++){
122  if ( !once ) {
123  N = size[i];
124  }
125  lda = N;
126  n2 = N*lda;
127  flops = FLOPS( (float)N ) / 1e6;
128 
129  /* ====================================================================
130  Initialize the matrix
131  =================================================================== */
132  lapackf77_slarnv( &ione, ISEED, &n2, h_A );
133  /* Make the matrix hermitian */
134  {
135  magma_int_t i, j;
136  for(i=0; i<N; i++) {
137  MAGMA_S_SET2REAL( h_A[i*lda+i], ( MAGMA_S_REAL(h_A[i*lda+i]) ) );
138  for(j=0; j<i; j++)
139  h_A[i*lda+j] = (h_A[j*lda+i]);
140  }
141  }
142  lapackf77_slacpy( MagmaUpperLowerStr, &N, &N, h_A, &lda, h_R, &lda );
143 
144  /* ====================================================================
145  Performs operation using MAGMA
146  =================================================================== */
147  start = get_current_time();
148  magma_ssytrd(uplo[0], N, h_R, lda, diag, offdiag,
149  tau, h_work, lwork, &info);
150  end = get_current_time();
151  if ( info < 0 )
152  printf("Argument %d of magma_ssytrd had an illegal value\n", -info);
153 
154  gpu_perf = flops / GetTimerValue(start,end);
155 
156  /* =====================================================================
157  Check the factorization
158  =================================================================== */
159  if ( checkres ) {
160 
161  lapackf77_slacpy(uplo, &N, &N, h_R, &lda, h_Q, &lda);
162  lapackf77_sorgtr(uplo, &N, h_Q, &lda, tau, h_work, &lwork, &info);
163 
164 #if defined(PRECISION_z) || defined(PRECISION_c)
165  lapackf77_ssyt21(&itwo, uplo, &N, &ione,
166  h_A, &lda, diag, offdiag,
167  h_Q, &lda, h_R, &lda,
168  tau, work, rwork, &result[0]);
169 
170  lapackf77_ssyt21(&ithree, uplo, &N, &ione,
171  h_A, &lda, diag, offdiag,
172  h_Q, &lda, h_R, &lda,
173  tau, work, rwork, &result[1]);
174 
175 #else
176 
177  lapackf77_ssyt21(&itwo, uplo, &N, &ione,
178  h_A, &lda, diag, offdiag,
179  h_Q, &lda, h_R, &lda,
180  tau, work, &result[0]);
181 
182  lapackf77_ssyt21(&ithree, uplo, &N, &ione,
183  h_A, &lda, diag, offdiag,
184  h_Q, &lda, h_R, &lda,
185  tau, work, &result[1]);
186 
187 #endif
188  }
189 
190  /* =====================================================================
191  Performs operation using LAPACK
192  =================================================================== */
193  start = get_current_time();
194  lapackf77_ssytrd(uplo, &N, h_A, &lda, diag, offdiag, tau,
195  h_work, &lwork, &info);
196  end = get_current_time();
197 
198  if (info < 0)
199  printf("Argument %d of lapackf77_ssytrd had an illegal value.\n", -info);
200 
201  cpu_perf = flops / GetTimerValue(start,end);
202 
203  /* =====================================================================
204  Print performance and error.
205  =================================================================== */
206  if ( checkres ) {
207  printf("%5d %6.2f %6.2f %e %e\n",
208  N, cpu_perf, gpu_perf,
209  result[0]*eps, result[1]*eps );
210  } else {
211  printf("%5d %6.2f %6.2f\n",
212  N, cpu_perf, gpu_perf );
213  }
214 
215  if ( once )
216  break;
217  }
218 
219  /* Memory clean up */
220  TESTING_FREE( h_A );
221  TESTING_FREE( tau );
222  TESTING_FREE( diag );
223  TESTING_FREE( offdiag );
224  TESTING_HOSTFREE( h_R );
225  TESTING_HOSTFREE( h_work );
226 
227  if ( checkres ) {
228  TESTING_FREE( h_Q );
229  TESTING_FREE( work );
230 #if defined(PRECISION_z) || defined(PRECISION_c)
231  TESTING_FREE( rwork );
232 #endif
233  }
234 
235  /* Shutdown */
237  return EXIT_SUCCESS;
238 }