Exemple #1
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;
}
Exemple #2
0
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);
}
Exemple #3
0
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;
}
Exemple #4
0
/* allocate mm_real */
static mm_real *
mm_real_alloc (void)
{
	mm_real	*x = (mm_real *) malloc (sizeof (mm_real));
	if (x == NULL) return NULL;

	x->m = 0;
	x->n = 0;
	x->nnz = 0;
	x->i = NULL;
	x->p = NULL;
	x->data = NULL;

	x->symm = MM_REAL_GENERAL;

	/* set typecode = "M_RG" : Matrix Real General */
	// typecode[3] = 'G' : General
	mm_initialize_typecode (&x->typecode);
	// typecode[0] = 'M' : Matrix
	mm_set_matrix (&x->typecode);
	// typecode[2] = 'R' : Real
	mm_set_real (&x->typecode);

	return x;
}
Exemple #5
0
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 CreateDenseMatrixGeneral(char *fileName, unsigned long int numRows, unsigned long int numCols, unsigned int seed, double min, double max) {
//Options: numRows numCols fileName seed

	FILE *output;
	//long nz;   
	unsigned long int i, j;
	MM_typecode outputmatcode;
	
	mm_initialize_typecode(&outputmatcode);
	mm_set_matrix(&outputmatcode);
	mm_set_coordinate(&outputmatcode);
	//mm_set_dense(&outputmatcode);
	mm_set_real(&outputmatcode);
	mm_set_general(&outputmatcode);
	
	if(strcmp(fileName,"stdout")==0){
		output = stdout;
	}
	else{
		if ((output = fopen(fileName, "w")) == NULL){
			fprintf(stderr,"[%s] Unable to open file for writing\n",__func__);
			return 0;
		}
	}
			
	
	double value = 0.0;
	
	srand (seed);

	mm_write_banner(output, outputmatcode);
	mm_write_mtx_crd_size(output, numRows, numCols, numRows*numCols);
	//ret_code = fprintf(output,"\%\%MatrixMarket matrix coordinate real symmetric\n");
	
	//unsigned long long int val1 = 0;
	//unsigned long long int val2 = 0;
	

	
	for(i = 0;i < numRows; i++){
	
		for(j = 0; j< numCols; j++){

            value = randfrom(min, max);
			fprintf(output, "%lu %lu %lg\n",i+1,j+1,value);

		}

	}
	

	fclose(output);

	return 1;
}
//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;
}
int CreateDenseMatrixGeneral(int argc, char *argv[]) {

	FILE *output;
	unsigned long int numRows, numCols, seed, i, j;
	char * outputpath;
	double value;
	MM_typecode outputmatcode;

	int verbose;
	struct st_option options[] = {
		FLAG_BOOL('v', "verbose", "Show more info when executing", &verbose, NULL),
		PARAM_INT("rows", "Number of rows of the matrix", &numRows, NULL),
		PARAM_INT("cols", "Number of columns of the matrix", &numCols, NULL),
		PARAM_STRING("outputfilename", "Filename to write the matrix into", &outputpath, NULL),
		PARAM_INT("seed", "Seed of the random number generator", &seed, "0"),
		OPTIONS_END,
	};

	if (options_parse(options, argc, argv)) {
		options_usage(options, "./MM-Suite CreateDenseMatrixGeneral");
		puts(option_err_msg);
		return 0;
	};

	if ((output = fopen(outputpath, "w")) == NULL){
		return 0;
	}


	mm_initialize_typecode(&outputmatcode);
	mm_set_matrix(&outputmatcode);
	mm_set_coordinate(&outputmatcode);
	mm_set_real(&outputmatcode);
	mm_set_general(&outputmatcode);

	srand(seed);

	mm_write_banner(output, outputmatcode);
	mm_write_mtx_crd_size(output, numRows, numCols, numRows*numCols);
	
	for (i = 0; i < numRows; i++) {
		for (j = 0; j < numCols; j++) {
			value = ((double) rand() / (double) RAND_MAX) / 100;
			fprintf(output, "%lu %lu %lg\n", i + 1, j + 1, value);
		}
	}
	
	fclose(output);

	return 1;
};
Exemple #9
0
int write_mm_matrix_crd_header(FILE *fp, const int m, const int n,
                               const int nnz, const char *comments)
{
    MM_typecode matcode;

    mm_initialize_typecode(&matcode);
    mm_set_matrix(&matcode);
    mm_set_coordinate(&matcode);
    mm_set_real(&matcode);

    /* banner */
    mm_write_banner(fp, matcode); 
    /* comments */
    fprintf(fp, "%s", comments);
    /* size */
    mm_write_mtx_crd_size(fp, m, n, nnz);
    /* contents */

    return 0;
}
Exemple #10
0
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 RowMatrixToMatrixMarketFile( const char *filename, const Epetra_RowMatrix & A, 
				 const char * matrixName,
				 const char *matrixDescription, 
				 bool writeHeader) {
  long long M = A.NumGlobalRows64();
  long long N = A.NumGlobalCols64();
  long long nz = A.NumGlobalNonzeros64();

  FILE * handle = 0;

  if (A.RowMatrixRowMap().Comm().MyPID()==0) { // Only PE 0 does this section

    handle = fopen(filename,"w");
    if (!handle) {EPETRA_CHK_ERR(-1);}
    MM_typecode matcode;
    mm_initialize_typecode(&matcode);
    mm_set_matrix(&matcode);
    mm_set_coordinate(&matcode);
    mm_set_real(&matcode);

    if (writeHeader==true) { // Only write header if requested (true by default)
    
      if (mm_write_banner(handle, matcode)!=0) {EPETRA_CHK_ERR(-1);}
      
      if (matrixName!=0) fprintf(handle, "%% \n%% %s\n", matrixName);
      if (matrixDescription!=0) fprintf(handle, "%% %s\n%% \n", matrixDescription);
      
      if (mm_write_mtx_crd_size(handle, M, N, nz)!=0) {EPETRA_CHK_ERR(-1);}
    }
  }
    
  if (RowMatrixToHandle(handle, A)!=0) {EPETRA_CHK_ERR(-1);}// Everybody calls this routine

  if (A.RowMatrixRowMap().Comm().MyPID()==0) // Only PE 0 opened a file
    if (fclose(handle)!=0) {EPETRA_CHK_ERR(-1);}
  return(0);
}
Exemple #12
0
void HostMatrixCOO<ValueType>::WriteFileMTX(const std::string filename) const {

  MM_typecode matcode; 
  FILE *f;

  LOG_INFO("WriteFileMTX: filename="<< filename << "; writing...");

  if ((f = fopen(filename.c_str(), "w")) == NULL) {
    LOG_INFO("WriteFileMTX cannot open file " << filename);
    FATAL_ERROR(__FILE__, __LINE__);
  }

  mm_initialize_typecode(&matcode);
  mm_set_matrix(&matcode);
  mm_set_coordinate(&matcode);
  mm_set_real(&matcode);
  
  mm_write_banner(f, matcode); 

  //  mm_write_mtx_crd_size(f, this->get_ncol(), this->get_nrow(), this->get_nnz());
  mm_write_mtx_crd_size(f, this->get_nrow(), this->get_ncol(), this->get_nnz());

  /* NOTE: matrix market files use 1-based indices, i.e. first element
     of a vector has index 1, not 0.  */

  for (int i=0; i<this->nnz_; i++)
    fprintf(f, "%d %d %2.14e\n",
            this->mat_.row[i]+1, 
            this->mat_.col[i]+1, 
            this->mat_.val[i]);
  
  LOG_INFO("WriteFileMTX: filename="<< filename << "; done");

  fclose(f);

}
int main()
{
    MM_typecode matcode;                        
    int I[nz] = { 0, 4, 2, 8 };
    int J[nz] = { 3, 8, 7, 5 };
    double val[nz] = {1.1, 2.2, 3.2, 4.4};
    int i;

    mm_initialize_typecode(&matcode);
    mm_set_matrix(&matcode);
    mm_set_coordinate(&matcode);
    mm_set_real(&matcode);

    mm_write_banner(stdout, matcode); 
    mm_write_mtx_crd_size(stdout, M, N, nz);

    /* NOTE: matrix market files use 1-based indices, i.e. first element
      of a vector has index 1, not 0.  */

    for (i=0; i<nz; i++)
        fprintf(stdout, "%d %d %10.3g\n", I[i]+1, J[i]+1, val[i]);

	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);
}
Exemple #15
0
//------------------------------------------------------------------------
int test_mm_write(std::string filename)
{
    const int nz = 4;
    const int M = 10;
    const int N= 10;

    MM_typecode matcode;                        
    int I[nz] = { 0, 4, 2, 8 };
    int J[nz] = { 3, 8, 7, 5 };
    double val[nz] = {1.1, 2.2, 3.2, 4.4};
    int i;

    int err = 0; 
    FILE *f; 
    f = fopen(filename.c_str(), "w"); 
    err += mm_initialize_typecode(&matcode);
    err += mm_set_matrix(&matcode);
    err += mm_set_coordinate(&matcode);
    err += mm_set_real(&matcode);

    err += mm_write_banner(f, matcode); 
    err += mm_write_mtx_crd_size(f, M, N, nz);

    /* NOTE: matrix market files use 1-based indices, i.e. first element
       of a vector has index 1, not 0.  */
    fprintf(stdout, "Writing file contents: \n"); 
    for (i=0; i<nz; i++)
    {
        fprintf(f, "%d %d %lg\n", I[i]+1, J[i]+1, val[i]);
        fprintf(stdout, "%d %d %lg\n", I[i]+1, J[i]+1, val[i]);
    }

    fclose(f);

    return err;
}
Exemple #16
0
/* Save a matrix column subset to a MatrixMarket formatted file,
   say to export the basis matrix for further numerical analysis.
   If colndx is NULL, then the full constraint matrix is assumed. */
