Actual source code: ex36.c
petsc-3.12.2 2019-11-22
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>
18: int main(int argc,char **argv)
19: {
21: PetscMPIInt rank;
22: PetscInt i,istart,iend,n = 6,m,*indices;
23: PetscScalar *values;
24: Vec x;
25: PetscBool set_option_negidx = PETSC_FALSE, set_values_negidx = PETSC_FALSE, get_values_negidx = PETSC_FALSE;
27: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
28: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
30: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
31: PetscOptionsGetBool(NULL,NULL, "-set_option_negidx", &set_option_negidx, NULL);
32: PetscOptionsGetBool(NULL,NULL, "-set_values_negidx", &set_values_negidx, NULL);
33: PetscOptionsGetBool(NULL,NULL, "-get_values_negidx", &get_values_negidx, NULL);
35: VecCreate(PETSC_COMM_WORLD,&x);
36: VecSetSizes(x,PETSC_DECIDE,n);
37: VecSetFromOptions(x);
39: /* If we want to use negative indices, set the option */
40: VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES,set_option_negidx);
42: VecGetOwnershipRange(x,&istart,&iend);
43: m = iend - istart;
45: PetscMalloc1(n,&values);
46: PetscMalloc1(n,&indices);
48: for (i=istart; i<iend; i++) {
49: values[i - istart] = (rank + 1) * i * 2;
50: if (set_values_negidx) indices[i - istart] = (-1 + 2*(i % 2)) * i;
51: else indices[i - istart] = i;
52: }
54: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Setting values...\n", rank);
55: for (i = 0; i<m; i++) {
56: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"%d: idx[%D] == %D; val[%D] == %f\n",rank,i,indices[i],i,(double)PetscRealPart(values[i]));
57: }
58: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
60: VecSetValues(x, m, indices, values, INSERT_VALUES);
62: /*
63: Assemble vector.
64: */
66: VecAssemblyBegin(x);
67: VecAssemblyEnd(x);
69: /*
70: Extract values from the vector.
71: */
73: for (i=0; i<m; i++) {
74: values[i] = -1.0;
75: if (get_values_negidx) indices[i] = (-1 + 2*((istart+i) % 2)) * (istart+i);
76: else indices[i] = istart+i;
77: }
79: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetching these values from vector...\n", rank);
80: for (i=0; i<m; i++) {
81: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%D] == %D\n", rank, i, indices[i]);
82: }
83: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
85: VecGetValues(x, m, indices, values);
87: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetched values:\n", rank);
88: for (i = 0; i<m; i++) {
89: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%D] == %D; val[%D] == %f\n",rank,i,indices[i],i,(double)PetscRealPart(values[i]));
90: }
91: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
93: /*
94: Free work space.
95: */
97: VecDestroy(&x);
98: PetscFree(values);
99: PetscFree(indices);
101: PetscFinalize();
102: return ierr;
103: }
107: /*TEST
109: test:
110: nsize: 2
111: args: -set_option_negidx -set_values_negidx -get_values_negidx
113: TEST*/