Date: 2022-03-31T04:46:29+00:00

Actual source code: ex16.c

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

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

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

     35:   /*
     36:       Create double-component vectors
     37:   */
     38:   VecCreate(PETSC_COMM_WORLD,&s);
     39:   VecSetSizes(s,PETSC_DECIDE,n/2);
     40:   VecSetBlockSize(s,2);
     41:   VecSetFromOptions(s);
     42:   VecDuplicate(s,&r);

     44:   vecs[0] = s;
     45:   vecs[1] = r;
     46:   /*
     47:      Set the vector values
     48:   */
     49:   VecGetOwnershipRange(v,&start,&end);
     50:   for (i=start; i<end; i++) {
     51:     value = i;
     52:     VecSetValues(v,1,&i,&value,INSERT_VALUES);
     53:   }

     55:   /*
     56:      Get the components from the multi-component vector to the other vectors
     57:   */
     58:   VecStrideGatherAll(v,vecs,INSERT_VALUES);

     60:   VecView(s,PETSC_VIEWER_STDOUT_WORLD);
     61:   VecView(r,PETSC_VIEWER_STDOUT_WORLD);

     63:   VecStrideScatterAll(vecs,v,ADD_VALUES);

     65:   VecView(v,PETSC_VIEWER_STDOUT_WORLD);

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

     78: /*TEST

     80:      test:
     81:        nsize: 2

     83: TEST*/