MYBOOL REPORT_mat_mmsave(lprec *lp, char *filename, int *colndx, MYBOOL includeOF, char *infotext)
{
  int         n, m, nz, i, j, k, kk;
  MATrec      *mat = lp->matA;
  MM_typecode matcode;
  FILE        *output = stdout;
  MYBOOL      ok;
  LPSREAL        *acol = NULL;
  int         *nzlist = NULL;

  /* Open file */
  ok = (MYBOOL) ((filename == NULL) || ((output = fopen(filename,"w")) != NULL));
  if(!ok)
    return(ok);
  if((filename == NULL) && (lp->outstream != NULL))
    output = lp->outstream;

  /* Compute column and non-zero counts */
  if(colndx == lp->var_basic) {
    if(!lp->basis_valid)
      return( FALSE );
    m = lp->rows;
  }
  else if(colndx != NULL)
    m = colndx[0];
  else
    m = lp->columns;
  n = lp->rows;
  nz = 0;

  for(j = 1; j <= m; j++) {
    k = (colndx == NULL ? n + j : colndx[j]);
    if(k > n) {
      k -= lp->rows;
      nz += mat_collength(mat, k);
      if(includeOF && is_OF_nz(lp, k))
        nz++;
    }
    else
      nz++;
  }
  kk = 0;
  if(includeOF) {
    n++;   /* Row count */
    kk++;  /* Row index offset */
  }

  /* Initialize */
  mm_initialize_typecode(&matcode);
  mm_set_matrix(&matcode);
  mm_set_coordinate(&matcode);
  mm_set_real(&matcode);

  mm_write_banner(output, matcode);
  mm_write_mtx_crd_size(output, n+kk, m, nz+(colndx == lp->var_basic ? 1 : 0));

  /* Allocate working arrays for sparse column storage */
  allocREAL(lp, &acol, n+2, FALSE);
  allocINT(lp, &nzlist, n+2, FALSE);

  /* Write the matrix non-zero values column-by-column.
     NOTE: matrixMarket files use 1-based indeces,
     i.e. first row of a vector has index 1, not 0. */
  if(infotext != NULL) {
    fprintf(output, "%%\n");
    fprintf(output, "%% %s\n", infotext);
    fprintf(output, "%%\n");
  }
  if(includeOF && (colndx == lp->var_basic))
    fprintf(output, "%d %d %g\n", 1, 1, 1.0);
  for(j = 1; j <= m; j++) {
    k = (colndx == NULL ? lp->rows + j : colndx[j]);
    if(k == 0)
      continue;
    nz = obtain_column(lp, k, acol, nzlist, NULL);
    for(i = 1; i <= nz; i++) {
      if(!includeOF && (nzlist[i] == 0))
        continue;
      fprintf(output, "%d %d %g\n", nzlist[i]+kk, j+kk, acol[i]);
    }
  }
  fprintf(output, "%% End of MatrixMarket file\n");

  /* Finish */
  FREE(acol);
  FREE(nzlist);
  fclose(output);

  return(ok);
}
Exemple #17
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;
}
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);
}
Exemple #19
0
void set_matcode(MM_typecode & matcode){
  mm_initialize_typecode(&matcode);
  mm_set_matrix(&matcode);
  mm_set_array(&matcode);
  mm_set_real(&matcode);
}