xref: /petsc/src/mat/tutorials/ex10.c (revision bef158480efac06de457f7a665168877ab3c2fd7)
1 
2 static char help[] = "Reads a PETSc matrix and computes the 2 norm of the columns\n\n";
3 
4 /*T
5    Concepts: Mat^loading a binary matrix;
6    Processors: n
7 T*/
8 
9 /*
10   Include "petscmat.h" so that we can use matrices.
11   automatically includes:
12      petscsys.h       - base PETSc routines   petscvec.h    - vectors
13      petscmat.h    - matrices
14      petscis.h     - index sets            petscviewer.h - viewers
15 */
16 #include <petscmat.h>
17 
18 
19 int main(int argc,char **args)
20 {
21   Mat            A;                       /* matrix */
22   PetscViewer    fd;                      /* viewer */
23   char           file[PETSC_MAX_PATH_LEN];            /* input file name */
24   PetscErrorCode ierr;
25   PetscReal      *norms;
26   PetscInt       n,cstart,cend;
27   PetscBool      flg;
28   PetscViewerFormat format;
29 
30   ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
31   /*
32      Determine files from which we read the matrix
33   */
34   ierr = PetscOptionsGetString(NULL,NULL,"-f",file,sizeof(file),&flg);CHKERRQ(ierr);
35   if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -f option");
36 
37   /*
38      Open binary file.  Note that we use FILE_MODE_READ to indicate
39      reading from this file.
40   */
41   ierr = PetscViewerCreate(PETSC_COMM_WORLD,&fd);CHKERRQ(ierr);
42   ierr = PetscViewerSetType(fd,PETSCVIEWERBINARY);CHKERRQ(ierr);
43   ierr = PetscViewerSetFromOptions(fd);CHKERRQ(ierr);
44   ierr = PetscOptionsGetEnum(NULL,NULL,"-viewer_format",PetscViewerFormats,(PetscEnum*)&format,&flg);CHKERRQ(ierr);
45   if (flg) {ierr = PetscViewerPushFormat(fd,format);CHKERRQ(ierr);}
46   ierr = PetscViewerFileSetMode(fd,FILE_MODE_READ);CHKERRQ(ierr);
47   ierr = PetscViewerFileSetName(fd,file);CHKERRQ(ierr);
48 
49   /*
50     Load the matrix; then destroy the viewer.
51     Matrix type is set automatically but you can override it by MatSetType() prior to MatLoad().
52     Do that only if you really insist on the given type.
53   */
54   ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
55   ierr = MatSetOptionsPrefix(A,"a_");CHKERRQ(ierr);
56   ierr = PetscObjectSetName((PetscObject) A,"A");CHKERRQ(ierr);
57   ierr = MatSetFromOptions(A);CHKERRQ(ierr);
58   ierr = MatLoad(A,fd);CHKERRQ(ierr);
59   ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr);
60 
61   ierr = MatGetSize(A,NULL,&n);CHKERRQ(ierr);
62   ierr = MatGetOwnershipRangeColumn(A,&cstart,&cend);CHKERRQ(ierr);
63   ierr = PetscMalloc1(n,&norms);CHKERRQ(ierr);
64   ierr = MatGetColumnNorms(A,NORM_2,norms);CHKERRQ(ierr);
65   ierr = PetscRealView(cend-cstart,norms+cstart,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
66   ierr = PetscFree(norms);CHKERRQ(ierr);
67 
68   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
69   ierr = MatGetOption(A,MAT_SYMMETRIC,&flg);CHKERRQ(ierr);
70   ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"MAT_SYMMETRIC: %D\n",flg);CHKERRQ(ierr);
71   ierr = MatViewFromOptions(A,NULL,"-mat_view");CHKERRQ(ierr);
72 
73   ierr = MatDestroy(&A);CHKERRQ(ierr);
74   ierr = PetscFinalize();
75   return ierr;
76 }
77 
78 
79 
80 /*TEST
81 
82    test:
83       suffix: mpiaij
84       nsize: 2
85       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
86       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type mpiaij
87       args: -a_matload_symmetric
88 
89    test:
90       suffix: mpiaij_hdf5
91       nsize: 2
92       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
93       args: -f ${DATAFILESPATH}/matrices/matlab/small.mat -a_mat_type mpiaij -viewer_type hdf5 -viewer_format hdf5_mat
94       args: -a_matload_symmetric
95 
96    test:
97       suffix: mpiaij_rect_hdf5
98       nsize: 2
99       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
100       args: -f ${DATAFILESPATH}/matrices/matlab/small_rect.mat -a_mat_type mpiaij -viewer_type hdf5 -viewer_format hdf5_mat
101 
102    test:
103       # test for more processes than rows
104       suffix: mpiaij_hdf5_tiny
105       nsize: 8
106       requires: double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
107       args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/tiny_system_with_x0.mat -a_mat_type mpiaij -viewer_type hdf5 -viewer_format hdf5_mat
108       args: -a_matload_symmetric
109 
110    test:
111       # test for more processes than rows, complex
112       TODO: not yet implemented for MATLAB complex format
113       suffix: mpiaij_hdf5_tiny_complex
114       nsize: 8
115       requires: double complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
116       args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/tiny_system_with_x0_complex.mat -a_mat_type mpiaij -viewer_type hdf5 -viewer_format hdf5_mat
117       args: -a_matload_symmetric
118 
119    test:
120       TODO: mpibaij not supported yet
121       suffix: mpibaij_hdf5
122       nsize: 2
123       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
124       args: -f ${DATAFILESPATH}/matrices/matlab/small.mat -a_mat_type mpibaij -a_mat_block_size 2 -viewer_type hdf5 -viewer_format hdf5_mat
125       args: -a_matload_symmetric
126 
127    test:
128       suffix: mpidense
129       nsize: 2
130       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
131       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type mpidense
132       args: -a_matload_symmetric
133 
134    test:
135       suffix: seqaij
136       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
137       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type seqaij
138       args: -a_matload_symmetric
139 
140    test:
141       suffix: seqaij_hdf5
142       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
143       args: -f ${DATAFILESPATH}/matrices/matlab/small.mat -a_mat_type seqaij -viewer_type hdf5 -viewer_format hdf5_mat
144       args: -a_matload_symmetric
145 
146    test:
147       suffix: seqaij_rect_hdf5
148       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
149       args: -f ${DATAFILESPATH}/matrices/matlab/small_rect.mat -a_mat_type seqaij -viewer_type hdf5 -viewer_format hdf5_mat
150 
151    test:
152       suffix: seqdense
153       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
154       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type seqdense
155       args: -a_matload_symmetric
156 
157    test:
158       suffix: seqdense_hdf5
159       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
160       args: -f ${DATAFILESPATH}/matrices/matlab/small_dense.mat -a_mat_type seqdense -viewer_type hdf5 -viewer_format hdf5_mat
161       args: -a_matload_symmetric
162 
163    test:
164       suffix: seqdense_rect_hdf5
165       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
166       args: -f ${DATAFILESPATH}/matrices/matlab/small_rect_dense.mat -a_mat_type seqdense -viewer_type hdf5 -viewer_format hdf5_mat
167 
168    test:
169       suffix: mpidense_hdf5
170       nsize: 2
171       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
172       args: -f ${DATAFILESPATH}/matrices/matlab/small_dense.mat -a_mat_type mpidense -viewer_type hdf5 -viewer_format hdf5_mat
173       args: -a_matload_symmetric
174 
175    test:
176       suffix: mpidense_rect_hdf5
177       nsize: 2
178       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 define(PETSC_HDF5_HAVE_ZLIB)
179       args: -f ${DATAFILESPATH}/matrices/matlab/small_rect_dense.mat -a_mat_type mpidense -viewer_type hdf5 -viewer_format hdf5_mat
180 TEST*/
181