Date: 2022-03-31T04:46: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: {
     20:   Vec            v,s;               /* vectors */
     21:   PetscInt       n   = 20;
     22:   PetscScalar    one = 1.0;

     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,2);
     33:   VecSetFromOptions(v);

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

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

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

     52:   VecView(s,PETSC_VIEWER_STDOUT_WORLD);

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

     59:   VecView(v,PETSC_VIEWER_STDOUT_WORLD);

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

     71: /*TEST

     73:      test:
     74:        nsize: 2

     76: TEST*/