Date: 2022-08-02T03:13:36+00:00

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;

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

     22:   /*
     23:       Create multi-component vector with 2 components
     24:   */
     25:   VecCreate(PETSC_COMM_WORLD,&v);
     26:   VecSetSizes(v,PETSC_DECIDE,n);
     27:   VecSetBlockSize(v,2);
     28:   VecSetFromOptions(v);

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

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

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

     47:   VecView(s,PETSC_VIEWER_STDOUT_WORLD);

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

     54:   VecView(v,PETSC_VIEWER_STDOUT_WORLD);

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

     66: /*TEST

     68:      test:
     69:        nsize: 2

     71: TEST*/