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

Actual source code: ex16.c

      2: static char help[] = "Demonstrates VecStrideScatter() and VecStrideGather() with subvectors that are also strided.\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,r,vecs[2];               /* vectors */
     16:   PetscInt       i,start,end,n = 20;
     17:   PetscScalar    value;

     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,4);
     28:   VecSetFromOptions(v);

     30:   /*
     31:       Create double-component vectors
     32:   */
     33:   VecCreate(PETSC_COMM_WORLD,&s);
     34:   VecSetSizes(s,PETSC_DECIDE,n/2);
     35:   VecSetBlockSize(s,2);
     36:   VecSetFromOptions(s);
     37:   VecDuplicate(s,&r);

     39:   vecs[0] = s;
     40:   vecs[1] = r;
     41:   /*
     42:      Set the vector values
     43:   */
     44:   VecGetOwnershipRange(v,&start,&end);
     45:   for (i=start; i<end; i++) {
     46:     value = i;
     47:     VecSetValues(v,1,&i,&value,INSERT_VALUES);
     48:   }

     50:   /*
     51:      Get the components from the multi-component vector to the other vectors
     52:   */
     53:   VecStrideGatherAll(v,vecs,INSERT_VALUES);

     55:   VecView(s,PETSC_VIEWER_STDOUT_WORLD);
     56:   VecView(r,PETSC_VIEWER_STDOUT_WORLD);

     58:   VecStrideScatterAll(vecs,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:   VecDestroy(&r);
     69:   PetscFinalize();
     70:   return 0;
     71: }

     73: /*TEST

     75:      test:
     76:        nsize: 2

     78: TEST*/