Exemplo n.º 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;
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
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;
};
Exemplo n.º 8
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;
}
Exemplo n.º 9
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;
}
Exemplo n.º 10
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);
}
Exemplo n.º 11
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;
}
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);
}
Exemplo n.º 13
0
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;
}
Exemplo n.º 14
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);

}
Exemplo n.º 15
0
void main(int argc, char *argv[])
{
    FILE *in_file;
    char Title[73], Key[9], Rhstype[4];
    char Type[4];
    char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
    int Ptrcrd, Indcrd, Valcrd, Rhscrd;
    int Indperline, Indwidth;
    int Valperline, Valwidth, Valprec;
    int Valflag;           /* Indicates 'E','D', or 'F' float format */
    int Nrow, Ncol, Nnzero;
    int Nrhs;
    char* ThisElement;
    char format[30];
    char rformat[30];
    int *colptr, *rowind;
    int *colcount;
    int i,j, repeat, count, col, ind, items, last;
    char line[BUFSIZ];
    MM_typecode matcode;

    if (argc != 2) {
      printf("Usage: %s HBfile\n\n", argv[0]);
      printf("   Sends Matrix Market formatted output to stdout\n");
      exit(-1);
    } 

    in_file = fopen( argv[1], "r");
    if (in_file == NULL)
    {
       fprintf(stderr,"Error: Cannot open file: %s\n",argv[1]);
       exit(1);
    }

    readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
                  Ptrfmt, Indfmt, Valfmt, Rhsfmt,
                  &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
    fclose(in_file);

    if (Type[0] == 'P' ) {
      fprintf(stderr,"This is a streaming translator for LARGE files with ");
      fprintf(stderr,"REAL or COMPLEX data.  Use 'hbmat2mtx' for PATTERN matrices.\n");
      exit(1);
    }
    in_file = readHB_newind(argv[1], &Nrow, &Ncol, &Nnzero, &colptr, &rowind);
    ParseIfmt(Indfmt,&Indperline,&Indwidth);
    sprintf(format,"%%%dd %%%dd ",Indwidth,Indwidth);
    ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
    sprintf(rformat,"%%%ds ",Valwidth);
    ThisElement = (char *) malloc(Valwidth+1);


/*  Skip to values in hb file: */

   mm_set_matrix(&matcode);
   mm_set_coordinate(&matcode);
   if ( Type[0] == 'R' )
    mm_set_real(&matcode);
   else if ( Type[0] == 'C' )
    mm_set_complex(&matcode);
   else if ( Type[0] == 'P' )
    mm_set_pattern(&matcode);
   else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",Type);
    exit(1);
   }
   if ( Type[1] == 'U' || Type[1] == 'R' )
    mm_set_general(&matcode);
   else if ( Type[1] == 'S' )
    mm_set_symmetric(&matcode);
   else if ( Type[1] == 'Z' )
    mm_set_skew(&matcode);
   else if ( Type[1] == 'H' )
    mm_set_hermitian(&matcode);
   else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",&Type[1]);
    exit(1);
   }
   if ( Type[2] != 'A' ){
    fprintf(stderr,"Unrecognized format in HB Type: %1s",&Type[2]);
    exit(1);
   }
    mm_write_banner(stdout, matcode);
    fprintf(stdout,"%% RBTitle: %s\n",Title);
    fprintf(stdout,"%% RBKey:   %s\n",Key);
    mm_write_mtx_crd_size(stdout, Nrow, Ncol, Nnzero);

    if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.\nhb2mtxstrm.c: Line 117 approx.");
    repeat=0;
    if ( Type[0] == 'C') repeat=1;
    count = 0;
    colcount = &colptr[1];
    items = 0; 
    for (i=0;i<Valcrd;i++)
    {
       fgets(line, BUFSIZ, in_file);
       if ( sscanf(line,"%*s") < 0 ) {
         fprintf(stderr,"iohb.c: Null (or blank) line in value data region of HB file.\n");
         exit(1);
       }
       if (Valflag == 'D') {
          while( strchr(line,'D') ) *strchr(line,'D') = 'E';
       }
       col =  0;
       for (ind = 0;ind<Valperline;ind++)
       {
          if (count == Nnzero) break;
          if (count == *colcount-1) colcount++;
          strncpy(ThisElement,line+col,Valwidth);
          *(ThisElement+Valwidth) = (char) NULL;
          if ( Valflag != 'F' && strchr(ThisElement,'E') == NULL ) {
             /* insert a char prefix for exp */
             last = strlen(ThisElement);
             for (j=last+1;j>=0;j--) {
                ThisElement[j] = ThisElement[j-1];
                if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) {
                   ThisElement[j-1] = Valflag;
                   break;
                }
             }
          }
         items++;
         if (items==1) fprintf(stdout,format,rowind[count],colcount-colptr);
         if (items > repeat) { 
            fprintf(stdout,rformat,ThisElement);
            fprintf(stdout,"\n");
            count++; 
            items=0;
         } else {
            fprintf(stdout,rformat,ThisElement);
         }
         col += Valwidth;
       }
    }
       
}
Exemplo n.º 16
0
// MANDATORY
int mm_read_banner ( FILE *f, MM_typecode *matcode )
/******************************************************************************/
/*
  Purpose:
    MM_READ_BANNER reads the header line of an MM file.
  Modified:
    31 October 2008
    15 Oct 2012 to replace int with long unsigned int 
  Parameters:
    Input, FILE *F, a pointer to the input file.
    Output, MM_typecode *MATCODE, the header information.
*/
{
    char line[MM_MAX_LINE_LENGTH];
    char banner[MM_MAX_TOKEN_LENGTH];
    char mtx[MM_MAX_TOKEN_LENGTH]; 
    char crd[MM_MAX_TOKEN_LENGTH];
    char data_type[MM_MAX_TOKEN_LENGTH];
    char storage_scheme[MM_MAX_TOKEN_LENGTH];
    char *p;
    mm_clear_typecode(matcode);  
    if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL) 
        return MM_PREMATURE_EOF;
    // banner: MatrixMarket
    // mtx: matrix or vector 
    // format: coordinate or dense array 
    // field : real or int of value 
    // symmetry: general, symmetric, skew-symmetric ... 
    if (sscanf(line, "%s %s %s %s %s", banner, mtx, crd, data_type, 
        storage_scheme) != 5)
        return MM_PREMATURE_EOF;
    for (p=mtx; *p!='\0'; *p=tolower(*p),p++);  /* convert to lower case */
    for (p=crd; *p!='\0'; *p=tolower(*p),p++);  
    for (p=data_type; *p!='\0'; *p=tolower(*p),p++);
    for (p=storage_scheme; *p!='\0'; *p=tolower(*p),p++);
