Actual source code: ex1.c
petsc-3.3-p7 2013-05-11
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: PetscPrintf(PETSC_COMM_WORLD,"Vector length %D\n",n);
92: VecMax(x,&maxind,&maxval);
93: PetscPrintf(PETSC_COMM_WORLD,"VecMax %g, VecInd %D\n",(double)maxval,maxind);
95: VecMin(x,&maxind,&maxval);
96: PetscPrintf(PETSC_COMM_WORLD,"VecMin %g, VecInd %D\n",(double)maxval,maxind);
97: PetscPrintf(PETSC_COMM_WORLD,"All other values should be near zero\n");
100: VecScale(x,two);
101: VecNorm(x,NORM_2,&norm);
102: v = norm-2.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
103: PetscPrintf(PETSC_COMM_WORLD,"VecScale %g\n",(double)v);
106: VecCopy(x,w);
107: VecNorm(w,NORM_2,&norm);
108: v = norm-2.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
109: PetscPrintf(PETSC_COMM_WORLD,"VecCopy %g\n",(double)v);
111: VecAXPY(y,three,x);
112: VecNorm(y,NORM_2,&norm);
113: v = norm-8.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
114: PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %g\n",(double)v);
116: VecAYPX(y,two,x);
117: VecNorm(y,NORM_2,&norm);
118: v = norm-18.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
119: PetscPrintf(PETSC_COMM_WORLD,"VecAYPX %g\n",(double)v);
121: VecSwap(x,y);
122: VecNorm(y,NORM_2,&norm);
123: v = norm-2.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
124: PetscPrintf(PETSC_COMM_WORLD,"VecSwap %g\n",(double)v);
125: VecNorm(x,NORM_2,&norm);
126: v = norm-18.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
127: PetscPrintf(PETSC_COMM_WORLD,"VecSwap %g\n",(double)v);
129: VecWAXPY(w,two,x,y);
130: VecNorm(w,NORM_2,&norm);
131: v = norm-38.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
132: PetscPrintf(PETSC_COMM_WORLD,"VecWAXPY %g\n",(double)v);
134: VecPointwiseMult(w,y,x);
135: VecNorm(w,NORM_2,&norm);
136: v = norm-36.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
137: PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseMult %g\n",(double)v);
139: VecPointwiseDivide(w,x,y);
140: VecNorm(w,NORM_2,&norm);
141: v = norm-9.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
142: PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseDivide %g\n",(double)v);
144: dots[0] = one;
145: dots[1] = three;
146: dots[2] = two;
147: VecSet(x,one);
148: VecMAXPY(x,3,dots,z);
149: VecNorm(z[0],NORM_2,&norm);
150: v = norm-sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
151: VecNorm(z[1],NORM_2,&norm);
152: v1 = norm-2.0*sqrt((double)n); if (v1 > -PETSC_SMALL && v1 < PETSC_SMALL) v1 = 0.0;
153: VecNorm(z[2],NORM_2,&norm);
154: v2 = norm-3.0*sqrt((double)n); if (v2 > -PETSC_SMALL && v2 < PETSC_SMALL) v2 = 0.0;
155: PetscPrintf(PETSC_COMM_WORLD,"VecMAXPY %g %g %g \n",(double)v,(double)v1,(double)v2);
157: /*
158: Free work space. All PETSc objects should be destroyed when they
159: are no longer needed.
160: */
161: VecDestroy(&x);
162: VecDestroy(&y);
163: VecDestroy(&w);
164: VecDestroyVecs(3,&z);
165: PetscFinalize();
166: return 0;
167: }
168: