Actual source code: ex2.c

petsc-3.10.5 2019-03-28
Report Typos and Errors

  2: static char help[] = "Builds a parallel vector with 1 component on the first processor, 2 on the second, etc.\n\
  3:   Then each processor adds one to all elements except the last rank.\n\n";

  5: /*T
  6:    Concepts: vectors^assembling vectors;
  7:    Processors: n
  8: T*/

 10: /*
 11:   Include "petscvec.h" so that we can use vectors.  Note that this file
 12:   automatically includes:
 13:      petscsys.h       - base PETSc routines   petscis.h     - index sets
 14:      petscviewer.h - viewers
 15: */
 16:  #include <petscvec.h>

 18: int main(int argc,char **argv)
 19: {
 21:   PetscMPIInt    rank;
 22:   PetscInt       i,N;
 23:   PetscScalar    one = 1.0;
 24:   Vec            x;

 26:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 27:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 29:   /*
 30:      Create a parallel vector.
 31:       - In this case, we specify the size of each processor's local
 32:         portion, and PETSc computes the global size.  Alternatively,
 33:         if we pass the global size and use PETSC_DECIDE for the
 34:         local size PETSc will choose a reasonable partition trying
 35:         to put nearly an equal number of elements on each processor.
 36:   */
 37:   VecCreate(PETSC_COMM_WORLD,&x);
 38:   VecSetSizes(x,rank+1,PETSC_DECIDE);
 39:   VecSetFromOptions(x);
 40:   VecGetSize(x,&N);
 41:   VecSet(x,one);

 43:   /*
 44:      Set the vector elements.
 45:       - Always specify global locations of vector entries.
 46:       - Each processor can contribute any vector entries,
 47:         regardless of which processor "owns" them; any nonlocal
 48:         contributions will be transferred to the appropriate processor
 49:         during the assembly process.
 50:       - In this example, the flag ADD_VALUES indicates that all
 51:         contributions will be added together.
 52:   */
 53:   for (i=0; i<N-rank; i++) {
 54:     VecSetValues(x,1,&i,&one,ADD_VALUES);
 55:   }

 57:   /*
 58:      Assemble vector, using the 2-step process:
 59:        VecAssemblyBegin(), VecAssemblyEnd()
 60:      Computations can be done while messages are in transition
 61:      by placing code between these two statements.
 62:   */
 63:   VecAssemblyBegin(x);
 64:   VecAssemblyEnd(x);

 66:   /*
 67:       View the vector; then destroy it.
 68:   */
 69:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);
 70:   VecDestroy(&x);

 72:   PetscFinalize();
 73:   return ierr;
 74: }

 76: /*TEST

 78:      test:
 79:        nsize: 2

 81: TEST*/