bool MatrixMarketReader<FloatType>::MMReadHeader( FILE* mm_file ) { int status = MMReadBanner( mm_file ); if( status != 0 ) { printf( "Error Reading Banner in Matrix-Market File !\n" ); return 1; } if( !mm_is_coordinate( Typecode ) ) { printf( "Handling only coordinate format\n" ); return( 1 ); } if( mm_is_complex( Typecode ) ) { printf( "Error: cannot handle complex format\n" ); return ( 1 ); } if( mm_is_symmetric( Typecode ) ) isSymmetric = 1; status = MMReadMtxCrdSize( mm_file ); if( status != 0 ) { printf( "Error reading Matrix Market crd_size %d\n", status ); return( 1 ); } return 0; }
char *mm_typecode_to_str(MM_typecode matcode) { char buffer[MM_MAX_LINE_LENGTH]; char *types[4]; int error =0; int i; /* check for MTX type */ if (mm_is_matrix(matcode)) types[0] = MM_MTX_STR; else error=1; /* check for CRD or ARR matrix */ if (mm_is_sparserow(matcode)) types[1] = MM_SPARSEROW_STR; else if (mm_is_coordinate(matcode)) types[1] = MM_COORDINATE_STR; else if (mm_is_dense(matcode)) types[1] = MM_DENSE_STR; else return NULL; /* check for element data type */ if (mm_is_real(matcode)) types[2] = MM_REAL_STR; else if (mm_is_complex(matcode)) types[2] = MM_COMPLEX_STR; else if (mm_is_pattern(matcode)) types[2] = MM_PATTERN_STR; else if (mm_is_integer(matcode)) types[2] = MM_INT_STR; else return NULL; /* check for symmetry type */ if (mm_is_general(matcode)) types[3] = MM_GENERAL_STR; else if (mm_is_symmetric(matcode)) types[3] = MM_SYMM_STR; else if (mm_is_hermitian(matcode)) types[3] = MM_HERM_STR; else if (mm_is_skew(matcode)) types[3] = MM_SKEW_STR; else return NULL; sprintf(buffer,"%s %s %s %s", types[0], types[1], types[2], types[3]); return strdup(buffer); }
int readBandedMatrix(char* filename, int& n, int& t, int& nz, long long*& AR, long long*& AC, double*& AV) { // try opening the files FILE *fp; if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "Error occurs while reading from file %s.\n", filename); return 1; } // try reading the banner MM_typecode type; if (mm_read_banner(fp, &type)) { fprintf(stderr, "Could not process Matrix Market banner.\n"); return 2; } // check the type if (!mm_is_matrix(type) || !mm_is_coordinate(type) || !mm_is_real(type) || !mm_is_general(type)) { fprintf(stderr, "Sorry, this application does not support Market Market type: [%s]\n", mm_typecode_to_str(type)); return 3; } // read the sizes and nnz of the matrix int m; if (mm_read_mtx_crd_size(fp, &n, &m, &nz)) { fprintf(stderr, "Could not read the size of the matrix.\n"); return 4; } printf("reading %s:\n\ta %d x %d banded matrix ", filename, n, m); // allocate the memory AR = new long long[nz]; AC = new long long[nz]; AV = new double[nz]; t = 0; for (int i = 0; i < nz; ++i) { fscanf(fp, "%d %d %lf\n", AR + i, AC + i, AV + i); --AR[i]; // 0-indexing --AC[i]; // 0-indexing t = std::max(t, (int)(AR[i] - AC[i])); } printf("with bandwidth (2m + 1) = %d...done\n", 2 * t + 1); // close the file fclose(fp); return 0; }
/*---------------------------------------------* * READ COO Matrix Market * *---------------------------------------------*/ int read_coo_MM(coo_t *coo, options_t *opts) { char *matfile = opts->fmatname; MM_typecode matcode; FILE *p = fopen(matfile,"r"); if (p == NULL) { printf("Unable to open file %s\n", matfile); exit(1); } /*----------- READ MM banner */ if (mm_read_banner(p, &matcode) != 0) { printf("Could not process Matrix Market banner.\n"); exit(1); } if (!mm_is_valid(matcode)) { printf("Invalid Matrix Market file.\n"); exit(1); } if (!(mm_is_real(matcode) && mm_is_coordinate(matcode) && mm_is_sparse(matcode))) { printf("Only sparse real-valued coordinate \ matrices are supported\n"); exit(1); }
int MatrixMarketFileToRowMap(const char* filename, const Epetra_Comm& comm, Epetra_BlockMap*& rowmap) { FILE* infile = fopen(filename, "r"); MM_typecode matcode; int err = mm_read_banner(infile, &matcode); if (err != 0) return(err); if (!mm_is_matrix(matcode) || !mm_is_coordinate(matcode) || !mm_is_real(matcode) || !mm_is_general(matcode)) { return(-1); } int numrows, numcols; err = mm_read_mtx_array_size(infile, &numrows, &numcols); if (err != 0) return(err); fclose(infile); rowmap = new Epetra_BlockMap(numrows, 1, 0, comm); return(0); }
int main(int argc, char *argv[]) { FILE *input_file, *output_file, *time_file; if (argc < 4) { fprintf(stderr, "Invalid input parameters\n"); fprintf(stderr, "Usage: %s inputfile outputfile timefile \n", argv[0]); exit(1); } else { input_file = fopen(argv[1], "r"); output_file = fopen(argv[2], "w"); time_file = fopen(argv[3], "w"); if (!input_file || !output_file || !time_file) exit(1); } MM_typecode matcode; if (mm_read_banner(input_file, &matcode) != 0) { printf("Could not process Matrix Market banner.\n"); exit(1); } if (!mm_is_matrix(matcode) || !mm_is_real(matcode) || !mm_is_coordinate(matcode)) { printf("Sorry, this application does not support "); printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode)); exit(1); } mtxMatrix inputMatrix, fullMatrix, LMatrix, UMatrix, UMatrixTranspose, MMatrix; ReadMatrix(inputMatrix, input_file); Timer timer; getRowIndex(&inputMatrix, inputMatrix.RowIndex); inputMatrix.RowIndex[inputMatrix.N] = inputMatrix.NZ; int diagNum = 0; for (int i = 0; i < inputMatrix.N; i++) { for (int j = inputMatrix.RowIndex[i]; j < inputMatrix.RowIndex[i + 1]; j++) { if (i == inputMatrix.Col[j]) diagNum++; } } if (mm_is_symmetric(matcode)) { InitializeMatrix(inputMatrix.N, 2 * inputMatrix.NZ - diagNum, fullMatrix); TriangleToFull(&inputMatrix, &fullMatrix); FreeMatrix(inputMatrix); } else { fullMatrix = inputMatrix; } int *diag = new int[fullMatrix.N]; for (int i = 0; i < fullMatrix.N; i++) { for (int j = fullMatrix.RowIndex[i]; j < fullMatrix.RowIndex[i + 1]; j++) { if (i == fullMatrix.Col[j]) diag[i] = j; } } // for (int i = 0; i < fullMatrix.N + 1; i++) { // printf("RowIndex[%i] = %i\n", i, fullMatrix.RowIndex[i]); // } // // // for (int i = 0; i < fullMatrix.N; i++) { // printf("input[%i]= %lf\n", i, fullMatrix.Value[inputMatrix.RowIndex[i]]); // } // // for (int i = 0; i < fullMatrix.N; i++) { // printf("diag[%i]= %d\n", i, diag[i]); // } timer.start(); ilu0(fullMatrix, fullMatrix.Value, diag); LUmatrixSeparation(fullMatrix, diag, LMatrix, UMatrix); Transpose(UMatrix, UMatrixTranspose); Multiplicate(LMatrix, UMatrixTranspose, MMatrix); timer.stop(); std::ofstream timeLog; timeLog.open(argv[3]); timeLog << timer.getElapsed(); WriteFullMatrix(MMatrix, output_file, matcode); FreeMatrix(fullMatrix); return 0; }
int MatrixMarketFileToBlockMaps(const char* filename, const Epetra_Comm& comm, Epetra_BlockMap*& rowmap, Epetra_BlockMap*& colmap, Epetra_BlockMap*& rangemap, Epetra_BlockMap*& domainmap) { FILE* infile = fopen(filename, "r"); if (infile == NULL) { return(-1); } MM_typecode matcode; int err = mm_read_banner(infile, &matcode); if (err != 0) return(err); if (!mm_is_matrix(matcode) || !mm_is_coordinate(matcode) || !mm_is_real(matcode) || !mm_is_general(matcode)) { return(-1); } int numrows, numcols, nnz; err = mm_read_mtx_crd_size(infile, &numrows, &numcols, &nnz); if (err != 0) return(err); //for this case, we'll assume that the row-map is the same as //the range-map. //create row-map and range-map with linear distributions. rowmap = new Epetra_BlockMap(numrows, 1, 0, comm); rangemap = new Epetra_BlockMap(numrows, 1, 0, comm); int I, J; double val, imag; int num_map_cols = 0, insertPoint, foundOffset; int allocLen = numcols; int* map_cols = new int[allocLen]; //read through all matrix data and construct a list of the column- //indices that occur in rows that are local to this processor. for(int i=0; i<nnz; ++i) { err = mm_read_mtx_crd_entry(infile, &I, &J, &val, &imag, matcode); if (err == 0) { --I; --J; if (rowmap->MyGID(I)) { foundOffset = Epetra_Util_binary_search(J, map_cols, num_map_cols, insertPoint); if (foundOffset < 0) { Epetra_Util_insert(J, insertPoint, map_cols, num_map_cols, allocLen); } } } } //create colmap with the list of columns associated with rows that are //local to this processor. colmap = new Epetra_Map(-1, num_map_cols, map_cols, 0, comm); //create domainmap which has a linear distribution domainmap = new Epetra_BlockMap(numcols, 1, 0, comm); delete [] map_cols; return(0); }
magma_int_t magma_d_csr_mtx( magma_d_matrix *A, const char *filename, magma_queue_t queue ) { char buffer[ 1024 ]; magma_int_t info = 0; int csr_compressor = 0; // checks for zeros in original file magma_d_matrix B={Magma_CSR}; magma_index_t *coo_col = NULL; magma_index_t *coo_row = NULL; double *coo_val = NULL; double *new_val = NULL; magma_index_t* new_row = NULL; magma_index_t* new_col = NULL; magma_int_t symmetric = 0; std::vector< std::pair< magma_index_t, double > > rowval; FILE *fid = NULL; MM_typecode matcode; fid = fopen(filename, "r"); if (fid == NULL) { printf("%% Unable to open file %s\n", filename); info = MAGMA_ERR_NOT_FOUND; goto cleanup; } printf("%% Reading sparse matrix from file (%s):", filename); fflush(stdout); if (mm_read_banner(fid, &matcode) != 0) { printf("\n%% Could not process Matrix Market banner: %s.\n", matcode); info = MAGMA_ERR_NOT_SUPPORTED; goto cleanup; } if (!mm_is_valid(matcode)) { printf("\n%% Invalid Matrix Market file.\n"); info = MAGMA_ERR_NOT_SUPPORTED; goto cleanup; } if ( ! ( ( mm_is_real(matcode) || mm_is_integer(matcode) || mm_is_pattern(matcode) || mm_is_real(matcode) ) && mm_is_coordinate(matcode) && mm_is_sparse(matcode) ) ) { mm_snprintf_typecode( buffer, sizeof(buffer), matcode ); printf("\n%% Sorry, MAGMA-sparse does not support Market Market type: [%s]\n", buffer ); printf("%% Only real-valued or pattern coordinate matrices are supported.\n"); info = MAGMA_ERR_NOT_SUPPORTED; goto cleanup; } magma_index_t num_rows, num_cols, num_nonzeros; if (mm_read_mtx_crd_size(fid, &num_rows, &num_cols, &num_nonzeros) != 0) { info = MAGMA_ERR_UNKNOWN; goto cleanup; } A->storage_type = Magma_CSR; A->memory_location = Magma_CPU; A->num_rows = num_rows; A->num_cols = num_cols; A->nnz = num_nonzeros; A->fill_mode = MagmaFull; CHECK( magma_index_malloc_cpu( &coo_col, A->nnz ) ); CHECK( magma_index_malloc_cpu( &coo_row, A->nnz ) ); CHECK( magma_dmalloc_cpu( &coo_val, A->nnz ) ); if (mm_is_real(matcode) || mm_is_integer(matcode)) { for(magma_int_t i = 0; i < A->nnz; ++i) { magma_index_t ROW, COL; double VAL; // always read in a double and convert later if necessary fscanf(fid, " %d %d %lf \n", &ROW, &COL, &VAL); if ( VAL == 0 ) csr_compressor = 1; coo_row[i] = ROW - 1; coo_col[i] = COL - 1; coo_val[i] = MAGMA_D_MAKE( VAL, 0.); } } else if (mm_is_pattern(matcode) ) { for(magma_int_t i = 0; i < A->nnz; ++i) { magma_index_t ROW, COL; fscanf(fid, " %d %d \n", &ROW, &COL ); coo_row[i] = ROW - 1; coo_col[i] = COL - 1; coo_val[i] = MAGMA_D_MAKE( 1.0, 0.); } } else if (mm_is_real(matcode) ){ for(magma_int_t i = 0; i < A->nnz; ++i) { magma_index_t ROW, COL; double VAL, VALC; // always read in a double and convert later if necessary fscanf(fid, " %d %d %lf %lf\n", &ROW, &COL, &VAL, &VALC); coo_row[i] = ROW - 1; coo_col[i] = COL - 1; coo_val[i] = MAGMA_D_MAKE( VAL, VALC); } // printf(" ...successfully read real matrix... "); } else { printf("\n%% Unrecognized data type\n"); info = MAGMA_ERR_NOT_SUPPORTED; goto cleanup; } fclose(fid); fid = NULL; printf(" done. Converting to CSR:"); fflush(stdout); A->sym = Magma_GENERAL; if( mm_is_symmetric(matcode) ) { symmetric = 1; } if ( mm_is_symmetric(matcode) || mm_is_symmetric(matcode) ) { // duplicate off diagonal entries printf("\n%% Detected symmetric case."); A->sym = Magma_SYMMETRIC; magma_index_t off_diagonals = 0; for(magma_int_t i = 0; i < A->nnz; ++i) { if (coo_row[i] != coo_col[i]) ++off_diagonals; } magma_index_t true_nonzeros = 2*off_diagonals + (A->nnz - off_diagonals); //printf("%% total number of nonzeros: %d\n%%", int(A->nnz)); CHECK( magma_index_malloc_cpu( &new_row, true_nonzeros )); CHECK( magma_index_malloc_cpu( &new_col, true_nonzeros )); CHECK( magma_dmalloc_cpu( &new_val, true_nonzeros )); magma_index_t ptr = 0; for(magma_int_t i = 0; i < A->nnz; ++i) { if (coo_row[i] != coo_col[i]) { new_row[ptr] = coo_row[i]; new_col[ptr] = coo_col[i]; new_val[ptr] = coo_val[i]; ptr++; new_col[ptr] = coo_row[i]; new_row[ptr] = coo_col[i]; new_val[ptr] = (symmetric == 0) ? coo_val[i] : conj(coo_val[i]); ptr++; } else { new_row[ptr] = coo_row[i]; new_col[ptr] = coo_col[i]; new_val[ptr] = coo_val[i]; ptr++; } } magma_free_cpu(coo_row); magma_free_cpu(coo_col); magma_free_cpu(coo_val); coo_row = new_row; coo_col = new_col; coo_val = new_val; A->nnz = true_nonzeros; //printf("total number of nonzeros: %d\n", A->nnz); } // end symmetric case CHECK( magma_dmalloc_cpu( &A->val, A->nnz )); CHECK( magma_index_malloc_cpu( &A->col, A->nnz )); CHECK( magma_index_malloc_cpu( &A->row, A->num_rows+1 )); // original code from Nathan Bell and Michael Garland for (magma_index_t i = 0; i < num_rows; i++) (A->row)[i] = 0; for (magma_index_t i = 0; i < A->nnz; i++) (A->row)[coo_row[i]]++; // cumulative sum the nnz per row to get row[] magma_int_t cumsum; cumsum = 0; for(magma_int_t i = 0; i < num_rows; i++) { magma_index_t temp = (A->row)[i]; (A->row)[i] = cumsum; cumsum += temp; } (A->row)[num_rows] = A->nnz; // write Aj,Ax into Bj,Bx for(magma_int_t i = 0; i < A->nnz; i++) { magma_index_t row_ = coo_row[i]; magma_index_t dest = (A->row)[row_]; (A->col)[dest] = coo_col[i]; (A->val)[dest] = coo_val[i]; (A->row)[row_]++; } magma_free_cpu(coo_row); magma_free_cpu(coo_col); magma_free_cpu(coo_val); coo_row = NULL; coo_col = NULL; coo_val = NULL; int last; last = 0; for(int i = 0; i <= num_rows; i++) { int temp = (A->row)[i]; (A->row)[i] = last; last = temp; } (A->row)[A->num_rows] = A->nnz; // sort column indices within each row // copy into vector of pairs (column index, value), sort by column index, then copy back for (magma_index_t k=0; k < A->num_rows; ++k) { int kk = (A->row)[k]; int len = (A->row)[k+1] - (A->row)[k]; rowval.resize( len ); for( int i=0; i < len; ++i ) { rowval[i] = std::make_pair( (A->col)[kk+i], (A->val)[kk+i] ); } std::sort( rowval.begin(), rowval.end(), compare_first ); for( int i=0; i < len; ++i ) { (A->col)[kk+i] = rowval[i].first; (A->val)[kk+i] = rowval[i].second; } } if ( csr_compressor > 0) { // run the CSR compressor to remove zeros //printf("removing zeros: "); CHECK( magma_dmtransfer( *A, &B, Magma_CPU, Magma_CPU, queue )); CHECK( magma_d_csr_compressor( &(A->val), &(A->row), &(A->col), &B.val, &B.row, &B.col, &B.num_rows, queue )); B.nnz = B.row[num_rows]; //printf(" remaining nonzeros:%d ", B.nnz); magma_free_cpu( A->val ); magma_free_cpu( A->row ); magma_free_cpu( A->col ); CHECK( magma_dmtransfer( B, A, Magma_CPU, Magma_CPU, queue )); //printf("done.\n"); } A->true_nnz = A->nnz; printf(" done.\n"); cleanup: if ( fid != NULL ) { fclose( fid ); fid = NULL; } magma_dmfree( &B, queue ); magma_free_cpu(coo_row); magma_free_cpu(coo_col); magma_free_cpu(coo_val); return info; }
int readSparseMatrix(char * filename, PetscInt & n, PetscInt & nz, PetscInt *& nnz, int *& i, int *& j, double *& v) { int in, m, inz; // try opening the files FILE *fp; if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "Error occurs while reading from file %s.\n", filename); return 1; } // try reading the banner MM_typecode type; if (mm_read_banner(fp, &type) != 0) { fprintf(stderr, "Could not process Matrix Market banner.\n"); return 2; } // check the type if (!mm_is_matrix(type) || !mm_is_coordinate(type) || !mm_is_real(type) || !mm_is_symmetric(type)) { fprintf(stderr, "Sorry, this application does not support Market Market type: [%s]\n", mm_typecode_to_str(type)); return 3; } // read the sizes of the vectors if (mm_read_mtx_crd_size(fp, &in, &m, &inz)) { fprintf(stderr, "Could not read the size of the matrix.\n"); return 4; } n = in; nz = inz; // check if it is a square matrix if (in != m) { fprintf(stderr, "Needs to be square.\n"); return 5; } // allocate the memory printf("reading %s:\n\ta %d x %d sparse matrix with %d nonzeros...", filename, m, in, inz); i = new int[nz]; j = new int[nz]; v = new double[nz]; nnz = new PetscInt[n](); for (int k = 0; k < inz; ++k) { fscanf(fp, "%u %u %lf\n", &j[k], &i[k], &v[k]); --i[k]; --j[k]; ++nnz[i[k]]; if (i[k] != j[k]) { ++nnz[j[k]]; } } printf("done\n"); // close the file fclose(fp); return 0; }