Actual source code: ex36.c
petsc-3.7.5 2017-01-01
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>
20: int main(int argc,char **argv)
21: {
23: PetscMPIInt rank;
24: PetscInt i,istart,iend,n = 6,m,*indices;
25: PetscScalar *values;
26: Vec x;
27: PetscBool set_option_negidx = PETSC_FALSE, set_values_negidx = PETSC_FALSE, get_values_negidx = PETSC_FALSE;
29: PetscInitialize(&argc,&argv,(char*)0,help);
30: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
32: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
33: PetscOptionsGetBool(NULL,NULL, "-set_option_negidx", &set_option_negidx, NULL);
34: PetscOptionsGetBool(NULL,NULL, "-set_values_negidx", &set_values_negidx, NULL);
35: PetscOptionsGetBool(NULL,NULL, "-get_values_negidx", &get_values_negidx, NULL);
37: VecCreate(PETSC_COMM_WORLD,&x);
38: VecSetSizes(x,PETSC_DECIDE,n);
39: VecSetFromOptions(x);
41: /* If we want to use negative indices, set the option */
42: VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES,set_option_negidx);
44: VecGetOwnershipRange(x,&istart,&iend);
45: m = iend - istart;
48: /* Set the vectors */
50: PetscMalloc1(n,&values);
51: PetscMalloc1(n,&indices);
53: for (i=istart; i<iend; i++) {
54: values[i - istart] = (rank + 1) * i * 2;
55: if (set_values_negidx) indices[i - istart] = (-1 + 2*(i % 2)) * i;
56: else indices[i - istart] = i;
57: }
59: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Setting values...\n", rank);
60: for (i = 0; i<m; i++) {
61: PetscSynchronizedPrintf(PETSC_COMM_WORLD,
62: "%d: idx[%D] == %D; val[%D] == %f\n",
63: rank, i, indices[i], i, (double)PetscRealPart(values[i]));
64: }
65: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
67: VecSetValues(x, m, indices, values, INSERT_VALUES);
69: /*
70: Assemble vector.
71: */
73: VecAssemblyBegin(x);
74: VecAssemblyEnd(x);
76: /*
77: Extract values from the vector.
78: */
80: for (i=0; i<m; i++) {
81: values[i] = -1.0;
82: if (get_values_negidx) indices[i] = (-1 + 2*((istart+i) % 2)) * (istart+i);
83: else indices[i] = istart+i;
84: }
86: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetching these values from vector...\n", rank);
87: for (i=0; i<m; i++) {
88: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%D] == %D\n", rank, i, indices[i]);
89: }
90: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
92: VecGetValues(x, m, indices, values);
94: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetched values:\n", rank);
95: for (i = 0; i<m; i++) {
96: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%D] == %D; val[%D] == %f\n",
97: rank, i, indices[i], i, (double)PetscRealPart(values[i]));
98: }
99: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
101: /*
102: Free work space.
103: */
105: VecDestroy(&x);
106: PetscFree(values);
107: PetscFree(indices);
109: PetscFinalize();
111: return 0;
112: }