xref: /petsc/src/mat/tutorials/ex1.c (revision 6dd63270497ad23dcf16ae500a87ff2b2a0b7474)
1 static char help[] = "Reads a PETSc matrix and vector from a file and reorders it.\n\
2   -f0 <input_file> : first file to load (small system)\n\
3   -f1 <input_file> : second file to load (larger system)\n\n";
4 
5 /*
6   Include "petscmat.h" so that we can use matrices.
7   automatically includes:
8      petscsys.h       - base PETSc routines   petscvec.h    - vectors
9      petscmat.h    - matrices
10      petscis.h     - index sets            petscviewer.h - viewers
11 */
12 #include <petscmat.h>
13 
14 int main(int argc, char **args)
15 {
16   Mat             A;                        /* matrix */
17   PetscViewer     fd;                       /* viewer */
18   char            file[PETSC_MAX_PATH_LEN]; /* input file name */
19   IS              isrow, iscol;             /* row and column permutations */
20   MatOrderingType rtype = MATORDERINGRCM;
21   PetscBool       flg;
22 
23   PetscFunctionBeginUser;
24   PetscCall(PetscInitialize(&argc, &args, NULL, help));
25   /*
26      Determine files from which we read the two linear systems
27      (matrix and right-hand-side vector).
28   */
29   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
30   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f option");
31 
32   /* -----------------------------------------------------------
33                   Beginning of loop
34      ----------------------------------------------------------- */
35   /*
36      Loop through the reordering 2 times.
37       - The intention here is to preload and solve a small system;
38         then load another (larger) system and solve it as well.
39         This process preloads the instructions with the smaller
40         system so that more accurate performance monitoring (via
41         -log_view) can be done with the larger one (that actually
42         is the system of interest).
43   */
44 
45   /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
46                          Load system i
47    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
48 
49   /*
50      Open binary file.  Note that we use FILE_MODE_READ to indicate
51      reading from this file.
52   */
53   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
54 
55   /*
56      Load the matrix; then destroy the viewer.
57   */
58   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
59   PetscCall(MatSetType(A, MATSEQAIJ));
60   PetscCall(MatLoad(A, fd));
61   PetscCall(PetscViewerDestroy(&fd));
62 
63   PetscCall(MatGetOrdering(A, rtype, &isrow, &iscol));
64   PetscCall(ISView(isrow, PETSC_VIEWER_STDOUT_WORLD));
65 
66   /*
67      Free work space.  All PETSc objects should be destroyed when they
68      are no longer needed.
69   */
70   PetscCall(MatDestroy(&A));
71   PetscCall(ISDestroy(&isrow));
72   PetscCall(ISDestroy(&iscol));
73 
74   PetscCall(PetscFinalize());
75   return 0;
76 }
77 
78 /*TEST
79 
80    test:
81       requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES)
82       args: -f ${DATAFILESPATH}/matrices/medium -viewer_binary_skip_info
83 
84 TEST*/
85