Actual source code: ex3.c

  2: static char help[] = "Tests parallel vector assembly.  Input arguments are\n\
  3:   -n <length> : local vector length\n\n";

 5:  #include petscvec.h

  9: int main(int argc,char **argv)
 10: {
 11:   PetscMPIInt    size,rank;
 13:   PetscInt       n = 5,idx;
 14:   PetscScalar    one = 1.0,two = 2.0,three = 3.0;
 15:   Vec            x,y;

 17:   PetscInitialize(&argc,&argv,(char*)0,help);
 18:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 19:   if (n < 5) n = 5;
 20:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 21:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 23:   if (size < 2) SETERRQ(1,"Must be run with at least two processors");

 25:   /* create two vector */
 26:   VecCreateSeq(PETSC_COMM_SELF,n,&x);
 27:   VecCreate(PETSC_COMM_WORLD,&y);
 28:   VecSetSizes(y,n,PETSC_DECIDE);
 29:   VecSetFromOptions(y);
 30:   VecSet(x,one);
 31:   VecSet(y,two);

 33:   if (rank == 1) {
 34:     idx = 2; VecSetValues(y,1,&idx,&three,INSERT_VALUES);
 35:     idx = 0; VecSetValues(y,1,&idx,&two,INSERT_VALUES);
 36:     idx = 0; VecSetValues(y,1,&idx,&one,INSERT_VALUES);
 37:   }
 38:   else {
 39:     idx = 7; VecSetValues(y,1,&idx,&three,INSERT_VALUES);
 40:   }
 41:   VecAssemblyBegin(y);
 42:   VecAssemblyEnd(y);

 44:   VecView(y,PETSC_VIEWER_STDOUT_WORLD);

 46:   VecDestroy(x);
 47:   VecDestroy(y);

 49:   PetscFinalize();
 50:   return 0;
 51: }
 52: