Actual source code: ex1.c
1: static char help[] = "Tests various DM routines.\n\n";
3: #include <petscdm.h>
4: #include <petscdmda.h>
6: int main(int argc, char **argv)
7: {
8: PetscMPIInt rank;
9: PetscInt M = 10, N = 8, m = PETSC_DECIDE, n = PETSC_DECIDE;
10: DM da;
11: PetscViewer viewer;
12: Vec local, global;
13: PetscScalar value;
15: PetscFunctionBeginUser;
16: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
17: PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD, 0, "", 300, 0, 300, 300, &viewer));
19: /* Read options */
20: PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
21: PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
22: PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
23: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
25: /* Create distributed array and get vectors */
26: PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_BOX, M, N, m, n, 1, 1, NULL, NULL, &da));
27: PetscCall(DMSetFromOptions(da));
28: PetscCall(DMSetUp(da));
29: PetscCall(DMCreateGlobalVector(da, &global));
30: PetscCall(DMCreateLocalVector(da, &local));
32: value = -3.0;
33: PetscCall(VecSet(global, value));
34: PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
35: PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
37: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
38: value = rank + 1;
39: PetscCall(VecScale(local, value));
40: PetscCall(DMLocalToGlobalBegin(da, local, ADD_VALUES, global));
41: PetscCall(DMLocalToGlobalEnd(da, local, ADD_VALUES, global));
43: PetscCall(VecView(global, PETSC_VIEWER_STDOUT_WORLD));
44: PetscCall(DMView(da, viewer));
46: /* Free memory */
47: PetscCall(PetscViewerDestroy(&viewer));
48: PetscCall(VecDestroy(&local));
49: PetscCall(VecDestroy(&global));
50: PetscCall(DMDestroy(&da));
51: PetscCall(PetscFinalize());
52: return 0;
53: }
55: /*TEST
57: test:
58: nsize: 2
59: args: -nox
60: filter: grep -v -i Object
62: test:
63: suffix: cuda1
64: requires: cuda
65: args: -dm_vec_type cuda -nox
66: filter: grep -v -i Object
68: test:
69: suffix: cuda2
70: nsize: 2
71: requires: cuda
72: args: -dm_vec_type cuda -nox
73: filter: grep -v -i Object
75: TEST*/