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