xref: /petsc/src/mat/tutorials/ex1.c (revision c4762a1b19cd2af06abeed90e8f9d34fb975dd94)
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   PetscErrorCode  ierr;
30   MatOrderingType rtype = MATORDERINGRCM;
31   PetscBool       flg;
32 
33   ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
34   /*
35      Determine files from which we read the two linear systems
36      (matrix and right-hand-side vector).
37   */
38   ierr = PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
39   if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -f option");
40 
41   /* -----------------------------------------------------------
42                   Beginning of loop
43      ----------------------------------------------------------- */
44   /*
45      Loop through the reordering 2 times.
46       - The intention here is to preload and solve a small system;
47         then load another (larger) system and solve it as well.
48         This process preloads the instructions with the smaller
49         system so that more accurate performance monitoring (via
50         -log_view) can be done with the larger one (that actually
51         is the system of interest).
52   */
53 
54   /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
55                          Load system i
56    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
57 
58   /*
59      Open binary file.  Note that we use FILE_MODE_READ to indicate
60      reading from this file.
61   */
62   ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr);
63 
64   /*
65      Load the matrix; then destroy the viewer.
66   */
67   ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
68   ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr);
69   ierr = MatLoad(A,fd);CHKERRQ(ierr);
70   ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr);
71 
72   ierr = MatGetOrdering(A,rtype,&isrow,&iscol);CHKERRQ(ierr);
73   ierr = ISView(isrow,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
74 
75   /*
76      Free work space.  All PETSc objects should be destroyed when they
77      are no longer needed.
78   */
79   ierr = MatDestroy(&A);CHKERRQ(ierr);
80   ierr = ISDestroy(&isrow);CHKERRQ(ierr);
81   ierr = ISDestroy(&iscol);CHKERRQ(ierr);
82 
83   ierr = PetscFinalize();
84   return ierr;
85 }
86 
87 
88 
89 /*TEST
90 
91    test:
92       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
93       args: -f ${DATAFILESPATH}/matrices/medium -viewer_binary_skip_info
94 
95 TEST*/
96