LisMatrix::LisMatrix(std::size_t n_rows, LisOption::MatrixType mat_type) : _n_rows(n_rows), _mat_type(mat_type), _is_assembled(false) { int ierr = lis_matrix_create(0, &_AA); checkLisError(ierr); ierr = lis_matrix_set_size(_AA, 0, n_rows); checkLisError(ierr); lis_matrix_get_range(_AA, &_is, &_ie); ierr = lis_vector_duplicate(_AA, &_diag); checkLisError(ierr); }
void lis_matrix_get_range_f(LIS_MATRIX_F *A, LIS_INT *is, LIS_INT *ie, LIS_INT *ierr) { LIS_DEBUG_FUNC_IN; *ierr = lis_matrix_get_range((LIS_MATRIX)LIS_V2P(A),is,ie); (*is)++; (*ie)++; LIS_DEBUG_FUNC_OUT; return; }
LisMatrix::LisMatrix(std::size_t n_rows, int nnz, IndexType *row_ptr, IndexType *col_idx, double *data) : _n_rows(n_rows), _mat_type(MatrixType::CRS), _is_assembled(false), _use_external_arrays(true) { int ierr = lis_matrix_create(0, &_AA); checkLisError(ierr); ierr = lis_matrix_set_size(_AA, 0, n_rows); checkLisError(ierr); ierr = lis_matrix_set_csr(nnz, row_ptr, col_idx, data, _AA); checkLisError(ierr); ierr = lis_matrix_assemble(_AA); checkLisError(ierr); _is_assembled = true; lis_matrix_get_range(_AA, &_is, &_ie); ierr = lis_vector_duplicate(_AA, &_diag); checkLisError(ierr); }
LIS_INT lis_esolver_get_evectors(LIS_ESOLVER esolver, LIS_MATRIX M) { LIS_INT i,ii,j,jj,n,gn,is,ie,js; LIS_INT ss,lis_esolver_evector_size; LIS_DEBUG_FUNC_IN; if ( esolver->options[LIS_EOPTIONS_ESOLVER] != LIS_ESOLVER_SI && esolver->options[LIS_EOPTIONS_ESOLVER] != LIS_ESOLVER_LI && esolver->options[LIS_EOPTIONS_ESOLVER] != LIS_ESOLVER_AI ) { LIS_SETERR1(LIS_ERR_ILL_ARG,"Parameter LIS_EOPTIONS_ESOLVER is %d (Set Subspace, Lanczos, or Arnoldi)\n", esolver->options[LIS_EOPTIONS_ESOLVER]); return LIS_ERR_ILL_ARG; } ss = esolver->options[LIS_EOPTIONS_SUBSPACE]; lis_esolver_evector_size = esolver->evector[0]->gn; lis_matrix_set_size(M,0,lis_esolver_evector_size); lis_matrix_get_size(M,&n,&gn); lis_matrix_get_range(M,&is,&ie); js=0; if( esolver->evector[0]->origin ) { is++; js++; } for(j=0;j<ss;j++) { for(i=0;i<n;i++) { ii=i+is; jj=j+js; lis_matrix_set_value(LIS_INS_VALUE,ii,jj,esolver->evector[j]->value[i],M); } } lis_matrix_set_type(M,LIS_MATRIX_CSR); lis_matrix_assemble(M); LIS_DEBUG_FUNC_OUT; return LIS_SUCCESS; }
LIS_INT main(LIS_INT argc, char* argv[]) { LIS_INT i,n,gn,is,ie; LIS_INT nprocs,my_rank; int int_nprocs,int_my_rank; LIS_INT nesol; LIS_MATRIX A; LIS_VECTOR x; LIS_REAL evalue0; LIS_ESOLVER esolver; LIS_REAL residual; LIS_INT iter; double time; double itime,ptime,p_c_time,p_i_time; char esolvername[128]; LIS_DEBUG_FUNC_IN; lis_initialize(&argc, &argv); #ifdef USE_MPI MPI_Comm_size(MPI_COMM_WORLD,&int_nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&int_my_rank); nprocs = int_nprocs; my_rank = int_my_rank; #else nprocs = 1; my_rank = 0; #endif if( argc < 2 ) { if( my_rank==0 ) { printf("Usage: %s n [eoptions]\n", argv[0]); } CHKERR(1); } if( my_rank==0 ) { printf("\n"); printf("number of processes = %d\n",nprocs); } #ifdef _OPENMP if( my_rank==0 ) { #ifdef _LONG__LONG printf("max number of threads = %lld\n",omp_get_num_procs()); printf("number of threads = %lld\n",omp_get_max_threads()); #else printf("max number of threads = %d\n",omp_get_num_procs()); printf("number of threads = %d\n",omp_get_max_threads()); #endif } #endif /* generate coefficient matrix for one dimensional Poisson equation */ n = atoi(argv[1]); lis_matrix_create(LIS_COMM_WORLD,&A); lis_matrix_set_size(A,0,n); lis_matrix_get_size(A,&n,&gn); lis_matrix_get_range(A,&is,&ie); for(i=is;i<ie;i++) { if( i>0 ) lis_matrix_set_value(LIS_INS_VALUE,i,i-1,-1.0,A); if( i<gn-1 ) lis_matrix_set_value(LIS_INS_VALUE,i,i+1,-1.0,A); lis_matrix_set_value(LIS_INS_VALUE,i,i,2.0,A); } lis_matrix_set_type(A,LIS_MATRIX_CSR); lis_matrix_assemble(A); lis_vector_duplicate(A,&x); lis_esolver_create(&esolver); lis_esolver_set_option("-eprint mem",esolver); lis_esolver_set_optionC(esolver); lis_esolve(A, x, &evalue0, esolver); lis_esolver_get_esolver(esolver,&nesol); lis_esolver_get_esolvername(nesol,esolvername); lis_esolver_get_residualnorm(esolver, &residual); lis_esolver_get_iter(esolver, &iter); lis_esolver_get_timeex(esolver,&time,&itime,&ptime,&p_c_time,&p_i_time); if( my_rank==0 ) { printf("%s: mode number = %d\n", esolvername, 0); #ifdef _LONG__DOUBLE printf("%s: eigenvalue = %Le\n", esolvername, evalue0); #else printf("%s: eigenvalue = %e\n", esolvername, evalue0); #endif #ifdef _LONG__LONG printf("%s: number of iterations = %lld\n",esolvername, iter); #else printf("%s: number of iterations = %d\n",esolvername, iter); #endif printf("%s: elapsed time = %e sec.\n", esolvername, time); printf("%s: preconditioner = %e sec.\n", esolvername, ptime); printf("%s: matrix creation = %e sec.\n", esolvername, p_c_time); printf("%s: linear solver = %e sec.\n", esolvername, itime); #ifdef _LONG__DOUBLE printf("%s: relative residual = %Le\n\n",esolvername, residual); #else printf("%s: relative residual = %e\n\n",esolvername, residual); #endif } /* lis_vector_nrm2(x, &xnrm2); lis_vector_scale((1/xnrm2*sqrt(n)), x); lis_vector_print(x); */ /* lis_vector_create(LIS_COMM_WORLD,&y); lis_matrix_create(LIS_COMM_WORLD,&B); lis_esolver_get_evalues(esolver,y); lis_esolver_get_evectors(esolver,B); lis_output_vector(y,LIS_FMT_MM,"evalues.out"); lis_output_matrix(B,LIS_FMT_MM,"evectors.out"); lis_vector_destroy(y); lis_matrix_destroy(B); */ lis_esolver_destroy(esolver); lis_matrix_destroy(A); lis_vector_destroy(x); lis_finalize(); LIS_DEBUG_FUNC_OUT; return 0; }
LIS_INT main(LIS_INT argc, char* argv[]) { LIS_MATRIX A0,A; LIS_VECTOR x,b,u; LIS_SOLVER solver; LIS_INT m,n,nn,nnz; LIS_INT i,j,ii,jj,ctr; LIS_INT is,ie; LIS_INT nprocs,my_rank; int int_nprocs,int_my_rank; LIS_INT nsol; LIS_INT err,iter,mtype,iter_double,iter_quad; double time,itime,ptime,p_c_time,p_i_time; LIS_REAL resid; char solvername[128]; LIS_INT *ptr,*index; LIS_SCALAR *value; LIS_DEBUG_FUNC_IN; lis_initialize(&argc, &argv); #ifdef USE_MPI MPI_Comm_size(MPI_COMM_WORLD,&int_nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&int_my_rank); nprocs = int_nprocs; my_rank = int_my_rank; #else nprocs = 1; my_rank = 0; #endif if( argc < 6 ) { if( my_rank==0 ) { printf("Usage: %s m n matrix_type solution_filename rhistory_filename [options]\n", argv[0]); } CHKERR(1); } m = atoi(argv[1]); n = atoi(argv[2]); mtype = atoi(argv[3]); if( m<=0 || n<=0 ) { #ifdef _LONGLONG if( my_rank==0 ) printf("m=%lld <=0 or n=%lld <=0\n",m,n); #else if( my_rank==0 ) printf("m=%d <=0 or n=%d <=0\n",m,n); #endif CHKERR(1); } if( my_rank==0 ) { printf("\n"); #ifdef _LONGLONG printf("number of processes = %lld\n",nprocs); #else printf("number of processes = %d\n",nprocs); #endif } #ifdef _OPENMP if( my_rank==0 ) { #ifdef _LONGLONG printf("max number of threads = %lld\n",omp_get_num_procs()); printf("number of threads = %lld\n",omp_get_max_threads()); #else printf("max number of threads = %d\n",omp_get_num_procs()); printf("number of threads = %d\n",omp_get_max_threads()); #endif } #endif /* create matrix and vectors */ nn = m*n; err = lis_matrix_create(LIS_COMM_WORLD,&A); err = lis_matrix_set_size(A,0,nn); CHKERR(err); ptr = (LIS_INT *)malloc((A->n+1)*sizeof(LIS_INT)); if( ptr==NULL ) CHKERR(1); index = (LIS_INT *)malloc(5*A->n*sizeof(LIS_INT)); if( index==NULL ) CHKERR(1); value = (LIS_SCALAR *)malloc(5*A->n*sizeof(LIS_SCALAR)); if( value==NULL ) CHKERR(1); lis_matrix_get_range(A,&is,&ie); ctr = 0; for(ii=is;ii<ie;ii++) { i = ii/m; j = ii - i*m; if( i>0 ) { jj = ii - m; index[ctr] = jj; value[ctr++] = -1.0;} if( i<n-1 ) { jj = ii + m; index[ctr] = jj; value[ctr++] = -1.0;} if( j>0 ) { jj = ii - 1; index[ctr] = jj; value[ctr++] = -1.0;} if( j<m-1 ) { jj = ii + 1; index[ctr] = jj; value[ctr++] = -1.0;} index[ctr] = ii; value[ctr++] = 4.0; ptr[ii-is+1] = ctr; } ptr[0] = 0; err = lis_matrix_set_csr(ptr[ie-is],ptr,index,value,A); CHKERR(err); err = lis_matrix_assemble(A); CHKERR(err); nnz = A->nnz; #ifdef USE_MPI MPI_Allreduce(&nnz,&i,1,LIS_MPI_INT,MPI_SUM,A->comm); nnz = i; #endif #ifdef _LONGLONG if( my_rank==0 ) printf("matrix size = %lld x %lld (%lld nonzero entries)\n\n",nn,nn,nnz); #else if( my_rank==0 ) printf("matrix size = %d x %d (%d nonzero entries)\n\n",nn,nn,nnz); #endif err = lis_matrix_duplicate(A,&A0); CHKERR(err); lis_matrix_set_type(A0,mtype); err = lis_matrix_convert(A,A0); CHKERR(err); lis_matrix_destroy(A); A = A0; err = lis_vector_duplicate(A,&u); CHKERR(err); err = lis_vector_duplicate(A,&b); CHKERR(err); err = lis_vector_duplicate(A,&x); CHKERR(err); err = lis_vector_set_all(1.0,u); lis_matvec(A,u,b); err = lis_solver_create(&solver); CHKERR(err); lis_solver_set_option("-print mem",solver); lis_solver_set_optionC(solver); err = lis_solve(A,b,x,solver); CHKERR(err); lis_solver_get_iterex(solver,&iter,&iter_double,&iter_quad); lis_solver_get_timeex(solver,&time,&itime,&ptime,&p_c_time,&p_i_time); lis_solver_get_residualnorm(solver,&resid); lis_solver_get_solver(solver,&nsol); lis_solver_get_solvername(nsol,solvername); if( my_rank==0 ) { #ifdef _LONGLONG #ifdef _LONG__DOUBLE printf("%s: number of iterations = %lld \n",solvername, iter); #else printf("%s: number of iterations = %lld (double = %lld, quad = %lld)\n",solvername,iter, iter_double, iter_quad); #endif #else #ifdef _LONG__DOUBLE printf("%s: number of iterations = %d \n",solvername, iter); #else printf("%s: number of iterations = %d (double = %d, quad = %d)\n",solvername,iter, iter_double, iter_quad); #endif #endif printf("%s: elapsed time = %e sec.\n",solvername,time); printf("%s: preconditioner = %e sec.\n",solvername, ptime); printf("%s: matrix creation = %e sec.\n",solvername, p_c_time); printf("%s: linear solver = %e sec.\n",solvername, itime); #ifdef _LONG__DOUBLE printf("%s: relative residual = %Le\n\n",solvername,resid); #else printf("%s: relative residual = %e\n\n",solvername,resid); #endif } /* write solution */ lis_output_vector(x,LIS_FMT_MM,argv[4]); /* write residual history */ lis_solver_output_rhistory(solver, argv[5]); lis_solver_destroy(solver); lis_matrix_destroy(A); lis_vector_destroy(b); lis_vector_destroy(x); lis_vector_destroy(u); lis_finalize(); LIS_DEBUG_FUNC_OUT; return 0; }
LIS_INT main(LIS_INT argc, char* argv[]) { LIS_MATRIX A; LIS_VECTOR x,b,u; LIS_SOLVER solver; LIS_INT k,n,gn,ii,jj; LIS_INT is,ie; LIS_INT nprocs,my_rank; int int_nprocs,int_my_rank; LIS_INT nsol; LIS_INT err,iter,iter_double,iter_quad; double time,itime,ptime,p_c_time,p_i_time; LIS_REAL resid; char solvername[128]; LIS_INT *ptr,*index; LIS_SCALAR *value,gamma; LIS_DEBUG_FUNC_IN; lis_initialize(&argc, &argv); #ifdef USE_MPI MPI_Comm_size(MPI_COMM_WORLD,&int_nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&int_my_rank); nprocs = int_nprocs; my_rank = int_my_rank; #else nprocs = 1; my_rank = 0; #endif if( argc < 3 ) { if( my_rank==0 ) { printf("Usage: %s n gamma [options]\n", argv[0]); } CHKERR(1); } gn = atoi(argv[1]); gamma = atof(argv[2]); if( gn<=0 ) { #ifdef _LONGLONG if( my_rank==0 ) printf("n=%lld <=0 \n",gn); #else if( my_rank==0 ) printf("n=%d <=0 \n",gn); #endif CHKERR(1); } if( my_rank==0 ) { printf("\n"); #ifdef _LONGLONG printf("number of processes = %lld\n",nprocs); #else printf("number of processes = %d\n",nprocs); #endif } #ifdef _OPENMP if( my_rank==0 ) { #ifdef _LONGLONG printf("max number of threads = %lld\n",omp_get_num_procs()); printf("number of threads = %lld\n",omp_get_max_threads()); #else printf("max number of threads = %d\n",omp_get_num_procs()); printf("number of threads = %d\n",omp_get_max_threads()); #endif } #endif if(my_rank==0) { #ifdef _LONGLONG printf("n = %lld, gamma = %f\n\n",gn,gamma); #else printf("n = %d, gamma = %f\n\n",gn,gamma); #endif } /* create matrix and vectors */ err = lis_matrix_create(LIS_COMM_WORLD,&A); CHKERR(err); err = lis_matrix_set_size(A,0,gn); CHKERR(err); err = lis_matrix_get_size(A,&n,&gn); CHKERR(err); err = lis_matrix_malloc_csr(n,3*n,&ptr,&index,&value); CHKERR(err); err = lis_matrix_get_range(A,&is,&ie); CHKERR(err); k = 0; ptr[0] = 0; for(ii=is;ii<ie;ii++) { if( ii>1 ) { jj = ii - 2; index[k] = jj; value[k++] = gamma;} if( ii<gn-1 ) { jj = ii + 1; index[k] = jj; value[k++] = 1.0;} index[k] = ii; value[k++] = 2.0; ptr[ii-is+1] = k; } err = lis_matrix_set_csr(ptr[ie-is],ptr,index,value,A); CHKERR(err); err = lis_matrix_assemble(A); CHKERR(err); err = lis_vector_duplicate(A,&u); CHKERR(err); err = lis_vector_duplicate(u,&b); CHKERR(err); err = lis_vector_duplicate(u,&x); CHKERR(err); err = lis_vector_set_all(1.0,u); lis_matvec(A,u,b); err = lis_solver_create(&solver); CHKERR(err); lis_solver_set_option("-print mem",solver); lis_solver_set_optionC(solver); err = lis_solve(A,b,x,solver); CHKERR(err); lis_solver_get_iterex(solver,&iter,&iter_double,&iter_quad); lis_solver_get_timeex(solver,&time,&itime,&ptime,&p_c_time,&p_i_time); lis_solver_get_residualnorm(solver,&resid); lis_solver_get_solver(solver,&nsol); lis_solver_get_solvername(nsol,solvername); if( my_rank==0 ) { #ifdef _LONGLONG #ifdef _LONG__DOUBLE printf("%s: number of iterations = %lld \n",solvername, iter); #else printf("%s: number of iterations = %lld (double = %lld, quad = %lld)\n",solvername,iter, iter_double, iter_quad); #endif #else #ifdef _LONG__DOUBLE printf("%s: number of iterations = %d \n",solvername, iter); #else printf("%s: number of iterations = %d (double = %d, quad = %d)\n",solvername,iter, iter_double, iter_quad); #endif #endif printf("%s: elapsed time = %e sec.\n",solvername,time); printf("%s: preconditioner = %e sec.\n",solvername, ptime); printf("%s: matrix creation = %e sec.\n",solvername, p_c_time); printf("%s: linear solver = %e sec.\n",solvername, itime); #ifdef _LONG__DOUBLE printf("%s: relative residual = %Le\n\n",solvername,resid); #else printf("%s: relative residual = %e\n\n",solvername,resid); #endif } lis_solver_destroy(solver); lis_matrix_destroy(A); lis_vector_destroy(b); lis_vector_destroy(x); lis_vector_destroy(u); lis_finalize(); LIS_DEBUG_FUNC_OUT; return 0; }
LIS_INT main(LIS_INT argc, char* argv[]) { LIS_MATRIX A,A0; LIS_VECTOR b,x; LIS_SCALAR *value; LIS_INT nprocs,my_rank; int int_nprocs,int_my_rank; LIS_INT nthreads,maxthreads; LIS_INT gn,nnz,np; LIS_INT i,j,k,si,sj,sk,ii,jj,ctr; LIS_INT l,m,n,nn; LIS_INT is,ie; LIS_INT err,iter,matrix_type,storage,ss,se; LIS_INT *ptr,*index; double time,time2,nnzs,nnzap,nnzt; LIS_SCALAR val; double commtime,comptime,flops; LIS_DEBUG_FUNC_IN; lis_initialize(&argc, &argv); #ifdef USE_MPI MPI_Comm_size(MPI_COMM_WORLD,&int_nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&int_my_rank); nprocs = int_nprocs; my_rank = int_my_rank; #else nprocs = 1; my_rank = 0; #endif if( argc < 5 ) { if( my_rank==0 ) { printf("Usage: %s l m n iter [matrix_type]\n", argv[0]); } CHKERR(1); } l = atoi(argv[1]); m = atoi(argv[2]); n = atoi(argv[3]); iter = atoi(argv[4]); if (argv[5] == NULL) { storage = 0; } else { storage = atoi(argv[5]); } if( iter<=0 ) { #ifdef _LONG__LONG if( my_rank==0 ) printf("iter=%lld <= 0\n",iter); #else if( my_rank==0 ) printf("iter=%d <= 0\n",iter); #endif CHKERR(1); } if( l<=0 || m<=0 || n<=0 ) { #ifdef _LONG__LONG if( my_rank==0 ) printf("l=%lld <=0, m=%lld <=0 or n=%lld <=0\n",l,m,n); #else if( my_rank==0 ) printf("l=%d <=0, m=%d <=0 or n=%d <=0\n",l,m,n); #endif CHKERR(1); } if( storage<0 || storage>11 ) { #ifdef _LONG__LONG if( my_rank==0 ) printf("matrix_type=%lld < 0 or matrix_type=%lld > 11\n",storage,storage); #else if( my_rank==0 ) printf("matrix_type=%d < 0 or matrix_type=%d > 11\n",storage,storage); #endif CHKERR(1); } if( my_rank==0 ) { printf("\n"); #ifdef _LONG__LONG printf("number of processes = %lld\n",nprocs); #else printf("number of processes = %d\n",nprocs); #endif } #ifdef _OPENMP nthreads = omp_get_num_procs(); maxthreads = omp_get_max_threads(); if( my_rank==0 ) { #ifdef _LONG__LONG printf("max number of threads = %lld\n", nthreads); printf("number of threads = %lld\n", maxthreads); #else printf("max number of threads = %d\n", nthreads); printf("number of threads = %d\n", maxthreads); #endif } #else nthreads = 1; maxthreads = 1; #endif /* create matrix and vectors */ nn = l*m*n; err = lis_matrix_create(LIS_COMM_WORLD,&A0); err = lis_matrix_set_size(A0,0,nn); CHKERR(err); ptr = (LIS_INT *)malloc((A0->n+1)*sizeof(LIS_INT)); if( ptr==NULL ) CHKERR(1); index = (LIS_INT *)malloc(27*A0->n*sizeof(LIS_INT)); if( index==NULL ) CHKERR(1); value = (LIS_SCALAR *)malloc(27*A0->n*sizeof(LIS_SCALAR)); if( value==NULL ) CHKERR(1); lis_matrix_get_range(A0,&is,&ie); ctr = 0; for(ii=is;ii<ie;ii++) { i = ii/(m*n); j = (ii - i*m*n)/n; k = ii - i*m*n - j*n; for(si=-1;si<=1;si++) { if( i+si>-1 && i+si<l ) { for(sj=-1;sj<=1;sj++) { if( j+sj>-1 && j+sj<m ) { for(sk=-1;sk<=1;sk++) { if( k+sk>-1 && k+sk<n ) { jj = ii + si*m*n + sj*n + sk; index[ctr] = jj; if( jj==ii ) { value[ctr++] = 26.0;} else { value[ctr++] = -1.0;} } } } } } } ptr[ii-is+1] = ctr; } ptr[0] = 0; err = lis_matrix_set_csr(ptr[ie-is],ptr,index,value,A0); CHKERR(err); err = lis_matrix_assemble(A0); CHKERR(err); n = A0->n; gn = A0->gn; nnz = A0->nnz; np = A0->np-n; #ifdef USE_MPI MPI_Allreduce(&nnz,&i,1,LIS_MPI_INT,MPI_SUM,A0->comm); nnzap = (double)i / (double)nprocs; nnzt = ((double)nnz -nnzap)*((double)nnz -nnzap); nnz = i; MPI_Allreduce(&nnzt,&nnzs,1,MPI_DOUBLE,MPI_SUM,A0->comm); nnzs = (nnzs / (double)nprocs)/nnzap; MPI_Allreduce(&np,&i,1,LIS_MPI_INT,MPI_SUM,A0->comm); np = i; #endif if( my_rank==0 ) { #ifdef _LONG__LONG printf("matrix size = %lld x %lld (%lld nonzero entries)\n",gn,gn,nnz); printf("number of iterations = %lld\n\n",iter); #else printf("matrix size = %d x %d (%d nonzero entries)\n",gn,gn,nnz); printf("number of iterations = %d\n\n",iter); #endif } err = lis_vector_duplicate(A0,&x); if( err ) CHKERR(err); err = lis_vector_duplicate(A0,&b); if( err ) CHKERR(err); lis_matrix_get_range(A0,&is,&ie); for(i=0;i<n;i++) { err = lis_vector_set_value(LIS_INS_VALUE,i+is,1.0,x); } for(i=0;i<n;i++) { lis_sort_id(A0->ptr[i],A0->ptr[i+1]-1,A0->index,A0->value); } /* MPI version of VBR is not implemented. DNS is also excluded to reduce memory usage. */ if (storage==0) { ss = 1; se = 11; } else { ss = storage; se = storage+1; } for (matrix_type=ss;matrix_type<se;matrix_type++) { if ( nprocs>1 && matrix_type==9 ) continue; lis_matrix_duplicate(A0,&A); lis_matrix_set_type(A,matrix_type); err = lis_matrix_convert(A0,A); if( err ) CHKERR(err); comptime = 0.0; commtime = 0.0; for(i=0;i<iter;i++) { #ifdef USE_MPI MPI_Barrier(A->comm); time = lis_wtime(); lis_send_recv(A->commtable,x->value); commtime += lis_wtime() - time; #endif time2 = lis_wtime(); lis_matvec(A,x,b); comptime += lis_wtime() - time2; } lis_vector_nrm2(b,&val); if( my_rank==0 ) { flops = 2.0*nnz*iter*1.0e-6 / comptime; #ifdef USE_MPI #ifdef _LONG__DOUBLE #ifdef _LONG__LONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #endif #else #ifdef _LONG__LONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #endif #endif #else #ifdef _LONG__DOUBLE #ifdef _LONG__LONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #endif #else #ifdef _LONG__LONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #endif #endif #endif } lis_matrix_destroy(A); } lis_matrix_destroy(A0); lis_vector_destroy(b); lis_vector_destroy(x); lis_finalize(); LIS_DEBUG_FUNC_OUT; return 0; }
LIS_INT main(LIS_INT argc, char* argv[]) { LIS_MATRIX A; LIS_VECTOR b,x,u; LIS_SOLVER solver; LIS_INT my_rank; #ifdef USE_MPI int int_nprocs,int_my_rank; #endif LIS_INT err,i,n,gn,is,ie,iter; n = 12; lis_initialize(&argc, &argv); #ifdef USE_MPI MPI_Comm_size(MPI_COMM_WORLD,&int_nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&int_my_rank); my_rank = int_my_rank; #else my_rank = 0; #endif lis_matrix_create(LIS_COMM_WORLD,&A); err = lis_matrix_set_size(A,0,n); CHKERR(err); lis_matrix_get_size(A,&n,&gn); lis_matrix_get_range(A,&is,&ie); for(i=is;i<ie;i++) { if( i>0 ) lis_matrix_set_value(LIS_INS_VALUE,i,i-1,-1.0,A); if( i<gn-1 ) lis_matrix_set_value(LIS_INS_VALUE,i,i+1,-1.0,A); lis_matrix_set_value(LIS_INS_VALUE,i,i,2.0,A); } lis_matrix_set_type(A,LIS_MATRIX_CSR); lis_matrix_assemble(A); lis_vector_duplicate(A,&u); lis_vector_duplicate(A,&b); lis_vector_duplicate(A,&x); lis_vector_set_all(1.0,u); lis_matvec(A,u,b); lis_solver_create(&solver); lis_solver_set_option("-print mem",solver); lis_solver_set_optionC(solver); lis_solve(A,b,x,solver); lis_solver_get_iter(solver,&iter); if (my_rank==0) { #ifdef _LONG__LONG printf("number of iterations = %lld\n",iter); #else printf("number of iterations = %d\n",iter); #endif printf("\n"); } lis_vector_print(x); lis_matrix_destroy(A); lis_vector_destroy(b); lis_vector_destroy(x); lis_vector_destroy(u); lis_solver_destroy(solver); lis_finalize(); return 0; }
LIS_INT main(LIS_INT argc, char* argv[]) { LIS_MATRIX A,A0; LIS_VECTOR b,x; LIS_INT nprocs,my_rank; int int_nprocs,int_my_rank; LIS_INT nthreads, maxthreads; LIS_INT nnz; LIS_INT i,n,np; LIS_INT block; LIS_INT is,ie; LIS_INT err,iter,matrix_type; double time,time2,nnzs,nnzap,nnzt; LIS_SCALAR val; double commtime,comptime,flops; char path[1024]; FILE *file; LIS_DEBUG_FUNC_IN; lis_initialize(&argc, &argv); #ifdef USE_MPI MPI_Comm_size(MPI_COMM_WORLD,&int_nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&int_my_rank); nprocs = int_nprocs; my_rank = int_my_rank; #else nprocs = 1; my_rank = 0; #endif if( argc < 3 ) { if( my_rank==0 ) { printf("Usage: %s matrix_filename_list iter [block] \n", argv[0]); } lis_finalize(); exit(0); } file = fopen(argv[1], "r"); if( file==NULL ) CHKERR(1); iter = atoi(argv[2]); if (argv[3] == NULL) { block = 2; } else { block = atoi(argv[3]); } if( iter<=0 ) { #ifdef _LONG__LONG printf("iter=%lld <= 0\n",iter); #else printf("iter=%d <= 0\n",iter); #endif CHKERR(1); } if( my_rank==0 ) { printf("\n"); #ifdef _LONG__LONG printf("number of processes = %lld\n",nprocs); #else printf("number of processes = %d\n",nprocs); #endif } #ifdef _OPENMP if( my_rank==0 ) { nthreads = omp_get_num_procs(); maxthreads = omp_get_max_threads(); #ifdef _LONG__LONG printf("max number of threads = %lld\n", nthreads); printf("number of threads = %lld\n", maxthreads); #else printf("max number of threads = %d\n", nthreads); printf("number of threads = %d\n", maxthreads); #endif } #else nthreads = 1; maxthreads = 1; #endif /* create matrix and vectors */ while( fscanf(file, "%s\n", path)==1 ) { if( my_rank==0 ) { printf("matrix_filename = %s\n", path); } lis_matrix_create(LIS_COMM_WORLD,&A0); err = lis_input(A0,NULL,NULL,path); if( err ) CHKERR(err); n = A0->n; nnz = A0->nnz; np = A0->np-n; #ifdef USE_MPI MPI_Allreduce(&nnz,&i,1,LIS_MPI_INT,MPI_SUM,A0->comm); nnzap = (double)i / (double)nprocs; nnzt = ((double)nnz -nnzap)*((double)nnz -nnzap); nnz = i; MPI_Allreduce(&nnzt,&nnzs,1,MPI_DOUBLE,MPI_SUM,A0->comm); nnzs = (nnzs / (double)nprocs)/nnzap; MPI_Allreduce(&np,&i,1,LIS_MPI_INT,MPI_SUM,A0->comm); np = i; #endif if( my_rank==0 ) { #ifdef _LONG__LONG printf("block size of BSR and BSC = %lld x %lld\n",block,block); printf("number of iterations = %lld\n\n",iter); #else printf("block size of BSR and BSC = %d x %d\n",block,block); printf("number of iterations = %d\n\n",iter); #endif } err = lis_vector_duplicate(A0,&x); if( err ) CHKERR(err); err = lis_vector_duplicate(A0,&b); if( err ) CHKERR(err); lis_matrix_get_range(A0,&is,&ie); for(i=0;i<n;i++) { err = lis_vector_set_value(LIS_INS_VALUE,i+is,1.0,x); } /* MPI version of VBR is not implemented. DNS is also excluded to reduce memory usage. */ for (matrix_type=1;matrix_type<11;matrix_type++) { if ( nprocs>1 && matrix_type==9 ) continue; lis_matrix_duplicate(A0,&A); lis_matrix_set_type(A,matrix_type); err = lis_matrix_convert(A0,A); if( err ) CHKERR(err); if( my_rank==0 ) { if( A->matrix_type==LIS_MATRIX_BSR || A->matrix_type==LIS_MATRIX_BSC ) { A->bnr = block; A->bnc = block; } } comptime = 0.0; commtime = 0.0; for(i=0;i<iter;i++) { #ifdef USE_MPI MPI_Barrier(A->comm); time = lis_wtime(); lis_send_recv(A->commtable,x->value); commtime += lis_wtime() - time; #endif time2 = lis_wtime(); lis_matvec(A,x,b); comptime += lis_wtime() - time2; } lis_vector_nrm2(b,&val); if( my_rank==0 ) { flops = 2.0*nnz*iter*1.0e-6 / comptime; #ifdef USE_MPI #ifdef _LONG__DOUBLE #ifdef _LONG__LONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #endif #else #ifdef _LONG__LONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #endif #endif #else #ifdef _LONG__DOUBLE #ifdef _LONG__LONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #endif #else #ifdef _LONG__LONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #endif #endif #endif } lis_matrix_destroy(A); } lis_matrix_destroy(A0); lis_vector_destroy(b); lis_vector_destroy(x); } fclose(file); lis_finalize(); LIS_DEBUG_FUNC_OUT; return 0; }
LIS_INT lis_input_hb_csr(LIS_MATRIX A, LIS_VECTOR b, LIS_VECTOR x, FILE *file) { char buf[BUFSIZE]; char title[128], key[128], mtx[64], dat[128]; char *p; char MXTYPE_F,MXTYPE_S,MXTYPE_T; char RHSTYP_F,RHSTYP_S,RHSTYP_T; LIS_INT TOTCRD,PTRCRD,INDCRD,VALCRD,RHSCRD; LIS_INT NROW,NCOL,NNZERO,NELTVL; LIS_INT NRHS,NRHSIX; LIS_INT iptr,iind,ival,irhs; LIS_INT wptr,wind,wval,wrhs; LIS_INT i,k,j,my_rank; LIS_INT err; LIS_INT n,is,ie; LIS_INT *ptr, *index; LIS_INT matrix_type; LIS_SCALAR *value; LIS_MATRIX B; #ifdef USE_MPI MPI_Comm_rank(A->comm,&my_rank); #else my_rank = 0; #endif matrix_type = A->matrix_type; /* Line 1 */ if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } strncpy(title, buf ,72); title[72] = '\0'; strncpy(key ,&buf[72], 8); key[8] = '\0'; printf("title: %s\n",title); printf("key : %s\n",key); /* Line 2 */ if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } #ifdef _LONGLONG if( sscanf(buf, "%14lld%14lld%14lld%14lld%14lld", &TOTCRD, &PTRCRD, &INDCRD, &VALCRD, &RHSCRD) != 5 ) #else if( sscanf(buf, "%14d%14d%14d%14d%14d", &TOTCRD, &PTRCRD, &INDCRD, &VALCRD, &RHSCRD) != 5 ) #endif { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } #ifdef _LONGLONG printf("%14lld%14lld%14lld%14lld%14lld\n",TOTCRD, PTRCRD, INDCRD, VALCRD, RHSCRD); #else printf("%14d%14d%14d%14d%14d\n",TOTCRD, PTRCRD, INDCRD, VALCRD, RHSCRD); #endif /* Line 3 */ if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } #ifdef _LONGLONG if( sscanf(buf, "%s %lld %lld %lld %lld", mtx, &NROW, &NCOL, &NNZERO, &NELTVL) != 5 ) #else if( sscanf(buf, "%s %d %d %d %d", mtx, &NROW, &NCOL, &NNZERO, &NELTVL) != 5 ) #endif { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } for(p=mtx;*p!='\0';p++) *p = (char)tolower(*p); MXTYPE_F = mtx[0]; MXTYPE_S = mtx[1]; MXTYPE_T = mtx[2]; if( mtx[0]!='r' ) { LIS_SETERR(LIS_ERR_FILE_IO,"Not real\n"); return LIS_ERR_FILE_IO; } /* if( mtx[1]!='u' ) { LIS_SETERR(LIS_ERR_FILE_IO,"Not unsymmetric\n"); return LIS_ERR_FILE_IO; } */ if( mtx[2]!='a' ) { LIS_SETERR(LIS_ERR_FILE_IO,"Not assembled\n"); return LIS_ERR_FILE_IO; } if( NROW!=NCOL ) { LIS_SETERR(LIS_ERR_FILE_IO,"matrix is not square\n"); return LIS_ERR_FILE_IO; } #ifdef _LONGLONG printf("%c%c%c %lld %lld %lld %lld\n",MXTYPE_F, MXTYPE_S, MXTYPE_T, NROW, NCOL, NNZERO, NELTVL); #else printf("%c%c%c %d %d %d %d\n",MXTYPE_F, MXTYPE_S, MXTYPE_T, NROW, NCOL, NNZERO, NELTVL); #endif /* Line 4 */ if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } lis_input_hb_get_fmt( buf ,16,&iptr,&wptr); lis_input_hb_get_fmt(&buf[16],16,&iind,&wind); lis_input_hb_get_fmt(&buf[32],20,&ival,&wval); lis_input_hb_get_fmt(&buf[52],20,&irhs,&wrhs); #ifdef _LONGLONG printf("%lld %lld %lld %lld\n",iptr,iind,ival,irhs); printf("%lld %lld %lld %lld\n",wptr,wind,wval,wrhs); #else printf("%d %d %d %d\n",iptr,iind,ival,irhs); printf("%d %d %d %d\n",wptr,wind,wval,wrhs); #endif /* Line 5 */ if( RHSCRD!=0 ) { if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } #ifdef _LONGLONG sscanf(buf, "%s %lld %lld", mtx, &NRHS, &NRHSIX); #else sscanf(buf, "%s %d %d", mtx, &NRHS, &NRHSIX); #endif /* #ifdef _LONGLONG if( sscanf(buf, "%s %lld %lld", mtx, &NRHS, &NRHSIX) != 3 ) #else if( sscanf(buf, "%s %d %d", mtx, &NRHS, &NRHSIX) != 3 ) #endif { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } */ for(p=mtx;*p!='\0';p++) *p = (char)tolower(*p); RHSTYP_F = mtx[0]; RHSTYP_S = mtx[1]; RHSTYP_T = mtx[2]; #ifdef _LONGLONG printf("%c%c%c %lld %lld\n",RHSTYP_F, RHSTYP_S, RHSTYP_T, NRHS, NRHSIX); #else printf("%c%c%c %d %d\n",RHSTYP_F, RHSTYP_S, RHSTYP_T, NRHS, NRHSIX); #endif } err = lis_matrix_set_size(A,0,NROW); if( err ) { return err; } n = A->n; lis_matrix_get_range(A,&is,&ie); err = lis_matrix_malloc_csr(n,NNZERO,&ptr,&index,&value); if( err ) { return err; } /* read data */ k = 0; for( i=0; i<PTRCRD; i++ ) { if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } p = buf; for(j=0;j<iptr&&k<n+1;j++) { strncpy(dat, p, wptr); dat[wptr] = '\0'; ptr[k] = atoi(dat) - 1; p += wptr; k++; } } k = 0; for( i=0; i<INDCRD; i++ ) { if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } p = buf; for(j=0;j<iind&&k<NNZERO;j++) { strncpy(dat, p, wind); dat[wind] = '\0'; index[k] = atoi(dat) - 1; p += wind; k++; } } k = 0; for( i=0; i<VALCRD; i++ ) { if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } p = buf; for(j=0;j<ival&&k<NNZERO;j++) { strncpy(dat, p, wval); dat[wval] = '\0'; value[k] = atof(dat); p += wval; k++; } } if( RHSCRD>0 ) { /* k = 0; for( i=0; i<RHSCRD; i++ ) { if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; return LIS_ERR_FILE_IO; } p = buf; for(j=0;j<ival&&k<NNZERO;j++) { strncpy(dat, p, wval); dat[wval] = '\0'; value[k] = atof(dat); p += wval; printf("%e ",value[k]); k++; } printf("\n"); } */ } err = lis_matrix_set_csc(NNZERO,ptr,index,value,A); if( err ) { return err; } err = lis_matrix_assemble(A); if( err ) return err; if( matrix_type!=LIS_MATRIX_CSC ) { err = lis_matrix_duplicate(A,&B); if( err ) return err; lis_matrix_set_type(B,LIS_MATRIX_CSR); err = lis_matrix_convert_csc2csr(A,B); if( err ) return err; lis_matrix_storage_destroy(A); lis_matrix_DLU_destroy(A); lis_matrix_diag_destroy(A->WD); if( A->l2g_map ) lis_free( A->l2g_map ); if( A->commtable ) lis_commtable_destroy( A->commtable ); if( A->ranges ) lis_free( A->ranges ); err = lis_matrix_copy_struct(B,A); if( err ) return err; lis_free(B); } return LIS_SUCCESS; }
LIS_INT lis_input_mm_csr(LIS_MATRIX A, LIS_VECTOR b, LIS_VECTOR x, FILE *file) { char buf[BUFSIZE]; LIS_INT nr,nc,nnz; LIS_INT i,j,my_rank; LIS_INT err; LIS_INT mmtype,mode; LIS_INT n,is,ie; LIS_INT ridx,cidx; LIS_INT *ptr, *index; LIS_INT *work; LIS_INT isb,isx,isbin; LIS_SCALAR val; LIS_SCALAR *value; LIS_MM_MATFMT matfmt; LIS_DEBUG_FUNC_IN; #ifdef USE_MPI my_rank = A->my_rank; #else my_rank = 0; #endif /* check banner */ err = lis_input_mm_banner(file,&mmtype); if( err ) return err; /* check size */ err = lis_input_mm_size(file,&nr,&nc,&nnz,&isb,&isx,&isbin); if( err ) return err; err = lis_matrix_set_size(A,0,nr); if( err ) return err; #ifdef _LONGLONG if( my_rank==0 ) printf("matrix size = %lld x %lld (%lld nonzero entries)\n\n",nr,nc,nnz); #else if( my_rank==0 ) printf("matrix size = %d x %d (%d nonzero entries)\n\n",nr,nc,nnz); #endif n = A->n; ptr = NULL; index = NULL; value = NULL; work = NULL; lis_matrix_get_range(A,&is,&ie); ptr = (LIS_INT *)lis_malloc( (n+1)*sizeof(LIS_INT),"lis_input_mm_csr::ptr" ); if( ptr==NULL ) { LIS_SETERR_MEM((n+1)*sizeof(LIS_INT)); lis_free2(4,ptr,index,value,work); return LIS_OUT_OF_MEMORY; } work = (LIS_INT *)lis_malloc( (n+1)*sizeof(LIS_INT),"lis_input_mm_csr::work" ); if( work==NULL ) { LIS_SETERR_MEM((n+1)*sizeof(LIS_INT)); lis_free2(4,ptr,index,value,work); return LIS_OUT_OF_MEMORY; } #ifdef _OPENMP #pragma omp parallel for private(i) #endif for(i=0;i<n+1;i++) { ptr[i] = 0; work[i] = 0; } /* read data */ mode = 1; mode = *(char *)&mode; if( mode!=(isbin-1) ) { mode = LIS_TRUE; } else { mode = LIS_FALSE; } for( i=0; i<nnz; i++ ) { if( isbin ) { if( fread(&matfmt, sizeof(matfmt), 1, file)!=1 ) { LIS_SETERR_FIO; lis_free2(4,ptr,index,value,work); return LIS_ERR_FILE_IO; } ridx = matfmt.i; cidx = matfmt.j; if( mode ) { lis_bswap_int(1,&ridx); lis_bswap_int(1,&cidx); } } else { if( fgets(buf, BUFSIZE, file)==NULL ) { LIS_SETERR_FIO; lis_free2(4,ptr,index,value,work); return LIS_ERR_FILE_IO; } #ifdef _LONGLONG #ifdef _LONG__DOUBLE if( sscanf(buf, "%lld %lld %Lg", &ridx, &cidx, &val) != 3 ) #else if( sscanf(buf, "%lld %lld %lg", &ridx, &cidx, &val) != 3 ) #endif #else #ifdef _LONG__DOUBLE if( sscanf(buf, "%d %d %Lg", &ridx, &cidx, &val) != 3 ) #else if( sscanf(buf, "%d %d %lg", &ridx, &cidx, &val) != 3 ) #endif #endif { LIS_SETERR_FIO; lis_free2(4,ptr,index,value,work); return LIS_ERR_FILE_IO; } } /* if( val!=0.0 )*/ { if( mmtype==MM_SYMM && ridx!=cidx ) { if( cidx>is && cidx<=ie ) work[cidx-is-1]++; } if( ridx>is && ridx<=ie ) { ptr[ridx-is]++; } } } ptr[0] = 0; for( i=0; i<n; i++ ) { if( mmtype==MM_SYMM ) { ptr[i+1] += ptr[i] + work[i]; } else { ptr[i+1] += ptr[i]; } work[i] = 0; } index = (LIS_INT *)lis_malloc( ptr[n]*sizeof(LIS_INT),"lis_input_mm_csr::index" ); if( index==NULL ) { LIS_SETERR_MEM(ptr[n]*sizeof(LIS_INT)); lis_free2(4,ptr,index,value,work); return LIS_OUT_OF_MEMORY; } value = (LIS_SCALAR *)lis_malloc( ptr[n]*sizeof(LIS_SCALAR),"lis_input_mm_csr::value" ); if( value==NULL ) { LIS_SETERR_MEM(ptr[n]*sizeof(LIS_SCALAR)); lis_free2(4,ptr,index,value,work); return LIS_OUT_OF_MEMORY; } #ifdef _OPENMP #pragma omp parallel for private(i,j) #endif for(i=0;i<n;i++) { for(j=ptr[i];j<ptr[i+1];j++) { index[j] = 0; value[j] = 0.0; } } rewind(file); if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; lis_free2(4,ptr,index,value,work); return LIS_ERR_FILE_IO; } do { if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; lis_free2(4,ptr,index,value,work); return LIS_ERR_FILE_IO; } }while( buf[0]=='%' ); for( i=0; i<nnz; i++ ) { if( isbin ) { if( fread(&matfmt, sizeof(matfmt), 1, file)!=1 ) { LIS_SETERR_FIO; lis_free2(4,ptr,index,value,work); return LIS_ERR_FILE_IO; } ridx = matfmt.i; cidx = matfmt.j; val = matfmt.value; if( mode ) { lis_bswap_int(1,&ridx); lis_bswap_int(1,&cidx); lis_bswap_scalar(1,&val); } } else { if( fgets(buf, BUFSIZE, file) == NULL ) { LIS_SETERR_FIO; lis_free2(4,ptr,index,value,work); return LIS_ERR_FILE_IO; } #ifdef _LONGLONG #ifdef _LONG__DOUBLE if( sscanf(buf, "%lld %lld %Lg", &ridx, &cidx, &val) != 3 ) #else if( sscanf(buf, "%lld %lld %lg", &ridx, &cidx, &val) != 3 ) #endif #else #ifdef _LONG__DOUBLE if( sscanf(buf, "%d %d %Lg", &ridx, &cidx, &val) != 3 ) #else if( sscanf(buf, "%d %d %lg", &ridx, &cidx, &val) != 3 ) #endif #endif { LIS_SETERR_FIO; lis_free2(4,ptr,index,value,work); return LIS_ERR_FILE_IO; } } ridx--; cidx--; if( ridx==cidx && val==0.0 ) { #ifdef _LONGLONG printf("diagonal element is zero (i=%lld)\n",ridx); #else printf("diagonal element is zero (i=%d)\n",ridx); #endif } /* if( val!=0.0 )*/ { if( mmtype==MM_SYMM && ridx!=cidx ) { if( cidx>=is && cidx<ie ) { value[ptr[cidx-is]+work[cidx-is]] = val; index[ptr[cidx-is]+work[cidx-is]] = ridx; work[cidx-is]++; } } if( ridx>=is && ridx<ie ) { value[ptr[ridx-is]+work[ridx-is]] = val; index[ptr[ridx-is]+work[ridx-is]] = cidx; work[ridx-is]++; } } } #ifdef USE_MPI MPI_Barrier(A->comm); #endif err = lis_matrix_set_csr(ptr[n],ptr,index,value,A); if( err ) { lis_free2(4,ptr,index,value,work); return err; } err = lis_matrix_assemble(A); if( err ) { lis_matrix_storage_destroy(A); lis_free(work); return err; } if( b!=NULL && x!=NULL ) { err = lis_input_mm_vec(A,b,x,file,isb,isx,isbin); if( err ) { lis_matrix_storage_destroy(A); lis_free(work); } } lis_free(work); LIS_DEBUG_FUNC_OUT; return LIS_SUCCESS; }
LIS_INT main(LIS_INT argc, char* argv[]) { LIS_MATRIX A,A0; LIS_VECTOR b,x,v; LIS_SCALAR ntimes,nmflops,nnrm2; LIS_SCALAR *value; LIS_INT nprocs,my_rank; int int_nprocs,int_my_rank; LIS_INT nthreads, maxthreads; LIS_INT gn,nnz,mode; LIS_INT i,j,jj,j0,j1,l,k,n,np,h,ih; LIS_INT m,nn,ii; LIS_INT block; LIS_INT rn,rmin,rmax,rb; LIS_INT is,ie,clsize,ci,*iw; LIS_INT err,iter,matrix_type; LIS_INT *ptr,*index; double mem,val,ra,rs,ri,ria,ca,time,time2,convtime,val2,nnzs,nnzap,nnzt; double commtime,comptime,flops; FILE *file; char path[1024]; LIS_DEBUG_FUNC_IN; lis_initialize(&argc, &argv); #ifdef USE_MPI MPI_Comm_size(MPI_COMM_WORLD,&int_nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&int_my_rank); nprocs = int_nprocs; my_rank = int_my_rank; #else nprocs = 1; my_rank = 0; #endif if( argc < 4 ) { if( my_rank==0 ) printf("Usage: spmvtest5 matrix_filename matrix_type iter [block] \n"); lis_finalize(); exit(0); } file = fopen(argv[1], "r"); if( file==NULL ) CHKERR(1); matrix_type = atoi(argv[2]); iter = atoi(argv[3]); if (argv[4] == NULL) { block = 2; } else { block = atoi(argv[4]); } if( matrix_type<1 || matrix_type>11 ) { if( my_rank==0 ) printf("matrix_type=%d <1 or matrix_type=%d >11\n",matrix_type,matrix_type); CHKERR(1); } if( iter<=0 ) { if( my_rank==0 ) printf("iter=%d <= 0\n",iter); CHKERR(1); } if( my_rank==0 ) { printf("\n"); printf("number of processes = %d\n",nprocs); } #ifdef _OPENMP if( my_rank==0 ) { nthreads = omp_get_num_procs(); maxthreads = omp_get_max_threads(); printf("max number of threads = %d\n", nthreads); printf("number of threads = %d\n", maxthreads); } #else nthreads = 1; maxthreads = 1; #endif /* create matrix and vectors */ lis_matrix_create(LIS_COMM_WORLD,&A0); err = lis_input(A0,NULL,NULL,argv[1]); CHKERR(err); n = A0->n; gn = A0->gn; nnz = A0->nnz; np = A0->np-n; #ifdef USE_MPI MPI_Allreduce(&nnz,&i,1,LIS_MPI_INT,MPI_SUM,A0->comm); nnzap = (double)i / (double)nprocs; nnzt = ((double)nnz -nnzap)*((double)nnz -nnzap); nnz = i; MPI_Allreduce(&nnzt,&nnzs,1,MPI_DOUBLE,MPI_SUM,A0->comm); nnzs = (nnzs / (double)nprocs)/nnzap; MPI_Allreduce(&np,&i,1,LIS_MPI_INT,MPI_SUM,A0->comm); np = i; #endif if( my_rank==0 ) { printf("block size of BSR and BSC = %d x %d\n",block,block); printf("iteration count = %d\n\n",iter); } err = lis_vector_duplicate(A0,&x); if( err ) CHKERR(err); err = lis_vector_duplicate(A0,&b); if( err ) CHKERR(err); lis_matrix_get_range(A0,&is,&ie); for(i=0;i<n;i++) { err = lis_vector_set_value(LIS_INS_VALUE,i+is,1.0,x); } lis_matrix_duplicate(A0,&A); lis_matrix_set_type(A,matrix_type); err = lis_matrix_convert(A0,A); if( err ) CHKERR(err); if( A->matrix_type==LIS_MATRIX_BSR || A->matrix_type==LIS_MATRIX_BSC ) { A->bnr = block; A->bnc = block; } comptime = 0.0; commtime = 0.0; for(i=0;i<iter;i++) { #ifdef USE_MPI MPI_Barrier(A->comm); time = lis_wtime(); lis_send_recv(A->commtable,x->value); commtime += lis_wtime() - time; #endif time2 = lis_wtime(); lis_matvec(A,x,b); comptime += lis_wtime() - time2; } lis_vector_nrm2(b,&val); if( my_rank==0 ) { flops = 2.0*nnz*iter*1.0e-6 / comptime; printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); } lis_matrix_destroy(A); lis_matrix_destroy(A0); lis_vector_destroy(b); lis_vector_destroy(x); lis_finalize(); LIS_DEBUG_FUNC_OUT; return 0; }
LIS_INT main(LIS_INT argc, char* argv[]) { LIS_MATRIX A,A0; LIS_VECTOR b,x,v; LIS_SCALAR ntimes,nmflops,nnrm2; LIS_SCALAR *value; LIS_INT nprocs,my_rank; int int_nprocs,int_my_rank; LIS_INT nthreads, maxthreads; LIS_INT gn,nnz,mode; LIS_INT i,ii,j,jj,j0,j1,l,k,n,np,h,ih; LIS_INT rn,rmin,rmax,rb; LIS_INT is,ie,clsize,ci,*iw; LIS_INT err,iter,matrix_type,s,ss,se; LIS_INT *ptr,*index; double mem,ra,rs,ri,ria,ca,time,time2,convtime,nnzs,nnzap,nnzt; LIS_SCALAR val; double commtime,comptime,flops; FILE *file; char path[1024]; LIS_DEBUG_FUNC_IN; lis_initialize(&argc, &argv); #ifdef USE_MPI MPI_Comm_size(MPI_COMM_WORLD,&int_nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&int_my_rank); nprocs = int_nprocs; my_rank = int_my_rank; #else nprocs = 1; my_rank = 0; #endif if( argc < 3 ) { if( my_rank==0 ) { printf("Usage: %s n iter [matrix_type]\n", argv[0]); } CHKERR(1); } n = atoi(argv[1]); iter = atoi(argv[2]); if (argv[3] == NULL) { s = 0; } else { s = atoi(argv[3]); } if( n<=0 ) { #ifdef _LONGLONG if( my_rank==0 ) printf("n=%lld <=0\n",n); #else if( my_rank==0 ) printf("n=%d <=0\n",n); #endif CHKERR(1); } if( iter<=0 ) { #ifdef _LONGLONG if( my_rank==0 ) printf("iter=%lld <= 0\n",iter); #else if( my_rank==0 ) printf("iter=%d <= 0\n",iter); #endif CHKERR(1); } if( s<0 || s>11 ) { #ifdef _LONGLONG if( my_rank==0 ) printf("matrix_type=%lld < 0 or matrix_type=%lld > 11\n",s,s); #else if( my_rank==0 ) printf("matrix_type=%d < 0 or matrix_type=%d > 11\n",s,s); #endif CHKERR(1); } if( my_rank==0 ) { printf("\n"); #ifdef _LONGLONG printf("number of processes = %lld\n",nprocs); #else printf("number of processes = %d\n",nprocs); #endif } #ifdef _OPENMP nthreads = omp_get_num_procs(); maxthreads = omp_get_max_threads(); if( my_rank==0 ) { #ifdef _LONGLONG printf("max number of threads = %lld\n", nthreads); printf("number of threads = %lld\n", maxthreads); #else printf("max number of threads = %d\n", nthreads); printf("number of threads = %d\n", maxthreads); #endif } #else nthreads = 1; maxthreads = 1; #endif /* create matrix and vectors */ lis_matrix_create(LIS_COMM_WORLD,&A0); lis_matrix_set_size(A0,0,n); lis_matrix_get_size(A0,&n,&gn); lis_matrix_get_range(A0,&is,&ie); k = 0; for(i=is;i<ie;i++) { if( i>0 ) lis_matrix_set_value(LIS_INS_VALUE,i,i-1,-1.0,A0); if( i<gn-1 ) lis_matrix_set_value(LIS_INS_VALUE,i,i+1,-1.0,A0); lis_matrix_set_value(LIS_INS_VALUE,i,i,2.0,A0); } err = lis_matrix_assemble(A0); CHKERR(err); n = A0->n; gn = A0->gn; nnz = A0->nnz; np = A0->np-n; #ifdef USE_MPI MPI_Allreduce(&nnz,&i,1,LIS_MPI_INT,MPI_SUM,A0->comm); nnzap = (double)i / (double)nprocs; nnzt = ((double)nnz -nnzap)*((double)nnz -nnzap); nnz = i; MPI_Allreduce(&nnzt,&nnzs,1,MPI_DOUBLE,MPI_SUM,A0->comm); nnzs = (nnzs / (double)nprocs)/nnzap; MPI_Allreduce(&np,&i,1,LIS_MPI_INT,MPI_SUM,A0->comm); np = i; #endif if( my_rank==0 ) { #ifdef _LONGLONG printf("matrix size = %lld x %lld (%lld nonzero entries)\n",gn,gn,nnz); printf("iteration count = %lld\n\n",iter); #else printf("matrix size = %d x %d (%d nonzero entries)\n",gn,gn,nnz); printf("iteration count = %d\n\n",iter); #endif } err = lis_vector_duplicate(A0,&x); if( err ) CHKERR(err); err = lis_vector_duplicate(A0,&b); if( err ) CHKERR(err); lis_matrix_get_range(A0,&is,&ie); for(i=0;i<n;i++) { err = lis_vector_set_value(LIS_INS_VALUE,i+is,1.0,x); } /* MPI version of VBR is not implemented. DNS is also excluded to reduce memory usage. */ if (s==0) { ss = 1; se = 11; } else { ss = s; se = s+1; } for (matrix_type=ss;matrix_type<se;matrix_type++) { if ( nprocs>1 && matrix_type==9 ) continue; lis_matrix_duplicate(A0,&A); lis_matrix_set_type(A,matrix_type); err = lis_matrix_convert(A0,A); if( err ) CHKERR(err); comptime = 0.0; commtime = 0.0; for(i=0;i<iter;i++) { #ifdef USE_MPI MPI_Barrier(A->comm); time = lis_wtime(); lis_send_recv(A->commtable,x->value); commtime += lis_wtime() - time; MPI_Barrier(A->comm); #endif time2 = lis_wtime(); lis_matvec(A,x,b); comptime += lis_wtime() - time2; } lis_vector_nrm2(b,&val); if( my_rank==0 ) { flops = 2.0*nnz*iter*1.0e-6 / comptime; #ifdef USE_MPI #ifdef _LONG__DOUBLE #ifdef _LONGLONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #endif #else #ifdef _LONGLONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, communication = %e sec, communication/computation = %3.3f %%, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,commtime,commtime/comptime*100,val); #endif #endif #else #ifdef _LONG__DOUBLE #ifdef _LONGLONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %Le\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #endif #else #ifdef _LONGLONG printf("matrix_type = %2lld (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #else printf("matrix_type = %2d (%s), computation = %e sec, %8.3f MFLOPS, 2-norm = %e\n",matrix_type,lis_storagename2[matrix_type-1],comptime,flops,val); #endif #endif #endif } lis_matrix_destroy(A); } lis_matrix_destroy(A0); lis_vector_destroy(b); lis_vector_destroy(x); lis_finalize(); LIS_DEBUG_FUNC_OUT; return 0; }