Actual source code: ex45.c

  1: static char help[] = "Tests MatView()/MatLoad() with binary viewers for BAIJ 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);
 25:       w = PetscRealPart(val);
 27:     }
 28:   }
 29:   return 0;
 30: }

 32: int main(int argc, char **args)
 33: {
 34:   Mat         A;
 35:   PetscInt    M = 24, N = 48, bs = 2;
 36:   PetscInt    rstart, rend, i, j;
 37:   PetscViewer view;

 40:   PetscInitialize(&argc, &args, NULL, help);
 41:   /*
 42:       Create a parallel BAIJ matrix shared by all processors
 43:   */
 44:   MatCreateBAIJ(PETSC_COMM_WORLD, bs, PETSC_DECIDE, PETSC_DECIDE, M, N, PETSC_DECIDE, NULL, PETSC_DECIDE, NULL, &A);

 46:   /*
 47:       Set values into the matrix
 48:   */
 49:   MatGetSize(A, &M, &N);
 50:   MatGetOwnershipRange(A, &rstart, &rend);
 51:   for (i = rstart; i < rend; i++) {
 52:     for (j = 0; j < N; j++) {
 53:       PetscReal v = MakeValue(i, j, M);
 54:       if (PetscAbsReal(v) > 0) MatSetValue(A, i, j, v, INSERT_VALUES);
 55:     }
 56:   }
 57:   MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
 58:   MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
 59:   MatViewFromOptions(A, NULL, "-mat_base_view");

 61:   /*
 62:       Store the binary matrix to a file
 63:   */
 64:   PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_WRITE, &view);
 65:   for (i = 0; i < 3; i++) MatView(A, view);
 66:   PetscViewerDestroy(&view);
 67:   MatDestroy(&A);

 69:   /*
 70:       Now reload the matrix and check its values
 71:   */
 72:   PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_READ, &view);
 73:   MatCreate(PETSC_COMM_WORLD, &A);
 74:   MatSetType(A, MATBAIJ);
 75:   for (i = 0; i < 3; i++) {
 76:     if (i > 0) MatZeroEntries(A);
 77:     MatLoad(A, view);
 78:     CheckValuesAIJ(A);
 79:   }
 80:   PetscViewerDestroy(&view);
 81:   MatViewFromOptions(A, NULL, "-mat_load_view");
 82:   MatDestroy(&A);

 84:   /*
 85:       Reload in SEQBAIJ matrix and check its values
 86:   */
 87:   PetscViewerBinaryOpen(PETSC_COMM_SELF, "matrix.dat", FILE_MODE_READ, &view);
 88:   MatCreate(PETSC_COMM_SELF, &A);
 89:   MatSetType(A, MATSEQBAIJ);
 90:   for (i = 0; i < 3; i++) {
 91:     if (i > 0) MatZeroEntries(A);
 92:     MatLoad(A, view);
 93:     CheckValuesAIJ(A);
 94:   }
 95:   PetscViewerDestroy(&view);
 96:   MatDestroy(&A);

 98:   /*
 99:      Reload in MPIBAIJ matrix and check its values
100:   */
101:   PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_READ, &view);
102:   MatCreate(PETSC_COMM_WORLD, &A);
103:   MatSetType(A, MATMPIBAIJ);
104:   for (i = 0; i < 3; i++) {
105:     if (i > 0) MatZeroEntries(A);
106:     MatLoad(A, view);
107:     CheckValuesAIJ(A);
108:   }
109:   PetscViewerDestroy(&view);
110:   MatDestroy(&A);

112:   PetscFinalize();
113:   return 0;
114: }

116: /*TEST

118:    testset:
119:       args: -viewer_binary_mpiio 0
120:       output_file: output/ex45.out
121:       test:
122:         suffix: stdio_1
123:         nsize: 1
124:       test:
125:         suffix: stdio_2
126:         nsize: 2
127:       test:
128:         suffix: stdio_3
129:         nsize: 3
130:       test:
131:         suffix: stdio_4
132:         nsize: 4
133:       test:
134:         suffix: stdio_5
135:         nsize: 4

137:    testset:
138:       requires: mpiio
139:       args: -viewer_binary_mpiio 1
140:       output_file: output/ex45.out
141:       test:
142:         suffix: mpiio_1
143:         nsize: 1
144:       test:
145:         suffix: mpiio_2
146:         nsize: 2
147:       test:
148:         suffix: mpiio_3
149:         nsize: 3
150:       test:
151:         suffix: mpiio_4
152:         nsize: 4
153:       test:
154:         suffix: mpiio_5
155:         nsize: 5

157: TEST*/