Actual source code: ex1.c
2: static char help[] = "Basic vector routines.\n\n";
4: /*T
5: Concepts: vectors^basic routines;
6: Processors: n
7: T*/
9: /*
10: Include "petscvec.h" so that we can use vectors. Note that this file
11: automatically includes:
12: petscsys.h - base PETSc routines petscis.h - index sets
13: petscviewer.h - viewers
14: */
16: #include <petscvec.h>
18: int main(int argc,char **argv)
19: {
20: Vec x,y,w; /* vectors */
21: Vec *z; /* array of vectors */
22: PetscReal norm,v,v1,v2,maxval;
23: PetscInt n = 20,maxind;
25: PetscScalar one = 1.0,two = 2.0,three = 3.0,dots[3],dot;
27: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
28: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
30: /*
31: Create a vector, specifying only its global dimension.
32: When using VecCreate(), VecSetSizes() and VecSetFromOptions(), the vector format
33: (currently parallel, shared, or sequential) is determined at runtime. Also, the
34: parallel partitioning of the vector is determined by PETSc at runtime.
36: Routines for creating particular vector types directly are:
37: VecCreateSeq() - uniprocessor vector
38: VecCreateMPI() - distributed vector, where the user can
39: determine the parallel partitioning
40: VecCreateShared() - parallel vector that uses shared memory
41: (available only on the SGI); otherwise,
42: is the same as VecCreateMPI()
44: With VecCreate(), VecSetSizes() and VecSetFromOptions() the option -vec_type mpi or
45: -vec_type shared causes the particular type of vector to be formed.
47: */
48: VecCreate(PETSC_COMM_WORLD,&x);
49: VecSetSizes(x,PETSC_DECIDE,n);
50: VecSetFromOptions(x);
52: /*
53: Duplicate some work vectors (of the same format and
54: partitioning as the initial vector).
55: */
56: VecDuplicate(x,&y);
57: VecDuplicate(x,&w);
59: /*
60: Duplicate more work vectors (of the same format and
61: partitioning as the initial vector). Here we duplicate
62: an array of vectors, which is often more convenient than
63: duplicating individual ones.
64: */
65: VecDuplicateVecs(x,3,&z);
66: /*
67: Set the vectors to entries to a constant value.
68: */
69: VecSet(x,one);
70: VecSet(y,two);
71: VecSet(z[0],one);
72: VecSet(z[1],two);
73: VecSet(z[2],three);
74: /*
75: Demonstrate various basic vector routines.
76: */
77: VecDot(x,y,&dot);
78: VecMDot(x,3,z,dots);
80: /*
81: Note: If using a complex numbers version of PETSc, then
82: PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
83: (when using real numbers) it is undefined.
84: */
86: PetscPrintf(PETSC_COMM_WORLD,"Vector length %D\n",n);
87: VecMax(x,&maxind,&maxval);
88: PetscPrintf(PETSC_COMM_WORLD,"VecMax %g, VecInd %D\n",(double)maxval,maxind);
90: VecMin(x,&maxind,&maxval);
91: PetscPrintf(PETSC_COMM_WORLD,"VecMin %g, VecInd %D\n",(double)maxval,maxind);
92: PetscPrintf(PETSC_COMM_WORLD,"All other values should be near zero\n");
94: VecScale(x,two);
95: VecNorm(x,NORM_2,&norm);
96: v = norm-2.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
97: PetscPrintf(PETSC_COMM_WORLD,"VecScale %g\n",(double)v);
99: VecCopy(x,w);
100: VecNorm(w,NORM_2,&norm);
101: v = norm-2.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
102: PetscPrintf(PETSC_COMM_WORLD,"VecCopy %g\n",(double)v);
104: VecAXPY(y,three,x);
105: VecNorm(y,NORM_2,&norm);
106: v = norm-8.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
107: PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %g\n",(double)v);
109: VecAYPX(y,two,x);
110: VecNorm(y,NORM_2,&norm);
111: v = norm-18.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
112: PetscPrintf(PETSC_COMM_WORLD,"VecAYPX %g\n",(double)v);
114: VecSwap(x,y);
115: VecNorm(y,NORM_2,&norm);
116: v = norm-2.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
117: PetscPrintf(PETSC_COMM_WORLD,"VecSwap %g\n",(double)v);
118: VecNorm(x,NORM_2,&norm);
119: v = norm-18.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
120: PetscPrintf(PETSC_COMM_WORLD,"VecSwap %g\n",(double)v);
122: VecWAXPY(w,two,x,y);
123: VecNorm(w,NORM_2,&norm);
124: v = norm-38.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
125: PetscPrintf(PETSC_COMM_WORLD,"VecWAXPY %g\n",(double)v);
127: VecPointwiseMult(w,y,x);
128: VecNorm(w,NORM_2,&norm);
129: v = norm-36.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
130: PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseMult %g\n",(double)v);
132: VecPointwiseDivide(w,x,y);
133: VecNorm(w,NORM_2,&norm);
134: v = norm-9.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
135: PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseDivide %g\n",(double)v);
137: dots[0] = one;
138: dots[1] = three;
139: dots[2] = two;
141: VecSet(x,one);
142: VecMAXPY(x,3,dots,z);
143: VecNorm(z[0],NORM_2,&norm);
144: v = norm-PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
145: VecNorm(z[1],NORM_2,&norm);
146: v1 = norm-2.0*PetscSqrtReal((PetscReal)n); if (v1 > -PETSC_SMALL && v1 < PETSC_SMALL) v1 = 0.0;
147: VecNorm(z[2],NORM_2,&norm);
148: v2 = norm-3.0*PetscSqrtReal((PetscReal)n); if (v2 > -PETSC_SMALL && v2 < PETSC_SMALL) v2 = 0.0;
149: PetscPrintf(PETSC_COMM_WORLD,"VecMAXPY %g %g %g \n",(double)v,(double)v1,(double)v2);
151: /*
152: Free work space. All PETSc objects should be destroyed when they
153: are no longer needed.
154: */
155: VecDestroy(&x);
156: VecDestroy(&y);
157: VecDestroy(&w);
158: VecDestroyVecs(3,&z);
159: PetscFinalize();
160: return ierr;
161: }
163: /*TEST
165: testset:
166: output_file: output/ex1_1.out
167: # This is a test where the exact numbers are critical
168: diff_args: -j
170: test:
172: test:
173: suffix: cuda
174: args: -vec_type cuda
175: requires: cuda
177: test:
178: suffix: kokkos
179: args: -vec_type kokkos
180: requires: kokkos_kernels
182: test:
183: suffix: hip
184: args: -vec_type hip
185: requires: hip
187: test:
188: suffix: 2
189: nsize: 2
191: test:
192: suffix: 2_cuda
193: nsize: 2
194: args: -vec_type cuda
195: requires: cuda
197: test:
198: suffix: 2_kokkos
199: nsize: 2
200: args: -vec_type kokkos
201: requires: kokkos_kernels
203: test:
204: suffix: 2_hip
205: nsize: 2
206: args: -vec_type hip
207: requires: hip
209: TEST*/