Actual source code: mmloader.c
1: #include "mmloader.h"
3: PetscErrorCode MatCreateFromMTX(Mat *A, const char *filein, PetscBool aijonly)
4: {
5: MM_typecode matcode;
6: FILE *file;
7: PetscInt M, N, ninput;
8: PetscInt *ia, *ja;
9: PetscInt i, j, nz, *rownz;
10: PetscScalar *val;
11: PetscBool sametype, symmetric = PETSC_FALSE, skew = PETSC_FALSE;
13: /* Read in matrix */
14: PetscFunctionBeginUser;
15: PetscCall(PetscFOpen(PETSC_COMM_SELF, filein, "r", &file));
16: PetscCheck(mm_read_banner(file, &matcode) == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not process Matrix Market banner.");
17: /* This is how one can screen matrix types if their application */
18: /* only supports a subset of the Matrix Market data types. */
19: PetscCheck(mm_is_matrix(matcode) && mm_is_sparse(matcode) && (mm_is_real(matcode) || mm_is_integer(matcode)), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input must be a sparse real or integer matrix. Market Market type: [%s]", mm_typecode_to_str(matcode));
21: if (mm_is_symmetric(matcode)) symmetric = PETSC_TRUE;
22: if (mm_is_skew(matcode)) skew = PETSC_TRUE;
24: /* Find out size of sparse matrix .... */
25: PetscCheck(mm_read_mtx_crd_size(file, &M, &N, &nz) == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Size of sparse matrix is wrong.");
27: /* Reserve memory for matrices */
28: PetscCall(PetscMalloc4(nz, &ia, nz, &ja, nz, &val, M, &rownz));
29: for (i = 0; i < M; i++) rownz[i] = 0;
31: /* NOTE: when reading in doubles, ANSI C requires the use of the "l" */
32: /* specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
33: /* (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15) */
34: for (i = 0; i < nz; i++) {
35: ninput = fscanf(file, "%d %d %lg\n", &ia[i], &ja[i], &val[i]);
36: PetscCheck(ninput >= 3, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Badly formatted input file");
37: ia[i]--;
38: ja[i]--; /* adjust from 1-based to 0-based */
39: if ((symmetric && aijonly) || skew) { /* transpose */
40: rownz[ia[i]]++;
41: rownz[ja[i]]++;
42: } else rownz[ia[i]]++;
43: }
44: PetscCall(PetscFClose(PETSC_COMM_SELF, file));
46: /* Create, preallocate, and then assemble the matrix */
47: PetscCall(MatCreate(PETSC_COMM_SELF, A));
48: PetscCall(MatSetSizes(*A, PETSC_DECIDE, PETSC_DECIDE, M, N));
50: if (symmetric && !aijonly) {
51: PetscCall(MatSetType(*A, MATSEQSBAIJ));
52: PetscCall(MatSetFromOptions(*A));
53: PetscCall(MatSeqSBAIJSetPreallocation(*A, 1, 0, rownz));
54: PetscCall(PetscObjectTypeCompare((PetscObject)(*A), MATSEQSBAIJ, &sametype));
55: PetscCheck(sametype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Only AIJ and SBAIJ are supported. Your mattype is not supported");
56: } else {
57: PetscCall(MatSetType(*A, MATSEQAIJ));
58: PetscCall(MatSetFromOptions(*A));
59: PetscCall(MatSeqAIJSetPreallocation(*A, 0, rownz));
60: PetscCall(PetscObjectTypeCompare((PetscObject)(*A), MATSEQAIJ, &sametype));
61: PetscCheck(sametype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Only AIJ and SBAIJ are supported. Your mattype is not supported");
62: }
63: /* Add values to the matrix, these correspond to lower triangular part for symmetric or skew matrices */
64: for (j = 0; j < nz; j++) PetscCall(MatSetValues(*A, 1, &ia[j], 1, &ja[j], &val[j], INSERT_VALUES));
66: /* Add values to upper triangular part for some cases */
67: if (symmetric && aijonly) {
68: /* MatrixMarket matrix stores symm matrix in lower triangular part. Take its transpose */
69: for (j = 0; j < nz; j++) PetscCall(MatSetValues(*A, 1, &ja[j], 1, &ia[j], &val[j], INSERT_VALUES));
70: }
71: if (skew) {
72: for (j = 0; j < nz; j++) {
73: val[j] = -val[j];
74: PetscCall(MatSetValues(*A, 1, &ja[j], 1, &ia[j], &val[j], INSERT_VALUES));
75: }
76: }
77: PetscCall(MatAssemblyBegin(*A, MAT_FINAL_ASSEMBLY));
78: PetscCall(MatAssemblyEnd(*A, MAT_FINAL_ASSEMBLY));
79: PetscCall(PetscFree4(ia, ja, val, rownz));
80: PetscFunctionReturn(PETSC_SUCCESS);
81: }