Ejemplo n.º 1
0
int mm_read_mtx_crd_entry(FILE *f, int *I, int *J,
        double *real, double *imag, MM_typecode matcode)
{
    if (mm_is_complex(matcode))
    {
            if (fscanf(f, "%d %d %lg %lg", I, J, real, imag)
                != 4) return MM_PREMATURE_EOF;
    }
    else if (mm_is_real(matcode))
    {
            if (fscanf(f, "%d %d %lg\n", I, J, real)
                != 3) return MM_PREMATURE_EOF;

    }

    else if (mm_is_pattern(matcode))
    {
            if (fscanf(f, "%d %d", I, J) != 2) return MM_PREMATURE_EOF;
    }
    else
        return MM_UNSUPPORTED_TYPE;

    return 0;
        
}
Ejemplo n.º 2
0
/* READ_MTX - read symmetric sparse matrix in MatrixMarket format
 */
void read_MTX_SSS(char *fname, int *n,
                  double **va, double **da, int **ja, int **ia) {
  int m, nz, ret_code, i;
  double *v_coo;
  int *i_coo, *j_coo;
  MM_typecode matcode;
  FILE *f;

  f = fopen(fname, "r");
  assert(f != NULL);
  ret_code = mm_read_banner(f, &matcode);
  assert(ret_code == 0);
  assert(mm_is_real(matcode) && mm_is_matrix(matcode) &&
         mm_is_sparse(matcode) && mm_is_symmetric(matcode));
  ret_code = mm_read_mtx_crd_size(f, &m, n, &nz);
  assert(ret_code == 0);
  assert(m == *n);
  /* read COO format */
  i_coo = (int *)malloc(nz * sizeof(int));
  j_coo = (int *)malloc(nz * sizeof(int));
  v_coo = (double *)malloc(nz * sizeof(double));
  assert(i_coo && j_coo && v_coo);
  for (i = 0; i < nz; i ++) {
    fscanf(f, "%d %d %lg\n", &i_coo[i], &j_coo[i], &v_coo[i]);
    i_coo[i]--;  /* adjust from 1-based to 0-based */
    j_coo[i]--;
  }
  fclose(f);
  /* convert to SSS format */
  convert_COO_SSS(*n, nz, i_coo, j_coo, v_coo, ia, ja, va, da);
  free(i_coo); free(j_coo); free(v_coo);
}
Ejemplo n.º 3
0
void MatrixMarketReader<FloatType>::MMGenerateCOOFromFile( FILE *infile, cl_bool read_explicit_zeroes )
{
    clsparseIdx_t unsym_actual_nnz = 0;
    FloatType val;
    clsparseIdx_t ir, ic;

    const int exp_zeroes = read_explicit_zeroes;

    //silence warnings from fscanf (-Wunused-result)
    clsparseIdx_t rv = 0;

    for ( clsparseIdx_t i = 0; i < nNZ; i++)
    {
        if( mm_is_real( Typecode ) )
        {
            fscanf(infile, "%" SIZET "u", &ir);
            fscanf(infile, "%" SIZET "u", &ic);

            if (typeid(FloatType) == typeid(float))
                rv = fscanf(infile, "%f\n", (float*)(&val));

            else if( typeid( FloatType ) == typeid( double ) )
              rv = fscanf( infile, "%lf\n", (double*)( &val ) );

            if( exp_zeroes == 0 && val == 0 )
                continue;
            else
                FillCoordData( Typecode, unsym_coords, unsym_actual_nnz, ir, ic, val );
        }
        else if( mm_is_integer( Typecode ) )
        {
            fscanf(infile, "%" SIZET "u", &ir);
            fscanf(infile, "%" SIZET "u", &ic);

            if(typeid(FloatType) == typeid(float))
               rv = fscanf(infile, "%f\n", (float*)( &val ) );
            else if(typeid(FloatType) == typeid(double))
               rv = fscanf(infile, "%lf\n", (double*)( &val ) );

            if( exp_zeroes == 0 && val == 0 )
                continue;
            else
                FillCoordData( Typecode, unsym_coords, unsym_actual_nnz, ir, ic, val );

        }
        else if( mm_is_pattern( Typecode ) )
        {
            rv = fscanf(infile, "%" SIZET "u", &ir);
            rv = fscanf(infile, "%" SIZET "u", &ic);

            val = static_cast<FloatType>( MAX_RAND_VAL * ( rand( ) / ( RAND_MAX + 1.0 ) ) );

            if( exp_zeroes == 0 && val == 0 )
                continue;
            else
                FillCoordData( Typecode, unsym_coords, unsym_actual_nnz, ir, ic, val );
        }
    }
    nNZ = unsym_actual_nnz;
}
Ejemplo n.º 4
0
char  *mm_typecode_to_str(MM_typecode matcode)
{
    char buffer[MM_MAX_LINE_LENGTH];
    char *types[4];
    int error =0;
    int i;

    /* check for MTX type */
    if (mm_is_matrix(matcode))
        types[0] = MM_MTX_STR;
    else
        error=1;

    /* check for CRD or ARR matrix */
    if (mm_is_sparserow(matcode))
        types[1] = MM_SPARSEROW_STR;
    else
    if (mm_is_coordinate(matcode))
        types[1] = MM_COORDINATE_STR;
    else
    if (mm_is_dense(matcode))
        types[1] = MM_DENSE_STR;
    else
        return NULL;

    /* check for element data type */
    if (mm_is_real(matcode))
        types[2] = MM_REAL_STR;
    else
    if (mm_is_complex(matcode))
        types[2] = MM_COMPLEX_STR;
    else
    if (mm_is_pattern(matcode))
        types[2] = MM_PATTERN_STR;
    else
    if (mm_is_integer(matcode))
        types[2] = MM_INT_STR;
    else
        return NULL;


    /* check for symmetry type */
    if (mm_is_general(matcode))
        types[3] = MM_GENERAL_STR;
    else
    if (mm_is_symmetric(matcode))
        types[3] = MM_SYMM_STR;
    else
    if (mm_is_hermitian(matcode))
        types[3] = MM_HERM_STR;
    else
    if (mm_is_skew(matcode))
        types[3] = MM_SKEW_STR;
    else
        return NULL;

    sprintf(buffer,"%s %s %s %s", types[0], types[1], types[2], types[3]);
    return strdup(buffer);

}
Ejemplo n.º 5
0
int mm_read_mtx_crd_data(FILE *f, int M, int N, int nz, int I[], int J[],
        double val[], MM_typecode matcode)
{
    int i;
    if (mm_is_complex(matcode))
    {
        for (i=0; i<nz; i++)
            if (fscanf(f, "%d %d %lg %lg", &I[i], &J[i], &val[2*i], &val[2*i+1])
                != 4) return MM_PREMATURE_EOF;
    }
    else if (mm_is_real(matcode))
    {
        for (i=0; i<nz; i++)
        {
            if (fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i])
                != 3) return MM_PREMATURE_EOF;

        }
    }

    else if (mm_is_pattern(matcode))
    {
        for (i=0; i<nz; i++)
            if (fscanf(f, "%d %d", &I[i], &J[i])
                != 2) return MM_PREMATURE_EOF;
    }
    else
        return MM_UNSUPPORTED_TYPE;

    return 0;
        
}
Ejemplo n.º 6
0
int mm_is_valid ( MM_typecode matcode )
/******************************************************************************/
/*
  Purpose:
    MM_IS_VALID checks whether the MM header information is valid.
  Modified:
    31 October 2008
  Parameters:
    Input, MM_typecode MATCODE, the header information.
    Output, int MM_IS_VALID, is TRUE if the matrix code is valid.
*/
{
  if ( !mm_is_matrix ( matcode ) ) 
  {
    return 0;
  }
  if ( mm_is_dense ( matcode ) && mm_is_pattern ( matcode ) ) 
  {
    return 0;
  }
  if ( mm_is_real ( matcode ) && mm_is_hermitian ( matcode ) ) 
  {
    return 0;
  }
  if ( mm_is_pattern ( matcode ) && 
      ( mm_is_hermitian ( matcode ) || mm_is_skew ( matcode ) ) ) 
  {
    return 0;
  }
  return 1;
}
Ejemplo n.º 7
0
int mm_write_mtx_crd(char fname[], int M, int N, int nz, int I[], int J[],
        double val[], MM_typecode matcode)

