void WriteMatrixMarketVector(std::vector<T> v, std::string filename) { /* Open the file */ FILE *f = fopen(filename.c_str(), "w"); if (f == NULL) { std::cout<<"failed to open file "<<filename<<std::endl; } /* Setup matcode, write banner and size */ MM_typecode matcode; mm_initialize_typecode(&matcode); mm_set_matrix(&matcode); mm_set_array(&matcode); mm_set_real(&matcode); mm_write_banner(f, matcode); mm_write_mtx_array_size(f, v.size(), 1); for(unsigned int n = 0; n < v.size(); n++) fprintf(f, "%e\n", v[n]); /* Close file and return */ fclose(f); return; }
int writeMatrix(char* filename, int m, int n, double* v) { // try opening the file FILE *fp; if ((fp = fopen(filename, "w")) == NULL) { fprintf(stderr, "Error occurs while writing to file %s\n", filename); return 1; } // set the type MM_typecode type; mm_initialize_typecode(&type); mm_set_matrix(&type); mm_set_array(&type); mm_set_real(&type); mm_set_general(&type); // write banner and sizes mm_write_banner(fp, type); mm_write_mtx_array_size(fp, m, n); printf("writing %s:\n\ta %d x %d matrix...", filename, m, n); for (int j = 0; j < n; ++j) { for (int i = 0; i < m; ++i) { fprintf(fp, "%lf\n", v[i * n + j]); } } printf("done\n"); // close the file fclose(fp); return 0; }
/* convert sparse to dense */ bool mm_real_sparse_to_dense (mm_sparse *s) { int j, k; int m; int n; int nnz; double *tmp_data; double *td; int *si; int *sp; double *sd; if (!mm_real_is_sparse (s)) return false; m = s->m; n = s->n; nnz = m * n; /* copy s->data */ tmp_data = (double *) malloc (s->nnz * sizeof (double)); for (k = 0; k < s->nnz; k++) tmp_data[k] = s->data[k]; td = tmp_data; /* reallocate s->data */ free (s->data); s->data = (double *) malloc (nnz * sizeof (double)); mm_real_array_set_all (nnz, s->data, 0.); /* convert sparse to dense */ si = s->i; sp = s->p; sd = s->data; for (j = 0; j < n; j++) { int np = sp[1] - *sp; for (k = 0; k < np; k++) { sd[*si] = *td; si++; td++; } sd += s->m; sp++; } free (tmp_data); mm_set_array (&s->typecode); s->nnz = nnz; free (s->i); s->i = NULL; free (s->p); s->p = NULL; return true; }
void set_matcode(MM_typecode & matcode, bool sparse = false){ mm_initialize_typecode(&matcode); mm_set_matrix(&matcode); if (sparse) mm_set_coordinate(&matcode); else mm_set_array(&matcode); mm_set_real(&matcode); }
//int CreateDenseMatrixSymmetric(unsigned long numRows,unsigned long numCols, char *fileName, unsigned int seed) { int CreateDenseVector(int argc, char *argv[]) { //Options: numItems fileName seed FILE *output; //long nz; int i; MM_typecode outputmatcode; mm_initialize_typecode(&outputmatcode); mm_set_array(&outputmatcode); mm_set_dense(&outputmatcode); //mm_set_coordinate(&outputmatcode); mm_set_real(&outputmatcode); //mm_set_symmetric(&outputmatcode); unsigned long int numItems = 0; unsigned int seed = 0; //int ret_code; if (argc < 4) { fprintf(stderr, "[%s] Usage: %s [num-items] [output-filename] [seed]\n",__func__, argv[0]); return 0; } if ((output = fopen(argv[2], "w")) == NULL){ return 0; } numItems = atoi(argv[1]); seed = atoi(argv[3]); //unsigned long long int nnz = numItems; srand (seed); mm_write_banner(output, outputmatcode); //mm_write_mtx_crd_size(output, numRows, numCols, numRows*numCols); mm_write_mtx_array_size(output, numItems, 1); //ret_code = fprintf(output,"\%\%MatrixMarket matrix coordinate real symmetric\n"); for(i = 0;i < numItems; i++){ fprintf(output, "%f\n",((double)rand() / (double)RAND_MAX)); } fclose(output); return 1; }
/*** create new mm_real object * MMRealFormat format: MM_REAL_DENSE or MM_REAL_SPARSE * MMRealSymm symm : MM_REAL_GENERAL, MM_REAL_SYMMETRIC_UPPER or MM_REAL_SYMMETRIC_LOWER * int m, n : rows and columns of the matrix * int nnz : number of nonzero elements of the matrix ***/ mm_real * mm_real_new (MMRealFormat format, MMRealSymm symm, const int m, const int n, const int nnz) { mm_real *x; bool symmetric; if (!is_format_valid (format)) error_and_exit ("mm_real_new", "invalid MMRealFormat format.", __FILE__, __LINE__); if (!is_symm_valid (symm)) error_and_exit ("mm_real_new", "invalid MMRealSymm symm.", __FILE__, __LINE__); symmetric = symm & MM_SYMMETRIC; if (symmetric && m != n) error_and_exit ("mm_real_new", "symmetric matrix must be square.", __FILE__, __LINE__); x = mm_real_alloc (); if (x == NULL) error_and_exit ("mm_real_new", "failed to allocate object.", __FILE__, __LINE__); x->m = m; x->n = n; x->nnz = nnz; // typecode[1] = 'C' or 'A' if (format == MM_REAL_SPARSE) { mm_set_coordinate (&x->typecode); x->i = (int *) malloc (x->nnz * sizeof (int)); x->p = (int *) malloc ((x->n + 1) * sizeof (int)); if (x->i == NULL || x->p == NULL) error_and_exit ("mm_real_new", "cannot allocate memory.", __FILE__, __LINE__); // initialize x->p[0] x->p[0] = 0; } else mm_set_array (&x->typecode); x->symm = symm; // typecode[3] = 'G' -> 'S' if (symmetric) mm_set_symmetric (&x->typecode); if (!is_type_supported (x->typecode)) { char msg[128]; sprintf (msg, "matrix type does not supported :[%s].", mm_typecode_to_str (x->typecode)); error_and_exit ("mm_real_new", msg, __FILE__, __LINE__); } // allocate arrays x->data = (double *) malloc (x->nnz * sizeof (double)); if (x->data == NULL) error_and_exit ("mm_real_new", "cannot allocate memory.", __FILE__, __LINE__); return x; }
int write_mm_vector(const char *file, const int m, const double *vec, const char *comments, const int type) { int i; FILE *fp; MM_typecode matcode; mm_initialize_typecode(&matcode); mm_set_matrix(&matcode); mm_set_array(&matcode); mm_set_real(&matcode); if ((fp = fopen(file, "w")) == NULL) { fprintf(stderr,"ERROR: Could not open file: %s\n",file); exit(1); } /* banner */ mm_write_banner(fp, matcode); /* comments */ fprintf(fp, "%s", comments); /* size */ mm_write_mtx_array_size(fp, m, 1); /* contents */ if (type == TYPE_G) { for (i = 0; i < m; i++) fprintf(fp, "%10g\n", vec[i]); } else { for (i = 0; i < m; i++) fprintf(fp, "%22.15e\n", vec[i]); } fclose(fp); return 0; }
int MultiVectorToMatrixMarketFile( const char *filename, const Epetra_MultiVector & A, const char * matrixName, const char *matrixDescription, bool writeHeader) { int M = A.GlobalLength(); int N = A.NumVectors(); FILE * handle = 0; if (A.Map().Comm().MyPID()==0) { // Only PE 0 does this section handle = fopen(filename,"w"); if (!handle) return(-1); MM_typecode matcode; mm_initialize_typecode(&matcode); mm_set_matrix(&matcode); mm_set_array(&matcode); mm_set_real(&matcode); if (writeHeader==true) { // Only write header if requested (true by default) if (mm_write_banner(handle, matcode)) return(-1); if (matrixName!=0) fprintf(handle, "%% \n%% %s\n", matrixName); if (matrixDescription!=0) fprintf(handle, "%% %s\n%% \n", matrixDescription); if (mm_write_mtx_array_size(handle, M, N)) return(-1); } } if (MultiVectorToMatrixMarketHandle(handle, A)) return(-1); // Everybody calls this routine if (A.Map().Comm().MyPID()==0) // Only PE 0 opened a file if (fclose(handle)) return(-1); return(0); }
int BlockMapToMatrixMarketFile( const char *filename, const Epetra_BlockMap & map, const char * mapName, const char *mapDescription, bool writeHeader) { int M = map.NumGlobalElements(); int N = 1; if (map.MaxElementSize()>1) N = 2; // Non-trivial block map, store element sizes in second column FILE * handle = 0; if (map.Comm().MyPID()==0) { // Only PE 0 does this section handle = fopen(filename,"w"); if (!handle) return(-1); MM_typecode matcode; mm_initialize_typecode(&matcode); mm_set_matrix(&matcode); mm_set_array(&matcode); mm_set_integer(&matcode); if (writeHeader==true) { // Only write header if requested (true by default) if (mm_write_banner(handle, matcode)) return(-1); if (mapName!=0) fprintf(handle, "%% \n%% %s\n", mapName); if (mapDescription!=0) fprintf(handle, "%% %s\n%% \n", mapDescription); } } if (writeHeader==true) { // Only write header if requested (true by default) // Make an Epetra_IntVector of length numProc such that all elements are on PE 0 and // the ith element is NumMyElements from the ith PE Epetra_Map map1(-1, 1, 0, map.Comm()); // map with one element on each processor int length = 0; if (map.Comm().MyPID()==0) length = map.Comm().NumProc(); Epetra_Map map2(-1, length, 0, map.Comm()); Epetra_Import lengthImporter(map2, map1); Epetra_IntVector v1(map1); Epetra_IntVector v2(map2); v1[0] = map.NumMyElements(); if (v2.Import(v1, lengthImporter, Insert)) return(-1); if (map.Comm().MyPID()==0) { fprintf(handle, "%s", "%Format Version:\n"); //int version = 1; // We may change the format scheme at a later date. fprintf(handle, "%% %d \n", map.Comm().NumProc()); fprintf(handle, "%s", "%NumProc: Number of processors:\n"); fprintf(handle, "%% %d \n", map.Comm().NumProc()); fprintf(handle, "%s", "%MaxElementSize: Maximum element size:\n"); fprintf(handle, "%% %d \n", map.MaxElementSize()); fprintf(handle, "%s", "%MinElementSize: Minimum element size:\n"); fprintf(handle, "%% %d \n", map.MinElementSize()); fprintf(handle, "%s", "%IndexBase: Index base of map:\n"); fprintf(handle, "%% %d \n", map.IndexBase()); fprintf(handle, "%s", "%NumGlobalElements: Total number of GIDs in map:\n"); fprintf(handle, "%% %d \n", map.NumGlobalElements()); fprintf(handle, "%s", "%NumMyElements: BlockMap lengths per processor:\n"); for ( int i=0; i< v2.MyLength(); i++) fprintf(handle, "%% %d\n", v2[i]); if (mm_write_mtx_array_size(handle, M, N)) return(-1); } } if (BlockMapToHandle(handle, map)) return(-1); // Everybody calls this routine if (map.Comm().MyPID()==0) // Only PE 0 opened a file if (fclose(handle)) return(-1); return(0); }
void set_matcode(MM_typecode & matcode){ mm_initialize_typecode(&matcode); mm_set_matrix(&matcode); mm_set_array(&matcode); mm_set_real(&matcode); }
int write_mm_matrix(const char *file, dmatrix *mat, const char *comments, const int type) { int i, j, m, n; FILE *fp; m = mat->m; n = mat->n; MM_typecode matcode; mm_initialize_typecode(&matcode); mm_set_matrix(&matcode); if (mat->nz < 0) mm_set_array(&matcode); else mm_set_coordinate(&matcode); mm_set_real(&matcode); if ((fp = fopen(file, "w")) == NULL) { fprintf(stderr,"ERROR: Could not open file: %s\n",file); exit(1); } /* banner */ mm_write_banner(fp, matcode); /* comments */ fprintf(fp, "%s", comments); /* size */ if (mat->nz < 0) mm_write_mtx_array_size(fp, m, n); else mm_write_mtx_crd_size(fp, m, n, mat->nz); /* contents */ if (mat->nz < 0) { double *val; val = mat->val; if (type == TYPE_G) { for (j = 0; j < n; j++) for (i = 0; i < m; i++) fprintf(fp, "%10g\n", val[i*n+j]); } else { for (j = 0; j < n; j++) for (i = 0; i < m; i++) fprintf(fp, "%22.15e\n", val[i*n+j]); } } else { int *idx; int *jdx; double *val; idx = mat->idx; jdx = mat->jdx; val = mat->val; if (type == TYPE_G) { for (i = 0; i < mat->nz; i++) { fprintf(fp, "%d %d %15g\n", idx[i]+1, jdx[i]+1, val[i]); } } else { for (i = 0; i < mat->nz; i++) { fprintf(fp, "%d %d %22.15e\n", idx[i]+1, jdx[i]+1, val[i]); } } } fclose(fp); return 0; }