Actual source code: ex12.c

petsc-3.3-p7 2013-05-11
  2: /* Program usage:  mpiexec ex1 [-help] [all PETSc options] */

  4: static char help[] = "Demonstrates VecStrideScatter() and VecStrideGather().\n\n";

  6: /*T
  7:    Concepts: vectors^sub-vectors;
  8:    Processors: n
  9: T*/

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

 18: #include <petscvec.h>

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

 29:   PetscInitialize(&argc,&argv,(char*)0,help);
 30:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);

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

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

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

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

 57:   VecView(s,PETSC_VIEWER_STDOUT_WORLD);

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

 64:   VecView(v,PETSC_VIEWER_STDOUT_WORLD);

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