void save_matrix_market_format_vector(const std::string datafile, const vec & output, bool issparse, std::string comment) { MM_typecode matcode; mm_initialize_typecode(&matcode); mm_set_matrix(&matcode); mm_set_coordinate(&matcode); if (issparse) mm_set_sparse(&matcode); else mm_set_dense(&matcode); set_typecode<vec>(matcode); FILE * f = fopen(datafile.c_str(),"w"); if (f == NULL) logstream(LOG_FATAL)<<"Failed to open file: " << datafile << " for writing. " << std::endl; mm_write_banner(f, matcode); if (comment.size() > 0) // add a comment to the matrix market header fprintf(f, "%c%s\n", '%', comment.c_str()); if (issparse) mm_write_mtx_crd_size(f, output.size(), 1, output.size()); else mm_write_mtx_array_size(f, output.size(), 1); for (int j=0; j<(int)output.size(); j++){ write_row(j+1, 1, output[j], f, issparse); if (!issparse) fprintf(f, "\n"); } fclose(f); }
MMOutputter_mat(std::string fname, uint start, uint end, std::string comment, std::vector<vertex_data> & latent_factors_inmem, int size = 0) { assert(start < end); MM_typecode matcode; set_matcode(matcode, R_output_format); FILE * outf = open_file(fname.c_str(), "w"); mm_write_banner(outf, matcode); if (comment != "") fprintf(outf, "%%%s\n", comment.c_str()); int actual_Size = size > 0 ? size : latent_factors_inmem[start].pvec.size(); if (R_output_format) mm_write_mtx_crd_size(outf, end-start, actual_Size, (end-start)*actual_Size); else mm_write_mtx_array_size(outf, end-start, actual_Size); for (uint i=start; i < end; i++){ for(int j=0; j < actual_Size; j++) { if (R_output_format) fprintf(outf, "%d %d %12.8g\n", i-start+input_file_offset, j+input_file_offset, latent_factors_inmem[i].get_val(j)); else { fprintf(outf, "%1.12e ", latent_factors_inmem[i].get_val(j)); if (j == actual_Size -1) fprintf(outf, "\n"); } } } fclose(outf); }
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; }
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; }
/* fwrite dense */ static void mm_real_fwrite_dense (FILE *stream, const mm_dense *d, const char *format) { int k; mm_write_banner (stream, d->typecode); mm_write_mtx_array_size (stream, d->m, d->n); for (k = 0; k < d->nnz; k++) { fprintf (stream, format, d->data[k]); fprintf (stream, "\n"); } return; }
//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; }
MMOutputter2(std::string fname, uint start, uint end, std::string comment) { MM_typecode matcode; set_matcode(matcode); outf = fopen(fname.c_str(), "w"); assert(outf != NULL); mm_write_banner(outf, matcode); if (comment != "") fprintf(outf, "%%%s\n", comment.c_str()); mm_write_mtx_array_size(outf, end-start, D); for (uint i=start; i < end; i++) fprintf(outf, "%1.12e\n", latent_factors_inmem[i].mean_rating); }
MMOutputter_vec(std::string fname, uint start, uint end, int index, std::string comment, std::vector<vertex_data> & latent_factors_inmem) { MM_typecode matcode; set_matcode(matcode, R_output_format); FILE * outf = open_file(fname.c_str(), "w"); mm_write_banner(outf, matcode); if (comment != "") fprintf(outf, "%%%s\n", comment.c_str()); if (R_output_format) mm_write_mtx_crd_size(outf, end-start, 1, end-start); else mm_write_mtx_array_size(outf, end-start, 1); for (uint i=start; i< end; i++) if (R_output_format) fprintf(outf, "%d %d %12.8g\n", i-start+input_file_offset, 1, latent_factors_inmem[i].get_val(index)); else fprintf(outf, "%1.12e\n", latent_factors_inmem[i].get_val(index)); fclose(outf); }
MMOutputter_scalar(std::string fname, std::string comment, double val) { MM_typecode matcode; set_matcode(matcode, R_output_format); FILE * outf = open_file(fname.c_str(), "w"); mm_write_banner(outf, matcode); if (comment != "") fprintf(outf, "%%%s\n", comment.c_str()); if (R_output_format) mm_write_mtx_crd_size(outf, 1, 1, 1); else mm_write_mtx_array_size(outf, 1, 1); if (R_output_format) fprintf(outf, "%d %d %12.8g\n", 1, 1, val); else fprintf(outf, "%1.12e\n", val); fclose(outf); }
MMOutputter_ids(std::string fname, uint start, uint end, std::string comment) { assert(start < end); MM_typecode matcode; set_matcode(matcode); FILE * outf = fopen(fname.c_str(), "w"); assert(outf != NULL); mm_write_banner(outf, matcode); if (comment != "") fprintf(outf, "%%%s\n", comment.c_str()); mm_write_mtx_array_size(outf, end-start, num_ratings+1); for (uint i=start; i < end; i++){ fprintf(outf, "%u ", i+1); for(int j=0; j < latent_factors_inmem[i].ids.size(); j++) { fprintf(outf, "%u ", (int)latent_factors_inmem[i].ids[j]+1);//go back to item ids starting from 1,2,3, (and not from zero as in c) } fprintf(outf, "\n"); } fclose(outf); }
MMOutputter_ratings(std::string fname, uint start, uint end, std::string comment) { assert(start < end); MM_typecode matcode; set_matcode(matcode); FILE * outf = fopen(fname.c_str(), "w"); assert(outf != NULL); mm_write_banner(outf, matcode); if (comment != "") fprintf(outf, "%%%s\n", comment.c_str()); mm_write_mtx_array_size(outf, end-start, num_ratings+1); for (uint i=start; i < end; i++){ fprintf(outf, "%u ", i+1); for(int j=0; j < latent_factors_inmem[i].ratings.size(); j++) { fprintf(outf, "%1.12e ", latent_factors_inmem[i].ratings[j]); } fprintf(outf, "\n"); } fclose(outf); }
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); }
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; }