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