Actual source code: ex4.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  1: static char help[] = "Tests basic vector routines.\n\n";

  3: /*
  4:   Include "petscthreadcomm.h" so that we can use the PetscThreadComm interface.
  5: */
  6: #include <petscthreadcomm.h>
  7: #include <petscvec.h>

 11: int main(int argc,char **argv)
 12: {
 14:   PetscScalar    dot=0.0,v;
 15:   Vec            x,y;
 16:   PetscInt       N  =8;
 17:   PetscScalar    one=1.0,two=2.0,alpha=2.0;

 19:   PetscInitialize(&argc,&argv,(char*)0,help);

 21:   PetscThreadCommView(PETSC_COMM_WORLD,PETSC_VIEWER_STDOUT_WORLD);
 22:   PetscOptionsGetInt(NULL,"-N",&N,NULL);

 24:   VecCreate(PETSC_COMM_WORLD,&x);
 25:   VecSetSizes(x,PETSC_DECIDE,N);
 26:   VecSetFromOptions(x);
 27:   VecSet(x,one);
 28:   PetscPrintf(PETSC_COMM_WORLD,"x = %lf\n",PetscRealPart(one));

 30:   VecCreate(PETSC_COMM_WORLD,&y);
 31:   VecSetSizes(y,PETSC_DECIDE,N);
 32:   VecSetFromOptions(y);
 33:   VecSet(y,two);
 34:   PetscPrintf(PETSC_COMM_WORLD,"y = %lf\n",PetscRealPart(two));

 36:   VecAXPY(y,alpha,x);
 37:   v    = two+alpha*one;
 38:   PetscPrintf(PETSC_COMM_WORLD,"y+%lfx = %lf\n",alpha,PetscRealPart(v));

 40:   VecDot(x,y,&dot);

 42:   PetscThreadCommBarrier(PETSC_COMM_WORLD);

 44:   PetscPrintf(PETSC_COMM_WORLD,"Dot product %d*(%lf*%lf) is %lf\n",N,PetscRealPart(one),PetscRealPart(v),PetscRealPart(dot));
 45:   VecDestroy(&x);
 46:   VecDestroy(&y);
 47:   PetscFinalize();
 48:   return 0;
 49: }