Actual source code: ex37.c

  1: static char help[] = "Test MatGetMultiProcBlock() and MatCreateRedundantMatrix() \n\
  2: Reads a PETSc matrix and vector from a file and solves a linear system.\n\n";
  3: /*
  4:   Example:
  5:   mpiexec -n 4 ./ex37 -f <input_file> -nsubcomm 2 -psubcomm_view -subcomm_type <1 or 2>
  6: */

  8: #include <petscksp.h>
  9: #include <petscsys.h>

 11: int main(int argc, char **args)
 12: {
 13:   KSP          subksp;
 14:   Mat          A, subA;
 15:   Vec          x, b, u, subb, subx, subu;
 16:   PetscViewer  fd;
 17:   char         file[PETSC_MAX_PATH_LEN];
 18:   PetscBool    flg;
 19:   PetscInt     i, m, n, its;
 20:   PetscReal    norm;
 21:   PetscMPIInt  rank, size;
 22:   MPI_Comm     comm, subcomm;
 23:   PetscSubcomm psubcomm;
 24:   PetscInt     nsubcomm = 1, id;
 25:   PetscScalar *barray, *xarray, *uarray, *array, one = 1.0;
 26:   PetscInt     type = 1;

 28:   PetscFunctionBeginUser;
 29:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 30:   /* Load the matrix */
 31:   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
 32:   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER_INPUT, "Must indicate binary file with the -f option");
 33:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));

 35:   /* Load the matrix; then destroy the viewer.*/
 36:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 37:   PetscCall(MatLoad(A, fd));
 38:   PetscCall(PetscViewerDestroy(&fd));

 40:   PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
 41:   PetscCallMPI(MPI_Comm_size(comm, &size));
 42:   PetscCallMPI(MPI_Comm_rank(comm, &rank));

 44:   /* Create rhs vector b */
 45:   PetscCall(MatGetLocalSize(A, &m, NULL));
 46:   PetscCall(VecCreate(PETSC_COMM_WORLD, &b));
 47:   PetscCall(VecSetSizes(b, m, PETSC_DECIDE));
 48:   PetscCall(VecSetFromOptions(b));
 49:   PetscCall(VecSet(b, one));

 51:   PetscCall(VecDuplicate(b, &x));
 52:   PetscCall(VecDuplicate(b, &u));

 54:   /* Test MatGetMultiProcBlock() */
 55:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-nsubcomm", &nsubcomm, NULL));
 56:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-subcomm_type", &type, NULL));

 58:   PetscCall(PetscSubcommCreate(comm, &psubcomm));
 59:   PetscCall(PetscSubcommSetNumber(psubcomm, nsubcomm));
 60:   if (type == PETSC_SUBCOMM_GENERAL) { /* user provides color, subrank and duprank */
 61:     PetscMPIInt color, subrank, duprank, subsize;
 62:     duprank = size - 1 - rank;
 63:     subsize = size / nsubcomm;
 64:     PetscCheck(subsize * nsubcomm == size, comm, PETSC_ERR_SUP, "This example requires nsubcomm %" PetscInt_FMT " divides size %d", nsubcomm, size);
 65:     color   = duprank / subsize;
 66:     subrank = duprank - color * subsize;
 67:     PetscCall(PetscSubcommSetTypeGeneral(psubcomm, color, subrank));
 68:   } else if (type == PETSC_SUBCOMM_CONTIGUOUS) {
 69:     PetscCall(PetscSubcommSetType(psubcomm, PETSC_SUBCOMM_CONTIGUOUS));
 70:   } else if (type == PETSC_SUBCOMM_INTERLACED) {
 71:     PetscCall(PetscSubcommSetType(psubcomm, PETSC_SUBCOMM_INTERLACED));
 72:   } else SETERRQ(psubcomm->parent, PETSC_ERR_SUP, "PetscSubcommType %" PetscInt_FMT " is not supported yet", type);
 73:   PetscCall(PetscSubcommSetFromOptions(psubcomm));
 74:   subcomm = PetscSubcommChild(psubcomm);

 76:   /* Test MatCreateRedundantMatrix() */
 77:   if (size > 1) {
 78:     PetscMPIInt subrank = -1, color = -1;
 79:     MPI_Comm    dcomm;

 81:     if (rank == 0) {
 82:       color   = 0;
 83:       subrank = 0;
 84:     } else if (rank == 1) {
 85:       color   = 0;
 86:       subrank = 1;
 87:     } else {
 88:       color   = 1;
 89:       subrank = 0;
 90:     }

 92:     PetscCall(PetscCommDuplicate(PETSC_COMM_WORLD, &dcomm, NULL));
 93:     PetscCallMPI(MPI_Comm_split(dcomm, color, subrank, &subcomm));

 95:     PetscCall(MatCreate(subcomm, &subA));
 96:     PetscCall(MatSetSizes(subA, PETSC_DECIDE, PETSC_DECIDE, 10, 10));
 97:     PetscCall(MatSetFromOptions(subA));
 98:     PetscCall(MatSetUp(subA));
 99:     PetscCall(MatAssemblyBegin(subA, MAT_FINAL_ASSEMBLY));
100:     PetscCall(MatAssemblyEnd(subA, MAT_FINAL_ASSEMBLY));
101:     PetscCall(MatZeroEntries(subA));

103:     /* Test MatMult() */
104:     PetscCall(MatCreateVecs(subA, &subx, &subb));
105:     PetscCall(VecSet(subx, 1.0));
106:     PetscCall(MatMult(subA, subx, subb));

108:     PetscCall(VecDestroy(&subx));
109:     PetscCall(VecDestroy(&subb));
110:     PetscCall(MatDestroy(&subA));
111:     PetscCall(PetscCommDestroy(&dcomm));
112:   }

114:   /* Create subA */
115:   PetscCall(MatGetMultiProcBlock(A, subcomm, MAT_INITIAL_MATRIX, &subA));
116:   PetscCall(MatGetMultiProcBlock(A, subcomm, MAT_REUSE_MATRIX, &subA));

118:   /* Create sub vectors without arrays. Place b's and x's local arrays into subb and subx */
119:   PetscCall(MatGetLocalSize(subA, &m, &n));
120:   PetscCall(VecCreateMPIWithArray(subcomm, 1, m, PETSC_DECIDE, NULL, &subb));
121:   PetscCall(VecCreateMPIWithArray(subcomm, 1, n, PETSC_DECIDE, NULL, &subx));
122:   PetscCall(VecCreateMPIWithArray(subcomm, 1, n, PETSC_DECIDE, NULL, &subu));

124:   PetscCall(VecGetArray(b, &barray));
125:   PetscCall(VecGetArray(x, &xarray));
126:   PetscCall(VecGetArray(u, &uarray));
127:   PetscCall(VecPlaceArray(subb, barray));
128:   PetscCall(VecPlaceArray(subx, xarray));
129:   PetscCall(VecPlaceArray(subu, uarray));

131:   /* Create linear solvers associated with subA */
132:   PetscCall(KSPCreate(subcomm, &subksp));
133:   PetscCall(KSPSetOperators(subksp, subA, subA));
134:   PetscCall(KSPSetFromOptions(subksp));

136:   /* Solve sub systems */
137:   PetscCall(KSPSolve(subksp, subb, subx));
138:   PetscCall(KSPGetIterationNumber(subksp, &its));

140:   /* check residual */
141:   PetscCall(MatMult(subA, subx, subu));
142:   PetscCall(VecAXPY(subu, -1.0, subb));
143:   PetscCall(VecNorm(u, NORM_2, &norm));
144:   if (norm > 1.e-4 && rank == 0) {
145:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "[%d]  Number of iterations = %3" PetscInt_FMT "\n", rank, its));
146:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error: Residual norm of each block |subb - subA*subx |= %g\n", (double)norm));
147:   }
148:   PetscCall(VecResetArray(subb));
149:   PetscCall(VecResetArray(subx));
150:   PetscCall(VecResetArray(subu));

152:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-subvec_view", &id, &flg));
153:   if (flg && rank == id) {
154:     PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] subb:\n", rank));
155:     PetscCall(VecGetArray(subb, &array));
156:     for (i = 0; i < m; i++) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%g\n", (double)PetscRealPart(array[i])));
157:     PetscCall(VecRestoreArray(subb, &array));
158:     PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] subx:\n", rank));
159:     PetscCall(VecGetArray(subx, &array));
160:     for (i = 0; i < m; i++) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%g\n", (double)PetscRealPart(array[i])));
161:     PetscCall(VecRestoreArray(subx, &array));
162:   }

164:   PetscCall(VecRestoreArray(x, &xarray));
165:   PetscCall(VecRestoreArray(b, &barray));
166:   PetscCall(VecRestoreArray(u, &uarray));
167:   PetscCall(MatDestroy(&subA));
168:   PetscCall(VecDestroy(&subb));
169:   PetscCall(VecDestroy(&subx));
170:   PetscCall(VecDestroy(&subu));
171:   PetscCall(KSPDestroy(&subksp));
172:   PetscCall(PetscSubcommDestroy(&psubcomm));
173:   if (size > 1) PetscCallMPI(MPI_Comm_free(&subcomm));
174:   PetscCall(MatDestroy(&A));
175:   PetscCall(VecDestroy(&b));
176:   PetscCall(VecDestroy(&u));
177:   PetscCall(VecDestroy(&x));

179:   PetscCall(PetscFinalize());
180:   return 0;
181: }

183: /*TEST

185:     test:
186:       args: -f ${DATAFILESPATH}/matrices/small -nsubcomm 1
187:       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
188:       output_file: output/empty.out

190:     test:
191:       suffix: 2
192:       args: -f ${DATAFILESPATH}/matrices/small -nsubcomm 2
193:       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
194:       nsize: 4
195:       output_file: output/empty.out

197:     test:
198:       suffix: mumps
199:       args: -f ${DATAFILESPATH}/matrices/small -nsubcomm 2 -pc_factor_mat_solver_type mumps -pc_type lu
200:       requires: datafilespath mumps !complex double !defined(PETSC_USE_64BIT_INDICES)
201:       nsize: 4
202:       output_file: output/empty.out

204:     test:
205:       suffix: 3
206:       nsize: 4
207:       args: -f ${DATAFILESPATH}/matrices/small -nsubcomm 2 -subcomm_type 0
208:       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
209:       output_file: output/empty.out

211:     test:
212:       suffix: 4
213:       nsize: 4
214:       args: -f ${DATAFILESPATH}/matrices/small -nsubcomm 2 -subcomm_type 1
215:       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
216:       output_file: output/empty.out

218:     test:
219:       suffix: 5
220:       nsize: 4
221:       args: -f ${DATAFILESPATH}/matrices/small -nsubcomm 2 -subcomm_type 2
222:       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
223:       output_file: output/empty.out

225: TEST*/