Actual source code: ex8.c

petsc-3.7.5 2017-01-01
Report Typos and Errors
  2: static char help[] = "Demonstrates using a local ordering to set values into a parallel vector.\n\n";

  4: /*T
  5:    Concepts: vectors^assembling vectors with local ordering;
  6:    Processors: n
  7: T*/

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

 19: int main(int argc,char **argv)
 20: {
 22:   PetscMPIInt    rank;
 23:   PetscInt       i,N,ng,*gindices,rstart,rend,M;
 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:         PETSc could determine the vector's distribution if we specify
 35:         just the global size.
 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 local to global ordering for the vector. Each processor
 45:      generates a list of the global indices for each local index. Note that
 46:      the local indices are just whatever is convenient for a particular application.
 47:      In this case we treat the vector as lying on a one dimensional grid and
 48:      have one ghost point on each end of the blocks owned by each processor.
 49:   */

 51:   VecGetSize(x,&M);
 52:   VecGetOwnershipRange(x,&rstart,&rend);
 53:   ng   = rend - rstart + 2;
 54:   PetscMalloc1(ng,&gindices);
 55:   gindices[0] = rstart - 1;
 56:   for (i=0; i<ng-1; i++) gindices[i+1] = gindices[i] + 1;
 57:   /* map the first and last point as periodic */
 58:   if (gindices[0]    == -1) gindices[0]    = M - 1;
 59:   if (gindices[ng-1] == M)  gindices[ng-1] = 0;
 60:   {
 61:     ISLocalToGlobalMapping ltog;
 62:     ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,1,ng,gindices,PETSC_COPY_VALUES,&ltog);
 63:     VecSetLocalToGlobalMapping(x,ltog);
 64:     ISLocalToGlobalMappingDestroy(&ltog);
 65:   }
 66:   PetscFree(gindices);

 68:   /*
 69:      Set the vector elements.
 70:       - In this case set the values using the local ordering
 71:       - Each processor can contribute any vector entries,
 72:         regardless of which processor "owns" them; any nonlocal
 73:         contributions will be transferred to the appropriate processor
 74:         during the assembly process.
 75:       - In this example, the flag ADD_VALUES indicates that all
 76:         contributions will be added together.
 77:   */
 78:   for (i=0; i<ng; i++) {
 79:     VecSetValuesLocal(x,1,&i,&one,ADD_VALUES);
 80:   }

 82:   /*
 83:      Assemble vector, using the 2-step process:
 84:        VecAssemblyBegin(), VecAssemblyEnd()
 85:      Computations can be done while messages are in transition
 86:      by placing code between these two statements.
 87:   */
 88:   VecAssemblyBegin(x);
 89:   VecAssemblyEnd(x);

 91:   /*
 92:       View the vector; then destroy it.
 93:   */
 94:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);
 95:   VecDestroy(&x);

 97:   PetscFinalize();
 98:   return 0;
 99: }