/*--------------------------------------------------------------------*/ int main ( int argc, char *argv[] ) /* ------------------------------------------------------------------ generate a random matrix and test a matrix-matrix multiply method. the output is a matlab file to test correctness. created -- 98jan29, cca -------------------------------------------------------------------- */ { DenseMtx *X, *Y, *Y2 ; double alpha[2] ; double alphaImag, alphaReal, t1, t2 ; double *zvec ; Drand *drand ; int col, dataType, ii, msglvl, ncolA, nitem, nops, nrhs, nrowA, nrowX, nrowY, nthread, row, seed, storageMode, symflag, transposeflag ; int *colids, *rowids ; InpMtx *A ; FILE *msgFile ; if ( argc != 15 ) { fprintf(stdout, "\n\n %% usage : %s msglvl msgFile symflag storageMode " "\n %% nrow ncol nent nrhs seed alphaReal alphaImag nthread" "\n %% msglvl -- message level" "\n %% msgFile -- message file" "\n %% dataType -- type of matrix entries" "\n %% 1 -- real" "\n %% 2 -- complex" "\n %% symflag -- symmetry flag" "\n %% 0 -- symmetric" "\n %% 1 -- hermitian" "\n %% 2 -- nonsymmetric" "\n %% storageMode -- storage mode" "\n %% 1 -- by rows" "\n %% 2 -- by columns" "\n %% 3 -- by chevrons, (requires nrow = ncol)" "\n %% transpose -- transpose flag" "\n %% 0 -- Y := Y + alpha * A * X" "\n %% 1 -- Y := Y + alpha * A^H * X, nonsymmetric only" "\n %% 2 -- Y := Y + alpha * A^T * X, nonsymmetric only" "\n %% nrowA -- number of rows in A" "\n %% ncolA -- number of columns in A" "\n %% nitem -- number of items" "\n %% nrhs -- number of right hand sides" "\n %% seed -- random number seed" "\n %% alphaReal -- y := y + alpha*A*x" "\n %% alphaImag -- y := y + alpha*A*x" "\n %% nthread -- # of threads" "\n", argv[0]) ; return(0) ; } msglvl = atoi(argv[1]) ; if ( strcmp(argv[2], "stdout") == 0 ) { msgFile = stdout ; } else if ( (msgFile = fopen(argv[2], "a")) == NULL ) { fprintf(stderr, "\n fatal error in %s" "\n unable to open file %s\n", argv[0], argv[2]) ; return(-1) ; } dataType = atoi(argv[3]) ; symflag = atoi(argv[4]) ; storageMode = atoi(argv[5]) ; transposeflag = atoi(argv[6]) ; nrowA = atoi(argv[7]) ; ncolA = atoi(argv[8]) ; nitem = atoi(argv[9]) ; nrhs = atoi(argv[10]) ; seed = atoi(argv[11]) ; alphaReal = atof(argv[12]) ; alphaImag = atof(argv[13]) ; nthread = atoi(argv[14]) ; fprintf(msgFile, "\n %% %s " "\n %% msglvl -- %d" "\n %% msgFile -- %s" "\n %% dataType -- %d" "\n %% symflag -- %d" "\n %% storageMode -- %d" "\n %% transposeflag -- %d" "\n %% nrowA -- %d" "\n %% ncolA -- %d" "\n %% nitem -- %d" "\n %% nrhs -- %d" "\n %% seed -- %d" "\n %% alphaReal -- %e" "\n %% alphaImag -- %e" "\n %% nthread -- %d" "\n", argv[0], msglvl, argv[2], dataType, symflag, storageMode, transposeflag, nrowA, ncolA, nitem, nrhs, seed, alphaReal, alphaImag, nthread) ; fflush(msgFile) ; if ( dataType != 1 && dataType != 2 ) { fprintf(stderr, "\n invalid value %d for dataType\n", dataType) ; spoolesFatal(); } if ( symflag != 0 && symflag != 1 && symflag != 2 ) { fprintf(stderr, "\n invalid value %d for symflag\n", symflag) ; spoolesFatal(); } if ( storageMode != 1 && storageMode != 2 && storageMode != 3 ) { fprintf(stderr, "\n invalid value %d for storageMode\n", storageMode) ; spoolesFatal(); } if ( transposeflag < 0 || transposeflag > 2 ) { fprintf(stderr, "\n error, transposeflag = %d, must be 0, 1 or 2", transposeflag) ; spoolesFatal(); } if ( (transposeflag == 1 && symflag != 2) || (transposeflag == 2 && symflag != 2) ) { fprintf(stderr, "\n error, transposeflag = %d, symflag = %d", transposeflag, symflag) ; spoolesFatal(); } if ( transposeflag == 1 && dataType != 2 ) { fprintf(stderr, "\n error, transposeflag = %d, dataType = %d", transposeflag, dataType) ; spoolesFatal(); } if ( symflag == 1 && dataType != 2 ) { fprintf(stderr, "\n symflag = 1 (hermitian), dataType != 2 (complex)") ; spoolesFatal(); } if ( nrowA <= 0 || ncolA <= 0 || nitem <= 0 ) { fprintf(stderr, "\n invalid value: nrow = %d, ncol = %d, nitem = %d", nrowA, ncolA, nitem) ; spoolesFatal(); } if ( symflag < 2 && nrowA != ncolA ) { fprintf(stderr, "\n invalid data: symflag = %d, nrow = %d, ncol = %d", symflag, nrowA, ncolA) ; spoolesFatal(); } alpha[0] = alphaReal ; alpha[1] = alphaImag ; /* ---------------------------- initialize the matrix object ---------------------------- */ A = InpMtx_new() ; InpMtx_init(A, storageMode, dataType, 0, 0) ; drand = Drand_new() ; /* ---------------------------------- generate a vector of nitem triples ---------------------------------- */ rowids = IVinit(nitem, -1) ; Drand_setUniform(drand, 0, nrowA) ; Drand_fillIvector(drand, nitem, rowids) ; colids = IVinit(nitem, -1) ; Drand_setUniform(drand, 0, ncolA) ; Drand_fillIvector(drand, nitem, colids) ; Drand_setUniform(drand, 0.0, 1.0) ; if ( INPMTX_IS_REAL_ENTRIES(A) ) { zvec = DVinit(nitem, 0.0) ; Drand_fillDvector(drand, nitem, zvec) ; } else if ( INPMTX_IS_COMPLEX_ENTRIES(A) ) { zvec = ZVinit(nitem, 0.0, 0.0) ; Drand_fillDvector(drand, 2*nitem, zvec) ; } /* ----------------------------------- assemble the entries entry by entry ----------------------------------- */ if ( msglvl > 1 ) { fprintf(msgFile, "\n\n A = zeros(%d,%d) ;", nrowA, ncolA) ; } if ( symflag == 1 ) { /* ---------------- hermitian matrix ---------------- */ for ( ii = 0 ; ii < nitem ; ii++ ) { if ( rowids[ii] == colids[ii] ) { zvec[2*ii+1] = 0.0 ; } if ( rowids[ii] <= colids[ii] ) { row = rowids[ii] ; col = colids[ii] ; } else { row = colids[ii] ; col = rowids[ii] ; } InpMtx_inputComplexEntry(A, row, col, zvec[2*ii], zvec[2*ii+1]) ; } } else if ( symflag == 0 ) { /* ---------------- symmetric matrix ---------------- */ if ( INPMTX_IS_REAL_ENTRIES(A) ) { for ( ii = 0 ; ii < nitem ; ii++ ) { if ( rowids[ii] <= colids[ii] ) { row = rowids[ii] ; col = colids[ii] ; } else { row = colids[ii] ; col = rowids[ii] ; } InpMtx_inputRealEntry(A, row, col, zvec[ii]) ; } } else if ( INPMTX_IS_COMPLEX_ENTRIES(A) ) { for ( ii = 0 ; ii < nitem ; ii++ ) { if ( rowids[ii] <= colids[ii] ) { row = rowids[ii] ; col = colids[ii] ; } else { row = colids[ii] ; col = rowids[ii] ; } InpMtx_inputComplexEntry(A, row, col, zvec[2*ii], zvec[2*ii+1]) ; } } } else { /* ------------------- nonsymmetric matrix ------------------- */ if ( INPMTX_IS_REAL_ENTRIES(A) ) { for ( ii = 0 ; ii < nitem ; ii++ ) { InpMtx_inputRealEntry(A, rowids[ii], colids[ii], zvec[ii]) ; } } else if ( INPMTX_IS_COMPLEX_ENTRIES(A) ) { for ( ii = 0 ; ii < nitem ; ii++ ) { InpMtx_inputComplexEntry(A, rowids[ii], colids[ii], zvec[2*ii], zvec[2*ii+1]) ; } } } InpMtx_changeStorageMode(A, INPMTX_BY_VECTORS) ; DVfree(zvec) ; if ( symflag == 0 || symflag == 1 ) { if ( INPMTX_IS_REAL_ENTRIES(A) ) { nops = 4*A->nent*nrhs ; } else if ( INPMTX_IS_COMPLEX_ENTRIES(A) ) { nops = 16*A->nent*nrhs ; } } else { if ( INPMTX_IS_REAL_ENTRIES(A) ) { nops = 2*A->nent*nrhs ; } else if ( INPMTX_IS_COMPLEX_ENTRIES(A) ) { nops = 8*A->nent*nrhs ; } } if ( msglvl > 1 ) { /* ------------------------------------------- write the assembled matrix to a matlab file ------------------------------------------- */ InpMtx_writeForMatlab(A, "A", msgFile) ; if ( symflag == 0 ) { fprintf(msgFile, "\n for k = 1:%d" "\n for j = k+1:%d" "\n A(j,k) = A(k,j) ;" "\n end" "\n end", nrowA, ncolA) ; } else if ( symflag == 1 ) { fprintf(msgFile, "\n for k = 1:%d" "\n for j = k+1:%d" "\n A(j,k) = ctranspose(A(k,j)) ;" "\n end" "\n end", nrowA, ncolA) ; } } /* ------------------------------- generate dense matrices X and Y ------------------------------- */ if ( transposeflag == 0 ) { nrowX = ncolA ; nrowY = nrowA ; } else { nrowX = nrowA ; nrowY = ncolA ; } X = DenseMtx_new() ; Y = DenseMtx_new() ; Y2 = DenseMtx_new() ; if ( INPMTX_IS_REAL_ENTRIES(A) ) { DenseMtx_init(X, SPOOLES_REAL, 0, 0, nrowX, nrhs, 1, nrowX) ; Drand_fillDvector(drand, nrowX*nrhs, DenseMtx_entries(X)) ; DenseMtx_init(Y, SPOOLES_REAL, 0, 0, nrowY, nrhs, 1, nrowY) ; Drand_fillDvector(drand, nrowY*nrhs, DenseMtx_entries(Y)) ; DenseMtx_init(Y2, SPOOLES_REAL, 0, 0, nrowY, nrhs, 1, nrowY) ; DVcopy(nrowY*nrhs, DenseMtx_entries(Y2), DenseMtx_entries(Y)) ; } else if ( INPMTX_IS_COMPLEX_ENTRIES(A) ) { DenseMtx_init(X, SPOOLES_COMPLEX, 0, 0, nrowX, nrhs, 1, nrowX) ; Drand_fillDvector(drand, 2*nrowX*nrhs, DenseMtx_entries(X)) ; DenseMtx_init(Y, SPOOLES_COMPLEX, 0, 0, nrowY, nrhs, 1, nrowY) ; Drand_fillDvector(drand, 2*nrowY*nrhs, DenseMtx_entries(Y)) ; DenseMtx_init(Y2, SPOOLES_COMPLEX, 0, 0, nrowY, nrhs, 1, nrowY) ; DVcopy(2*nrowY*nrhs, DenseMtx_entries(Y2), DenseMtx_entries(Y)) ; } if ( msglvl > 1 ) { fprintf(msgFile, "\n X = zeros(%d,%d) ;", nrowX, nrhs) ; DenseMtx_writeForMatlab(X, "X", msgFile) ; fprintf(msgFile, "\n Y = zeros(%d,%d) ;", nrowY, nrhs) ; DenseMtx_writeForMatlab(Y, "Y", msgFile) ; } /* -------------------------------------------- perform the matrix-matrix multiply in serial -------------------------------------------- */ if ( msglvl > 1 ) { fprintf(msgFile, "\n alpha = %20.12e + %20.2e*i;", alpha[0], alpha[1]); fprintf(msgFile, "\n Z = zeros(%d,1) ;", nrowY) ; } if ( transposeflag == 0 ) { MARKTIME(t1) ; if ( symflag == 0 ) { InpMtx_sym_mmm(A, Y, alpha, X) ; } else if ( symflag == 1 ) { InpMtx_herm_mmm(A, Y, alpha, X) ; } else if ( symflag == 2 ) { InpMtx_nonsym_mmm(A, Y, alpha, X) ; } MARKTIME(t2) ; if ( msglvl > 1 ) { DenseMtx_writeForMatlab(Y, "Z", msgFile) ; fprintf(msgFile, "\n maxerr = max(Z - Y - alpha*A*X) ") ; fprintf(msgFile, "\n") ; } } else if ( transposeflag == 1 ) { MARKTIME(t1) ; InpMtx_nonsym_mmm_H(A, Y, alpha, X) ; MARKTIME(t2) ; if ( msglvl > 1 ) { DenseMtx_writeForMatlab(Y, "Z", msgFile) ; fprintf(msgFile, "\n maxerr = max(Z - Y - alpha*ctranspose(A)*X) ") ; fprintf(msgFile, "\n") ; } } else if ( transposeflag == 2 ) { MARKTIME(t1) ; InpMtx_nonsym_mmm_T(A, Y, alpha, X) ; MARKTIME(t2) ; if ( msglvl > 1 ) { DenseMtx_writeForMatlab(Y, "Z", msgFile) ; fprintf(msgFile, "\n maxerr = max(Z - Y - alpha*transpose(A)*X) ") ; fprintf(msgFile, "\n") ; } } fprintf(msgFile, "\n %% %d ops, %.3f time, %.3f serial mflops", nops, t2 - t1, 1.e-6*nops/(t2 - t1)) ; /* -------------------------------------------------------- perform the matrix-matrix multiply in multithreaded mode -------------------------------------------------------- */ if ( msglvl > 1 ) { fprintf(msgFile, "\n alpha = %20.12e + %20.2e*i;", alpha[0], alpha[1]); fprintf(msgFile, "\n Z = zeros(%d,1) ;", nrowY) ; } if ( transposeflag == 0 ) { MARKTIME(t1) ; if ( symflag == 0 ) { InpMtx_MT_sym_mmm(A, Y2, alpha, X, nthread, msglvl, msgFile) ; } else if ( symflag == 1 ) { InpMtx_MT_herm_mmm(A, Y2, alpha, X, nthread, msglvl, msgFile) ; } else if ( symflag == 2 ) { InpMtx_MT_nonsym_mmm(A, Y2, alpha, X, nthread, msglvl, msgFile) ; } MARKTIME(t2) ; if ( msglvl > 1 ) { DenseMtx_writeForMatlab(Y2, "Z2", msgFile) ; fprintf(msgFile, "\n maxerr2 = max(Z2 - Y - alpha*A*X) ") ; fprintf(msgFile, "\n") ; } } else if ( transposeflag == 1 ) { MARKTIME(t1) ; InpMtx_MT_nonsym_mmm_H(A, Y2, alpha, X, nthread, msglvl, msgFile) ; MARKTIME(t2) ; if ( msglvl > 1 ) { DenseMtx_writeForMatlab(Y2, "Z2", msgFile) ; fprintf(msgFile, "\n maxerr2 = max(Z2 - Y - alpha*ctranspose(A)*X) ") ; fprintf(msgFile, "\n") ; } } else if ( transposeflag == 2 ) { MARKTIME(t1) ; InpMtx_MT_nonsym_mmm_T(A, Y2, alpha, X, nthread, msglvl, msgFile) ; MARKTIME(t2) ; if ( msglvl > 1 ) { DenseMtx_writeForMatlab(Y2, "Z2", msgFile) ; fprintf(msgFile, "\n maxerr2 = max(Z2 - Y - alpha*transpose(A)*X) ") ; fprintf(msgFile, "\n") ; } } fprintf(msgFile, "\n %% %d ops, %.3f time, %.3f MT mflops", nops, t2 - t1, 1.e-6*nops/(t2 - t1)) ; /* ------------------------ free the working storage ------------------------ */ InpMtx_free(A) ; DenseMtx_free(X) ; DenseMtx_free(Y) ; DenseMtx_free(Y2) ; IVfree(rowids) ; IVfree(colids) ; Drand_free(drand) ; fclose(msgFile) ; return(1) ; }
/*--------------------------------------------------------------------*/ int main ( int argc, char *argv[] ) /* ----------------------------------------------- test the DenseMtx_frobNorm routine. when msglvl > 1, the output of this program can be fed into Matlab to check for errors created -- 98dec03, ycp ----------------------------------------------- */ { DenseMtx *A ; double t1, t2, value ; Drand *drand ; FILE *msgFile ; int inc1, inc2, msglvl, nrow, ncol, seed, type ; if ( argc != 9 ) { fprintf(stdout, "\n\n usage : %s msglvl msgFile type nrow ncol inc1 inc2 " "\n , seed " "\n msglvl -- message level" "\n msgFile -- message file" "\n type -- entries type" "\n 1 -- real" "\n 2 -- complex" "\n nrow -- # of rows " "\n ncol -- # of columns " "\n inc1 -- row increment " "\n inc2 -- column increment " "\n seed -- random number seed" "\n", argv[0]) ; return(0) ; } if ( (msglvl = atoi(argv[1])) < 0 ) { fprintf(stderr, "\n message level must be positive\n") ; spoolesFatal(); } if ( strcmp(argv[2], "stdout") == 0 ) { msgFile = stdout ; } else if ( (msgFile = fopen(argv[2], "a")) == NULL ) { fprintf(stderr, "\n unable to open file %s\n", argv[2]) ; return(-1) ; } type = atoi(argv[3]) ; nrow = atoi(argv[4]) ; ncol = atoi(argv[5]) ; inc1 = atoi(argv[6]) ; inc2 = atoi(argv[7]) ; if ( type < 1 || type > 2 || nrow < 0 || ncol < 0 || inc1 < 1 || inc2 < 1 ) { fprintf(stderr, "\n fatal error, type %d, nrow %d, ncol %d, inc1 %d, inc2 %d", type, nrow, ncol, inc1, inc2) ; spoolesFatal(); } seed = atoi(argv[8]) ; fprintf(msgFile, "\n\n %% %s :" "\n %% msglvl = %d" "\n %% msgFile = %s" "\n %% type = %d" "\n %% nrow = %d" "\n %% ncol = %d" "\n %% inc1 = %d" "\n %% inc2 = %d" "\n %% seed = %d" "\n", argv[0], msglvl, argv[2], type, nrow, ncol, inc1, inc2, seed) ; /* ---------------------------- initialize the matrix object ---------------------------- */ MARKTIME(t1) ; A = DenseMtx_new() ; DenseMtx_init(A, type, 0, 0, nrow, ncol, inc1, inc2) ; MARKTIME(t2) ; fprintf(msgFile, "\n %% CPU : %.3f to initialize matrix object", t2 - t1) ; MARKTIME(t1) ; drand = Drand_new() ; Drand_setSeed(drand, seed) ; seed++ ; Drand_setUniform(drand, -1.0, 1.0) ; DenseMtx_fillRandomEntries(A, drand) ; MARKTIME(t2) ; fprintf(msgFile, "\n %% CPU : %.3f to fill matrix with random numbers", t2 - t1) ; if ( msglvl > 3 ) { fprintf(msgFile, "\n matrix A") ; DenseMtx_writeForHumanEye(A, msgFile) ; } if ( msglvl > 1 ) { fprintf(msgFile, "\n %% matrix A") ; fprintf(msgFile, "\n nrow = %d ;", nrow) ; fprintf(msgFile, "\n ncol = %d ;", ncol) ; fprintf(msgFile, "\n"); DenseMtx_writeForMatlab(A, "A", msgFile) ; } /* -------------------------- compute the frobenius norm -------------------------- */ value = DenseMtx_frobNorm(A); if ( msglvl > 1 ) { fprintf(msgFile, "\n %% Frobenius Norm = %e", value) ; fprintf(msgFile, "\n"); fflush(msgFile) ; } /* ------------------------ free the working storage ------------------------ */ DenseMtx_free(A) ; Drand_free(drand) ; return(1) ; }
/*--------------------------------------------------------------------*/ int main ( int argc, char *argv[] ) /* ------------------------------------------------------ generate a random matrix and test the InpMtx_*_gmmm*() matrix-matrix multiply methods. the output is a matlab file to test correctness. created -- 98nov14, cca ------------------------------------------------------ */ { DenseMtx *X, *Y ; double alpha[2], beta[2] ; double alphaImag, alphaReal, betaImag, betaReal ; Drand *drand ; int dataType, msglvl, ncolA, nitem, nrhs, nrowA, nrowX, nrowY, seed, coordType, rc, symflag, transposeflag ; InpMtx *A ; FILE *msgFile ; if ( argc != 16 ) { fprintf(stdout, "\n\n %% usage : %s msglvl msgFile symflag coordType transpose" "\n %% nrow ncol nent nrhs seed " "\n %% alphaReal alphaImag betaReal betaImag" "\n %% msglvl -- message level" "\n %% msgFile -- message file" "\n %% dataType -- type of matrix entries" "\n %% 1 -- real" "\n %% 2 -- complex" "\n %% symflag -- symmetry flag" "\n %% 0 -- symmetric" "\n %% 1 -- hermitian" "\n %% 2 -- nonsymmetric" "\n %% coordType -- storage mode" "\n %% 1 -- by rows" "\n %% 2 -- by columns" "\n %% 3 -- by chevrons, (requires nrow = ncol)" "\n %% transpose -- transpose flag" "\n %% 0 -- Y := beta * Y + alpha * A * X" "\n %% 1 -- Y := beta * Y + alpha * A^H * X, nonsymmetric only" "\n %% 2 -- Y := beta * Y + alpha * A^T * X, nonsymmetric only" "\n %% nrowA -- number of rows in A" "\n %% ncolA -- number of columns in A" "\n %% nitem -- number of items" "\n %% nrhs -- number of right hand sides" "\n %% seed -- random number seed" "\n %% alphaReal -- y := beta*y + alpha*A*x" "\n %% alphaImag -- y := beta*y + alpha*A*x" "\n %% betaReal -- y := beta*y + alpha*A*x" "\n %% betaImag -- y := beta*y + alpha*A*x" "\n", argv[0]) ; return(0) ; } msglvl = atoi(argv[1]) ; if ( strcmp(argv[2], "stdout") == 0 ) { msgFile = stdout ; } else if ( (msgFile = fopen(argv[2], "a")) == NULL ) { fprintf(stderr, "\n fatal error in %s" "\n unable to open file %s\n", argv[0], argv[2]) ; return(-1) ; } dataType = atoi(argv[3]) ; symflag = atoi(argv[4]) ; coordType = atoi(argv[5]) ; transposeflag = atoi(argv[6]) ; nrowA = atoi(argv[7]) ; ncolA = atoi(argv[8]) ; nitem = atoi(argv[9]) ; nrhs = atoi(argv[10]) ; seed = atoi(argv[11]) ; alphaReal = atof(argv[12]) ; alphaImag = atof(argv[13]) ; betaReal = atof(argv[14]) ; betaImag = atof(argv[15]) ; fprintf(msgFile, "\n %% %s " "\n %% msglvl -- %d" "\n %% msgFile -- %s" "\n %% dataType -- %d" "\n %% symflag -- %d" "\n %% coordType -- %d" "\n %% transposeflag -- %d" "\n %% nrowA -- %d" "\n %% ncolA -- %d" "\n %% nitem -- %d" "\n %% nrhs -- %d" "\n %% seed -- %d" "\n %% alphaReal -- %e" "\n %% alphaImag -- %e" "\n %% betaReal -- %e" "\n %% betaImag -- %e" "\n", argv[0], msglvl, argv[2], dataType, symflag, coordType, transposeflag, nrowA, ncolA, nitem, nrhs, seed, alphaReal, alphaImag, betaReal, betaImag) ; fflush(msgFile) ; if ( dataType != 1 && dataType != 2 ) { fprintf(stderr, "\n invalid value %d for dataType\n", dataType) ; exit(-1) ; } if ( symflag != 0 && symflag != 1 && symflag != 2 ) { fprintf(stderr, "\n invalid value %d for symflag\n", symflag) ; exit(-1) ; } if ( coordType != 1 && coordType != 2 && coordType != 3 ) { fprintf(stderr, "\n invalid value %d for coordType\n", coordType) ; exit(-1) ; } if ( transposeflag < 0 || transposeflag > 2 ) { fprintf(stderr, "\n error, transposeflag = %d, must be 0, 1 or 2", transposeflag) ; exit(-1) ; } if ( (transposeflag == 1 && symflag != 2) || (transposeflag == 2 && symflag != 2) ) { fprintf(stderr, "\n error, transposeflag = %d, symflag = %d", transposeflag, symflag) ; exit(-1) ; } if ( transposeflag == 1 && dataType != 2 ) { fprintf(stderr, "\n error, transposeflag = %d, dataType = %d", transposeflag, dataType) ; exit(-1) ; } if ( symflag == 1 && dataType != 2 ) { fprintf(stderr, "\n symflag = 1 (hermitian), dataType != 2 (complex)") ; exit(-1) ; } if ( nrowA <= 0 || ncolA <= 0 || nitem <= 0 ) { fprintf(stderr, "\n invalid value: nrow = %d, ncol = %d, nitem = %d", nrowA, ncolA, nitem) ; exit(-1) ; } if ( symflag < 2 && nrowA != ncolA ) { fprintf(stderr, "\n invalid data: symflag = %d, nrow = %d, ncol = %d", symflag, nrowA, ncolA) ; exit(-1) ; } alpha[0] = alphaReal ; alpha[1] = alphaImag ; beta[0] = betaReal ; beta[1] = betaImag ; drand = Drand_new() ; Drand_setSeed(drand, seed) ; Drand_setUniform(drand, -1.0, 1.0) ; /* ---------------------------- initialize the matrix object and fill with random entries ---------------------------- */ A = InpMtx_new() ; InpMtx_init(A, coordType, dataType, 0, 0) ; rc = InpMtx_randomMatrix(A, dataType, coordType, INPMTX_BY_VECTORS, nrowA, ncolA, symflag, 1, nitem, seed) ; if ( rc != 1 ) { fprintf(stderr, "\n error return %d from InpMtx_randomMatrix()", rc); exit(-1) ; } /* ------------------------------------------- write the assembled matrix to a matlab file ------------------------------------------- */ InpMtx_writeForMatlab(A, "A", msgFile) ; if ( symflag == 0 ) { fprintf(msgFile, "\n for k = 1:%d" "\n for j = k+1:%d" "\n A(j,k) = A(k,j) ;" "\n end" "\n end", nrowA, ncolA) ; } else if ( symflag == 1 ) { fprintf(msgFile, "\n for k = 1:%d" "\n for j = k+1:%d" "\n A(j,k) = ctranspose(A(k,j)) ;" "\n end" "\n end", nrowA, ncolA) ; } /* ------------------------------- generate dense matrices X and Y ------------------------------- */ if ( transposeflag == 0 ) { nrowX = ncolA ; nrowY = nrowA ; } else { nrowX = nrowA ; nrowY = ncolA ; } X = DenseMtx_new() ; Y = DenseMtx_new() ; DenseMtx_init(X, dataType, 0, 0, nrowX, nrhs, 1, nrowX) ; DenseMtx_fillRandomEntries(X, drand) ; DenseMtx_init(Y, dataType, 0, 0, nrowY, nrhs, 1, nrowY) ; DenseMtx_fillRandomEntries(Y, drand) ; fprintf(msgFile, "\n X = zeros(%d,%d) ;", nrowX, nrhs) ; DenseMtx_writeForMatlab(X, "X", msgFile) ; fprintf(msgFile, "\n Y = zeros(%d,%d) ;", nrowY, nrhs) ; DenseMtx_writeForMatlab(Y, "Y", msgFile) ; /* ---------------------------------- perform the matrix-matrix multiply ---------------------------------- */ fprintf(msgFile, "\n beta = %20.12e + %20.2e*i;", beta[0], beta[1]); fprintf(msgFile, "\n alpha = %20.12e + %20.2e*i;", alpha[0], alpha[1]); fprintf(msgFile, "\n Z = zeros(%d,1) ;", nrowY) ; if ( transposeflag == 0 ) { if ( symflag == 0 ) { InpMtx_sym_gmmm(A, beta, Y, alpha, X) ; } else if ( symflag == 1 ) { InpMtx_herm_gmmm(A, beta, Y, alpha, X) ; } else if ( symflag == 2 ) { InpMtx_nonsym_gmmm(A, beta, Y, alpha, X) ; } DenseMtx_writeForMatlab(Y, "Z", msgFile) ; fprintf(msgFile, "\n maxerr = max(Z - beta*Y - alpha*A*X) ") ; fprintf(msgFile, "\n") ; } else if ( transposeflag == 1 ) { InpMtx_nonsym_gmmm_H(A, beta, Y, alpha, X) ; DenseMtx_writeForMatlab(Y, "Z", msgFile) ; fprintf(msgFile, "\n maxerr = max(Z - beta*Y - alpha*ctranspose(A)*X) ") ; fprintf(msgFile, "\n") ; } else if ( transposeflag == 2 ) { InpMtx_nonsym_gmmm_T(A, beta, Y, alpha, X) ; DenseMtx_writeForMatlab(Y, "Z", msgFile) ; fprintf(msgFile, "\n maxerr = max(Z - beta*Y - alpha*transpose(A)*X) ") ; fprintf(msgFile, "\n") ; } /* ------------------------ free the working storage ------------------------ */ InpMtx_free(A) ; DenseMtx_free(X) ; DenseMtx_free(Y) ; Drand_free(drand) ; fclose(msgFile) ; return(1) ; }
/*--------------------------------------------------------------------*/ int main ( int argc, char *argv[] ) /* ------------------------------------------------------- test the DenseMtx_mmm routine. C = alpha*A*B + beta*C, where A, B and C are DenseMtx. alpha and beta are scalars. when msglvl > 1, the output of this program can be fed into Matlab to check for errors created -- 98dec14, ycp ------------------------------------------------------- */ { DenseMtx *mtxA, *mtxB, *mtxC; double t1, t2, value[2] = {1.0, 1.0} ; Drand *drand ; FILE *msgFile ; int i, j, k, msglvl, nrow, nk, ncol, cnrow, cncol, seed, type ; int ainc1, ainc2, binc1, binc2, cinc1, cinc2; double alpha[2], beta[2], one[2] = {1.0, 0.0}, rvalue; char A_opt[1]=" ", B_opt[1]=" "; if ( argc != 20 ) { fprintf(stdout, "\n\n usage : %s msglvl msgFile type nrow nk ncol ainc1 ainc2 binc1 " "\n binc2 cinc1 cinc2 A_opt B_opt ralpha ialpha rbeta ibeta seed " "\n msglvl -- message level" "\n msgFile -- message file" "\n type -- entries type" "\n 1 -- real" "\n 2 -- complex" "\n nrow -- # of rows of mtxA " "\n nk -- # of columns of mtxA " "\n ncol -- # of columns of mtxB " "\n ainc1 -- A row increment " "\n ainc2 -- A column increment " "\n binc1 -- B row increment " "\n binc2 -- B column increment " "\n binc1 -- C row increment " "\n binc2 -- C column increment " "\n A_opt -- A option " "\n B_opt -- B option " "\n ralpha -- real(alpha)" "\n ialpha -- imag(alpha)" "\n rbeta -- real(beta)" "\n ibeta -- imag(beta)" "\n seed -- random number seed" "\n", argv[0]) ; return(0) ; } if ( (msglvl = atoi(argv[1])) < 0 ) { fprintf(stderr, "\n message level must be positive\n") ; spoolesFatal(); } if ( strcmp(argv[2], "stdout") == 0 ) { msgFile = stdout ; } else if ( (msgFile = fopen(argv[2], "a")) == NULL ) { fprintf(stderr, "\n unable to open file %s\n", argv[2]) ; return(-1) ; } type = atoi(argv[3]) ; nrow = atoi(argv[4]) ; nk = atoi(argv[5]) ; ncol = atoi(argv[6]) ; ainc1= atoi(argv[7]) ; ainc2= atoi(argv[8]) ; binc1= atoi(argv[9]) ; binc2= atoi(argv[10]) ; cinc1= atoi(argv[11]) ; cinc2= atoi(argv[12]) ; if ( type < 1 || type > 2 || nrow < 0 || ncol < 0 || ainc1 < 1 || ainc2 < 1 || binc1 < 1 || binc2 < 1 ) { fprintf(stderr, "\n fatal error, type %d, nrow %d, ncol %d, ainc1 %d, ainc2 %d" ", binc1 %d, binc2 %d", type, nrow, ncol, ainc1, ainc2, binc1, binc2) ; spoolesFatal(); } A_opt[0] = *argv[13] ; B_opt[0] = *argv[14] ; alpha[0]= atof (argv[15]); alpha[1]= atof (argv[16]); beta[0] = atof (argv[17]); beta[1] = atof (argv[18]); seed = atoi (argv[19]) ; fprintf(msgFile, "\n\n %% %s :" "\n %% msglvl = %d" "\n %% msgFile = %s" "\n %% type = %d" "\n %% nrow = %d" "\n %% nk = %d" "\n %% ncol = %d" "\n %% ainc1 = %d" "\n %% ainc2 = %d" "\n %% binc1 = %d" "\n %% binc2 = %d" "\n %% cinc1 = %d" "\n %% cinc2 = %d" "\n %% a_opt = %c" "\n %% b_opt = %c" "\n %% ralpha = %e" "\n %% ialpha = %e" "\n %% rbeta = %e" "\n %% ibeta = %e" "\n %% seed = %d" "\n", argv[0], msglvl, argv[2], type, nrow, nk, ncol, ainc1, ainc2, binc1, binc2, cinc1, cinc2, A_opt[0], B_opt[0], alpha[0], alpha[1], beta[0], beta[1], seed) ; /* ---------------------------- initialize the matrix object ---------------------------- */ MARKTIME(t1) ; mtxA = DenseMtx_new() ; DenseMtx_init(mtxA, type, 0, 0, nrow, nk, ainc1, ainc2) ; MARKTIME(t2) ; fprintf(msgFile, "\n %% CPU : %.3f to initialize matrix object", t2 - t1) ; MARKTIME(t1) ; drand = Drand_new() ; Drand_setSeed(drand, seed) ; seed++ ; Drand_setUniform(drand, -1.0, 1.0) ; DenseMtx_fillRandomEntries(mtxA, drand) ; MARKTIME(t2) ; fprintf(msgFile, "\n %% CPU : %.3f to fill matrix A with random numbers", t2 - t1) ; MARKTIME(t1) ; mtxB = DenseMtx_new() ; DenseMtx_init(mtxB, type, 0, 0, nk, ncol, binc1, binc2) ; MARKTIME(t2) ; fprintf(msgFile, "\n %% CPU : %.3f to initialize matrix object", t2 - t1) ; MARKTIME(t1) ; drand = Drand_new() ; Drand_setSeed(drand, seed) ; seed++ ; Drand_setUniform(drand, -1.0, 1.0) ; DenseMtx_fillRandomEntries(mtxB, drand) ; MARKTIME(t2) ; fprintf(msgFile, "\n %% CPU : %.3f to fill matrix B with random numbers", t2 - t1) ; cnrow = nrow; cncol = ncol; MARKTIME(t1) ; mtxC = DenseMtx_new() ; if ( A_opt[0] == 't' || A_opt[0] == 'T' || A_opt[0] == 'c' || A_opt[0] == 'C') { cnrow = nk; } if ( B_opt[0] == 't' || B_opt[0] == 'T' || B_opt[0] == 'c' || B_opt[0] == 'C') { cncol = nk; } if ( cinc1 == 1 && cinc2 == nrow ){ /* stored by column */ cinc1 = 1; cinc2 = cnrow; } else { /* stored by row */ cinc1 = cncol; cinc2 = 1; } DenseMtx_init(mtxC, type, 0, 0, cnrow, cncol, cinc1, cinc2) ; MARKTIME(t2) ; fprintf(msgFile, "\n %% CPU : %.3f to initialize matrix object", t2 - t1) ; MARKTIME(t1) ; drand = Drand_new() ; Drand_setSeed(drand, seed) ; seed++ ; Drand_setUniform(drand, -1.0, 1.0) ; DenseMtx_fillRandomEntries(mtxC, drand) ; MARKTIME(t2) ; fprintf(msgFile, "\n %% CPU : %.3f to fill matrix C with random numbers", t2 - t1) ; if ( msglvl > 3 ) { fprintf(msgFile, "\n matrix A") ; DenseMtx_writeForHumanEye(mtxA, msgFile) ; fprintf(msgFile, "\n matrix B") ; DenseMtx_writeForHumanEye(mtxB, msgFile) ; fprintf(msgFile, "\n matrix C") ; DenseMtx_writeForHumanEye(mtxC, msgFile) ; } if ( msglvl > 1 ) { fprintf(msgFile, "\n\n %% beta = (%f, %f)", beta[0], beta[1]) ; fprintf(msgFile, "\n %% alpha = (%f, %f)\n", alpha[0], alpha[1]) ; fprintf(msgFile, "\n %% matrix A") ; fprintf(msgFile, "\n nrow = %d ;", nrow) ; fprintf(msgFile, "\n ncol = %d ;", nk) ; DenseMtx_writeForMatlab(mtxA, "A", msgFile) ; fprintf(msgFile, "\n"); fprintf(msgFile, "\n %% matrix B") ; fprintf(msgFile, "\n nrow = %d ;", nk) ; fprintf(msgFile, "\n ncol = %d ;", ncol) ; DenseMtx_writeForMatlab(mtxB, "B", msgFile) ; fprintf(msgFile, "\n"); fprintf(msgFile, "\n %% matrix C") ; fprintf(msgFile, "\n nrow = %d ;", cnrow) ; fprintf(msgFile, "\n ncol = %d ;", cncol) ; DenseMtx_writeForMatlab(mtxC, "C", msgFile) ; } /* -------------------------- performs the matrix-matrix operations C = alpha*(A)*(B) + beta*C -------------------------- */ DenseMtx_mmm(A_opt, B_opt, &beta, mtxC, &alpha, mtxA, mtxB); if ( msglvl > 1 ) { fprintf(msgFile, "\n"); fprintf(msgFile, "\n %% *** Output matrix C ***") ; fprintf(msgFile, "\n nrow = %d ;", cnrow) ; fprintf(msgFile, "\n ncol = %d ;", cncol) ; DenseMtx_writeForMatlab(mtxC, "C", msgFile) ; fprintf(msgFile, "\n"); fflush(msgFile) ; } /* ------------------------ free the working storage ------------------------ */ DenseMtx_free(mtxA) ; DenseMtx_free(mtxB) ; DenseMtx_free(mtxC) ; Drand_free(drand) ; return(1) ; }