void SVDMatrix_magma(Tensor_core<complex<double>,2>& U, Tensor_core<double,1>& D, Tensor_core<complex<double>,2>& V) { if( U.rank(0)!=U.rank(1) || U.rank(1)!=D.rank(0) || D.rank(0)!=V.rank(0) || V.rank(0)!=V.rank(1) ) { cout<<"Size is not consistent in SVDMatrix_magma! Only support square matrix."<<endl; exit(1); } magma_int_t m=U.rank(0); magma_int_t n=V.rank(0); magma_vec_t jobz(MagmaOverwriteVec); magma_int_t lda=m; magmaDoubleComplex* u=nullptr; magma_int_t ldu=1; magma_int_t ldv=n; magmaDoubleComplex work_test[1]; magma_int_t lwork=-1; double* rwork; magma_int_t* iwork; magma_dmalloc_cpu( &rwork, 5*m*m+7*m ); magma_imalloc_cpu(&iwork, 8*m); magma_int_t info; magma_zgesdd(jobz, m, n, (magmaDoubleComplex *) U.data(), lda, D.data(), u, ldu, (magmaDoubleComplex *) V.data(), ldv, work_test, lwork, rwork, iwork, &info); lwork=lround( MAGMA_Z_REAL(work_test[0]) ); magmaDoubleComplex* work; magma_zmalloc_cpu(&work, lwork); magma_zgesdd(jobz, m, n, (magmaDoubleComplex *) U.data(), lda, D.data(), u, ldu, (magmaDoubleComplex *) V.data(), ldv, work, lwork, rwork, iwork, &info); magma_free_cpu(work); magma_free_cpu(rwork); magma_free_cpu(iwork); if(info!=0) { cout<<"SVDMatrix_magma is not suceesful, info= "<<info<<endl; exit(1); } }
/* //////////////////////////////////////////////////////////////////////////// -- Testing zgesdd (SVD with Divide & Conquer) Please keep code in testing_zgesdd.cpp and testing_zgesvd.cpp similar. */ int main( int argc, char** argv) { TESTING_INIT(); real_Double_t gpu_time, cpu_time; magmaDoubleComplex *h_A, *h_R, *U, *VT, *h_work; magmaDoubleComplex dummy[1]; double *S1, *S2; #ifdef COMPLEX magma_int_t lrwork=0; double *rwork; #endif magma_int_t *iwork; magma_int_t M, N, N_U, M_VT, lda, ldu, ldv, n2, min_mn, max_mn, info, nb, lwork; magma_int_t ione = 1; magma_int_t ISEED[4] = {0,0,0,1}; magma_vec_t jobz; magma_int_t status = 0; MAGMA_UNUSED( max_mn ); // used only in complex magma_opts opts; opts.parse_opts( argc, argv ); double tol = opts.tolerance * lapackf77_dlamch("E"); jobz = opts.jobu; magma_vec_t jobs[] = { MagmaNoVec, MagmaSomeVec, MagmaOverwriteVec, MagmaAllVec }; if ( opts.check && ! opts.all && (jobz == MagmaNoVec)) { printf( "%% NOTE: some checks require that singular vectors are computed;\n" "%% set jobz (option -U[NASO]) to be S, O, or A.\n\n" ); } printf("%% jobz M N CPU time (sec) GPU time (sec) |S1-S2| |A-USV^H| |I-UU^H|/M |I-VV^H|/N S sorted\n"); printf("%%==========================================================================================================\n"); for( int itest = 0; itest < opts.ntest; ++itest ) { for( int ijobz = 0; ijobz < 4; ++ijobz ) { for( int iter = 0; iter < opts.niter; ++iter ) { if ( opts.all ) { jobz = jobs[ ijobz ]; } else if ( ijobz > 0 ) { // if not testing all, run only once, when ijobz = 0, // but jobz come from opts (above loops). continue; } M = opts.msize[itest]; N = opts.nsize[itest]; min_mn = min(M, N); max_mn = max(M, N); N_U = (jobz == MagmaAllVec ? M : min_mn); M_VT = (jobz == MagmaAllVec ? N : min_mn); lda = M; ldu = M; ldv = M_VT; n2 = lda*N; nb = magma_get_zgesvd_nb( M, N ); // x and y abbreviations used in zgesdd and dgesdd documentation magma_int_t x = max(M,N); magma_int_t y = min(M,N); #ifdef COMPLEX bool tall = (x >= int(y*17/9.)); // true if tall (m >> n) or wide (n >> m) #else bool tall = (x >= int(y*11/6.)); // true if tall (m >> n) or wide (n >> m) #endif // query or use formula for workspace size switch( opts.svd_work ) { case 0: { // query for workspace size lwork = -1; magma_zgesdd( jobz, M, N, NULL, lda, NULL, NULL, ldu, NULL, ldv, dummy, lwork, #ifdef COMPLEX NULL, #endif NULL, &info ); lwork = (int) MAGMA_Z_REAL( dummy[0] ); break; } case 1: // minimum case 2: // optimal case 3: { // optimal (for gesdd, 2 & 3 are same; for gesvd, they differ) // formulas from zgesdd and dgesdd documentation bool sml = (opts.svd_work == 1); // 1 is small workspace, 2,3 are large workspace #ifdef COMPLEX // ---------------------------------------- if (jobz == MagmaNoVec) { if (tall) { lwork = 2*y + (2*y)*nb; } else { lwork = 2*y + (x+y)*nb; } } if (jobz == MagmaOverwriteVec) { if (tall) { if (sml) { lwork = 2*y*y + 2*y + (2*y)*nb; } else { lwork = y*y + x*y + 2*y + (2*y)*nb; } // not big deal } else { //if (sml) { lwork = 2*y + max( (x+y)*nb, y*y + y ); } //else { lwork = 2*y + max( (x+y)*nb, x*y + y*nb ); } // LAPACK 3.4.2 over-estimates workspaces. For compatability, use these: if (sml) { lwork = 2*y + max( (x+y)*nb, y*y + x ); } else { lwork = 2*y + (x+y)*nb + x*y; } } } if (jobz == MagmaSomeVec) { if (tall) { lwork = y*y + 2*y + (2*y)*nb; } else { lwork = 2*y + (x+y)*nb; } } if (jobz == MagmaAllVec) { if (tall) { if (sml) { lwork = y*y + 2*y + max( (2*y)*nb, x ); } else { lwork = y*y + 2*y + max( (2*y)*nb, x*nb ); } } else { lwork = 2*y + (x+y)*nb; } } #else // REAL ---------------------------------------- if (jobz == MagmaNoVec) { if (tall) { lwork = 3*y + max( (2*y)*nb, 7*y ); } else { lwork = 3*y + max( (x+y)*nb, 7*y ); } } if (jobz == MagmaOverwriteVec) { if (tall) { if (sml) { lwork = y*y + 3*y + max( (2*y)*nb, 4*y*y + 4*y ); } else { lwork = y*y + 3*y + max( max( (2*y)*nb, 4*y*y + 4*y ), y*y + y*nb ); } } else { if (sml) { lwork = 3*y + max( (x+y)*nb, 4*y*y + 4*y ); } else { lwork = 3*y + max( (x+y)*nb, 3*y*y + 4*y + x*y ); } // extra space not too important? } } if (jobz == MagmaSomeVec) { if (tall) { lwork = y*y + 3*y + max( (2*y)*nb, 3*y*y + 4*y ); } else { lwork = 3*y + max( (x+y)*nb, 3*y*y + 4*y ); } } if (jobz == MagmaAllVec) { if (tall) { if (sml) { lwork = y*y + max( 3*y + max( (2*y)*nb, 3*y*y + 4*y ), y + x ); } else { lwork = y*y + max( 3*y + max( (2*y)*nb, 3*y*y + 4*y ), y + x*nb ); } // LAPACK 3.4.2 over-estimates workspaces. For compatability, use these: //if (sml) { lwork = y*y + 3*y + max( (2*y)*nb, 3*y*y + 3*y + x ); } //else { lwork = y*y + max( 3*y + max( (2*y)*nb, max( 3*y*y + 3*y + x, 3*y*y + 4*y )), y + x*nb ); } } else { lwork = 3*y + max( (x+y)*nb, 3*y*y + 4*y ); } } #endif break; } default: { fprintf( stderr, "Error: unknown option svd_work %d\n", (int) opts.svd_work ); return -1; break; } } TESTING_MALLOC_CPU( h_A, magmaDoubleComplex, lda*N ); TESTING_MALLOC_CPU( VT, magmaDoubleComplex, ldv*N ); // N x N (jobz=A) or min(M,N) x N TESTING_MALLOC_CPU( U, magmaDoubleComplex, ldu*N_U ); // M x M (jobz=A) or M x min(M,N) TESTING_MALLOC_CPU( S1, double, min_mn ); TESTING_MALLOC_CPU( S2, double, min_mn ); TESTING_MALLOC_CPU( iwork, magma_int_t, 8*min_mn ); TESTING_MALLOC_PIN( h_R, magmaDoubleComplex, lda*N ); TESTING_MALLOC_PIN( h_work, magmaDoubleComplex, lwork ); #ifdef COMPLEX if (jobz == MagmaNoVec) { // requires 5*min_mn, but MKL (11.1) seems to have a bug // requiring 7*min_mn in some cases (e.g., jobz=N, m=100, n=170) lrwork = 7*min_mn; } else if (tall) { lrwork = 5*min_mn*min_mn + 5*min_mn; } else { lrwork = max( 5*min_mn*min_mn + 5*min_mn, 2*max_mn*min_mn + 2*min_mn*min_mn + min_mn ); } TESTING_MALLOC_CPU( rwork, double, lrwork ); #endif /* Initialize the matrix */ lapackf77_zlarnv( &ione, ISEED, &n2, h_A ); lapackf77_zlacpy( MagmaFullStr, &M, &N, h_A, &lda, h_R, &lda ); /* ==================================================================== Performs operation using MAGMA =================================================================== */ gpu_time = magma_wtime(); magma_zgesdd( jobz, M, N, h_R, lda, S1, U, ldu, VT, ldv, h_work, lwork, #ifdef COMPLEX rwork, #endif iwork, &info ); gpu_time = magma_wtime() - gpu_time; if (info != 0) { printf("magma_zgesdd returned error %d: %s.\n", (int) info, magma_strerror( info )); } double eps = lapackf77_dlamch( "E" ); double result[5] = { -1/eps, -1/eps, -1/eps, -1/eps, -1/eps }; if ( opts.check ) { /* ===================================================================== Check the results following the LAPACK's [zcds]drvbd routine. A is factored as A = U diag(S) VT and the following 4 tests computed: (1) | A - U diag(S) VT | / ( |A| max(M,N) ) (2) | I - U^H U | / ( M ) (3) | I - VT VT^H | / ( N ) (4) S contains MNMIN nonnegative values in decreasing order. (Return 0 if true, 1/ULP if false.) =================================================================== */ magma_int_t izero = 0; // get size and location of U and V^T depending on jobz // U2=NULL and VT2=NULL if they were not computed (e.g., jobz=N) magmaDoubleComplex *U2 = NULL; magmaDoubleComplex *VT2 = NULL; if ( jobz == MagmaSomeVec || jobz == MagmaAllVec ) { U2 = U; VT2 = VT; } else if ( jobz == MagmaOverwriteVec ) { if ( M >= N ) { U2 = h_R; ldu = lda; VT2 = VT; } else { U2 = U; VT2 = h_R; ldv = lda; } } // zbdt01 needs M+N // zunt01 prefers N*(N+1) to check U; M*(M+1) to check V magma_int_t lwork_err = M+N; if ( U2 != NULL ) { lwork_err = max( lwork_err, N_U*(N_U+1) ); } if ( VT2 != NULL ) { lwork_err = max( lwork_err, M_VT*(M_VT+1) ); } magmaDoubleComplex *h_work_err; TESTING_MALLOC_CPU( h_work_err, magmaDoubleComplex, lwork_err ); // zbdt01 and zunt01 need max(M,N), depending double *rwork_err; TESTING_MALLOC_CPU( rwork_err, double, max(M,N) ); if ( U2 != NULL && VT2 != NULL ) { // since KD=0 (3rd arg), E is not referenced so pass NULL (9th arg) lapackf77_zbdt01(&M, &N, &izero, h_A, &lda, U2, &ldu, S1, NULL, VT2, &ldv, h_work_err, #ifdef COMPLEX rwork_err, #endif &result[0]); } if ( U2 != NULL ) { lapackf77_zunt01("Columns", &M, &N_U, U2, &ldu, h_work_err, &lwork_err, #ifdef COMPLEX rwork_err, #endif &result[1]); } if ( VT2 != NULL ) { lapackf77_zunt01("Rows", &M_VT, &N, VT2, &ldv, h_work_err, &lwork_err, #ifdef COMPLEX rwork_err, #endif &result[2]); } result[3] = 0.; for (int j=0; j < min_mn-1; j++) { if ( S1[j] < S1[j+1] ) result[3] = 1.; if ( S1[j] < 0. ) result[3] = 1.; } if (min_mn > 1 && S1[min_mn-1] < 0.) result[3] = 1.; result[0] *= eps; result[1] *= eps; result[2] *= eps; TESTING_FREE_CPU( h_work_err ); TESTING_FREE_CPU( rwork_err ); } /* ===================================================================== Performs operation using LAPACK =================================================================== */ if ( opts.lapack ) { cpu_time = magma_wtime(); lapackf77_zgesdd( lapack_vec_const(jobz), &M, &N, h_A, &lda, S2, U, &ldu, VT, &ldv, h_work, &lwork, #ifdef COMPLEX rwork, #endif iwork, &info); cpu_time = magma_wtime() - cpu_time; if (info != 0) { printf("lapackf77_zgesdd returned error %d: %s.\n", (int) info, magma_strerror( info )); } /* ===================================================================== Check the result compared to LAPACK =================================================================== */ double work[1], c_neg_one = -1; blasf77_daxpy(&min_mn, &c_neg_one, S1, &ione, S2, &ione); result[4] = lapackf77_dlange("f", &min_mn, &ione, S2, &min_mn, work); result[4] /= lapackf77_dlange("f", &min_mn, &ione, S1, &min_mn, work); printf(" %c %5d %5d %7.2f %7.2f %8.2e", lapack_vec_const(jobz)[0], (int) M, (int) N, cpu_time, gpu_time, result[4] ); } else { printf(" %c %5d %5d --- %7.2f --- ", lapack_vec_const(jobz)[0], (int) M, (int) N, gpu_time ); } if ( opts.check ) { if ( result[0] < 0. ) { printf(" --- "); } else { printf(" %#9.3g", result[0]); } if ( result[1] < 0. ) { printf(" --- "); } else { printf(" %#9.3g", result[1]); } if ( result[2] < 0. ) { printf(" --- "); } else { printf(" %#9.3g", result[2]); } bool okay = (result[0] < tol) && (result[1] < tol) && (result[2] < tol) && (result[3] == 0.) && (result[4] < tol); printf(" %3s %s\n", (result[3] == 0. ? "yes" : "no"), (okay ? "ok" : "failed")); status += ! okay; } else { printf("\n"); } TESTING_FREE_CPU( h_A ); TESTING_FREE_CPU( VT ); TESTING_FREE_CPU( U ); TESTING_FREE_CPU( S1 ); TESTING_FREE_CPU( S2 ); TESTING_FREE_CPU( iwork ); #ifdef COMPLEX TESTING_FREE_CPU( rwork ); #endif TESTING_FREE_PIN( h_R ); TESTING_FREE_PIN( h_work ); fflush( stdout ); }} if ( opts.all || opts.niter > 1 ) { printf("\n"); } } opts.cleanup(); TESTING_FINALIZE(); return status; }