Actual source code: ex50.c

  1: static char help[] = "Tests MatView()/MatLoad() with binary viewers for SBAIJ 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;
 18:   PetscBool   seqsbaij, mpisbaij, sbaij;

 20:   MatGetSize(A, &M, &N);
 21:   MatGetOwnershipRange(A, &rstart, &rend);
 22:   PetscObjectTypeCompare((PetscObject)A, MATSEQSBAIJ, &seqsbaij);
 23:   PetscObjectTypeCompare((PetscObject)A, MATMPISBAIJ, &mpisbaij);
 24:   sbaij = (seqsbaij || mpisbaij) ? PETSC_TRUE : PETSC_FALSE;
 25:   for (i = rstart; i < rend; i++) {
 26:     for (j = (sbaij ? i : 0); j < N; j++) {
 27:       MatGetValue(A, i, j, &val);
 28:       v = MakeValue(i, j, M);
 29:       w = PetscRealPart(val);
 31:     }
 32:   }
 33:   return 0;
 34: }

 36: int main(int argc, char **args)
 37: {
 38:   Mat         A;
 39:   PetscInt    M = 24, N = 24, bs = 3;
 40:   PetscInt    rstart, rend, i, j;
 41:   PetscViewer view;

 44:   PetscInitialize(&argc, &args, NULL, help);
 45:   /*
 46:       Create a parallel SBAIJ matrix shared by all processors
 47:   */
 48:   MatCreateSBAIJ(PETSC_COMM_WORLD, bs, PETSC_DECIDE, PETSC_DECIDE, M, N, PETSC_DECIDE, NULL, PETSC_DECIDE, NULL, &A);

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

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

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

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

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

116:   PetscFinalize();
117:   return 0;
118: }

120: /*TEST

122:    testset:
123:       args: -viewer_binary_mpiio 0
124:       output_file: output/ex50.out
125:       test:
126:         suffix: stdio_1
127:         nsize: 1
128:       test:
129:         suffix: stdio_2
130:         nsize: 2
131:       test:
132:         suffix: stdio_3
133:         nsize: 3
134:       test:
135:         suffix: stdio_4
136:         nsize: 4
137:       test:
138:         suffix: stdio_5
139:         nsize: 4

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

161: TEST*/