Actual source code: ex36.c
1: static char help[] = "Parallel vector layout.\n\n";
3: /*T
4: Concepts: vectors^setting values
5: Concepts: vectors^local access to
6: Concepts: vectors^drawing vectors;
7: Processors: n
8: T*/
10: /*
11: Include "petscvec.h" so that we can use vectors. Note that this file
12: automatically includes:
13: petsc.h - base PETSc routines petscis.h - index sets
14: petscsys.h - system routines petscviewer.h - viewers
15: */
16: #include petscvec.h
17: #include "stdlib.h"
21: int main(int argc,char **argv)
22: {
24: PetscMPIInt rank;
25: PetscInt i,istart,iend,n = 6,m,*indices;
26: PetscScalar *values;
27: Vec x;
28: PetscTruth set_option_negidx = PETSC_FALSE, set_values_negidx = PETSC_FALSE, get_values_negidx = PETSC_FALSE;
30: PetscInitialize(&argc,&argv,(char*)0,help);
31: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
33: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
34: PetscOptionsGetTruth(PETSC_NULL, "-set_option_negidx", &set_option_negidx, PETSC_NULL);
35: PetscOptionsGetTruth(PETSC_NULL, "-set_values_negidx", &set_values_negidx, PETSC_NULL);
36: PetscOptionsGetTruth(PETSC_NULL, "-get_values_negidx", &get_values_negidx, PETSC_NULL);
37:
38: VecCreate(PETSC_COMM_WORLD,&x);
39: VecSetSizes(x,PETSC_DECIDE,n);
40: VecSetFromOptions(x);
42: /* If we want to use negative indices, set the option */
43: if (set_option_negidx == PETSC_TRUE) {
44: VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES);
45: }
47: VecGetOwnershipRange(x,&istart,&iend);
48: m = iend - istart;
51: /* Set the vectors */
52:
53: PetscMalloc(n*sizeof(PetscScalar),&values);
54: PetscMalloc(n * sizeof(PetscInt),&indices);
56: for (i=istart; i<iend; i++) {
57: values[i - istart] = (rank + 1) * i * 2;
58: if (set_values_negidx == PETSC_TRUE) {
59: indices[i - istart] = (-1 + 2*(i % 2)) * i;
60: }
61: else {
62: indices[i - istart] = i;
63: }
64: }
66: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Setting values...\n", rank);
67: for (i = 0; i<m; i++) {
68: PetscSynchronizedPrintf(PETSC_COMM_WORLD,
69: "%d: idx[%d] == %d; val[%d] == %f\n",
70: rank, i, indices[i], i, PetscRealPart(values[i]));
71: }
72: PetscSynchronizedFlush(PETSC_COMM_WORLD);
74: VecSetValues(x, m, indices, values, INSERT_VALUES);
76: /*
77: Assemble vector.
78: */
79:
80: VecAssemblyBegin(x);
81: VecAssemblyEnd(x);
83: /*
84: Extract values from the vector.
85: */
86:
87: for (i=0; i<m; i++) {
88: values[i] = -1.0;
89: if (get_values_negidx) {
90: indices[i] = (-1 + 2*((istart+i) % 2)) * (istart+i);
91: }
92: else {
93: indices[i] = istart+i;
94: }
95: }
97: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetching these values from vector...\n", rank);
98: for (i=0; i<m; i++) {
99: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%d] == %d\n", rank, i, indices[i]);
100: }
101: PetscSynchronizedFlush(PETSC_COMM_WORLD);
103: VecGetValues(x, m, indices, values);
105: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetched values:\n", rank);
106: for (i = 0; i<m; i++) {
107: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%d] == %d; val[%d] == %f\n",
108: rank, i, indices[i], i, PetscRealPart(values[i]));
109: }
110: PetscSynchronizedFlush(PETSC_COMM_WORLD);
112: /*
113: Free work space.
114: */
116: VecDestroy(x);
117: PetscFree(values);
118: PetscFree(indices);
120: PetscFinalize();
122: return 0;
123: }
124: