Ejemplo n.º 1
0
/** returns the zero indexed array of values. */
void read_mm_array(FILE *f, MM_typecode code, int nnzs, double *values) {
  if (mm_is_symmetric(code)) {
    printf("Array can't be symmetric!\n");
    exit(1);
  }

  int i, x, y;
  double val;
  if (mm_is_array(code)) {
    // Is this actually stored in an array format?
    for (i = 0; i < nnzs; i++) {
      fscanf(f, "%lg", &val);
      values[i] = val;
    }
  } else {
    // the array was stored in COO format
    for (i = 0; i < nnzs; i++) {
      fscanf(f, "%d %d %lg", &x, &y, &val);
      if (y != 1) {
        printf("An array should only have ONE column!\n");
        exit(1);
      }
      values[x - 1] = val;
    }
  }
}
Ejemplo n.º 2
0
int readSymmMatrix(char* filename, int& n, double*& v)
{
	int m;

	// 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_symmetric(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 square matrix
	if (n != m) {
		fprintf(stderr, "Needs to be square.\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 = j; i < m; ++i) {
			fscanf(fp, "%lf\n", &v[i * n + j]);
			if (i != j) {
				v[j * n + i] = v[i * n + j];
			}
		}
	}
	printf("done\n");

	// close the file
	fclose(fp);

	return 0;
}
Ejemplo n.º 3
0
/** Reads the dimensions of the matrix (n - number of rows, m - number
    of columns, nnzs - number of non-zeros) from the given Matrix
    Market file.  If the given file contains an array rather than a
    COO matrix, nnzs will be set to n;
*/
void read_mm_matrix_size(FILE *f, int *n, int *m, int *nnzs, MM_typecode* mcode) {
  if (mm_read_banner(f, mcode) != 0) {
    printf("Could not process Matrix Market banner.\n");
    exit(1);
  }

  if (mm_is_array(*mcode)) {
    mm_read_mtx_array_size(f, n, m);
    *nnzs = *n;
  } else {
    mm_read_mtx_crd_size(f, n, m, nnzs);
  }
}
Ejemplo n.º 4
0
static void read_mtx_and_return_csr(
	int argc, char **argv, int *mm, int *nn, int **ia, int **ja, double **aa)
{
	int ret_code;
    MM_typecode matcode;
    FILE *f;
    int m, n, nz;   
    int i, *I, *J;
    double *val;

	if (argc < 2)
	{
		fprintf(stderr, "Usage: %s [martix-market-filename]\n", argv[0]);
		exit(1);
	}
    else    
    {
		printf("\n===================================\n");
		printf("mtx file = %s\n", argv[1]);
		if ((f = fopen(argv[1], "r")) == NULL)
		{
			printf("Could not open %s\n", argv[1]); 
            exit(1);
		}
    }

    if (mm_read_banner(f, &matcode) != 0)
    {
        printf("Could not process Matrix Market banner.\n");
        exit(1);
    }

    /*  This is how one can screen matrix types if their application */
    /*  only supports a subset of the Matrix Market data types.      */
	if ( mm_is_pattern(matcode) 
		|| mm_is_dense(matcode)
		|| mm_is_array(matcode) )
	{
		printf("Sorry, this application does not support ");
        printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
		exit(1);
	}
	

    if (mm_is_complex(matcode) && mm_is_matrix(matcode) && 
            mm_is_sparse(matcode) )
    {
        printf("Sorry, this application does not support ");
        printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
        exit(1);
    }

    /* find out size of sparse matrix .... */

    if ((ret_code = mm_read_mtx_crd_size(f, &m, &n, &nz)) !=0)
        exit(1);

    /* reseve memory for matrices */

    I = (int *) malloc(nz * sizeof(int));
    J = (int *) malloc(nz * sizeof(int));
    val = (double *) malloc(nz * sizeof(double));


    /* NOTE: when reading in doubles, ANSI C requires the use of the "l"  */
    /*   specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
    /*  (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15)            */

    for (i=0; i<nz; i++)
    {
        fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]);
        I[i]--;  /* adjust from 1-based to 0-based */
        J[i]--;
    }

    if (f !=stdin) fclose(f);

	if (m != n) exit(1);
	
	*mm = m;	
	*nn = n;
	*ia = (int*) malloc (sizeof(int) * (n+1));
    *ja = (int*) malloc (sizeof(int) * nz);
    *aa = (double*) malloc (sizeof(double) * nz);
	coo2csr(n, nz, val, I, J, *aa, *ja, *ia);
	
	free (I);
	free (J);
	free (val);
}