Actual source code: ex12.c


  2: static char help[] = "Demonstrates VecStrideScatter() and VecStrideGather().\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; /* vectors */
 16:   PetscInt    n   = 20;
 17:   PetscScalar one = 1.0;

 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, 2);
 29:   VecSetFromOptions(v);

 31:   /*
 32:       Create single-component vector
 33:   */
 34:   VecCreate(PETSC_COMM_WORLD, &s);
 35:   VecSetSizes(s, PETSC_DECIDE, n / 2);
 36:   VecSetFromOptions(s);

 38:   /*
 39:      Set the vectors to entries to a constant value.
 40:   */
 41:   VecSet(v, one);

 43:   /*
 44:      Get the first component from the multi-component vector to the single vector
 45:   */
 46:   VecStrideGather(v, 0, s, INSERT_VALUES);

 48:   VecView(s, PETSC_VIEWER_STDOUT_WORLD);

 50:   /*
 51:      Put the values back into the second component
 52:   */
 53:   VecStrideScatter(s, 1, v, ADD_VALUES);

 55:   VecView(v, PETSC_VIEWER_STDOUT_WORLD);

 57:   /*
 58:      Free work space.  All PETSc objects should be destroyed when they
 59:      are no longer needed.
 60:   */
 61:   VecDestroy(&v);
 62:   VecDestroy(&s);
 63:   PetscFinalize();
 64:   return 0;
 65: }

 67: /*TEST

 69:      test:
 70:        nsize: 2

 72: TEST*/