Actual source code: ex1.c

  1: /* Program usage:  mpiexec ex1 [-help] [all PETSc options] */

  3: static char help[] = "Basic vector routines.\n\n";

  5: /*T
  6:    Concepts: vectors^basic routines;
  7:    Processors: n
  8: T*/

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

 17: #include <petscvec.h>

 21: int main(int argc,char **argv)
 22: {
 23:   Vec            x,y,w;               /* vectors */
 24:   Vec            *z;                    /* array of vectors */
 25:   PetscReal      norm,v,v1,v2,maxval;
 26:   PetscInt       n = 20,maxind;
 28:   PetscScalar    one = 1.0,two = 2.0,three = 3.0,dots[3],dot;

 30:   PetscInitialize(&argc,&argv,(char*)0,help);
 31:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);

 33:   /* 
 34:      Create a vector, specifying only its global dimension.
 35:      When using VecCreate(), VecSetSizes() and VecSetFromOptions(), the vector format 
 36:      (currently parallel, shared, or sequential) is determined at runtime.  Also, the 
 37:      parallel partitioning of the vector is determined by PETSc at runtime.

 39:      Routines for creating particular vector types directly are:
 40:         VecCreateSeq() - uniprocessor vector
 41:         VecCreateMPI() - distributed vector, where the user can
 42:                          determine the parallel partitioning
 43:         VecCreateShared() - parallel vector that uses shared memory
 44:                             (available only on the SGI); otherwise,
 45:                             is the same as VecCreateMPI()

 47:      With VecCreate(), VecSetSizes() and VecSetFromOptions() the option -vec_type mpi or 
 48:      -vec_type shared causes the particular type of vector to be formed.
 49: y

 51:   */

 53:   VecCreate(PETSC_COMM_WORLD,&x);
 54:   VecSetSizes(x,PETSC_DECIDE,n);
 55:   VecSetFromOptions(x);
 56:   /*
 57:      Duplicate some work vectors (of the same format and
 58:      partitioning as the initial vector).
 59:   */
 60:   VecDuplicate(x,&y);
 61:   VecDuplicate(x,&w);

 63:   /*
 64:      Duplicate more work vectors (of the same format and
 65:      partitioning as the initial vector).  Here we duplicate
 66:      an array of vectors, which is often more convenient than
 67:      duplicating individual ones.
 68:   */
 69:   VecDuplicateVecs(x,3,&z);
 70:   /*
 71:      Set the vectors to entries to a constant value.
 72:   */
 73:   VecSet(x,one);
 74:   VecSet(y,two);
 75:   VecSet(z[0],one);
 76:   VecSet(z[1],two);
 77:   VecSet(z[2],three);
 78:   /*
 79:      Demonstrate various basic vector routines.
 80:   */
 81:   MPI_Barrier(PETSC_COMM_WORLD);
 82:   VecDot(x,y,&dot);
 83:   VecMDot(x,3,z,dots);

 85:   /* 
 86:      Note: If using a complex numbers version of PETSc, then
 87:      PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
 88:      (when using real numbers) it is undefined.
 89:   */

 91: #if defined(PETSC_USE_COMPLEX)
 92:   PetscPrintf(PETSC_COMM_WORLD,"Vector length %D\n",(PetscInt) (PetscRealPart(dot)));
 93:   PetscPrintf(PETSC_COMM_WORLD,"Vector length %D %D %D\n",(PetscInt)PetscRealPart(dots[0]),
 94:                              (PetscInt)PetscRealPart(dots[1]),(PetscInt)PetscRealPart(dots[2]));
 95: #else
 96:   PetscPrintf(PETSC_COMM_WORLD,"Vector length %D\n",n);
 97:   /*  PetscPrintf(PETSC_COMM_WORLD,"Vector length %D %D %D\n",(PetscInt)dots[0],
 98:                              (PetscInt)dots[1],(PetscInt)dots[2]);
 99:   */
100: #endif
101:   VecMax(x,&maxind,&maxval);
102:   PetscPrintf(PETSC_COMM_WORLD,"VecMax %g, VecInd %D\n",(double)maxval,maxind);

104:   VecMin(x,&maxind,&maxval);
105:   PetscPrintf(PETSC_COMM_WORLD,"VecMin %g, VecInd %D\n",(double)maxval,maxind);
106:   PetscPrintf(PETSC_COMM_WORLD,"All other values should be near zero\n");


109:   VecScale(x,two);
110:   VecNorm(x,NORM_2,&norm);
111:   v = norm-2.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
112:   PetscPrintf(PETSC_COMM_WORLD,"VecScale %g\n",(double)v);


115:   VecCopy(x,w);
116:   VecNorm(w,NORM_2,&norm);
117:   v = norm-2.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
118:   PetscPrintf(PETSC_COMM_WORLD,"VecCopy  %g\n",(double)v);

120:   VecAXPY(y,three,x);
121:   VecNorm(y,NORM_2,&norm);
122:   v = norm-8.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
123:   PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %g\n",(double)v);

125:   VecAYPX(y,two,x);
126:   VecNorm(y,NORM_2,&norm);
127:   v = norm-18.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
128:   PetscPrintf(PETSC_COMM_WORLD,"VecAYPX %g\n",(double)v);

130:   VecSwap(x,y);
131:   VecNorm(y,NORM_2,&norm);
132:   v = norm-2.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
133:   PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %g\n",(double)v);
134:   VecNorm(x,NORM_2,&norm);
135:   v = norm-18.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
136:   PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %g\n",(double)v);

138:   VecWAXPY(w,two,x,y);
139:   VecNorm(w,NORM_2,&norm);
140:   v = norm-38.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
141:   PetscPrintf(PETSC_COMM_WORLD,"VecWAXPY %g\n",(double)v);

143:   VecPointwiseMult(w,y,x);
144:   VecNorm(w,NORM_2,&norm);
145:   v = norm-36.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
146:   PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseMult %g\n",(double)v);

148:   VecPointwiseDivide(w,x,y);
149:   VecNorm(w,NORM_2,&norm);
150:   v = norm-9.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
151:   PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseDivide %g\n",(double)v);

153:   dots[0] = one;
154:   dots[1] = three;
155:   dots[2] = two;
156:   VecSet(x,one);
157:   VecMAXPY(x,3,dots,z);
158:   VecNorm(z[0],NORM_2,&norm);
159:   v = norm-sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
160:   VecNorm(z[1],NORM_2,&norm);
161:   v1 = norm-2.0*sqrt((double)n); if (v1 > -PETSC_SMALL && v1 < PETSC_SMALL) v1 = 0.0;
162:   VecNorm(z[2],NORM_2,&norm);
163:   v2 = norm-3.0*sqrt((double)n); if (v2 > -PETSC_SMALL && v2 < PETSC_SMALL) v2 = 0.0;
164:   PetscPrintf(PETSC_COMM_WORLD,"VecMAXPY %g %g %g \n",(double)v,(double)v1,(double)v2);

166:   /* 
167:      Free work space.  All PETSc objects should be destroyed when they
168:      are no longer needed.
169:   */
170:   VecDestroy(&x);
171:   VecDestroy(&y);
172:   VecDestroy(&w);
173:   VecDestroyVecs(3,&z);
174:   PetscFinalize();
175:   return 0;
176: }
177: