Actual source code: ex12.c

petsc-3.8.4 2018-03-24
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>

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

 25:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 26:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

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

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

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

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

 53:   VecView(s,PETSC_VIEWER_STDOUT_WORLD);

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

 60:   VecView(v,PETSC_VIEWER_STDOUT_WORLD);

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