Actual source code: arrayview.c

  1: #include <petsc/private/petscimpl.h>

  3: /*@
  4:   PetscIntViewNumColumns - Prints an array of integers; useful for debugging.

  6:   Collective

  8:   Input Parameters:
  9: + N      - number of integers in array
 10: . Ncol   - number of integers to print per row
 11: . idx    - array of integers
 12: - viewer - an optional `PetscViewer` visualization context

 14:   Level: intermediate

 16:   Note:
 17:   This may be called from within the debugger, passing 0 as the viewer

 19:   This API may be removed in the future.

 21:   Developer Note:
 22:   `idx` cannot be const because may be passed to binary viewer where temporary byte swapping may be done

 24: .seealso: `PetscViewer`, `PetscIntView()`, `PetscRealView()`
 25: @*/
 26: PetscErrorCode PetscIntViewNumColumns(PetscInt N, PetscInt Ncol, const PetscInt idx[], PetscViewer viewer)
 27: {
 28:   PetscMPIInt rank, size;
 29:   PetscInt    j, i, n = N / Ncol, p = N % Ncol;
 30:   PetscBool   isascii, isbinary;
 31:   MPI_Comm    comm;

 33:   PetscFunctionBegin;
 34:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
 35:   if (N) PetscAssertPointer(idx, 3);
 37:   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
 38:   PetscCallMPI(MPI_Comm_size(comm, &size));
 39:   PetscCallMPI(MPI_Comm_rank(comm, &rank));

 41:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
 42:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
 43:   if (isascii) {
 44:     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
 45:     for (i = 0; i < n; i++) {
 46:       if (size > 1) {
 47:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %" PetscInt_FMT ":", rank, Ncol * i));
 48:       } else {
 49:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%" PetscInt_FMT ":", Ncol * i));
 50:       }
 51:       for (j = 0; j < Ncol; j++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, idx[i * Ncol + j]));
 52:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
 53:     }
 54:     if (p) {
 55:       if (size > 1) {
 56:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %" PetscInt_FMT ":", rank, Ncol * n));
 57:       } else {
 58:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%" PetscInt_FMT ":", Ncol * n));
 59:       }
 60:       for (i = 0; i < p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, idx[Ncol * n + i]));
 61:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
 62:     }
 63:     PetscCall(PetscViewerFlush(viewer));
 64:     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
 65:   } else if (isbinary) {
 66:     PetscMPIInt *sizes, Ntotal, *displs, NN;
 67:     PetscInt    *array;

 69:     PetscCall(PetscMPIIntCast(N, &NN));

 71:     if (size > 1) {
 72:       if (rank) {
 73:         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, NULL, 0, MPI_INT, 0, comm));
 74:         PetscCallMPI(MPI_Gatherv(idx, NN, MPIU_INT, NULL, NULL, NULL, MPIU_INT, 0, comm));
 75:       } else {
 76:         PetscCall(PetscMalloc1(size, &sizes));
 77:         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, sizes, 1, MPI_INT, 0, comm));
 78:         Ntotal = sizes[0];
 79:         PetscCall(PetscMalloc1(size, &displs));
 80:         displs[0] = 0;
 81:         for (i = 1; i < size; i++) {
 82:           Ntotal += sizes[i];
 83:           displs[i] = displs[i - 1] + sizes[i - 1];
 84:         }
 85:         PetscCall(PetscMalloc1(Ntotal, &array));
 86:         PetscCallMPI(MPI_Gatherv(idx, NN, MPIU_INT, array, sizes, displs, MPIU_INT, 0, comm));
 87:         PetscCall(PetscViewerBinaryWrite(viewer, array, Ntotal, PETSC_INT));
 88:         PetscCall(PetscFree(sizes));
 89:         PetscCall(PetscFree(displs));
 90:         PetscCall(PetscFree(array));
 91:       }
 92:     } else {
 93:       PetscCall(PetscViewerBinaryWrite(viewer, idx, N, PETSC_INT));
 94:     }
 95:   } else {
 96:     const char *tname;
 97:     PetscCall(PetscObjectGetName((PetscObject)viewer, &tname));
 98:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle that PetscViewer of type %s", tname);
 99:   }
100:   PetscFunctionReturn(PETSC_SUCCESS);
101: }

103: /*@
104:   PetscRealViewNumColumns - Prints an array of doubles; useful for debugging.

106:   Collective

108:   Input Parameters:
109: + N      - number of `PetscReal` in array
110: . Ncol   - number of `PetscReal` to print per row
111: . idx    - array of `PetscReal`
112: - viewer - an optional `PetscViewer` visualization context

114:   Level: intermediate

116:   Note:
117:   This may be called from within the debugger, passing 0 as the viewer

119:   This API may be removed in the future.

121:   Developer Note:
122:   `idx` cannot be const because may be passed to binary viewer where temporary byte swapping may be done

124: .seealso: `PetscViewer`, `PetscRealView()`, `PetscIntView()`
125: @*/
126: PetscErrorCode PetscRealViewNumColumns(PetscInt N, PetscInt Ncol, const PetscReal idx[], PetscViewer viewer)
127: {
128:   PetscMPIInt rank, size;
129:   PetscInt    j, i, n = N / Ncol, p = N % Ncol;
130:   PetscBool   isascii, isbinary;
131:   MPI_Comm    comm;

133:   PetscFunctionBegin;
134:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
135:   if (N) PetscAssertPointer(idx, 3);
137:   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
138:   PetscCallMPI(MPI_Comm_size(comm, &size));
139:   PetscCallMPI(MPI_Comm_rank(comm, &rank));

141:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
142:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
143:   if (isascii) {
144:     PetscInt tab;

146:     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
147:     PetscCall(PetscViewerASCIIGetTab(viewer, &tab));
148:     for (i = 0; i < n; i++) {
149:       PetscCall(PetscViewerASCIISetTab(viewer, tab));
150:       if (size > 1) {
151:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, Ncol * i));
152:       } else {
153:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", Ncol * i));
154:       }
155:       PetscCall(PetscViewerASCIISetTab(viewer, 0));
156:       for (j = 0; j < Ncol; j++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[i * Ncol + j]));
157:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
158:     }
159:     if (p) {
160:       PetscCall(PetscViewerASCIISetTab(viewer, tab));
161:       if (size > 1) {
162:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, Ncol * n));
163:       } else {
164:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", Ncol * n));
165:       }
166:       PetscCall(PetscViewerASCIISetTab(viewer, 0));
167:       for (i = 0; i < p; i++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[Ncol * n + i]));
168:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
169:     }
170:     PetscCall(PetscViewerFlush(viewer));
171:     PetscCall(PetscViewerASCIISetTab(viewer, tab));
172:     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
173:   } else if (isbinary) {
174:     PetscMPIInt *sizes, *displs, Ntotal, NN;
175:     PetscReal   *array;

177:     PetscCall(PetscMPIIntCast(N, &NN));

179:     if (size > 1) {
180:       if (rank) {
181:         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, NULL, 0, MPI_INT, 0, comm));
182:         PetscCallMPI(MPI_Gatherv(idx, NN, MPIU_REAL, NULL, NULL, NULL, MPIU_REAL, 0, comm));
183:       } else {
184:         PetscCall(PetscMalloc1(size, &sizes));
185:         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, sizes, 1, MPI_INT, 0, comm));
186:         Ntotal = sizes[0];
187:         PetscCall(PetscMalloc1(size, &displs));
188:         displs[0] = 0;
189:         for (i = 1; i < size; i++) {
190:           Ntotal += sizes[i];
191:           displs[i] = displs[i - 1] + sizes[i - 1];
192:         }
193:         PetscCall(PetscMalloc1(Ntotal, &array));
194:         PetscCallMPI(MPI_Gatherv(idx, NN, MPIU_REAL, array, sizes, displs, MPIU_REAL, 0, comm));
195:         PetscCall(PetscViewerBinaryWrite(viewer, array, Ntotal, PETSC_REAL));
196:         PetscCall(PetscFree(sizes));
197:         PetscCall(PetscFree(displs));
198:         PetscCall(PetscFree(array));
199:       }
200:     } else {
201:       PetscCall(PetscViewerBinaryWrite(viewer, (void *)idx, N, PETSC_REAL));
202:     }
203:   } else {
204:     const char *tname;
205:     PetscCall(PetscObjectGetName((PetscObject)viewer, &tname));
206:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle that PetscViewer of type %s", tname);
207:   }
208:   PetscFunctionReturn(PETSC_SUCCESS);
209: }

211: /*@
212:   PetscScalarViewNumColumns - Prints an array of doubles; useful for debugging.

214:   Collective

216:   Input Parameters:
217: + N      - number of `PetscScalar` in array
218: . Ncol   - number of `PetscScalar` to print per row
219: . idx    - array of `PetscScalar`
220: - viewer - an optional `PetscViewer` visualization context

222:   Level: intermediate

224:   Note:
225:   This may be called from within the debugger, passing 0 as the viewer

227:   This API may be removed in the future.

229:   Developer Note:
230:   `idx` cannot be const because may be passed to binary viewer where temporary byte swapping may be done

232: .seealso: `PetscViewer`, `PetscRealView()`, `PetscScalarView()`, `PetscIntView()`
233: @*/
234: PetscErrorCode PetscScalarViewNumColumns(PetscInt N, PetscInt Ncol, const PetscScalar idx[], PetscViewer viewer)
235: {
236:   PetscMPIInt rank, size;
237:   PetscInt    j, i, n = N / Ncol, p = N % Ncol;
238:   PetscBool   isascii, isbinary;
239:   MPI_Comm    comm;

241:   PetscFunctionBegin;
242:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
243:   if (N) PetscAssertPointer(idx, 3);
245:   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
246:   PetscCallMPI(MPI_Comm_size(comm, &size));
247:   PetscCallMPI(MPI_Comm_rank(comm, &rank));

249:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
250:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
251:   if (isascii) {
252:     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
253:     for (i = 0; i < n; i++) {
254:       if (size > 1) {
255:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, Ncol * i));
256:       } else {
257:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", Ncol * i));
258:       }
259:       for (j = 0; j < Ncol; j++) {
260: #if PetscDefined(USE_COMPLEX)
261:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " (%12.4e,%12.4e)", (double)PetscRealPart(idx[i * Ncol + j]), (double)PetscImaginaryPart(idx[i * Ncol + j])));
262: #else
263:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[i * Ncol + j]));
264: #endif
265:       }
266:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
267:     }
268:     if (p) {
269:       if (size > 1) {
270:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] %2" PetscInt_FMT ":", rank, Ncol * n));
271:       } else {
272:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%2" PetscInt_FMT ":", Ncol * n));
273:       }
274:       for (i = 0; i < p; i++) {
275: #if PetscDefined(USE_COMPLEX)
276:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " (%12.4e,%12.4e)", (double)PetscRealPart(idx[n * Ncol + i]), (double)PetscImaginaryPart(idx[n * Ncol + i])));
277: #else
278:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %12.4e", (double)idx[Ncol * n + i]));
279: #endif
280:       }
281:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
282:     }
283:     PetscCall(PetscViewerFlush(viewer));
284:     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
285:   } else if (isbinary) {
286:     PetscMPIInt *sizes, Ntotal, *displs, NN;
287:     PetscScalar *array;

289:     PetscCall(PetscMPIIntCast(N, &NN));

291:     if (size > 1) {
292:       if (rank) {
293:         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, NULL, 0, MPI_INT, 0, comm));
294:         PetscCallMPI(MPI_Gatherv((void *)idx, NN, MPIU_SCALAR, NULL, NULL, NULL, MPIU_SCALAR, 0, comm));
295:       } else {
296:         PetscCall(PetscMalloc1(size, &sizes));
297:         PetscCallMPI(MPI_Gather(&NN, 1, MPI_INT, sizes, 1, MPI_INT, 0, comm));
298:         Ntotal = sizes[0];
299:         PetscCall(PetscMalloc1(size, &displs));
300:         displs[0] = 0;
301:         for (i = 1; i < size; i++) {
302:           Ntotal += sizes[i];
303:           displs[i] = displs[i - 1] + sizes[i - 1];
304:         }
305:         PetscCall(PetscMalloc1(Ntotal, &array));
306:         PetscCallMPI(MPI_Gatherv((void *)idx, NN, MPIU_SCALAR, array, sizes, displs, MPIU_SCALAR, 0, comm));
307:         PetscCall(PetscViewerBinaryWrite(viewer, array, Ntotal, PETSC_SCALAR));
308:         PetscCall(PetscFree(sizes));
309:         PetscCall(PetscFree(displs));
310:         PetscCall(PetscFree(array));
311:       }
312:     } else {
313:       PetscCall(PetscViewerBinaryWrite(viewer, (void *)idx, N, PETSC_SCALAR));
314:     }
315:   } else {
316:     const char *tname;
317:     PetscCall(PetscObjectGetName((PetscObject)viewer, &tname));
318:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot handle that PetscViewer of type %s", tname);
319:   }
320:   PetscFunctionReturn(PETSC_SUCCESS);
321: }

323: /*@
324:   PetscIntView - Prints an array of integers; useful for debugging.

326:   Collective

328:   Input Parameters:
329: + N      - number of integers in array
330: . idx    - array of integers
331: - viewer - an optional `PetscViewer` visualization context

333:   Level: intermediate

335:   Note:
336:   This may be called from within the debugger, passing 0 as the viewer

338:   This API may be removed in the future.

340:   Same as `PetscIntViewNumColumns()` with 20 values per row

342:   Developer Note:
343:   `idx` cannot be const because may be passed to binary viewer where temporary byte swapping may be done

345: .seealso: `PetscViewer`, `PetscIntViewNumColumns()`, `PetscIntCSRView()`, `PetscRealView()`
346: @*/
347: PetscErrorCode PetscIntView(PetscInt N, const PetscInt idx[], PetscViewer viewer)
348: {
349:   PetscFunctionBegin;
350:   PetscCall(PetscIntViewNumColumns(N, 20, idx, viewer));
351:   PetscFunctionReturn(PETSC_SUCCESS);
352: }

354: /*@
355:   PetscIntCSRView - Prints a graph represented in compressed sparse row (CSR) format; useful for debugging.

357:   Collective

359:   Input Parameters:
360: + N      - number of local vertices
361: . ia     - row pointers of length `N + 1`
362: . ja     - adjacency list of length `ia[N]`
363: - viewer - an optional `PetscViewer` visualization context

365:   Level: intermediate

367:   Notes:
368:   This may be called from within the debugger, passing `NULL` as the viewer.

370:   `ia[0]` must be zero and the row pointers must be nondecreasing.

372:   Only ASCII viewers are supported.

374: .seealso: `PetscViewer`, `PetscIntView()`, `PetscPartitionerPartition()`
375: @*/
376: PetscErrorCode PetscIntCSRView(PetscInt N, const PetscInt ia[], const PetscInt ja[], PetscViewer viewer)
377: {
378:   PetscMPIInt rank;
379:   PetscBool   isascii;

381:   PetscFunctionBegin;
382:   PetscCheck(N >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of rows must be non-negative");
383:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
384:   if (N) {
385:     PetscAssertPointer(ia, 2);
386:     PetscCheck(!ia[0], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "First row offset must be zero, got %" PetscInt_FMT, ia[0]);
387:     for (PetscInt i = 0; i < N; i++)
388:       PetscCheck(ia[i] <= ia[i + 1], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row offsets must be nondecreasing, got ia[%" PetscInt_FMT "] = %" PetscInt_FMT " > ia[%" PetscInt_FMT "] = %" PetscInt_FMT, i, ia[i], i + 1, ia[i + 1]);
389:     if (ia[N]) PetscAssertPointer(ja, 3);
390:   }
392:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
393:   PetscCheck(isascii, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Only ASCII viewers are supported");
394:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
395:   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
396:   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d]Nv: %" PetscInt_FMT "\n", rank, N));
397:   for (PetscInt i = 0; i < N; i++) {
398:     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d]  ", rank));
399:     for (PetscInt j = ia[i]; j < ia[i + 1]; j++) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%" PetscInt_FMT " ", ja[j]));
400:     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%" PetscInt_FMT "-%" PetscInt_FMT ")\n", ia[i], ia[i + 1]));
401:   }
402:   PetscCall(PetscViewerFlush(viewer));
403:   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
404:   PetscFunctionReturn(PETSC_SUCCESS);
405: }

407: /*@
408:   PetscRealView - Prints an array of doubles; useful for debugging.

410:   Collective

412:   Input Parameters:
413: + N      - number of `PetscReal` in array
414: . idx    - array of `PetscReal`
415: - viewer - an optional `PetscViewer` visualization context

417:   Level: intermediate

419:   Note:
420:   This may be called from within the debugger, passing 0 as the viewer

422:   This API may be removed in the future.

424:   Same as `PetscRealViewNumColumns()` with 5 values per row

426:   Developer Note:
427:   `idx` cannot be const because may be passed to binary viewer where temporary byte swapping may be done

429: .seealso: `PetscViewer`, `PetscIntView()`
430: @*/
431: PetscErrorCode PetscRealView(PetscInt N, const PetscReal idx[], PetscViewer viewer)
432: {
433:   PetscFunctionBegin;
434:   PetscCall(PetscRealViewNumColumns(N, 5, idx, viewer));
435:   PetscFunctionReturn(PETSC_SUCCESS);
436: }

438: /*@
439:   PetscScalarView - Prints an array of `PetscScalar`; useful for debugging.

441:   Collective

443:   Input Parameters:
444: + N      - number of scalars in array
445: . idx    - array of scalars
446: - viewer - an optional `PetscViewer` visualization context

448:   Level: intermediate

450:   Note:
451:   This may be called from within the debugger, passing 0 as the viewer

453:   This API may be removed in the future.

455:   Same as `PetscScalarViewNumColumns()` with 3 values per row

457:   Developer Note:
458:   `idx` cannot be const because may be passed to binary viewer where byte swapping may be done

460: .seealso: `PetscViewer`, `PetscIntView()`, `PetscRealView()`
461: @*/
462: PetscErrorCode PetscScalarView(PetscInt N, const PetscScalar idx[], PetscViewer viewer)
463: {
464:   PetscFunctionBegin;
465:   PetscCall(PetscScalarViewNumColumns(N, 3, idx, viewer));
466:   PetscFunctionReturn(PETSC_SUCCESS);
467: }