/* 
  check for banner 
*/
    if (strncmp(banner, MatrixMarketBanner, strlen(MatrixMarketBanner)) != 0)
        return MM_NO_HEADER;
/* 
  first field should be "mtx" 
*/
    if (strcmp(mtx, MM_MTX_STR) != 0)
        return  MM_UNSUPPORTED_TYPE;
    mm_set_matrix(matcode);
/* 
  second field describes whether this is a sparse matrix (in coordinate
  storgae) or a dense array 
*/
    if (strcmp(crd, MM_SPARSE_STR) == 0)
        mm_set_sparse(matcode);
    else
    if (strcmp(crd, MM_DENSE_STR) == 0)
            mm_set_dense(matcode);
    else
        return MM_UNSUPPORTED_TYPE;   
/*
  third field 
*/
    if (strcmp(data_type, MM_REAL_STR) == 0)
        mm_set_real(matcode);
    else
    if (strcmp(data_type, MM_COMPLEX_STR) == 0)
        mm_set_complex(matcode);
    else
    if (strcmp(data_type, MM_PATTERN_STR) == 0)
        mm_set_pattern(matcode);
    else
    if (strcmp(data_type, MM_INT_STR) == 0)
        mm_set_integer(matcode);
    else
        return MM_UNSUPPORTED_TYPE;    
