Actual source code: ex2.c

  1: /*$Id: ex2.c,v 1.44 2001/09/07 20:09:05 bsmith Exp $*/

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

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

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

 19: #undef __FUNCT__
 21: int main(int argc,char **argv)
 22: {
 23:   int          i,N,ierr,rank;
 24:   PetscScalar  one = 1.0;
 25:   Vec          x;

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

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

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

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

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

 73:   PetscFinalize();
 74:   return 0;
 75: }
 76: