Actual source code: ex2.c


  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: /*
  6:   Include "petscvec.h" so that we can use vectors.  Note that this file
  7:   automatically includes:
  8:      petscsys.h       - base PETSc routines   petscis.h     - index sets
  9:      petscviewer.h - viewers
 10: */
 11: #include <petscvec.h>

 13: int main(int argc,char **argv)
 14: {
 15:   PetscMPIInt    rank;
 16:   PetscInt       i,N;
 17:   PetscScalar    one = 1.0;
 18:   Vec            x;

 20:   PetscInitialize(&argc,&argv,(char*)0,help);
 21:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

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

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

 51:   /*
 52:      Assemble vector, using the 2-step process:
 53:        VecAssemblyBegin(), VecAssemblyEnd()
 54:      Computations can be done while messages are in transition
 55:      by placing code between these two statements.
 56:   */
 57:   VecAssemblyBegin(x);
 58:   VecAssemblyEnd(x);

 60:   /*
 61:       View the vector; then destroy it.
 62:   */
 63:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);
 64:   VecDestroy(&x);

 66:   PetscFinalize();
 67:   return 0;
 68: }

 70: /*TEST

 72:      test:
 73:        nsize: 2

 75: TEST*/