/*
  fourth field 
*/
    if (strcmp(storage_scheme, MM_GENERAL_STR) == 0)
        mm_set_general(matcode);
    else
    if (strcmp(storage_scheme, MM_SYMM_STR) == 0)
        mm_set_symmetric(matcode);
    else
    if (strcmp(storage_scheme, MM_HERM_STR) == 0)
        mm_set_hermitian(matcode);
    else
    if (strcmp(storage_scheme, MM_SKEW_STR) == 0)
        mm_set_skew(matcode);
    else
        return MM_UNSUPPORTED_TYPE;        
    return 0;
}
Exemplo n.º 17
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);
}
Exemplo n.º 18
0
int mm_read_banner(FILE *f, MM_typecode *matcode)
{
    char line[MM_MAX_LINE_LENGTH];
    char banner[MM_MAX_TOKEN_LENGTH];
    char mtx[MM_MAX_TOKEN_LENGTH]; 
    char crd[MM_MAX_TOKEN_LENGTH];
    char data_type[MM_MAX_TOKEN_LENGTH];
    char storage_scheme[MM_MAX_TOKEN_LENGTH];
    char *p;


    mm_clear_typecode(matcode);  

    if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL) 
        return MM_PREMATURE_EOF;

    if (sscanf(line, "%s %s %s %s %s", banner, mtx, crd, data_type, 
        storage_scheme) != 5)
        return MM_PREMATURE_EOF;

    for (p=mtx; *p!='\0'; *p=tolower(*p),p++);  /* convert to lower case */
    for (p=crd; *p!='\0'; *p=tolower(*p),p++);  
    for (p=data_type; *p!='\0'; *p=tolower(*p),p++);
    for (p=storage_scheme; *p!='\0'; *p=tolower(*p),p++);

    /* check for banner */
    if (strncmp(banner, MatrixMarketBanner, strlen(MatrixMarketBanner)) != 0)
        return MM_NO_HEADER;

    /* first field should be "mtx" */
    if (strcmp(mtx, MM_MTX_STR) != 0)
        return  MM_UNSUPPORTED_TYPE;
    mm_set_matrix(matcode);


    /* second field describes whether this is a sparse matrix (in coordinate
            storgae) or a dense array */


    if (strcmp(crd, MM_SPARSE_STR) == 0)
        mm_set_sparse(matcode);
    else
    if (strcmp(crd, MM_DENSE_STR) == 0)
            mm_set_dense(matcode);
    else
        return MM_UNSUPPORTED_TYPE;
    

    /* third field */

    if (strcmp(data_type, MM_REAL_STR) == 0)
        mm_set_real(matcode);
    else
    if (strcmp(data_type, MM_COMPLEX_STR) == 0)
        mm_set_complex(matcode);
    else
    if (strcmp(data_type, MM_PATTERN_STR) == 0)
        mm_set_pattern(matcode);
    else
    if (strcmp(data_type, MM_INT_STR) == 0)
        mm_set_integer(matcode);
    else
        return MM_UNSUPPORTED_TYPE;
    

    /* fourth field */

    if (strcmp(storage_scheme, MM_GENERAL_STR) == 0)
        mm_set_general(matcode);
    else
    if (strcmp(storage_scheme, MM_SYMM_STR) == 0)
        mm_set_symmetric(matcode);
    else
    if (strcmp(storage_scheme, MM_HERM_STR) == 0)
        mm_set_hermitian(matcode);
    else
    if (strcmp(storage_scheme, MM_SKEW_STR) == 0)
        mm_set_skew(matcode);
    else
        return MM_UNSUPPORTED_TYPE;
        

    return 0;
}
Exemplo n.º 19
0
inline void set_typecode<vec>(MM_typecode & matcode){
  mm_set_real(&matcode);
}
Exemplo n.º 20
0
int main(int argc, char *argv[])
{
  FILE *in_file;
  char Title[73], Key[9], Rhstype[4];
  char Type[4];
  char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
  int Ptrcrd, Indcrd, Valcrd;
  int Rhscrd = 0;
  int Indperline, Indwidth;
  int Valperline, Valwidth, Valprec;
  int Valflag;           /* Indicates 'E','D', or 'F' float format */
  int Nrow, Ncol, Nnzero;
  int Nrhs;
  char *valc = NULL;
  char *Valfmtc;
  char *tmp1;
  char *tmp2;
  char format[30];
  char rformat[30];
  char cformat[30];
  int *colptr, *rowind;
  int i,j, indxval, rowp1, colp1;
  MM_typecode matcode;

  if (argc != 2) {
    printf("Usage: %s HBfile \n", argv[0]);
    exit(-1);
  }

  in_file = fopen( argv[1], "r");
  if (in_file == NULL)
  {
    fprintf(stderr,"Error: Cannot open file: %s\n",argv[1]);
    exit(1);
  }

  readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
      Ptrfmt, Indfmt, Valfmt, Rhsfmt,
      &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
  fclose(in_file);

  readHB_newmat_char(argv[1], &Nrow, &Ncol, &Nnzero, &colptr, &rowind, &valc,
      &Valfmtc);
  ParseIfmt(Indfmt,&Indperline,&Indwidth);
  ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
  sprintf(format,"%%%dd %%%dd \n",Indwidth,Indwidth);
  sprintf(rformat,"%%%dd %%%dd %%%ds\n",Indwidth,Indwidth,Valwidth);
  sprintf(cformat,"%%%dd %%%dd %%%ds %%%ds\n",Indwidth,Indwidth,Valwidth,Valwidth);


  mm_set_matrix(&matcode);
  mm_set_coordinate(&matcode);
  if ( Type[0] == 'R' )
    mm_set_real(&matcode);
  else if ( Type[0] == 'C' )
    mm_set_complex(&matcode);
  else if ( Type[0] == 'P' )
    mm_set_pattern(&matcode);
  else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",Type);
    exit(1);
  }
  if ( Type[1] == 'U' || Type[1] == 'R' )
    mm_set_general(&matcode);
  else if ( Type[1] == 'S' )
    mm_set_symmetric(&matcode);
  else if ( Type[1] == 'Z' )
    mm_set_skew(&matcode);
  else if ( Type[1] == 'H' )
    mm_set_hermitian(&matcode);
  else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",&Type[1]);
    exit(1);
  }
  if ( Type[2] != 'A' ){
    fprintf(stderr,"Unrecognized format in HB Type: %1s",&Type[2]);
    exit(1);
  }
  mm_write_banner(stdout, matcode);
  fprintf(stdout,"%% RBTitle: %s\n",Title);
  fprintf(stdout,"%% RBKey:   %s\n",Key);
  mm_write_mtx_crd_size(stdout, Nrow, Ncol, Nnzero);

  if ( Type[0] == 'C' ) {
    /*  Loop through columns */
    for (j = 0; j < Ncol ; j++)
      for (i=colptr[j];i<colptr[j+1];i++)
      {
        indxval = 2*(i-1);
        rowp1 = rowind[i-1]+1-1;
        colp1 = j + 1;
        tmp1 = substr(valc,indxval*Valwidth,Valwidth);
        tmp2 = substr(valc,(indxval+1)*Valwidth,Valwidth);
        fprintf(stdout,cformat,rowp1,colp1,tmp1,tmp2);
      }
  } else if ( Type[0] == 'R' ) {
    /*  Loop through columns */
    for (j = 0; j < Ncol ; j++)
      for (i=colptr[j];i<colptr[j+1];i++)
      {
        rowp1 = rowind[i-1];
        colp1 = j + 1;
        tmp1 = substr(valc,(i-1)*Valwidth,Valwidth);
        fprintf(stdout,rformat,rowp1,colp1,tmp1);
      }
  } else {
    /*  Loop through columns */
    for (j = 0; j < Ncol ; j++)
      for (i=colptr[j];i<colptr[j+1];i++)
      {
        rowp1 = rowind[i-1];
        colp1 = j + 1;
        fprintf(stdout,format,rowp1,colp1);
      }
  }
  return 0;
}
Exemplo n.º 21
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;
}
Exemplo n.º 22
0
void set_matcode(MM_typecode & matcode){
  mm_initialize_typecode(&matcode);
  mm_set_matrix(&matcode);
  mm_set_array(&matcode);
  mm_set_real(&matcode);
}