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); }
void mm_typecode_to_str(MM_typecode matcode, char * buffer) { char type0[20]; char type1[20]; char type2[20]; char type3[20]; int error =0; /* check for MTX type */ if (mm_is_matrix(matcode)) strcpy(type0, MM_MTX_STR); else error=1; /* check for CRD or ARR matrix */ if (mm_is_sparse(matcode)) strcpy(type1, MM_SPARSE_STR); else if (mm_is_dense(matcode)) strcpy(type1, MM_DENSE_STR); else return; /* check for element data type */ if (mm_is_real(matcode)) strcpy(type2, MM_REAL_STR); else if (mm_is_complex(matcode)) strcpy(type2, MM_COMPLEX_STR); else if (mm_is_pattern(matcode)) strcpy(type2, MM_PATTERN_STR); else if (mm_is_integer(matcode)) strcpy(type2, MM_INT_STR); else return; /* check for symmetry type */ if (mm_is_general(matcode)) strcpy(type3, MM_GENERAL_STR); else if (mm_is_symmetric(matcode)) strcpy(type3, MM_SYMM_STR); else if (mm_is_hermitian(matcode)) strcpy(type3, MM_HERM_STR); else if (mm_is_skew(matcode)) strcpy(type3, MM_SKEW_STR); else return; sprintf(buffer,"%s %s %s %s", type0, type1, type2, type3); return; }
int readMatrix(char* filename, int& m, int& n, double*& v, bool is_vector) { // 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_array(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 of the vectors if (mm_read_mtx_array_size(fp, &m, &n)) { fprintf(stderr, "Could not read the size of the matrix.\n"); return 4; } // check if it is a vector if (is_vector && n != 1) { fprintf(stderr, "Needs to be a vector.\n"); return 5; } // allocate the memory printf("reading %s:\n\ta %d x %d matrix...", filename, m, n); v = new double[m * n]; for (int j = 0; j < n; ++j) { for (int i = 0; i < m; ++i) { fscanf(fp, "%lf\n", &v[j * n + i]); } } printf("done\n"); // close the file fclose(fp); return 0; }
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; }
bool loadMmProperties(int *rowsCount, int *columnsCount, int *nonZerosCount, bool *isStoredSparse, int* matrixStorage, int* matrixType, FILE *file) { MM_typecode matcode; // supports only valid matrices if ((mm_read_banner(file, &matcode) != 0) || (!mm_is_matrix(matcode)) || (!mm_is_valid(matcode))) return false; if ( mm_read_mtx_crd_size(file, rowsCount, columnsCount, nonZerosCount) != 0 ) return false; // is it stored sparse? if (mm_is_sparse(matcode)) *isStoredSparse = true; else *isStoredSparse = false; if (mm_is_integer(matcode)) *matrixStorage = MATRIX_STORAGE_INTEGER; else if (mm_is_real(matcode)) *matrixStorage = MATRIX_STORAGE_REAL; else if (mm_is_complex(matcode)) *matrixStorage = MATRIX_STORAGE_COMPLEX; else if (mm_is_pattern(matcode)) *matrixStorage = MATRIX_STORAGE_PATTERN; if (mm_is_general(matcode)) *matrixType = MATRIX_TYPE_GENERAL; else if (mm_is_symmetric(matcode)) *matrixType = MATRIX_TYPE_SYMMETRIC; else if (mm_is_skew(matcode)) *matrixType = MATRIX_TYPE_SKEW; else if (mm_is_hermitian(matcode)) *matrixType = MATRIX_TYPE_HERMITIAN; return true; }
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 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); }
bool LoadMatrixMarketFile(const std::string& file_path, SparseMatrix<T>& A, unsigned int& height, unsigned int& width, unsigned int& nnz) { std::ifstream infile(file_path); if (!infile) return false; char mm_typecode[4]; // read the matrix market banner (header) if (0 != mm_read_banner(infile, mm_typecode)) return false; if (!mm_is_valid(mm_typecode)) return false; // this reader supports these matrix types: // // sparse, real/integer/pattern, general/symm/skew // if (!mm_is_sparse(mm_typecode)) { std::cerr << "Only sparse MatrixMarket files are supported." << std::endl; return false; } if (!mm_is_real(mm_typecode) && !mm_is_integer(mm_typecode) && !mm_is_pattern(mm_typecode)) { std::cerr << "Only real, integer, and pattern MatrixMarket formats are supported." << std::endl; return false; } if (!mm_is_general(mm_typecode) && !mm_is_symmetric(mm_typecode) && !mm_is_skew(mm_typecode)) { std::cerr << "Only general, symmetric, and skew-symmetric MatrixMarket formats are supported." << std::endl; return false; } // read the number of rows, cols, nonzeros if (0 != mm_read_mtx_crd_size(infile, height, width, nnz)) { std::cerr << "could not read matrix coordinate information" << std::endl; height = width = nnz = 0; return false; } // read the data according to the type bool is_real = mm_is_real(mm_typecode); bool is_int = mm_is_integer(mm_typecode); bool is_symmetric = mm_is_symmetric(mm_typecode); bool is_skew = mm_is_skew(mm_typecode); std::string line; unsigned int reserve_size = nnz; if (is_symmetric || is_skew) reserve_size *= 2; A.Clear(); A.Reserve(height, width, reserve_size); // load num random entries of A A.BeginLoad(); unsigned int row, col, count; if (is_real) { double val; for (count=0; count != nnz; ++count) { infile >> row; assert(row >= 1); infile >> col; assert(col >= 1); infile >> val; // convert to 0-based indexing row -= 1; col -= 1; A.Load(row, col, val); if (row != col) { if (is_symmetric) A.Load(col, row, val); else if (is_skew) A.Load(col, row, -val); } } } else if (is_int)
char *mm_typecode_to_str(MM_typecode matcode) { char buffer[MM_MAX_LINE_LENGTH]; const char *types[4]; char *mm_strdup(const char *); int error =0; /* check for MTX type */ if (mm_is_matrix(matcode)) types[0] = MM_MTX_STR; else { types[0] = NULL; error=1; } /* check for CRD or ARR matrix */ if (mm_is_sparse(matcode)) types[1] = MM_SPARSE_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; if( error == 1 ) sprintf(buffer,"Object to write is not a matrix; this is unsupported by the current mmio code."); else sprintf(buffer,"%s %s %s %s", types[0], types[1], types[2], types[3]); return mm_strdup(buffer); }
char *mm_typecode_to_str ( MM_typecode matcode ) /******************************************************************************/ /* Purpose: MM_TYPECODE_TO_STR converts the internal typecode to an MM header string. Modified: 31 October 2008 */ { char buffer[MM_MAX_LINE_LENGTH]; char *types[4]; char *mm_strdup(const char *); //int error =0; /* 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_sparse(matcode)) types[1] = MM_SPARSE_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 mm_strdup(buffer); }
static PetscErrorCode loadmtx(const char* filename, Mat *M, PetscBool *pattern) { PetscErrorCode ierr; FILE *f; MM_typecode type; int m,n,nz,i,j,k; PetscInt low,high,lowj,highj,*d_nz,*o_nz; double re,im; PetscScalar s; long pos; PetscFunctionBegin; f = fopen(filename,"r"); if (!f) SETERRQ2(PETSC_COMM_SELF,1,"fopen '%s': %s",filename,strerror(errno)); /* first read to set matrix kind and size */ ierr = mm_read_banner(f,&type);CHKERRQ(ierr); if (!mm_is_valid(type) || !mm_is_sparse(type) || !(mm_is_real(type) || mm_is_complex(type) || mm_is_pattern(type) || mm_is_integer(type))) SETERRQ1(PETSC_COMM_SELF,1,"Matrix format '%s' not supported",mm_typecode_to_str(type)); #if !defined(PETSC_USE_COMPLEX) if (mm_is_complex(type)) SETERRQ(PETSC_COMM_SELF,1,"Complex matrix not supported in real configuration"); #endif if (pattern) *pattern = mm_is_pattern(type) ? PETSC_TRUE : PETSC_FALSE; ierr = mm_read_mtx_crd_size(f,&m,&n,&nz);CHKERRQ(ierr); pos = ftell(f); ierr = MatCreate(PETSC_COMM_WORLD,M);CHKERRQ(ierr); ierr = MatSetSizes(*M,PETSC_DECIDE,PETSC_DECIDE,(PetscInt)m,(PetscInt)n);CHKERRQ(ierr); ierr = MatSetFromOptions(*M);CHKERRQ(ierr); ierr = MatSetUp(*M);CHKERRQ(ierr); ierr = MatGetOwnershipRange(*M,&low,&high);CHKERRQ(ierr); ierr = MatGetOwnershipRangeColumn(*M,&lowj,&highj);CHKERRQ(ierr); ierr = PetscMalloc(sizeof(PetscInt)*(high-low),&d_nz);CHKERRQ(ierr); ierr = PetscMalloc(sizeof(PetscInt)*(high-low),&o_nz);CHKERRQ(ierr); for (i=0; i<high-low;i++) { d_nz[i] = (i+low>=lowj && i+low<highj) ? 1 : 0; o_nz[i] = (i+low>=lowj && i+low<highj) ? 0 : 1; } for (k=0;k<nz;k++) { ierr = mm_read_mtx_crd_entry(f,&i,&j,&re,&im,type);CHKERRQ(ierr); i--; j--; if (i!=j) { if (i>=low && i<high) { if (j>=lowj && j<highj) d_nz[i-low]++; else o_nz[i-low]++; } if (j>=low && j<high && !mm_is_general(type)) { if (i>=low && i<high) d_nz[j-low]++; else o_nz[j-low]++; } } } ierr = preallocation(*M,d_nz,o_nz);CHKERRQ(ierr); ierr = PetscFree(d_nz);CHKERRQ(ierr); ierr = PetscFree(o_nz);CHKERRQ(ierr); /* second read to load the values */ ierr = fseek(f, pos, SEEK_SET); if (ierr) SETERRQ1(PETSC_COMM_SELF,1,"fseek: %s",strerror(errno)); re = 1.0; im = 0.0; /* Set the diagonal to zero */ for (i=low; i<PetscMin(high,n); i++) { ierr = MatSetValue(*M,i,i,0.0,INSERT_VALUES);CHKERRQ(ierr); } for (k=0;k<nz;k++) { ierr = mm_read_mtx_crd_entry(f,&i,&j,&re,&im,type); i--; j--; if (i>=low && i<high) { s = re + IMAGINARY * im; ierr = MatSetValue(*M,i,j,s,INSERT_VALUES);CHKERRQ(ierr); } if (j>=low && j<high && i != j && !mm_is_general(type)) { if (mm_is_symmetric(type)) s = re + IMAGINARY * im; else if (mm_is_hermitian(type)) s = re - IMAGINARY * im; else if (mm_is_skew(type)) s = -re - IMAGINARY * im; else { SETERRQ1(PETSC_COMM_SELF,1,"Matrix format '%s' not supported",mm_typecode_to_str(type)); } ierr = MatSetValue(*M,j,i,s,INSERT_VALUES);CHKERRQ(ierr); } } ierr = MatAssemblyBegin(*M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(*M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); if (mm_is_symmetric(type)) { ierr = MatSetOption(*M,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); } if ((mm_is_symmetric(type) && mm_is_real(type)) || mm_is_hermitian(type)) { ierr = MatSetOption(*M,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); } ierr = fclose(f); if (ierr) SETERRQ1(PETSC_COMM_SELF,1,"fclose: %s",strerror(errno)); PetscFunctionReturn(0); }