xref: /petsc/src/mat/tests/ex31.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
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   PetscFunctionBeginUser;
22   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
23   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
24   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
25   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
26   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
27   N    = m*n;
28 
29   /* PART 1:  Generate matrix, then write it in binary format */
30 
31   PetscCall(PetscLogEventRegister("Generate Matrix",0,&MATRIX_GENERATE));
32   PetscCall(PetscLogEventBegin(MATRIX_GENERATE,0,0,0,0));
33 
34   /* Generate matrix */
35   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
36   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N));
37   PetscCall(MatSetFromOptions(C));
38   PetscCall(MatSetUp(C));
39   PetscCall(MatGetOwnershipRange(C,&Istart,&Iend));
40   for (Ii=Istart; Ii<Iend; Ii++) {
41     v = -1.0; i = Ii/n; j = Ii - i*n;
42     if (i>0)   {J = Ii - n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));}
43     if (i<m-1) {J = Ii + n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));}
44     if (j>0)   {J = Ii - 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));}
45     if (j<n-1) {J = Ii + 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES));}
46     v = 4.0; PetscCall(MatSetValues(C,1,&Ii,1,&Ii,&v,ADD_VALUES));
47   }
48   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
49   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
50   PetscCall(MatView(C,PETSC_VIEWER_STDOUT_WORLD));
51 
52   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"writing matrix in binary to matrix.dat ...\n"));
53   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_WRITE,&viewer));
54   PetscCall(MatView(C,viewer));
55   PetscCall(PetscViewerDestroy(&viewer));
56   PetscCall(MatDestroy(&C));
57   PetscCall(PetscLogEventEnd(MATRIX_GENERATE,0,0,0,0));
58 
59   /* PART 2:  Read in matrix in binary format */
60 
61   /* All processors wait until test matrix has been dumped */
62   PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
63 
64   PetscCall(PetscLogEventRegister("Read Matrix",0,&MATRIX_READ));
65   PetscCall(PetscLogEventBegin(MATRIX_READ,0,0,0,0));
66   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"reading matrix in binary from matrix.dat ...\n"));
67   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_READ,&viewer));
68   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
69   PetscCall(MatLoad(C,viewer));
70   PetscCall(PetscViewerDestroy(&viewer));
71   PetscCall(PetscLogEventEnd(MATRIX_READ,0,0,0,0));
72   PetscCall(MatView(C,PETSC_VIEWER_STDOUT_WORLD));
73 
74   /* Free data structures */
75   PetscCall(MatDestroy(&C));
76 
77   PetscCall(PetscFinalize());
78   return 0;
79 }
80 
81 /*TEST
82 
83    test:
84       filter: grep -v " MPI process"
85 
86 TEST*/
87