Actual source code: ex12.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  2: static char help[] = "Demonstrates VecStrideScatter() and VecStrideGather().\n\n";

  4: /*T
  5:    Concepts: vectors^sub-vectors;
  6:    Processors: n
  7: T*/

  9: /*
 10:   Include "petscvec.h" so that we can use vectors.  Note that this file
 11:   automatically includes:
 12:      petscsys.h       - base PETSc routines   petscis.h     - index sets
 13:      petscviewer.h - viewers
 14: */

 16: #include <petscvec.h>

 20: int main(int argc,char **argv)
 21: {
 23:   Vec            v,s;               /* vectors */
 24:   PetscInt       n   = 20;
 25:   PetscScalar    one = 1.0;

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

 30:   /*
 31:       Create multi-component vector with 2 components
 32:   */
 33:   VecCreate(PETSC_COMM_WORLD,&v);
 34:   VecSetSizes(v,PETSC_DECIDE,n);
 35:   VecSetBlockSize(v,2);
 36:   VecSetFromOptions(v);

 38:   /*
 39:       Create single-component vector
 40:   */
 41:   VecCreate(PETSC_COMM_WORLD,&s);
 42:   VecSetSizes(s,PETSC_DECIDE,n/2);
 43:   VecSetFromOptions(s);

 45:   /*
 46:      Set the vectors to entries to a constant value.
 47:   */
 48:   VecSet(v,one);

 50:   /*
 51:      Get the first component from the multi-component vector to the single vector
 52:   */
 53:   VecStrideGather(v,0,s,INSERT_VALUES);

 55:   VecView(s,PETSC_VIEWER_STDOUT_WORLD);

 57:   /*
 58:      Put the values back into the second component
 59:   */
 60:   VecStrideScatter(s,1,v,ADD_VALUES);

 62:   VecView(v,PETSC_VIEWER_STDOUT_WORLD);

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