xref: /petsc/src/mat/tests/ex31.c (revision 6ffe77eaecce1557e50d00ca5347a7f48e598865)
1 
2 static char help[] = "Tests binary I/O of matrices and illustrates user-defined event logging.\n\n";
3 
4 #include <petscmat.h>
5 
6 /* Note:  Most applications would not read and write the same matrix within
7   the same program.  This example is intended only to demonstrate
8   both input and output. */
9 
10 int main(int argc,char **args)
11 {
12   Mat            C;
13   PetscScalar    v;
14   PetscInt       i,j,Ii,J,Istart,Iend,N,m = 4,n = 4;
15   PetscMPIInt    rank,size;
16   PetscViewer    viewer;
17 #if defined(PETSC_USE_LOG)
18   PetscLogEvent MATRIX_GENERATE,MATRIX_READ;
19 #endif
20 
21   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
22   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
23   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
24   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
25   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
26   N    = m*n;
27 
28   /* PART 1:  Generate matrix, then write it in binary format */
29 
30   PetscCall(PetscLogEventRegister("Generate Matrix",0,&MATRIX_GENERATE));
31   PetscCall(PetscLogEventBegin(MATRIX_GENERATE,0,0,0,0));
32 
33   /* Generate matrix */
34   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
35   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N));
36   PetscCall(MatSetFromOptions(C));
37   PetscCall(MatSetUp(C));
38   PetscCall(MatGetOwnershipRange(C,&Istart,&Iend));
39   for (Ii=Istart; Ii<Iend; Ii++) {
40     v = -1.0; i = Ii/n; j = Ii - i*n;
41     if (i>0)   {J = Ii - n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));}
42     if (i<m-1) {J = Ii + n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));}
43     if (j>0)   {J = Ii - 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));}
44     if (j<n-1) {J = Ii + 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));}
45     v = 4.0; PetscCall(MatSetValues(C,1,&Ii,1,&Ii,&v,ADD_VALUES));
46   }
47   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
48   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
49   PetscCall(MatView(C,PETSC_VIEWER_STDOUT_WORLD));
50 
51   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"writing matrix in binary to matrix.dat ...\n"));
52   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_WRITE,&viewer));
53   PetscCall(MatView(C,viewer));
54   PetscCall(PetscViewerDestroy(&viewer));
55   PetscCall(MatDestroy(&C));
56   PetscCall(PetscLogEventEnd(MATRIX_GENERATE,0,0,0,0));
57 
58   /* PART 2:  Read in matrix in binary format */
59 
60   /* All processors wait until test matrix has been dumped */
61   PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
62 
63   PetscCall(PetscLogEventRegister("Read Matrix",0,&MATRIX_READ));
64   PetscCall(PetscLogEventBegin(MATRIX_READ,0,0,0,0));
65   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"reading matrix in binary from matrix.dat ...\n"));
66   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_READ,&viewer));
67   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
68   PetscCall(MatLoad(C,viewer));
69   PetscCall(PetscViewerDestroy(&viewer));
70   PetscCall(PetscLogEventEnd(MATRIX_READ,0,0,0,0));
71   PetscCall(MatView(C,PETSC_VIEWER_STDOUT_WORLD));
72 
73   /* Free data structures */
74   PetscCall(MatDestroy(&C));
75 
76   PetscCall(PetscFinalize());
77   return 0;
78 }
79 
80 /*TEST
81 
82    test:
83       filter: grep -v " MPI process"
84 
85 TEST*/
86