/******************************************************************************/
/*
  Purpose:

    MM_WRITE_MTX_CRD writes an MM coordinate file.

  Modified:

    31 October 2008
*/
{
    FILE *f;
    int i;

    if (strcmp(fname, "stdout") == 0) 
        f = stdout;
    else
    if ((f = fopen(fname, "w")) == NULL)
        return MM_COULD_NOT_WRITE_FILE;
/*
  print banner followed by typecode.
*/
    fprintf(f, "%s ", MatrixMarketBanner);
    fprintf(f, "%s\n", mm_typecode_to_str(matcode));

/*
  print matrix sizes and nonzeros.
*/
    fprintf(f, "%d %d %d\n", M, N, nz);
/* 
  print values.
*/
    if (mm_is_pattern(matcode))
        for (i=0; i<nz; i++)
            fprintf(f, "%d %d\n", I[i], J[i]);
    else
    if (mm_is_real(matcode))
        for (i=0; i<nz; i++)
            fprintf(f, "%d %d %20.16g\n", I[i], J[i], val[i]);
    else
    if (mm_is_complex(matcode))
        for (i=0; i<nz; i++)
            fprintf(f, "%d %d %20.16g %20.16g\n", I[i], J[i], val[2*i], 
                        val[2*i+1]);
    else
    {
        if (f != stdout) fclose(f);
        return MM_UNSUPPORTED_TYPE;
    }

  if ( f !=stdout ) 
  {
    fclose(f);
  }
  return 0;
}
Ejemplo n.º 8
0
int mm_read_mtx_crd(char *fname, int *M, int *N, int *nz, int **I, int **J,
        double **val, MM_typecode *matcode)
{
    int ret_code;
    FILE *f;

    if (strcmp(fname, "stdin") == 0) f=stdin;
    else
    if ((f = fopen(fname, "r")) == NULL)
        return MM_COULD_NOT_READ_FILE;


    if ((ret_code = mm_read_banner(f, matcode)) != 0)
        return ret_code;

    if (!(mm_is_valid(*matcode) && mm_is_sparse(*matcode) &&
            mm_is_matrix(*matcode)))
        return MM_UNSUPPORTED_TYPE;

    if ((ret_code = mm_read_mtx_crd_size(f, M, N, nz)) != 0)
        return ret_code;


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

    *I = new int[*nz];
    *J = new int[*nz];
    *val = 0;

    if (mm_is_complex(*matcode))
    {
        //*val = (double *) malloc(*nz * 2 * sizeof(double));
        *val = new double[2*(*nz)];
        ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
                *matcode);
        if (ret_code != 0) return ret_code;
    }
    else if (mm_is_real(*matcode))
    {
        //*val = (double *) malloc(*nz * sizeof(double));
        *val = new double[*nz];
        ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
                *matcode);
        if (ret_code != 0) return ret_code;
    }

    else if (mm_is_pattern(*matcode))
    {
        ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
                *matcode);
        if (ret_code != 0) return ret_code;
    }

    if (f != stdin) fclose(f);
    return 0;
}
Ejemplo n.º 9
0
int mm_is_valid(MM_typecode matcode)
{
    if (!mm_is_matrix(matcode)) return 0;
    if (mm_is_dense(matcode) && mm_is_pattern(matcode)) return 0;
    if (mm_is_real(matcode) && mm_is_hermitian(matcode)) return 0;
    if (mm_is_pattern(matcode) && (mm_is_hermitian(matcode) || 
                mm_is_skew(matcode))) return 0;
    return 1;
}
Ejemplo n.º 10
0
 void mm_typecode_to_str(MM_typecode matcode, char * buffer)
{
    char type0[20];
    char type1[20];
    char type2[20];
    char type3[20];
    int error =0;

    /* check for MTX type */
    if (mm_is_matrix(matcode))
        strcpy(type0, MM_MTX_STR);
    else
        error=1;

    /* check for CRD or ARR matrix */
    if (mm_is_sparse(matcode))
        strcpy(type1, MM_SPARSE_STR);
    else
    if (mm_is_dense(matcode))
        strcpy(type1, MM_DENSE_STR);
    else
        return;

    /* check for element data type */
    if (mm_is_real(matcode))
        strcpy(type2, MM_REAL_STR);
    else
    if (mm_is_complex(matcode))
        strcpy(type2, MM_COMPLEX_STR);
    else
    if (mm_is_pattern(matcode))
        strcpy(type2, MM_PATTERN_STR);
    else
    if (mm_is_integer(matcode))
        strcpy(type2, MM_INT_STR);
    else
        return;

    /* check for symmetry type */
    if (mm_is_general(matcode))
        strcpy(type3, MM_GENERAL_STR);
    else
    if (mm_is_symmetric(matcode))
        strcpy(type3, MM_SYMM_STR);
    else
    if (mm_is_hermitian(matcode))
        strcpy(type3, MM_HERM_STR);
    else
    if (mm_is_skew(matcode))
        strcpy(type3, MM_SKEW_STR);
    else
        return;

    sprintf(buffer,"%s %s %s %s", type0, type1, type2, type3);
    return;
}
Ejemplo n.º 11
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.º 12
0
/** Reads a matrix market file for a symmetric real valued sparse
    matrix and returns the matrix in 1-indexed CSR form. */
void read_mm_sym_matrix(FILE* f, MM_typecode mcode,
                        int n, int nnzs,
                        double *values, int* col_ind, int *row_ptr
                        ) {

  if (!(mm_is_real(mcode) && mm_is_matrix(mcode) &&
        mm_is_sparse(mcode) && mm_is_symmetric(mcode)) ) {
    printf("First argument must be a symmetric, real-valued, sparse matrix\n");
    printf("Market Market type: [%s]\n", mm_typecode_to_str(mcode));
    exit(1);
  }

  // read Matrix Market matrix in COO format
  int* I = (int *) malloc(nnzs * sizeof(int));
  int *J = (int *) malloc(nnzs * sizeof(int));
  double *val = (double *) malloc(nnzs * sizeof(double));

  int i;
  for (i=0; i<nnzs; i++) {
    fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]);
    I[i]--;
    J[i]--;
  }

  MKL_INT job[] = {
    1, // convert COO to CSR
    1, // use 1 based indexing for CSR matrix (required by mkl_dcsrsymv)
    0,
    0, // Is this used?
    nnzs,
    0
  };

  MKL_INT info;

  // convert COO matrix to CSR
  mkl_dcsrcoo(job,
              &n,
              values,
              col_ind,
              row_ptr,
              &nnzs,
              val,
              I,
              J,
              &info);

  if (info != 0) {
    printf("CSR to COO conversion failed: not enough memory!\n");
    exit(1);
  }
}
Ejemplo n.º 13
0
int mm_read_mtx_crd_data(FILE *f, int M, int N, int nz, int I[], int J[],
        double val[], MM_typecode matcode)

/******************************************************************************/
/*
  Purpose:

    MM_READ_MTX_CRD_DATA reads the values in an MM coordinate file.

  Discussion:

    This function assumes the array storage has already been allocated.

  Modified:

    31 October 2008

  Parameters:

    Input, FILE *F, a pointer to the input file.
*/
{
    int i;
    if (mm_is_complex(matcode))
    {
        for (i=0; i<nz; i++)
            if (fscanf(f, "%d %d %lg %lg", &I[i], &J[i], &val[2*i], &val[2*i+1])
                != 4) return MM_PREMATURE_EOF;
    }
    else if (mm_is_real(matcode))
    {
        for (i=0; i<nz; i++)
        {
            if (fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i])
                != 3) return MM_PREMATURE_EOF;

        }
    }

    else if (mm_is_pattern(matcode))
    {
        for (i=0; i<nz; i++)
            if (fscanf(f, "%d %d", &I[i], &J[i])
                != 2) return MM_PREMATURE_EOF;
    }
    else
        return MM_UNSUPPORTED_TYPE;

    return 0;
        
}
Ejemplo n.º 14
0
int mm_read_mtx_crd(char *fname, int *M, int *N, int *nz, int **I, int **J,
	double **val, MM_typecode *matcode)
{
    int ret_code;
    ZOLTAN_FILE* f;

    if ((f = ZOLTAN_FILE_open(fname, "r", STANDARD)) == NULL)
      return MM_COULD_NOT_READ_FILE;


    if ((ret_code = mm_read_banner(f, matcode)) != 0)
	return ret_code;

    if (!(mm_is_valid(*matcode) && mm_is_sparse(*matcode) &&
	    mm_is_matrix(*matcode)))
	return MM_UNSUPPORTED_TYPE;

    if ((ret_code = mm_read_mtx_crd_size(f, M, N, nz)) != 0)
	return ret_code;


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

    if (mm_is_complex(*matcode))
    {
	*val = (double *) malloc(*nz * 2 * sizeof(double));
	ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
		*matcode);
	if (ret_code != 0) return ret_code;
    }
    else if (mm_is_real(*matcode))
    {
	*val = (double *) malloc(*nz * sizeof(double));
	ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
		*matcode);
	if (ret_code != 0) return ret_code;
    }

    else if (mm_is_pattern(*matcode))
    {
	ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
		*matcode);
	if (ret_code != 0) return ret_code;
    }

    ZOLTAN_FILE_close(f);
    return 0;
}
Ejemplo n.º 15
0
int readBandedMatrix(char* filename, int& n, int& t, int& nz, long long*& AR, long long*& AC, double*& AV)
{
	// 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)) {
		fprintf(stderr, "Could not process Matrix Market banner.\n");
		return 2;
	}

	// check the type
	if (!mm_is_matrix(type) || !mm_is_coordinate(type) || !mm_is_real(type) || !mm_is_general(type)) {
		fprintf(stderr, "Sorry, this application does not support Market Market type: [%s]\n",
				mm_typecode_to_str(type));
		return 3;
	}

	// read the sizes and nnz of the matrix
	int m;
	if (mm_read_mtx_crd_size(fp, &n, &m, &nz)) {
		fprintf(stderr, "Could not read the size of the matrix.\n");
		return 4;
	}
	
	printf("reading %s:\n\ta %d x %d banded matrix ", filename, n, m);
	// allocate the memory
	AR = new long long[nz];
	AC = new long long[nz];
	AV = new double[nz];
	t = 0;
	for (int i = 0; i < nz; ++i) {
		fscanf(fp, "%d %d %lf\n", AR + i, AC + i, AV + i);
		--AR[i];		// 0-indexing
		--AC[i];		// 0-indexing
		t = std::max(t, (int)(AR[i] - AC[i]));
	}
	printf("with bandwidth (2m + 1) = %d...done\n", 2 * t + 1);

	// close the file
	fclose(fp);

	return 0;
}
Ejemplo n.º 16
0
void MatrixMarketReader<FloatType>::MMGenerateCOOFromFile( FILE *infile )
{
    int unsym_actual_nnz = 0;
    FloatType val;
    int ir, ic;

    const int exp_zeroes = 0;

    for( int i = 0; i < nNZ; i++ )
    {
        if( mm_is_real( Typecode ) )
        {
            if( typeid( FloatType ) == typeid( float ) )
                fscanf( infile, "%d %d %f\n", &ir, &ic, &val );
            else if( typeid( FloatType ) == typeid( double ) )
                fscanf( infile, "%d %d %lf\n", &ir, &ic, &val );

            if( exp_zeroes == 0 && val == 0 )
                continue;
            else
                FillCoordData( Typecode, unsym_coords, unsym_actual_nnz, ir, ic, val );
        }
        else if( mm_is_integer( Typecode ) )
        {
            if(typeid(FloatType) == typeid(float))
                fscanf(infile, "%d %d %f\n", &ir, &ic, &val);
            else if(typeid(FloatType) == typeid(double))
                fscanf(infile, "%d %d %lf\n", &ir, &ic, &val);

            if( exp_zeroes == 0 && val == 0 )
                continue;
            else
                FillCoordData( Typecode, unsym_coords, unsym_actual_nnz, ir, ic, val );

        }
        else if( mm_is_pattern( Typecode ) )
        {
            fscanf( infile, "%d %d", &ir, &ic );
            val = static_cast<FloatType>( MAX_RAND_VAL * ( rand( ) / ( RAND_MAX + 1.0 ) ) );

            if( exp_zeroes == 0 && val == 0 )
                continue;
            else
                FillCoordData( Typecode, unsym_coords, unsym_actual_nnz, ir, ic, val );
        }
    }
    nNZ = unsym_actual_nnz;
}
Ejemplo n.º 17
0
Archivo: mmio.c Proyecto: nschloe/hbio
int mm_write_mtx_crd(char fname[], int M, int N, int nz, int I[], int J[],
    double val[], MM_typecode matcode)
{
  FILE *f;
  int i;
  char * tmp;

  if (strcmp(fname, "stdout") == 0)
    f = stdout;
  else
    if ((f = fopen(fname, "w")) == NULL)
      return MM_COULD_NOT_WRITE_FILE;

  /* print banner followed by typecode */
  fprintf(f, "%s ", MatrixMarketBanner);
  tmp = mm_typecode_to_str(matcode);
  fprintf(f, "%s\n", tmp);
  if (tmp) {
    free(tmp);
    tmp = NULL;
  }

  /* print matrix sizes and nonzeros */
  fprintf(f, "%d %d %d\n", M, N, nz);

  /* print values */
  if (mm_is_pattern(matcode))
    for (i=0; i<nz; i++)
      fprintf(f, "%d %d\n", I[i], J[i]);
  else
    if (mm_is_real(matcode))
      for (i=0; i<nz; i++)
        fprintf(f, "%d %d %20.16g\n", I[i], J[i], val[i]);
    else
      if (mm_is_complex(matcode))
        for (i=0; i<nz; i++)
          fprintf(f, "%d %d %20.16g %20.16g\n", I[i], J[i], val[2*i],
              val[2*i+1]);
      else
      {
        if (f != stdout) fclose(f);
        return MM_UNSUPPORTED_TYPE;
      }

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

  return 0;
}
Ejemplo n.º 18
0
bool loadMmProperties(int *rowsCount,
	int *columnsCount,
	int *nonZerosCount,
	bool *isStoredSparse,
	int* matrixStorage,
	int* matrixType,
	FILE *file)
{
	MM_typecode matcode;

	// supports only valid matrices
	if ((mm_read_banner(file, &matcode) != 0) 
		|| (!mm_is_matrix(matcode))
		|| (!mm_is_valid(matcode))) 
		return false;

	if ( mm_read_mtx_crd_size(file, rowsCount, columnsCount, nonZerosCount) != 0 ) 
		return false;

	// is it stored sparse?
	if (mm_is_sparse(matcode))
		*isStoredSparse = true;
	else
		*isStoredSparse = false;

	if (mm_is_integer(matcode))
		*matrixStorage = MATRIX_STORAGE_INTEGER;
	else if (mm_is_real(matcode))
		*matrixStorage = MATRIX_STORAGE_REAL;
	else if (mm_is_complex(matcode))
		*matrixStorage = MATRIX_STORAGE_COMPLEX;
	else if (mm_is_pattern(matcode))
		*matrixStorage = MATRIX_STORAGE_PATTERN;
	
	if (mm_is_general(matcode))
		*matrixType = MATRIX_TYPE_GENERAL;
	else if (mm_is_symmetric(matcode))
		*matrixType = MATRIX_TYPE_SYMMETRIC;
	else if (mm_is_skew(matcode))
		*matrixType = MATRIX_TYPE_SKEW;
	else if (mm_is_hermitian(matcode))
		*matrixType = MATRIX_TYPE_HERMITIAN;

	return true;
}
Ejemplo n.º 19
0
int get_mm_info(const char *file, int *m, int *n, int *nz)
{
    FILE *fp;
    MM_typecode matcode;

    if ((fp = fopen(file, "r")) == NULL)
    {
        fprintf(stderr,"ERROR: Could not open file: %s\n",file);
        exit(1);
    }
    if (mm_read_banner(fp, &matcode) != 0) 
    {
        fprintf(stderr,"ERROR: Could not process Matrix Market banner.\n");
        exit(1);
    }
    if (!(mm_is_real(matcode) || mm_is_integer(matcode))) 
    {
        fprintf(stderr,"ERROR: Market Market type: [%s] not supported\n",
                mm_typecode_to_str(matcode));
        exit(1);
    }

    if (mm_is_sparse(matcode))
    {
        if (mm_read_mtx_crd_size(fp, m, n, nz) !=0) 
        {   /* find out size of sparse matrix */
            exit(1);
        }
    }
    else
    {
        if (mm_read_mtx_array_size(fp, m, n) !=0) 
        {   /* find out size of dense matrix */
            exit(1);
        }
        *nz = -1;
    }
    fclose(fp);

    return 0;
}
Ejemplo n.º 20
0
int mm_read_mtx_crd_entry(FILE *f, int *I, int *J,
        double *real, double *imag, MM_typecode matcode)

/******************************************************************************/
/*
  Purpose:

    MM_READ_MTX_CRD_ENTRY ???

  Modified:

    31 October 2008

  Parameters:

    Input, FILE *F, a pointer to the input file.
*/
{
    if (mm_is_complex(matcode))
    {
            if (fscanf(f, "%d %d %lg %lg", I, J, real, imag)
                != 4) return MM_PREMATURE_EOF;
    }
    else if (mm_is_real(matcode))
    {
            if (fscanf(f, "%d %d %lg\n", I, J, real)
                != 3) return MM_PREMATURE_EOF;

    }

    else if (mm_is_pattern(matcode))
    {
            if (fscanf(f, "%d %d", I, J) != 2) return MM_PREMATURE_EOF;
    }
    else
        return MM_UNSUPPORTED_TYPE;

    return 0;
        
}
Ejemplo n.º 21
0
/*---------------------------------------------*
 *             READ COO Matrix Market          *
 *---------------------------------------------*/
int read_coo_MM(coo_t *coo, options_t *opts) {
    char *matfile = opts->fmatname;
    MM_typecode matcode;
    FILE *p = fopen(matfile,"r");
    if (p == NULL) {
        printf("Unable to open file %s\n", matfile);
        exit(1);
    }
    /*----------- READ MM banner */
    if (mm_read_banner(p, &matcode) != 0) {
        printf("Could not process Matrix Market banner.\n");
        exit(1);
    }
    if (!mm_is_valid(matcode)) {
        printf("Invalid Matrix Market file.\n");
        exit(1);
    }
    if (!(mm_is_real(matcode) && mm_is_coordinate(matcode)
            && mm_is_sparse(matcode))) {
        printf("Only sparse real-valued coordinate \
    matrices are supported\n");
        exit(1);
    }
int MatrixMarketFileToRowMap(const char* filename,
                             const Epetra_Comm& comm,
                             Epetra_BlockMap*& rowmap)
{
  FILE* infile = fopen(filename, "r");
  MM_typecode matcode;

  int err = mm_read_banner(infile, &matcode);
  if (err != 0) return(err);

  if (!mm_is_matrix(matcode) || !mm_is_coordinate(matcode) ||
      !mm_is_real(matcode)   || !mm_is_general(matcode)) {
    return(-1);
  }

  int numrows, numcols;
  err = mm_read_mtx_array_size(infile, &numrows, &numcols);
  if (err != 0) return(err);

  fclose(infile);

  rowmap = new Epetra_BlockMap(numrows, 1, 0, comm);
  return(0);
}
Ejemplo n.º 23
0
char  *mm_typecode_to_str(MM_typecode matcode)
{
    char buffer[MM_MAX_LINE_LENGTH];
    const char *types[4];
	char *mm_strdup(const char *);
    int error =0;

    /* check for MTX type */
    if (mm_is_matrix(matcode)) 
        types[0] = MM_MTX_STR;
    else {
        types[0] = NULL;
        error=1;
    }

    /* check for CRD or ARR matrix */
    if (mm_is_sparse(matcode))
        types[1] = MM_SPARSE_STR;
    else
    if (mm_is_dense(matcode))
        types[1] = MM_DENSE_STR;
    else
        return NULL;

    /* check for element data type */
    if (mm_is_real(matcode))
        types[2] = MM_REAL_STR;
    else
    if (mm_is_complex(matcode))
        types[2] = MM_COMPLEX_STR;
    else
    if (mm_is_pattern(matcode))
        types[2] = MM_PATTERN_STR;
    else
    if (mm_is_integer(matcode))
        types[2] = MM_INT_STR;
    else
        return NULL;


    /* check for symmetry type */
    if (mm_is_general(matcode))
        types[3] = MM_GENERAL_STR;
    else
    if (mm_is_symmetric(matcode))
        types[3] = MM_SYMM_STR;
    else 
    if (mm_is_hermitian(matcode))
        types[3] = MM_HERM_STR;
    else 
    if (mm_is_skew(matcode))
        types[3] = MM_SKEW_STR;
    else
        return NULL;

    if( error == 1 )
        sprintf(buffer,"Object to write is not a matrix; this is unsupported by the current mmio code.");
    else
        sprintf(buffer,"%s %s %s %s", types[0], types[1], types[2], types[3]);
    return mm_strdup(buffer);

}
Ejemplo n.º 24
0
int mm_read_unsymmetric_sparse(const char *fname, int *M_, int *N_, int *nz_,
                double **val_, int **I_, int **J_)
{
    FILE *f;
    MM_typecode matcode;
    int M, N, nz;
    int i;
    double *val;
    int *I, *J;
 
    if ((f = fopen(fname, "r")) == NULL)
            return -1;
 
 
    if (mm_read_banner(f, &matcode) != 0)
    {
        printf("mm_read_unsymetric: Could not process Matrix Market banner ");
        printf(" in file [%s]\n", fname);
        return -1;
    }
 
 
 
    if ( !(mm_is_real(matcode) && mm_is_matrix(matcode) &&
            mm_is_sparse(matcode)))
    {
        fprintf(stderr, "Sorry, this application does not support ");
        fprintf(stderr, "Market Market type: [%s]\n",
                mm_typecode_to_str(matcode));
        return -1;
    }
 
    /* find out size of sparse matrix: M, N, nz .... */
 
    if (mm_read_mtx_crd_size(f, &M, &N, &nz) !=0)
    {
        fprintf(stderr, "read_unsymmetric_sparse(): could not parse matrix size.\n");
        return -1;
    }
 
    *M_ = M;
    *N_ = N;
    *nz_ = nz;
 
    /* reseve memory for matrices */
 
    I = (int *) malloc(nz * sizeof(int));
    J = (int *) malloc(nz * sizeof(int));
    val = (double *) malloc(nz * sizeof(double));
 
    *val_ = val;
    *I_ = I;
    *J_ = J;
 
    /* 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]--;
    }
    fclose(f);
 
    return 0;
}
Ejemplo n.º 25
0
int main(int argc, char *argv[]) {
    FILE *input_file, *output_file, *time_file;
    if (argc < 4) {
        fprintf(stderr, "Invalid input parameters\n");
        fprintf(stderr, "Usage: %s inputfile outputfile timefile \n", argv[0]);
        exit(1);
    }
    else {
        input_file = fopen(argv[1], "r");
        output_file = fopen(argv[2], "w");
        time_file = fopen(argv[3], "w");
        if (!input_file || !output_file || !time_file)
            exit(1);
    }

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

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

    mtxMatrix inputMatrix, fullMatrix, LMatrix, UMatrix, UMatrixTranspose, MMatrix;
    ReadMatrix(inputMatrix, input_file);

    Timer timer;

    getRowIndex(&inputMatrix, inputMatrix.RowIndex);
    inputMatrix.RowIndex[inputMatrix.N] = inputMatrix.NZ;

	int diagNum = 0;
	for (int i = 0; i < inputMatrix.N; i++) {
        for (int j = inputMatrix.RowIndex[i]; j < inputMatrix.RowIndex[i + 1]; j++) {
            if (i == inputMatrix.Col[j]) diagNum++;
        }
    }

    if (mm_is_symmetric(matcode)) {
        InitializeMatrix(inputMatrix.N, 2 * inputMatrix.NZ - diagNum, fullMatrix);
        TriangleToFull(&inputMatrix, &fullMatrix);
        FreeMatrix(inputMatrix);
    }
    else {
        fullMatrix = inputMatrix;
    }

    int *diag = new int[fullMatrix.N];

    for (int i = 0; i < fullMatrix.N; i++) {
        for (int j = fullMatrix.RowIndex[i]; j < fullMatrix.RowIndex[i + 1]; j++) {
            if (i == fullMatrix.Col[j]) diag[i] = j;
        }
    }

//    for (int i = 0; i < fullMatrix.N + 1; i++) {
//        printf("RowIndex[%i] = %i\n", i, fullMatrix.RowIndex[i]);
//    }
//
//
//    for (int i = 0; i < fullMatrix.N; i++) {
//        printf("input[%i]= %lf\n", i, fullMatrix.Value[inputMatrix.RowIndex[i]]);
//    }
//
//    for (int i = 0; i < fullMatrix.N; i++) {
//        printf("diag[%i]= %d\n", i, diag[i]);
//    }

    timer.start();
    ilu0(fullMatrix, fullMatrix.Value, diag);
	LUmatrixSeparation(fullMatrix, diag, LMatrix, UMatrix);
	Transpose(UMatrix, UMatrixTranspose);
	Multiplicate(LMatrix, UMatrixTranspose, MMatrix);
    timer.stop();

    std::ofstream timeLog;
    timeLog.open(argv[3]);
    timeLog << timer.getElapsed();

    WriteFullMatrix(MMatrix, output_file, matcode);

    FreeMatrix(fullMatrix);

    return 0;
}
Ejemplo n.º 26
0
int readSparseMatrix(char * filename, PetscInt & n, PetscInt & nz, PetscInt *& nnz, int *& i, int *& j, double *& v)
{
	int in, m, inz;

	// 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_coordinate(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_crd_size(fp, &in, &m, &inz)) {
		fprintf(stderr, "Could not read the size of the matrix.\n");
		return 4;
	}

	n = in;
	nz = inz;

	// check if it is a square matrix
	if (in != m) {
		fprintf(stderr, "Needs to be square.\n");
		return 5;
	}

	// allocate the memory
	printf("reading %s:\n\ta %d x %d sparse matrix with %d nonzeros...", filename, m, in, inz);
	i = new int[nz];
	j = new int[nz];
	v = new double[nz];
	nnz = new PetscInt[n]();
	for (int k = 0; k < inz; ++k) {
		fscanf(fp, "%u %u %lf\n", &j[k], &i[k], &v[k]);
		--i[k];
		--j[k];
		++nnz[i[k]];
		if (i[k] != j[k]) {
			++nnz[j[k]];
		}
	}
	printf("done\n");

	// close the file
	fclose(fp);

	return 0;
}
int MatrixMarketFileToBlockMaps(const char* filename,
                                const Epetra_Comm& comm,
                                Epetra_BlockMap*& rowmap,
                                Epetra_BlockMap*& colmap,
                                Epetra_BlockMap*& rangemap,
                                Epetra_BlockMap*& domainmap)
{
  FILE* infile = fopen(filename, "r");
  if (infile == NULL) {
    return(-1);
  }

  MM_typecode matcode;

  int err = mm_read_banner(infile, &matcode);
  if (err != 0) return(err);

  if (!mm_is_matrix(matcode) || !mm_is_coordinate(matcode) ||
      !mm_is_real(matcode)   || !mm_is_general(matcode)) {
    return(-1);
  }

  int numrows, numcols, nnz;
  err = mm_read_mtx_crd_size(infile, &numrows, &numcols, &nnz);
  if (err != 0) return(err);

  //for this case, we'll assume that the row-map is the same as
  //the range-map.
  //create row-map and range-map with linear distributions.

  rowmap = new Epetra_BlockMap(numrows, 1, 0, comm);
  rangemap = new Epetra_BlockMap(numrows, 1, 0, comm);

  int I, J;
  double val, imag;

  int num_map_cols = 0, insertPoint, foundOffset;
  int allocLen = numcols;
  int* map_cols = new int[allocLen];

  //read through all matrix data and construct a list of the column-
  //indices that occur in rows that are local to this processor.
 
  for(int i=0; i<nnz; ++i) {
    err = mm_read_mtx_crd_entry(infile, &I, &J, &val,
                                &imag, matcode);

    if (err == 0) {
      --I;
      --J;
      if (rowmap->MyGID(I)) {
        foundOffset = Epetra_Util_binary_search(J, map_cols, num_map_cols,
                                                insertPoint);
        if (foundOffset < 0) {
          Epetra_Util_insert(J, insertPoint, map_cols,
                             num_map_cols, allocLen);
        }
      }
    } 
  }

  //create colmap with the list of columns associated with rows that are
  //local to this processor.
  colmap = new Epetra_Map(-1, num_map_cols, map_cols, 0, comm);

  //create domainmap which has a linear distribution
  domainmap = new Epetra_BlockMap(numcols, 1, 0, comm);

  delete [] map_cols;

  return(0);
}
Ejemplo n.º 28
0
magma_int_t
magma_d_csr_mtx(
    magma_d_matrix *A,
    const char *filename,
    magma_queue_t queue )
{
    char buffer[ 1024 ];
    magma_int_t info = 0;

    int csr_compressor = 0;       // checks for zeros in original file
    
    magma_d_matrix B={Magma_CSR};

    magma_index_t *coo_col = NULL;
    magma_index_t *coo_row = NULL;
    double *coo_val = NULL;
    double *new_val = NULL;
    magma_index_t* new_row = NULL;
    magma_index_t* new_col = NULL;
    magma_int_t symmetric = 0;
    
    std::vector< std::pair< magma_index_t, double > > rowval;
    
    FILE *fid = NULL;
    MM_typecode matcode;
    fid = fopen(filename, "r");
    
    if (fid == NULL) {
        printf("%% Unable to open file %s\n", filename);
        info = MAGMA_ERR_NOT_FOUND;
        goto cleanup;
    }
    
    printf("%% Reading sparse matrix from file (%s):", filename);
    fflush(stdout);
    
    if (mm_read_banner(fid, &matcode) != 0) {
        printf("\n%% Could not process Matrix Market banner: %s.\n", matcode);
        info = MAGMA_ERR_NOT_SUPPORTED;
        goto cleanup;
    }
    
    if (!mm_is_valid(matcode)) {
        printf("\n%% Invalid Matrix Market file.\n");
        info = MAGMA_ERR_NOT_SUPPORTED;
        goto cleanup;
    }
    
    if ( ! ( ( mm_is_real(matcode)    ||
               mm_is_integer(matcode) ||
               mm_is_pattern(matcode) ||
               mm_is_real(matcode) ) &&
             mm_is_coordinate(matcode)  &&
             mm_is_sparse(matcode) ) )
    {
        mm_snprintf_typecode( buffer, sizeof(buffer), matcode );
        printf("\n%% Sorry, MAGMA-sparse does not support Market Market type: [%s]\n", buffer );
        printf("%% Only real-valued or pattern coordinate matrices are supported.\n");
        info = MAGMA_ERR_NOT_SUPPORTED;
        goto cleanup;
    }

    magma_index_t num_rows, num_cols, num_nonzeros;
    if (mm_read_mtx_crd_size(fid, &num_rows, &num_cols, &num_nonzeros) != 0) {
        info = MAGMA_ERR_UNKNOWN;
        goto cleanup;
    }
    
    A->storage_type    = Magma_CSR;
    A->memory_location = Magma_CPU;
    A->num_rows        = num_rows;
    A->num_cols        = num_cols;
    A->nnz             = num_nonzeros;
    A->fill_mode       = MagmaFull;
    
    CHECK( magma_index_malloc_cpu( &coo_col, A->nnz ) );
    CHECK( magma_index_malloc_cpu( &coo_row, A->nnz ) );
    CHECK( magma_dmalloc_cpu( &coo_val, A->nnz ) );

    if (mm_is_real(matcode) || mm_is_integer(matcode)) {
        for(magma_int_t i = 0; i < A->nnz; ++i) {
            magma_index_t ROW, COL;
            double VAL;  // always read in a double and convert later if necessary
            
            fscanf(fid, " %d %d %lf \n", &ROW, &COL, &VAL);
            if ( VAL == 0 )
                csr_compressor = 1;
            coo_row[i] = ROW - 1;
            coo_col[i] = COL - 1;
            coo_val[i] = MAGMA_D_MAKE( VAL, 0.);
        }
    } else if (mm_is_pattern(matcode) ) {
        for(magma_int_t i = 0; i < A->nnz; ++i) {
            magma_index_t ROW, COL;
            
            fscanf(fid, " %d %d \n", &ROW, &COL );
            
            coo_row[i] = ROW - 1;
            coo_col[i] = COL - 1;
            coo_val[i] = MAGMA_D_MAKE( 1.0, 0.);
        }
    } else if (mm_is_real(matcode) ){
       for(magma_int_t i = 0; i < A->nnz; ++i) {
            magma_index_t ROW, COL;
            double VAL, VALC;  // always read in a double and convert later if necessary
            
            fscanf(fid, " %d %d %lf %lf\n", &ROW, &COL, &VAL, &VALC);
            
            coo_row[i] = ROW - 1;
            coo_col[i] = COL - 1;
            coo_val[i] = MAGMA_D_MAKE( VAL, VALC);
        }
        // printf(" ...successfully read real matrix... ");
    } else {
        printf("\n%% Unrecognized data type\n");
        info = MAGMA_ERR_NOT_SUPPORTED;
        goto cleanup;
    }
    fclose(fid);
    fid = NULL;
    printf(" done. Converting to CSR:");
    fflush(stdout);
    
    A->sym = Magma_GENERAL;


    if( mm_is_symmetric(matcode) ) {
        symmetric = 1;
    }
    if ( mm_is_symmetric(matcode) || mm_is_symmetric(matcode) ) { 
                                        // duplicate off diagonal entries
        printf("\n%% Detected symmetric case.");
        A->sym = Magma_SYMMETRIC;
        magma_index_t off_diagonals = 0;
        for(magma_int_t i = 0; i < A->nnz; ++i) {
            if (coo_row[i] != coo_col[i])
                ++off_diagonals;
        }
        magma_index_t true_nonzeros = 2*off_diagonals + (A->nnz - off_diagonals);
        
        //printf("%% total number of nonzeros: %d\n%%", int(A->nnz));

        CHECK( magma_index_malloc_cpu( &new_row, true_nonzeros ));
        CHECK( magma_index_malloc_cpu( &new_col, true_nonzeros ));
        CHECK( magma_dmalloc_cpu( &new_val, true_nonzeros ));
        
        magma_index_t ptr = 0;
        for(magma_int_t i = 0; i < A->nnz; ++i) {
            if (coo_row[i] != coo_col[i]) {
                new_row[ptr] = coo_row[i];
                new_col[ptr] = coo_col[i];
                new_val[ptr] = coo_val[i];
                ptr++;
                new_col[ptr] = coo_row[i];
                new_row[ptr] = coo_col[i];
                new_val[ptr] = (symmetric == 0) ? coo_val[i] : conj(coo_val[i]);
                ptr++;
            } else {
                new_row[ptr] = coo_row[i];
                new_col[ptr] = coo_col[i];
                new_val[ptr] = coo_val[i];
                ptr++;
            }
        }
        
        magma_free_cpu(coo_row);
        magma_free_cpu(coo_col);
        magma_free_cpu(coo_val);

        coo_row = new_row;
        coo_col = new_col;
        coo_val = new_val;
        A->nnz = true_nonzeros;
        //printf("total number of nonzeros: %d\n", A->nnz);
    } // end symmetric case
    
    CHECK( magma_dmalloc_cpu( &A->val, A->nnz ));
    CHECK( magma_index_malloc_cpu( &A->col, A->nnz ));
    CHECK( magma_index_malloc_cpu( &A->row, A->num_rows+1 ));
    
    // original code from Nathan Bell and Michael Garland
    for (magma_index_t i = 0; i < num_rows; i++)
        (A->row)[i] = 0;
    
    for (magma_index_t i = 0; i < A->nnz; i++)
        (A->row)[coo_row[i]]++;
        
    // cumulative sum the nnz per row to get row[]
    magma_int_t cumsum;
    cumsum = 0;
    for(magma_int_t i = 0; i < num_rows; i++) {
        magma_index_t temp = (A->row)[i];
        (A->row)[i] = cumsum;
        cumsum += temp;
    }
    (A->row)[num_rows] = A->nnz;
    
    // write Aj,Ax into Bj,Bx
    for(magma_int_t i = 0; i < A->nnz; i++) {
        magma_index_t row_ = coo_row[i];
        magma_index_t dest = (A->row)[row_];
        (A->col)[dest] = coo_col[i];
        (A->val)[dest] = coo_val[i];
        (A->row)[row_]++;
    }    
    magma_free_cpu(coo_row);
    magma_free_cpu(coo_col);
    magma_free_cpu(coo_val);
    coo_row = NULL;
    coo_col = NULL;
    coo_val = NULL;

    int last;
    last = 0;
    for(int i = 0; i <= num_rows; i++) {
        int temp    = (A->row)[i];
        (A->row)[i] = last;
        last        = temp;
    }
    (A->row)[A->num_rows] = A->nnz;
    
    // sort column indices within each row
    // copy into vector of pairs (column index, value), sort by column index, then copy back
    for (magma_index_t k=0; k < A->num_rows; ++k) {
        int kk  = (A->row)[k];
        int len = (A->row)[k+1] - (A->row)[k];
        rowval.resize( len );
        for( int i=0; i < len; ++i ) {
            rowval[i] = std::make_pair( (A->col)[kk+i], (A->val)[kk+i] );
        }
        std::sort( rowval.begin(), rowval.end(), compare_first );
        for( int i=0; i < len; ++i ) {
            (A->col)[kk+i] = rowval[i].first;
            (A->val)[kk+i] = rowval[i].second;
        }
    }

    if ( csr_compressor > 0) { // run the CSR compressor to remove zeros
        //printf("removing zeros: ");
        CHECK( magma_dmtransfer( *A, &B, Magma_CPU, Magma_CPU, queue ));
        CHECK( magma_d_csr_compressor(
            &(A->val), &(A->row), &(A->col),
            &B.val, &B.row, &B.col, &B.num_rows, queue ));
        B.nnz = B.row[num_rows];
        //printf(" remaining nonzeros:%d ", B.nnz);
        magma_free_cpu( A->val );
        magma_free_cpu( A->row );
        magma_free_cpu( A->col );
        CHECK( magma_dmtransfer( B, A, Magma_CPU, Magma_CPU, queue ));
        //printf("done.\n");
    }
    A->true_nnz = A->nnz;
    printf(" done.\n");
cleanup:
    if ( fid != NULL ) {
        fclose( fid );
        fid = NULL;
    }
    magma_dmfree( &B, queue );
    magma_free_cpu(coo_row);
    magma_free_cpu(coo_col);
    magma_free_cpu(coo_val);
    return info;
}
Ejemplo n.º 29
0
Archivo: mmio.c Proyecto: blickly/ptii
csr_matrix_t *csr_mm_load(char *filename)
{
    int ret_code;
    MM_typecode matcode;
    FILE *f;

    int entry_count;
    int i, M, N, nz;
    matrix_entry_t *entries;
    matrix_entry_t *entry;

    int *row_start;
    int *col_index;
    double *val;

    csr_matrix_t *A;

    if ((f = fopen(filename, "r")) == NULL)
        exit(1);

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

    if (!mm_is_sparse(matcode) || !mm_is_symmetric(matcode) ||
        !mm_is_real(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);


    /* reserve memory for matrices */

    row_start = (int *) malloc((M + 1) * sizeof(int));
    col_index = (int *) malloc((2 * nz - M) * sizeof(int));
    val = (double *) malloc((2 * nz - M) * sizeof(double));

    entries =
        (matrix_entry_t *) malloc((2 * nz - M) * sizeof(matrix_entry_t));


    /* 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)            */

    entry = entries;
    entry_count = 0;

    for (i = 0; i < nz; i++) {

        int row, col;
        double val;

        fscanf(f, "%d %d %lg\n", &row, &col, &val);
        --row;                  /* adjust to 0-based */
        --col;

        assert(row >= 0 && col >= 0);
        assert(entry_count++ < 2 * nz - M);
        entry->i = row;
        entry->j = col;
        entry->val = val;
        ++entry;

        if (row != col) {       /* Fill out the other half... */
            assert(entry_count++ < 2 * nz - M);
            entry->i = col;
            entry->j = row;
            entry->val = val;
            ++entry;
        }
    }

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

    /**********************************/
    /* now make CSR version of matrix */
    /**********************************/

    nz = 2 * nz - M;
    qsort(entries, nz, sizeof(matrix_entry_t), entry_comparison);

    entry = entries;

    row_start[0] = 0;
    for (i = 0; i < nz; ++i) {
        row_start[entry->i + 1] = i + 1;
        col_index[i] = entry->j;
        val[i] = entry->val;
        ++entry;
    }

    free(entries);

    A = (csr_matrix_t *) malloc(sizeof(csr_matrix_t));
    A->m = M;
    A->n = N;
    A->nz = nz;
    A->row_start = row_start;
    A->col_idx = col_index;
    A->val = val;

    return A;
}
Ejemplo n.º 30
0
bool LoadMatrixMarketFile(const std::string& file_path, 
                          SparseMatrix<T>& A,
                          unsigned int& height,
                          unsigned int& width,
                          unsigned int& nnz)
{
    std::ifstream infile(file_path);
    if (!infile)
        return false;

    char mm_typecode[4];

    // read the matrix market banner (header)
    if (0 != mm_read_banner(infile, mm_typecode))
        return false;

    if (!mm_is_valid(mm_typecode))
        return false;

    // this reader supports these matrix types:
    //
    //  sparse, real/integer/pattern, general/symm/skew
    //

    if (!mm_is_sparse(mm_typecode))
    {
        std::cerr << "Only sparse MatrixMarket files are supported." << std::endl;
        return false;
    }

    if (!mm_is_real(mm_typecode) && !mm_is_integer(mm_typecode) && !mm_is_pattern(mm_typecode))
    {
        std::cerr << "Only real, integer, and pattern MatrixMarket formats are supported." << std::endl;
        return false;
    }

    if (!mm_is_general(mm_typecode) && !mm_is_symmetric(mm_typecode) && !mm_is_skew(mm_typecode))
    {
        std::cerr << "Only general, symmetric, and skew-symmetric MatrixMarket formats are supported." 
                  << std::endl;
        return false;
    }

    // read the number of rows, cols, nonzeros
    if (0 != mm_read_mtx_crd_size(infile, height, width, nnz))
    {
        std::cerr << "could not read matrix coordinate information" << std::endl;
        height = width = nnz = 0;
        return false;
    }

    // read the data according to the type 

    bool is_real      = mm_is_real(mm_typecode);
    bool is_int       = mm_is_integer(mm_typecode);
    bool is_symmetric = mm_is_symmetric(mm_typecode);
    bool is_skew      = mm_is_skew(mm_typecode);

    std::string line;
    unsigned int reserve_size = nnz;
    if (is_symmetric || is_skew)
        reserve_size *= 2;

    A.Clear();
    A.Reserve(height, width, reserve_size);

    // load num random entries of A
    A.BeginLoad();
    
    unsigned int row, col, count;

    if (is_real)
    {
        double val;
        for (count=0; count != nnz; ++count)
        {
            infile >> row; assert(row >= 1);
            infile >> col; assert(col >= 1);
            infile >> val;
            
            // convert to 0-based indexing
            row -= 1;
            col -= 1;
            A.Load(row, col, val);

            if (row != col)
            {
                if (is_symmetric)
                    A.Load(col, row, val);
                else if (is_skew)
                    A.Load(col, row, -val);
            }
        }
    }
    else if (is_int)