Actual source code: ex13.c

  1: /*$Id: ex13.c,v 1.15 2001/08/07 03:02:34 balay Exp $*/

  3: static char help[] = "Tests PetscObjectPublish().nn";

  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:      petsc.h       - base PETSc routines   petscis.h     - index sets
 14:      petscsys.h    - system routines       petscviewer.h - viewers
 15: */
 16:  #include petscvec.h

 18: #undef __FUNCT__
 20: int main(int argc,char **argv)
 21: {
 22:   int     i,n,ierr,rank;
 23:   PetscScalar  one = 1.0,*array;
 24:   Vec     x,xlocal;

 26:   PetscInitialize(&argc,&argv,(char *)0,help);
 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:   VecCreateMPI(PETSC_COMM_WORLD,rank+4,PETSC_DECIDE,&x);
 38:   PetscObjectPublish((PetscObject)x);
 39:   VecGetLocalSize(x,&n);
 40:   VecSet(&one,x);

 42:   VecCreateSeq(PETSC_COMM_SELF,rank+4,&xlocal);
 43:   PetscObjectPublish((PetscObject)xlocal);
 44:   VecSet(&one,xlocal);

 46:   while (1) {

 48:     /*
 49:        Access the vector entries and add to them
 50:     */
 51:     PetscBarrier((PetscObject)x);
 52:     VecGetArray(x,&array);
 53:     for (i=0; i<n; i++) {
 54:       array[i]++;
 55:     }
 56:     VecRestoreArray(x,&array);

 58:     VecGetArray(xlocal,&array);
 59:     for (i=0; i<n; i++) {
 60:       array[i]++;
 61:     }
 62:     VecRestoreArray(xlocal,&array);
 63:   }

 65:   /*
 66:         Destroy the vectors
 67:   */
 68:   VecDestroy(x);
 69:   VecDestroy(xlocal);

 71:   PetscFinalize();
 72:   return 0;
 73: }
 74: