xref: /petsc/src/mat/tests/ex72.c (revision 0fdf79fb08699bf9be0aa4d8ba0185e387a216c8)
1 static char help[] = "Read a non-complex sparse matrix from a Matrix Market (v. 2.0) file\n\
2 and write it to a file in petsc sparse binary format. If the matrix is symmetric, the binary file is in \n\
3 PETSc MATSBAIJ format, otherwise it is in MATAIJ format \n\
4 Usage:  ./ex72 -fin <infile> -fout <outfile> \n\
5 (See https://math.nist.gov/MatrixMarket/ for details.)\n\
6 The option -permute <natural,rcm,nd,...> permutes the matrix using the ordering type.\n\
7 The option -aij_only allows to use MATAIJ for all cases.\n\\n";
8 
9 /*
10    NOTES:
11 
12    1) Matrix Market files are always 1-based, i.e. the index of the first
13       element of a matrix is (1,1), not (0,0) as in C.  ADJUST THESE
14       OFFSETS ACCORDINGLY offsets accordingly when reading and writing
15       to files.
16 
17    2) ANSI C requires one to use the "l" format modifier when reading
18       double precision floating point numbers in scanf() and
19       its variants.  For example, use "%lf", "%lg", or "%le"
20       when reading doubles, otherwise errors will occur.
21 */
22 #include <petscmat.h>
23 #include "mmloader.h"
24 
25 int main(int argc, char **argv)
26 {
27   MM_typecode matcode;
28   FILE       *file;
29   PetscInt    M, N, nz;
30   Mat         A;
31   char        filein[PETSC_MAX_PATH_LEN], fileout[PETSC_MAX_PATH_LEN];
32   char        ordering[256] = MATORDERINGRCM;
33   PetscViewer view;
34   PetscBool   flag, symmetric = PETSC_FALSE, aijonly = PETSC_FALSE, permute = PETSC_FALSE;
35   IS          rowperm = NULL, colperm = NULL;
36   PetscMPIInt size;
37 
38   PetscInitialize(&argc, &argv, (char *)0, help);
39   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
40   PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");
41 
42   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Matrix Market example options", "");
43   {
44     PetscCall(PetscOptionsString("-fin", "Input Matrix Market file", "", filein, filein, sizeof(filein), &flag));
45     PetscCheck(flag, PETSC_COMM_SELF, PETSC_ERR_USER_INPUT, "Please use -fin <filename> to specify the input file name!");
46     PetscCall(PetscOptionsString("-fout", "Output file in petsc sparse binary format", "", fileout, fileout, sizeof(fileout), &flag));
47     PetscCheck(flag, PETSC_COMM_SELF, PETSC_ERR_USER_INPUT, "Please use -fout <filename> to specify the output file name!");
48     PetscCall(PetscOptionsBool("-aij_only", "Use MATAIJ for all cases", "", aijonly, &aijonly, NULL));
49     PetscCall(PetscOptionsFList("-permute", "Permute matrix and vector to solving in new ordering", "", MatOrderingList, ordering, ordering, sizeof(ordering), &permute));
50   }
51   PetscOptionsEnd();
52 
53   PetscCall(MatCreateFromMTX(&A, filein, aijonly));
54   PetscCall(PetscFOpen(PETSC_COMM_SELF, filein, "r", &file));
55   PetscCall(mm_read_banner(file, &matcode));
56   if (mm_is_symmetric(matcode)) symmetric = PETSC_TRUE;
57   PetscCall(mm_write_banner(stdout, matcode));
58   PetscCall(mm_read_mtx_crd_size(file, &M, &N, &nz));
59   PetscCall(PetscFClose(PETSC_COMM_SELF, file));
60   PetscCall(PetscPrintf(PETSC_COMM_SELF, "M: %d, N: %d, nnz: %d\n", M, N, nz));
61   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Reading matrix completes.\n"));
62   if (permute) {
63     Mat Aperm;
64     PetscCall(MatGetOrdering(A, ordering, &rowperm, &colperm));
65     PetscCall(MatPermute(A, rowperm, colperm, &Aperm));
66     PetscCall(MatDestroy(&A));
67     A = Aperm; /* Replace original operator with permuted version */
68   }
69 
70   /* Write out matrix */
71   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Writing matrix to binary file %s using PETSc %s format ...\n", fileout, (symmetric && !aijonly) ? "SBAIJ" : "AIJ"));
72   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_SELF, fileout, FILE_MODE_WRITE, &view));
73   PetscCall(MatView(A, view));
74   PetscCall(PetscViewerDestroy(&view));
75   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Writing matrix completes.\n"));
76 
77   PetscCall(MatDestroy(&A));
78   PetscCall(ISDestroy(&rowperm));
79   PetscCall(ISDestroy(&colperm));
80   PetscCall(PetscFinalize());
81   return 0;
82 }
83 
84 /*TEST
85 
86    build:
87       requires:  !complex double !defined(PETSC_USE_64BIT_INDICES)
88       depends: mmloader.c mmio.c
89 
90    test:
91       suffix: 1
92       args: -fin ${wPETSC_DIR}/share/petsc/datafiles/matrices/amesos2_test_mat0.mtx -fout petscmat.aij
93       output_file: output/ex72_1.out
94 
95    test:
96       suffix: 2
97       args: -fin ${wPETSC_DIR}/share/petsc/datafiles/matrices/LFAT5.mtx -fout petscmat.sbaij
98       output_file: output/ex72_2.out
99 
100    test:
101       suffix: 3
102       args: -fin ${wPETSC_DIR}/share/petsc/datafiles/matrices/m_05_05_crk.mtx -fout petscmat2.aij
103       output_file: output/ex72_3.out
104 
105    test:
106       suffix: 4
107       args: -fin ${wPETSC_DIR}/share/petsc/datafiles/matrices/amesos2_test_mat0.mtx -fout petscmat.aij -permute rcm
108       output_file: output/ex72_4.out
109 TEST*/
110