Actual source code: ex13.c


  2: static char help[] = "Demonstrates VecStrideSum().\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         x; /* vectors */
 16:   PetscScalar sum;
 17:   PetscInt    n   = 20;
 18:   PetscScalar one = 1.0;

 20:   PetscFunctionBeginUser;
 21:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
 22:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));

 24:   /*
 25:      Create a vector, specifying only its global dimension.
 26:      When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 27:      the vector format (currently parallel,
 28:      shared, or sequential) is determined at runtime.  Also, the parallel
 29:      partitioning of the vector is determined by PETSc at runtime.

 31:      Routines for creating particular vector types directly are:
 32:         VecCreateSeq() - uniprocessor vector
 33:         VecCreateMPI() - distributed vector, where the user can
 34:                          determine the parallel partitioning
 35:         VecCreateShared() - parallel vector that uses shared memory
 36:                             (available only on the SGI); otherwise,
 37:                             is the same as VecCreateMPI()

 39:      With VecCreate(), VecSetSizes() and VecSetFromOptions() the option
 40:      -vec_type mpi or -vec_type shared causes the
 41:      particular type of vector to be formed.

 43:   */
 44:   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
 45:   PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
 46:   PetscCall(VecSetBlockSize(x, 2));
 47:   PetscCall(VecSetFromOptions(x));

 49:   /*
 50:      Set the subvector entries to a constant value.
 51:   */
 52:   PetscCall(VecSet(x, one));
 53:   PetscCall(VecStrideScale(x, 1, -2));

 55:   PetscCall(VecSum(x, &sum));
 56:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Sum of entire vector: %g\n", (double)PetscRealPart(sum)));

 58:   PetscCall(VecStrideSum(x, 0, &sum));
 59:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Sum of sub-vector 0: %g\n", (double)PetscRealPart(sum)));

 61:   PetscCall(VecStrideSum(x, 1, &sum));
 62:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Sum of sub-vector 1: %g\n", (double)PetscRealPart(sum)));

 64:   /*
 65:      Free work space.  All PETSc objects should be destroyed when they
 66:      are no longer needed.
 67:   */
 68:   PetscCall(VecDestroy(&x));
 69:   PetscCall(PetscFinalize());
 70:   return 0;
 71: }

 73: /*TEST

 75:      test:
 76:        nsize: 2

 78: TEST*/