1 static char help[] = "Tests DMDAGlobalToNaturalAllCreate() using contour plotting for 2d DMDAs.\n\n";
2
3 #include <petscdm.h>
4 #include <petscdmda.h>
5 #include <petscdraw.h>
6
main(int argc,char ** argv)7 int main(int argc, char **argv)
8 {
9 PetscInt i, j, M = 10, N = 8, m = PETSC_DECIDE, n = PETSC_DECIDE;
10 PetscMPIInt rank;
11 PetscBool flg = PETSC_FALSE;
12 DM da;
13 PetscViewer viewer;
14 Vec localall, global;
15 PetscScalar value, *vlocal;
16 DMBoundaryType bx = DM_BOUNDARY_NONE, by = DM_BOUNDARY_NONE;
17 DMDAStencilType stype = DMDA_STENCIL_BOX;
18 VecScatter tolocalall, fromlocalall;
19 PetscInt start, end;
20
21 PetscFunctionBeginUser;
22 PetscCall(PetscInitialize(&argc, &argv, NULL, help));
23 PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD, 0, "", 300, 0, 300, 300, &viewer));
24
25 /* Read options */
26 PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
27 PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
28 PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
29 PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
30 PetscCall(PetscOptionsGetBool(NULL, NULL, "-star_stencil", &flg, NULL));
31 if (flg) stype = DMDA_STENCIL_STAR;
32
33 /* Create distributed array and get vectors */
34 PetscCall(DMDACreate2d(PETSC_COMM_WORLD, bx, by, stype, M, N, m, n, 1, 1, NULL, NULL, &da));
35 PetscCall(DMSetFromOptions(da));
36 PetscCall(DMSetUp(da));
37
38 PetscCall(DMCreateGlobalVector(da, &global));
39 PetscCall(VecCreateSeq(PETSC_COMM_SELF, M * N, &localall));
40
41 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
42 PetscCall(VecGetOwnershipRange(global, &start, &end));
43 for (i = start; i < end; i++) {
44 value = 5.0 * rank;
45 PetscCall(VecSetValues(global, 1, &i, &value, INSERT_VALUES));
46 }
47 PetscCall(VecAssemblyBegin(global));
48 PetscCall(VecAssemblyEnd(global));
49 PetscCall(VecView(global, viewer));
50
51 /*
52 Create Scatter from global DMDA parallel vector to local vector that
53 contains all entries
54 */
55 PetscCall(DMDAGlobalToNaturalAllCreate(da, &tolocalall));
56 PetscCall(DMDANaturalAllToGlobalCreate(da, &fromlocalall));
57
58 PetscCall(VecScatterBegin(tolocalall, global, localall, INSERT_VALUES, SCATTER_FORWARD));
59 PetscCall(VecScatterEnd(tolocalall, global, localall, INSERT_VALUES, SCATTER_FORWARD));
60
61 PetscCall(VecGetArray(localall, &vlocal));
62 for (j = 0; j < N; j++) {
63 for (i = 0; i < M; i++) *vlocal++ += i + j * M;
64 }
65 PetscCall(VecRestoreArray(localall, &vlocal));
66
67 /* scatter back to global vector */
68 PetscCall(VecScatterBegin(fromlocalall, localall, global, INSERT_VALUES, SCATTER_FORWARD));
69 PetscCall(VecScatterEnd(fromlocalall, localall, global, INSERT_VALUES, SCATTER_FORWARD));
70
71 PetscCall(VecView(global, viewer));
72
73 /* Free memory */
74 PetscCall(VecScatterDestroy(&tolocalall));
75 PetscCall(VecScatterDestroy(&fromlocalall));
76 PetscCall(PetscViewerDestroy(&viewer));
77 PetscCall(VecDestroy(&localall));
78 PetscCall(VecDestroy(&global));
79 PetscCall(DMDestroy(&da));
80 PetscCall(PetscFinalize());
81 return 0;
82 }
83
84 /*TEST
85
86 build:
87 requires: !complex
88
89 test:
90 nsize: 3
91 output_file: output/empty.out
92
93 TEST*/
94