Actual source code: ex3.c

petsc-3.12.2 2019-11-22
Report Typos and Errors

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

  5:  #include <petscvec.h>

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

 15:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 16:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 17:   if (n < 5) n = 5;
 18:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 19:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

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

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

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

 41:   VecView(y,PETSC_VIEWER_STDOUT_WORLD);

 43:   VecDestroy(&x);
 44:   VecDestroy(&y);

 46:   PetscFinalize();
 47:   return ierr;
 48: }

 50: /*TEST

 52:      test:
 53:        nsize: 2

 55:      test:
 56:        suffix: 2
 57:        nsize: 2
 58:        args: -vec_view ascii::ascii_info

 60: TEST*/