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 PetscFunctionBeginUser; 39 PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 40 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 41 PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!"); 42 43 PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Matrix Market example options", ""); 44 { 45 PetscCall(PetscOptionsString("-fin", "Input Matrix Market file", "", filein, filein, sizeof(filein), &flag)); 46 PetscCheck(flag, PETSC_COMM_SELF, PETSC_ERR_USER_INPUT, "Please use -fin <filename> to specify the input file name!"); 47 PetscCall(PetscOptionsString("-fout", "Output file in petsc sparse binary format", "", fileout, fileout, sizeof(fileout), &flag)); 48 PetscCheck(flag, PETSC_COMM_SELF, PETSC_ERR_USER_INPUT, "Please use -fout <filename> to specify the output file name!"); 49 PetscCall(PetscOptionsBool("-aij_only", "Use MATAIJ for all cases", "", aijonly, &aijonly, NULL)); 50 PetscCall(PetscOptionsFList("-permute", "Permute matrix and vector to solving in new ordering", "", MatOrderingList, ordering, ordering, sizeof(ordering), &permute)); 51 } 52 PetscOptionsEnd(); 53 54 PetscCall(MatCreateFromMTX(&A, filein, aijonly)); 55 PetscCall(PetscFOpen(PETSC_COMM_SELF, filein, "r", &file)); 56 PetscCallExternal(mm_read_banner, file, &matcode); 57 if (mm_is_symmetric(matcode)) symmetric = PETSC_TRUE; 58 PetscCallExternal(mm_write_banner, stdout, matcode); 59 PetscCallExternal(mm_read_mtx_crd_size, file, &M, &N, &nz); 60 PetscCall(PetscFClose(PETSC_COMM_SELF, file)); 61 PetscCall(PetscPrintf(PETSC_COMM_SELF, "M: %d, N: %d, nnz: %d\n", M, N, nz)); 62 PetscCall(PetscPrintf(PETSC_COMM_SELF, "Reading matrix completes.\n")); 63 if (permute) { 64 Mat Aperm; 65 PetscCall(MatGetOrdering(A, ordering, &rowperm, &colperm)); 66 PetscCall(MatPermute(A, rowperm, colperm, &Aperm)); 67 PetscCall(MatDestroy(&A)); 68 A = Aperm; /* Replace original operator with permuted version */ 69 } 70 71 /* Write out matrix */ 72 PetscCall(PetscPrintf(PETSC_COMM_SELF, "Writing matrix to binary file %s using PETSc %s format ...\n", fileout, (symmetric && !aijonly) ? "SBAIJ" : "AIJ")); 73 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_SELF, fileout, FILE_MODE_WRITE, &view)); 74 PetscCall(MatView(A, view)); 75 PetscCall(PetscViewerDestroy(&view)); 76 PetscCall(PetscPrintf(PETSC_COMM_SELF, "Writing matrix completes.\n")); 77 78 PetscCall(MatDestroy(&A)); 79 PetscCall(ISDestroy(&rowperm)); 80 PetscCall(ISDestroy(&colperm)); 81 PetscCall(PetscFinalize()); 82 return 0; 83 } 84 85 /*TEST 86 87 build: 88 requires: !complex double !defined(PETSC_USE_64BIT_INDICES) 89 depends: mmloader.c mmio.c 90 91 test: 92 suffix: 1 93 args: -fin ${wPETSC_DIR}/share/petsc/datafiles/matrices/amesos2_test_mat0.mtx -fout petscmat.aij 94 output_file: output/ex72_1.out 95 96 test: 97 suffix: 2 98 args: -fin ${wPETSC_DIR}/share/petsc/datafiles/matrices/LFAT5.mtx -fout petscmat.sbaij 99 output_file: output/ex72_2.out 100 101 test: 102 suffix: 3 103 args: -fin ${wPETSC_DIR}/share/petsc/datafiles/matrices/m_05_05_crk.mtx -fout petscmat2.aij 104 output_file: output/ex72_3.out 105 106 test: 107 suffix: 4 108 args: -fin ${wPETSC_DIR}/share/petsc/datafiles/matrices/amesos2_test_mat0.mtx -fout petscmat.aij -permute rcm 109 output_file: output/ex72_4.out 110 TEST*/ 111