Actual source code: ex44.c
1: static char help[] = "Tests MatView()/MatLoad() with binary viewers for AIJ matrices.\n\n";
3: #include <petscmat.h>
4: #include <petscviewer.h>
6: #include <petsc/private/hashtable.h>
7: static PetscReal MakeValue(PetscInt i,PetscInt j,PetscInt M)
8: {
9: PetscHash_t h = PetscHashCombine(PetscHashInt(i),PetscHashInt(j));
10: return (PetscReal) ((h % 5 == 0) ? (1 + i + j*M) : 0);
11: }
13: static PetscErrorCode CheckValuesAIJ(Mat A)
14: {
15: PetscInt M,N,rstart,rend,i,j;
16: PetscReal v,w;
17: PetscScalar val;
19: MatGetSize(A,&M,&N);
20: MatGetOwnershipRange(A,&rstart,&rend);
21: for (i=rstart; i<rend; i++) {
22: for (j=0; j<N; j++) {
23: MatGetValue(A,i,j,&val);
24: v = MakeValue(i,j,M); w = PetscRealPart(val);
26: }
27: }
28: return 0;
29: }
31: int main(int argc,char **args)
32: {
33: Mat A;
34: PetscInt M = 11,N = 13;
35: PetscInt rstart,rend,i,j;
37: PetscViewer view;
39: PetscInitialize(&argc,&args,NULL,help);
40: /*
41: Create a parallel AIJ matrix shared by all processors
42: */
43: MatCreateAIJ(PETSC_COMM_WORLD,
44: PETSC_DECIDE,PETSC_DECIDE,
45: M,N,
46: PETSC_DECIDE,NULL,
47: PETSC_DECIDE,NULL,
48: &A);
50: /*
51: Set values into the matrix
52: */
53: MatGetOwnershipRange(A,&rstart,&rend);
54: for (i=rstart; i<rend; i++) {
55: for (j=0; j<N; j++) {
56: PetscReal v = MakeValue(i,j,M);
57: if (PetscAbsReal(v) > 0) {
58: MatSetValue(A,i,j,v,INSERT_VALUES);
59: }
60: }
61: }
62: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
63: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
64: MatViewFromOptions(A,NULL,"-mat_base_view");
66: /*
67: Store the binary matrix to a file
68: */
69: PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_WRITE, &view);
70: for (i=0; i<3; i++) {
71: MatView(A,view);
72: }
73: PetscViewerDestroy(&view);
74: MatDestroy(&A);
76: /*
77: Now reload the matrix and check its values
78: */
79: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_READ,&view);
80: MatCreate(PETSC_COMM_WORLD,&A);
81: MatSetType(A,MATAIJ);
82: for (i=0; i<3; i++) {
83: if (i > 0) MatZeroEntries(A);
84: MatLoad(A,view);
85: CheckValuesAIJ(A);
86: }
87: PetscViewerDestroy(&view);
88: MatViewFromOptions(A,NULL,"-mat_load_view");
89: MatDestroy(&A);
91: /*
92: Reload in SEQAIJ matrix and check its values
93: */
94: PetscViewerBinaryOpen(PETSC_COMM_SELF,"matrix.dat",FILE_MODE_READ,&view);
95: MatCreate(PETSC_COMM_SELF,&A);
96: MatSetType(A,MATSEQAIJ);
97: for (i=0; i<3; i++) {
98: if (i > 0) MatZeroEntries(A);
99: MatLoad(A,view);
100: CheckValuesAIJ(A);
101: }
102: PetscViewerDestroy(&view);
103: MatDestroy(&A);
105: /*
106: Reload in MPIAIJ matrix and check its values
107: */
108: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_READ,&view);
109: MatCreate(PETSC_COMM_WORLD,&A);
110: MatSetType(A,MATMPIAIJ);
111: for (i=0; i<3; i++) {
112: if (i > 0) MatZeroEntries(A);
113: MatLoad(A,view);
114: CheckValuesAIJ(A);
115: }
116: PetscViewerDestroy(&view);
117: MatDestroy(&A);
119: PetscFinalize();
120: return 0;
121: }
123: /*TEST
125: testset:
126: args: -viewer_binary_mpiio 0
127: output_file: output/ex44.out
128: test:
129: suffix: stdio_1
130: nsize: 1
131: test:
132: suffix: stdio_2
133: nsize: 2
134: test:
135: suffix: stdio_3
136: nsize: 3
137: test:
138: suffix: stdio_4
139: nsize: 4
140: test:
141: suffix: stdio_15
142: nsize: 15
144: testset:
145: requires: mpiio
146: args: -viewer_binary_mpiio 1
147: output_file: output/ex44.out
148: test:
149: suffix: mpiio_1
150: nsize: 1
151: test:
152: suffix: mpiio_2
153: nsize: 2
154: test:
155: suffix: mpiio_3
156: nsize: 3
157: test:
158: suffix: mpiio_4
159: nsize: 4
160: test:
161: suffix: mpiio_15
162: nsize: 15
164: TEST*/