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>

 18: int main(int argc,char **argv)
 19: {
 20:   PetscMPIInt    rank;
 21:   PetscInt       i,istart,iend,n = 6,m,*indices;
 22:   PetscScalar    *values;
 23:   Vec            x;
 24:   PetscBool      set_option_negidx = PETSC_FALSE, set_values_negidx = PETSC_FALSE, get_values_negidx = PETSC_FALSE;

 26:   PetscInitialize(&argc,&argv,(char*)0,help);
 27:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 29:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 30:   PetscOptionsGetBool(NULL,NULL, "-set_option_negidx", &set_option_negidx, NULL);
 31:   PetscOptionsGetBool(NULL,NULL, "-set_values_negidx", &set_values_negidx, NULL);
 32:   PetscOptionsGetBool(NULL,NULL, "-get_values_negidx", &get_values_negidx, NULL);

 34:   VecCreate(PETSC_COMM_WORLD,&x);
 35:   VecSetSizes(x,PETSC_DECIDE,n);
 36:   VecSetFromOptions(x);

 38:   /* If we want to use negative indices, set the option */
 39:   VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES,set_option_negidx);

 41:   VecGetOwnershipRange(x,&istart,&iend);
 42:   m    = iend - istart;

 44:   PetscMalloc1(n,&values);
 45:   PetscMalloc1(n,&indices);

 47:   for (i=istart; i<iend; i++) {
 48:     values[i - istart] = (rank + 1) * i * 2;
 49:     if (set_values_negidx) indices[i - istart] = (-1 + 2*(i % 2)) * i;
 50:     else                   indices[i - istart] = i;
 51:   }

 53:   PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Setting values...\n", rank);
 54:   for (i = 0; i<m; i++) {
 55:     PetscSynchronizedPrintf(PETSC_COMM_WORLD,"%d: idx[%" PetscInt_FMT "] == %" PetscInt_FMT "; val[%" PetscInt_FMT "] == %f\n",rank,i,indices[i],i,(double)PetscRealPart(values[i]));
 56:   }
 57:   PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);

 59:   VecSetValues(x, m, indices, values, INSERT_VALUES);

 61:   /*
 62:      Assemble vector.
 63:   */

 65:   VecAssemblyBegin(x);
 66:   VecAssemblyEnd(x);

 68:   /*
 69:      Extract values from the vector.
 70:   */

 72:   for (i=0; i<m; i++) {
 73:     values[i] = -1.0;
 74:     if (get_values_negidx) indices[i] = (-1 + 2*((istart+i) % 2)) * (istart+i);
 75:     else                   indices[i] = istart+i;
 76:   }

 78:   PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetching these values from vector...\n", rank);
 79:   for (i=0; i<m; i++) {
 80:     PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%" PetscInt_FMT "] == %" PetscInt_FMT "\n", rank, i, indices[i]);
 81:   }
 82:   PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);

 84:   VecGetValues(x, m, indices, values);

 86:   PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: Fetched values:\n", rank);
 87:   for (i = 0; i<m; i++) {
 88:     PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%d: idx[%" PetscInt_FMT "] == %" PetscInt_FMT "; val[%" PetscInt_FMT "] == %f\n",rank,i,indices[i],i,(double)PetscRealPart(values[i]));
 89:   }
 90:   PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);

 92:   /*
 93:      Free work space.
 94:   */

 96:   VecDestroy(&x);
 97:   PetscFree(values);
 98:   PetscFree(indices);

100:   PetscFinalize();
101:   return 0;
102: }

104: /*TEST

106:    test:
107:       nsize: 2
108:       args: -set_option_negidx -set_values_negidx -get_values_negidx

110: TEST*/