Actual source code: ex3.c
1: static char help[] = "Parallel vector layout.\n\n";
3: /*
4: Include "petscvec.h" so that we can use vectors. Note that this file
5: automatically includes:
6: petscsys.h - base PETSc routines petscis.h - index sets
7: */
8: #include <petscvec.h>
10: int main(int argc, char **argv)
11: {
12: PetscMPIInt rank;
13: PetscInt i, istart, iend, n = 6, nlocal;
14: PetscScalar v, *array;
15: Vec x;
16: PetscViewer viewer;
18: PetscFunctionBeginUser;
19: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
20: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
22: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
24: /*
25: Create a vector, specifying only its global dimension.
26: When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
27: the vector format (currently parallel or sequential) is
28: determined at runtime. Also, the parallel partitioning of
29: the vector is determined by PETSc at runtime.
30: */
31: PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
32: PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
33: PetscCall(VecSetFromOptions(x));
35: /*
36: PETSc parallel vectors are partitioned by
37: contiguous chunks of rows across the processors. Determine
38: which vector are locally owned.
39: */
40: PetscCall(VecGetOwnershipRange(x, &istart, &iend));
42: /* --------------------------------------------------------------------
43: Set the vector elements.
44: - Always specify global locations of vector entries.
45: - Each processor can insert into any location, even ones it does not own
46: - In this case each processor adds values to all the entries,
47: this is not practical, but is merely done as an example
48: */
49: for (i = 0; i < n; i++) {
50: v = (PetscReal)(rank * i);
51: PetscCall(VecSetValues(x, 1, &i, &v, ADD_VALUES));
52: }
54: /*
55: Assemble vector, using the 2-step process:
56: VecAssemblyBegin(), VecAssemblyEnd()
57: Computations can be done while messages are in transition
58: by placing code between these two statements.
59: */
60: PetscCall(VecAssemblyBegin(x));
61: PetscCall(VecAssemblyEnd(x));
63: /*
64: Open an X-window viewer. Note that we specify the same communicator
65: for the viewer as we used for the distributed vector (PETSC_COMM_WORLD).
66: - Helpful runtime option:
67: -draw_pause <pause> : sets time (in seconds) that the
68: program pauses after PetscDrawPause() has been called
69: (0 is default, -1 implies until user input).
71: */
72: PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD, NULL, NULL, 0, 0, 300, 300, &viewer));
73: PetscCall(PetscObjectSetName((PetscObject)viewer, "Line graph Plot"));
74: PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_DRAW_LG));
75: /*
76: View the vector
77: */
78: PetscCall(VecView(x, viewer));
80: /* --------------------------------------------------------------------
81: Access the vector values directly. Each processor has access only
82: to its portion of the vector. For default PETSc vectors VecGetArray()
83: does NOT involve a copy
84: */
85: PetscCall(VecGetLocalSize(x, &nlocal));
86: PetscCall(VecGetArray(x, &array));
87: for (i = 0; i < nlocal; i++) array[i] = rank + 1;
88: PetscCall(VecRestoreArray(x, &array));
90: /*
91: View the vector
92: */
93: PetscCall(VecView(x, viewer));
95: /*
96: Free work space. All PETSc objects should be destroyed when they
97: are no longer needed.
98: */
99: PetscCall(PetscViewerPopFormat(viewer));
100: PetscCall(PetscViewerDestroy(&viewer));
101: PetscCall(VecDestroy(&x));
103: PetscCall(PetscFinalize());
104: return 0;
105: }
107: /*TEST
109: test:
110: nsize: 2
111: output_file: output/empty.out
113: TEST*/