Date: 2022-03-04T17:37:29+00:00

Actual source code: ex12.c

      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: }

     72: /*TEST

     74:      test:
     75:        nsize: 2

     77: TEST*/