Actual source code: ex13.c

      1: static char help[] = "Demonstrates VecStrideSum().\n\n";

      3: /*
      4:   Include "petscvec.h" so that we can use vectors.  Note that this file
      5:   automatically includes:
      6:      petscsys.h       - base PETSc routines   petscis.h     - index sets
      7:      petscviewer.h - viewers
      8: */

     10: #include <petscvec.h>

     12: int main(int argc, char **argv)
     13: {
     14:   Vec         x; /* vectors */
     15:   PetscScalar sum;
     16:   PetscInt    n   = 20;
     17:   PetscScalar one = 1.0;

     19:   PetscFunctionBeginUser;
     20:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
     21:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));

     23:   /*
     24:      Create a vector, specifying only its global dimension.
     25:      When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
     26:      the vector format (currently parallel,
     27:      shared, or sequential) is determined at runtime.  Also, the parallel
     28:      partitioning of the vector is determined by PETSc at runtime.
     29:   */
     30:   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
     31:   PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
     32:   PetscCall(VecSetBlockSize(x, 2));
     33:   PetscCall(VecSetFromOptions(x));

     35:   /*
     36:      Set the subvector entries to a constant value.
     37:   */
     38:   PetscCall(VecSet(x, one));
     39:   PetscCall(VecStrideScale(x, 1, -2));

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

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

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

     50:   /*
     51:      Free work space.  All PETSc objects should be destroyed when they
     52:      are no longer needed.
     53:   */
     54:   PetscCall(VecDestroy(&x));
     55:   PetscCall(PetscFinalize());
     56:   return 0;
     57: }

     59: /*TEST

     61:      test:
     62:        nsize: 2

     64: TEST*/