Actual source code: ex36.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: petscviewer.h - viewers
8: */
9: #include <petscvec.h>
11: int main(int argc,char **argv)
12: {
13: PetscMPIInt rank;
14: PetscInt i,istart,iend,n = 6,m,*indices;
15: PetscScalar *values;
16: Vec x;
17: PetscBool set_option_negidx = PETSC_FALSE, set_values_negidx = PETSC_FALSE, get_values_negidx = PETSC_FALSE;
19: PetscInitialize(&argc,&argv,(char*)0,help);
20: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
22: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
23: PetscOptionsGetBool(NULL,NULL, "-set_option_negidx", &set_option_negidx, NULL);
24: PetscOptionsGetBool(NULL,NULL, "-set_values_negidx", &set_values_negidx, NULL);
25: PetscOptionsGetBool(NULL,NULL, "-get_values_negidx", &get_values_negidx, NULL);
27: VecCreate(PETSC_COMM_WORLD,&x);
28: VecSetSizes(x,PETSC_DECIDE,n);
29: VecSetFromOptions(x);
31: /* If we want to use negative indices, set the option */
32: VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES,set_option_negidx);
34: VecGetOwnershipRange(x,&istart,&iend);
35: m = iend - istart;
37: PetscMalloc1(n,&values);
38: PetscMalloc1(n,&indices);
40: for (i=istart; i<iend; i++) {
41: values[i - istart] = (rank + 1) * i * 2;
42: if (set_values_negidx) indices[i - istart] = (-1 + 2*(i % 2)) * i;
43: else indices[i - istart] = i;
44: }
46: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Setting values...\n", rank);
47: for (i = 0; i<m; i++) {
48: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"%d: idx[%" PetscInt_FMT "] == %" PetscInt_FMT "; val[%" PetscInt_FMT "] == %f\n",rank,i,indices[i],i,(double)PetscRealPart(values[i]));
49: }
50: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
52: VecSetValues(x, m, indices, values, INSERT_VALUES);
54: /*
55: Assemble vector.
56: */
58: VecAssemblyBegin(x);
59: VecAssemblyEnd(x);
61: /*
62: Extract values from the vector.
63: */
65: for (i=0; i<m; i++) {
66: values[i] = -1.0;
67: if (get_values_negidx) indices[i] = (-1 + 2*((istart+i) % 2)) * (istart+i);
68: else indices[i] = istart+i;
69: }
71: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetching these values from vector...\n", rank);
72: for (i=0; i<m; i++) {
73: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%" PetscInt_FMT "] == %" PetscInt_FMT "\n", rank, i, indices[i]);
74: }
75: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
77: VecGetValues(x, m, indices, values);
79: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetched values:\n", rank);
80: for (i = 0; i<m; i++) {
81: PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%" PetscInt_FMT "] == %" PetscInt_FMT "; val[%" PetscInt_FMT "] == %f\n",rank,i,indices[i],i,(double)PetscRealPart(values[i]));
82: }
83: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
85: /*
86: Free work space.
87: */
89: VecDestroy(&x);
90: PetscFree(values);
91: PetscFree(indices);
93: PetscFinalize();
94: return 0;
95: }
97: /*TEST
99: test:
100: nsize: 2
101: args: -set_option_negidx -set_values_negidx -get_values_negidx
103: TEST*/