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: petscsys.h - base PETSc routines petscis.h - index sets
14: 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: VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES,set_option_negidx);
45: VecGetOwnershipRange(x,&istart,&iend);
46: m = iend - istart;
49: /* Set the vectors */
50:
51: PetscMalloc(n*sizeof(PetscScalar),&values);
52: PetscMalloc(n*sizeof(PetscInt),&indices);
54: for (i=istart; i<iend; i++) {
55: values[i - istart] = (rank + 1) * i * 2;
56: if (set_values_negidx) {
57: indices[i - istart] = (-1 + 2*(i % 2)) * i;
58: }
59: else {
60: indices[i - istart] = i;
61: }
62: }
64: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Setting values...\n", rank);
65: for (i = 0; i<m; i++) {
66: PetscSynchronizedPrintf(PETSC_COMM_WORLD,
67: "%d: idx[%D] == %D; val[%D] == %f\n",
68: rank, i, indices[i], i, PetscRealPart(values[i]));
69: }
70: PetscSynchronizedFlush(PETSC_COMM_WORLD);
72: VecSetValues(x, m, indices, values, INSERT_VALUES);
74: /*
75: Assemble vector.
76: */
77:
78: VecAssemblyBegin(x);
79: VecAssemblyEnd(x);
81: /*
82: Extract values from the vector.
83: */
84:
85: for (i=0; i<m; i++) {
86: values[i] = -1.0;
87: if (get_values_negidx) {
88: indices[i] = (-1 + 2*((istart+i) % 2)) * (istart+i);
89: }
90: else {
91: indices[i] = istart+i;
92: }
93: }
95: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetching these values from vector...\n", rank);
96: for (i=0; i<m; i++) {
97: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%D] == %D\n", rank, i, indices[i]);
98: }
99: PetscSynchronizedFlush(PETSC_COMM_WORLD);
101: VecGetValues(x, m, indices, values);
103: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetched values:\n", rank);
104: for (i = 0; i<m; i++) {
105: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%D] == %D; val[%D] == %f\n",
106: rank, i, indices[i], i, PetscRealPart(values[i]));
107: }
108: PetscSynchronizedFlush(PETSC_COMM_WORLD);
110: /*
111: Free work space.
112: */
114: VecDestroy(x);
115: PetscFree(values);
116: PetscFree(indices);
118: PetscFinalize();
120: return 0;
121: }
122: