Actual source code: ex16.c


  2: static char help[] = "Demonstrates VecStrideScatter() and VecStrideGather() with subvectors that are also strided.\n\n";

  4: /*
  5:   Include "petscvec.h" so that we can use vectors.  Note that this file
  6:   automatically includes:
  7:      petscsys.h       - base PETSc routines   petscis.h     - index sets
  8:      petscviewer.h - viewers
  9: */

 11: #include <petscvec.h>

 13: int main(int argc, char **argv)
 14: {
 15:   Vec         v, s, r, vecs[2]; /* vectors */
 16:   PetscInt    i, start, end, n = 20;
 17:   PetscScalar value;

 20:   PetscInitialize(&argc, &argv, (char *)0, help);
 21:   PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL);

 23:   /*
 24:       Create multi-component vector with 2 components
 25:   */
 26:   VecCreate(PETSC_COMM_WORLD, &v);
 27:   VecSetSizes(v, PETSC_DECIDE, n);
 28:   VecSetBlockSize(v, 4);
 29:   VecSetFromOptions(v);

 31:   /*
 32:       Create double-component vectors
 33:   */
 34:   VecCreate(PETSC_COMM_WORLD, &s);
 35:   VecSetSizes(s, PETSC_DECIDE, n / 2);
 36:   VecSetBlockSize(s, 2);
 37:   VecSetFromOptions(s);
 38:   VecDuplicate(s, &r);

 40:   vecs[0] = s;
 41:   vecs[1] = r;
 42:   /*
 43:      Set the vector values
 44:   */
 45:   VecGetOwnershipRange(v, &start, &end);
 46:   for (i = start; i < end; i++) {
 47:     value = i;
 48:     VecSetValues(v, 1, &i, &value, INSERT_VALUES);
 49:   }

 51:   /*
 52:      Get the components from the multi-component vector to the other vectors
 53:   */
 54:   VecStrideGatherAll(v, vecs, INSERT_VALUES);

 56:   VecView(s, PETSC_VIEWER_STDOUT_WORLD);
 57:   VecView(r, PETSC_VIEWER_STDOUT_WORLD);

 59:   VecStrideScatterAll(vecs, v, ADD_VALUES);

 61:   VecView(v, PETSC_VIEWER_STDOUT_WORLD);

 63:   /*
 64:      Free work space.  All PETSc objects should be destroyed when they
 65:      are no longer needed.
 66:   */
 67:   VecDestroy(&v);
 68:   VecDestroy(&s);
 69:   VecDestroy(&r);
 70:   PetscFinalize();
 71:   return 0;
 72: }

 74: /*TEST

 76:      test:
 77:        nsize: 2

 79: TEST*/