File: testing_ssyevd_gpu.cpp

package info (click to toggle)
magma 2.9.0%2Bds-3
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 83,556 kB
  • sloc: cpp: 709,115; fortran: 121,916; ansic: 32,343; python: 25,603; f90: 15,208; makefile: 945; xml: 253; csh: 232; sh: 203; perl: 104
file content (474 lines) | stat: -rw-r--r-- 20,458 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
    -- MAGMA (version 2.9.0) --
       Univ. of Tennessee, Knoxville
       Univ. of California, Berkeley
       Univ. of Colorado, Denver
       @date January 2025

       @author Raffaele Solca
       @author Azzam Haidar
       @author Stan Tomov
       @author Mark Gates

       @generated from testing/testing_zheevd_gpu.cpp, normal z -> s, Wed Jan 22 14:40:36 2025

*/

// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

// includes, project
#include "magma_v2.h"
#include "magma_lapack.h"
#include "magma_operators.h"
#include "testings.h"

#define REAL

/* ////////////////////////////////////////////////////////////////////////////
   -- Testing ssyevd_gpu
*/
int main( int argc, char** argv)
{
    TESTING_CHECK( magma_init() );
    magma_print_environment();

    /* Constants */
    const float d_zero = 0;
    const magma_int_t izero = 0;
    const magma_int_t ione  = 1;
    
    /* Local variables */
    real_Double_t   gpu_time, cpu_time;
    float *h_A, *h_R, *h_Z, *h_work, aux_work[1], unused[1];
    magmaFloat_ptr d_R, d_Z;
    #ifdef COMPLEX
    float *rwork, aux_rwork[1];
    magma_int_t lrwork;
    #endif
    float *w1, *w2, result[4]={0, 0, 0, 0}, eps, abstol, runused[1];
    magma_int_t *iwork, *isuppz, *ifail, aux_iwork[1];
    magma_int_t N, Nfound, info, lwork, liwork, lda, ldda;
    eps = lapackf77_slamch( "E" );
    int status = 0;

    magma_opts opts;
    opts.parse_opts( argc, argv );

    // checking NoVec requires LAPACK
    opts.lapack |= (opts.check && opts.jobz == MagmaNoVec);
    
    float tol    = opts.tolerance * lapackf77_slamch("E");
    float tolulp = opts.tolerance * lapackf77_slamch("P");

    #ifdef REAL
    if (opts.version == 3 || opts.version == 4) {
        printf("%% magma_ssyevr and magma_ssyevx not available for real precisions (single, float).\n");
        status = -1;
        return status;
    }
    #endif

    if (opts.version > 4) {
        fprintf( stderr, "%% error: no version %lld, only 1-4.\n", (long long) opts.version );
        status = -1;
        return status;
    }

    const char *versions[] = {
        "dummy",
        "ssyevd_gpu",
        "ssyevdx_gpu",
        "ssyevr_gpu (Complex only)",
        "ssyevx_gpu (Complex only)"
    };

    printf("%% jobz = %s, uplo = %s, version = %lld (%s)\n",
           lapack_vec_const(opts.jobz), lapack_uplo_const(opts.uplo),
           (long long)opts.version, versions[opts.version] );

    printf("%%   N   CPU Time (sec)   GPU Time (sec)   |S-S_magma|   |A-USU^H|   |I-U^H U|\n");
    printf("%%============================================================================\n");
    for( int itest = 0; itest < opts.ntest; ++itest ) {
        for( int iter = 0; iter < opts.niter; ++iter ) {
            N = opts.nsize[itest];
            Nfound = N;
            lda  = N;
            ldda = magma_roundup( N, opts.align );  // multiple of 32 by default
            abstol = 0;  // auto, in ssyevr
            
            magma_range_t range;
            magma_int_t il, iu;
            float vl, vu;
            opts.get_range( N, &range, &vl, &vu, &il, &iu );

            // query for workspace sizes
            if ( opts.version == 1 || opts.version == 2 ) {
                magma_ssyevd_gpu( opts.jobz, opts.uplo,
                                  N, NULL, ldda, NULL,  // A, w
                                  NULL, lda,            // host A
                                  aux_work,  -1,
                                  #ifdef COMPLEX
                                  aux_rwork, -1,
                                  #endif
                                  aux_iwork, -1,
                                  &info );
                if( opts.version == 2 && opts.jobz == MagmaNoVec ) {
                    // For LAPACK test using ssyevx.
                    #ifdef COMPLEX
                    aux_rwork[0] = float(7*N);
                    #endif
                    aux_iwork[0] = float(5*N);
                }
            }
            else if ( opts.version == 3 ) {
                #ifdef COMPLEX
                magma_ssyevr_gpu( opts.jobz, range, opts.uplo,
                                  N, NULL, ldda,     // A
                                  vl, vu, il, iu, abstol,
                                  &Nfound, NULL,         // w
                                  NULL, ldda, NULL,  // Z, isuppz
                                  NULL, lda,         // host A
                                  NULL, lda,         // host Z
                                  aux_work,  -1,
                                  #ifdef COMPLEX
                                  aux_rwork, -1,
                                  #endif
                                  aux_iwork, -1,
                                  &info );
                #endif
            }
            else if ( opts.version == 4 ) {
                #ifdef COMPLEX
                magma_ssyevx_gpu( opts.jobz, range, opts.uplo,
                                  N, NULL, ldda,     // A
                                  vl, vu, il, iu, abstol,
                                  &Nfound, NULL,         // w
                                  NULL, ldda,        // Z
                                  NULL, lda,         // host A
                                  NULL, lda,         // host Z
                                  aux_work,  -1,
                                  #ifdef COMPLEX
                                  aux_rwork,
                                  #endif
                                  aux_iwork,
                                  NULL,              // ifail
                                  &info );
                // ssyevx doesn't query rwork, iwork; set them for consistency
                aux_rwork[0] = float(7*N);
                aux_iwork[0] = float(5*N);
                #endif
            }
            lwork  = (magma_int_t) MAGMA_S_REAL( aux_work[0] );
            #ifdef COMPLEX
            lrwork = (magma_int_t) aux_rwork[0];
            #endif
            liwork = aux_iwork[0];
            
            /* Allocate host memory for the matrix */
            TESTING_CHECK( magma_smalloc_cpu( &h_A,    N*lda  ));
            TESTING_CHECK( magma_smalloc_cpu( &w1,     N      ));
            TESTING_CHECK( magma_smalloc_cpu( &w2,     N      ));
            #ifdef COMPLEX
            TESTING_CHECK( magma_smalloc_cpu( &rwork,  lrwork ));
            #endif
            TESTING_CHECK( magma_imalloc_cpu( &iwork,  liwork ));
            
            TESTING_CHECK( magma_smalloc_pinned( &h_R,    N*lda  ));
            TESTING_CHECK( magma_smalloc_pinned( &h_work, lwork  ));
            
            TESTING_CHECK( magma_smalloc( &d_R,    N*ldda ));
           
            if (opts.version == 2) {
                TESTING_CHECK( magma_smalloc_cpu( &h_Z,    N*lda      ));
                TESTING_CHECK( magma_imalloc_cpu( &ifail,  N          ));
            } 
            if (opts.version == 3) {
                TESTING_CHECK( magma_smalloc( &d_Z,    N*ldda     ));
                TESTING_CHECK( magma_smalloc_cpu( &h_Z,    N*lda      ));
                TESTING_CHECK( magma_imalloc_cpu( &isuppz, 2*max(1,N) ));
            }
            if (opts.version == 4) {
                TESTING_CHECK( magma_smalloc( &d_Z,    N*ldda     ));
                TESTING_CHECK( magma_smalloc_cpu( &h_Z,    N*lda      ));
                TESTING_CHECK( magma_imalloc_cpu( &ifail,  N          ));
            }
            
            /* Clear eigenvalues, for |S-S_magma| check when fraction < 1. */
            lapackf77_slaset( "Full", &N, &ione, &d_zero, &d_zero, w1, &N );
            lapackf77_slaset( "Full", &N, &ione, &d_zero, &d_zero, w2, &N );
            
            /* Initialize the matrix */
            magma_generate_matrix( opts, N, N, h_A, lda );
            magma_ssetmatrix( N, N, h_A, lda, d_R, ldda, opts.queue );
            
            /* ====================================================================
               Performs operation using MAGMA
               =================================================================== */
            gpu_time = magma_wtime();
            if ( opts.version == 1 ) {
                magma_ssyevd_gpu( opts.jobz, opts.uplo,
                                  N, d_R, ldda, w1,
                                  h_R, lda,
                                  h_work, lwork,
                                  #ifdef COMPLEX
                                  rwork, lrwork,
                                  #endif
                                  iwork, liwork,
                                  &info );
            }
            else if ( opts.version == 2 ) {  // version 2: ssyevdx computes selected eigenvalues/vectors
                magma_ssyevdx_gpu( opts.jobz, range, opts.uplo,
                                   N, d_R, ldda,
                                   vl, vu, il, iu,
                                   &Nfound, w1,
                                   h_R, lda,
                                   h_work, lwork,
                                   #ifdef COMPLEX
                                   rwork, lrwork,
                                   #endif
                                   iwork, liwork,
                                   &info );
                //printf( "il %lld, iu %lld, Nfound %lld\n", (long long) il, (long long) iu, (long long) Nfound );
            }
            else if ( opts.version == 3 ) {  // version 3: MRRR, computes selected eigenvalues/vectors
                // only real version available
                #ifdef COMPLEX
                magma_ssyevr_gpu( opts.jobz, range, opts.uplo,
                                  N, d_R, ldda,
                                  vl, vu, il, iu, abstol,
                                  &Nfound, w1,
                                  d_Z, ldda, isuppz,
                                  h_R, lda,  // host A
                                  h_Z, lda,  // host Z
                                  h_work, lwork,
                                  #ifdef COMPLEX
                                  rwork, lrwork,
                                  #endif
                                  iwork, liwork,
                                  &info );
                magmablas_slacpy( MagmaFull, N, N, d_Z, ldda, d_R, ldda, opts.queue );
                #endif
            }
            else if ( opts.version == 4 ) {  // version 3: ssyevx (QR iteration), computes selected eigenvalues/vectors
                // only real version available
                #ifdef COMPLEX
                magma_ssyevx_gpu( opts.jobz, range, opts.uplo,
                                  N, d_R, ldda,
                                  vl, vu, il, iu, abstol,
                                  &Nfound, w1,
                                  d_Z, ldda,
                                  h_R, lda,
                                  h_Z, lda,
                                  h_work, lwork,
                                  #ifdef COMPLEX
                                  rwork, /*lrwork,*/
                                  #endif
                                  iwork, /*liwork,*/
                                  ifail,
                                  &info );
                magmablas_slacpy( MagmaFull, N, N, d_Z, ldda, d_R, ldda, opts.queue );
                #endif
            }
            gpu_time = magma_wtime() - gpu_time;
            if (info != 0) {
                printf("magma_ssyevd_gpu returned error %lld: %s.\n",
                       (long long) info, magma_strerror( info ));
            }
            
            bool okay = true;
            if ( opts.check && opts.jobz != MagmaNoVec ) {
                /* =====================================================================
                   Check the results following the LAPACK's [zcds]drvst routine.
                   A is factored as A = U S U^H and the following 3 tests computed:
                   (1)    | A - U S U^H | / ( |A| N ) if all eigenvectors were computed
                          | U^H A U - S | / ( |A| Nfound ) otherwise
                   (2)    | I - U^H U   | / ( N )
                   (3)    | S(with U) - S(w/o U) | / | S |    // currently disabled, but compares to LAPACK
                   =================================================================== */
                magma_sgetmatrix( N, N, d_R, ldda, h_R, lda, opts.queue );
                
                float *work;
                TESTING_CHECK( magma_smalloc_cpu( &work, 2*N*N ));
                
                // e is unused since kband=0; tau is unused since itype=1
                if( Nfound == N ) {
                    lapackf77_ssyt21( &ione, lapack_uplo_const(opts.uplo), &N, &izero,
                                      h_A, &lda,
                                      w1, runused,
                                      h_R, &lda,
                                      h_R, &lda,
                                      unused, work,
                                      #ifdef COMPLEX
                                      rwork,
                                      #endif
                                      &result[0] );
                } else {
                    lapackf77_ssyt22( &ione, lapack_uplo_const(opts.uplo), &N, &Nfound, &izero,
                                      h_A, &lda,
                                      w1, runused,
                                      h_R, &lda,
                                      h_R, &lda,
                                      unused, work,
                                      #ifdef COMPLEX
                                      rwork,
                                      #endif
                                      &result[0] );
                }
                result[0] *= eps;
                result[1] *= eps;
                
                magma_free_cpu( work );  work=NULL;
                
                // Disable third eigenvalue check that calls routine again --
                // it obscures whether error occurs in first call above or in this call.
                // But see comparison to LAPACK below.
                //
                //magma_ssetmatrix( N, N, h_A, lda, d_R, ldda, opts.queue );
                //magma_ssyevd_gpu( MagmaNoVec, opts.uplo,
                //                  N, d_R, ldda, w2,
                //                  h_R, lda,
                //                  h_work, lwork,
                //                  #ifdef COMPLEX
                //                  rwork, lrwork,
                //                  #endif
                //                  iwork, liwork,
                //                  &info);
                //if (info != 0) {
                //    printf("magma_ssyevd_gpu returned error %lld: %s.\n",
                //           (long long) info, magma_strerror( info ));
                //}
                //
                //float maxw=0, diff=0;
                //for( int j=0; j < N; j++ ) {
                //    maxw = max(maxw, fabs(w1[j]));
                //    maxw = max(maxw, fabs(w2[j]));
                //    diff = max(diff, fabs(w1[j] - w2[j]));
                //}
                //result[2] = diff / (N*maxw);
            }
            
            /* =====================================================================
               Performs operation using LAPACK
               =================================================================== */
            if ( opts.lapack ) {
                cpu_time = magma_wtime();
                if ( opts.version == 1 ) {
                    lapackf77_ssyevd( lapack_vec_const(opts.jobz), lapack_uplo_const(opts.uplo),
                                      &N, h_A, &lda, w2,
                                      h_work, &lwork,
                                      #ifdef COMPLEX
                                      rwork, &lrwork,
                                      #endif
                                      iwork, &liwork,
                                      &info );
                }
                else if ( opts.version == 2 || opts.version == 4 ) {
                    lapackf77_ssyevx( lapack_vec_const(opts.jobz),
                                      lapack_range_const(range),
                                      lapack_uplo_const(opts.uplo),
                                      &N, h_A, &lda,
                                      &vl, &vu, &il, &iu, &abstol,
                                      &Nfound, w2,
                                      h_Z, &lda,
                                      h_work, &lwork,
                                      #ifdef COMPLEX
                                      rwork,
                                      #endif
                                      iwork,
                                      ifail,
                                      &info );
                    lapackf77_slacpy( "Full", &N, &N, h_Z, &lda, h_A, &lda );
                }
                else if ( opts.version == 3 ) {
                    lapackf77_ssyevr( lapack_vec_const(opts.jobz),
                                      lapack_range_const(range),
                                      lapack_uplo_const(opts.uplo),
                                      &N, h_A, &lda,
                                      &vl, &vu, &il, &iu, &abstol,
                                      &Nfound, w2,
                                      h_Z, &lda, isuppz,
                                      h_work, &lwork,
                                      #ifdef COMPLEX
                                      rwork, &lrwork,
                                      #endif
                                      iwork, &liwork,
                                      &info );
                    lapackf77_slacpy( "Full", &N, &N, h_Z, &lda, h_A, &lda );
                }
                cpu_time = magma_wtime() - cpu_time;
                if (info != 0) {
                    printf("lapackf77_ssyevd returned error %lld: %s.\n",
                           (long long) info, magma_strerror( info ));
                }
                
                // compare eigenvalues
                float maxw=0, diff=0;
                for( int j=0; j < Nfound; j++ ) {
                    maxw = max(maxw, fabs(w1[j]));
                    maxw = max(maxw, fabs(w2[j]));
                    diff = max(diff, fabs(w1[j] - w2[j]));
                }
                result[3] = diff / (N*maxw);
                
                okay = okay && (result[3] < tolulp);
                printf("%5lld   %9.4f        %9.4f         %8.2e  ",
                       (long long) N, cpu_time, gpu_time, result[3] );
            }
            else {
                printf("%5lld      ---           %9.4f           ---     ",
                       (long long) N, gpu_time);
            }
            
            // print error checks
            if ( opts.check && opts.jobz != MagmaNoVec ) {
                okay = okay && (result[0] < tol) && (result[1] < tol);
                printf("    %8.2e    %8.2e", result[0], result[1] );
            }
            else {
                printf("      ---         ---   ");
            }
            printf("   %s\n", (okay ? "ok" : "failed"));
            status += ! okay;

            magma_free_cpu( h_A    );
            magma_free_cpu( w1     );
            magma_free_cpu( w2     );
            #ifdef COMPLEX
            magma_free_cpu( rwork  );
            #endif
            magma_free_cpu( iwork  );
            
            magma_free_pinned( h_R    );
            magma_free_pinned( h_work );
            
            magma_free( d_R );
            
            if ( opts.version == 2 ) {
                magma_free_cpu( h_Z    );
                magma_free_cpu( ifail  );
            }
            if ( opts.version == 3 ) {
                magma_free( d_Z    );
                magma_free_cpu( h_Z    );
                magma_free_cpu( isuppz );
            }
            if ( opts.version == 4 ) {
                magma_free( d_Z    );
                magma_free_cpu( h_Z    );
                magma_free_cpu( ifail  );
            }
            fflush( stdout );
        }
        if ( opts.niter > 1 ) {
            printf( "\n" );
        }
    }
    
    opts.cleanup();
    TESTING_CHECK( magma_finalize() );
    return status;
}