xref: /petsc/src/mat/tutorials/ex1.c (revision 8fb5bd83c3955fefcf33a54e3bb66920a9fa884b)
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   PetscCall(PetscInitialize(&argc,&args,(char*)0,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