1 /*
2 Provides the interface functions for vector operations that do NOT have PetscScalar/PetscReal in the signature
3 These are the vector functions the user calls.
4 */
5 #include <petsc/private/vecimpl.h> /*I "petscvec.h" I*/
6
7 /* Logging support */
8 PetscClassId VEC_CLASSID;
9 PetscLogEvent VEC_View, VEC_Max, VEC_Min, VEC_Dot, VEC_MDot, VEC_TDot;
10 PetscLogEvent VEC_Norm, VEC_Normalize, VEC_Scale, VEC_Copy, VEC_Set, VEC_AXPY, VEC_AYPX, VEC_WAXPY;
11 PetscLogEvent VEC_MTDot, VEC_MAXPY, VEC_Swap, VEC_AssemblyBegin, VEC_ScatterBegin, VEC_ScatterEnd;
12 PetscLogEvent VEC_AssemblyEnd, VEC_PointwiseMult, VEC_SetValues, VEC_Load;
13 PetscLogEvent VEC_SetRandom, VEC_ReduceArithmetic, VEC_ReduceCommunication,VEC_ReduceBegin,VEC_ReduceEnd,VEC_Ops;
14 PetscLogEvent VEC_DotNorm2, VEC_AXPBYPCZ;
15 PetscLogEvent VEC_ViennaCLCopyFromGPU, VEC_ViennaCLCopyToGPU;
16 PetscLogEvent VEC_CUDACopyFromGPU, VEC_CUDACopyToGPU;
17 PetscLogEvent VEC_CUDACopyFromGPUSome, VEC_CUDACopyToGPUSome;
18
19 /*@
20 VecStashGetInfo - Gets how many values are currently in the vector stash, i.e. need
21 to be communicated to other processors during the VecAssemblyBegin/End() process
22
23 Not collective
24
25 Input Parameter:
26 . vec - the vector
27
28 Output Parameters:
29 + nstash - the size of the stash
30 . reallocs - the number of additional mallocs incurred.
31 . bnstash - the size of the block stash
32 - breallocs - the number of additional mallocs incurred.in the block stash
33
34 Level: advanced
35
36 .seealso: VecAssemblyBegin(), VecAssemblyEnd(), Vec, VecStashSetInitialSize(), VecStashView()
37
38 @*/
VecStashGetInfo(Vec vec,PetscInt * nstash,PetscInt * reallocs,PetscInt * bnstash,PetscInt * breallocs)39 PetscErrorCode VecStashGetInfo(Vec vec,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
40 {
41 PetscErrorCode ierr;
42
43 PetscFunctionBegin;
44 ierr = VecStashGetInfo_Private(&vec->stash,nstash,reallocs);CHKERRQ(ierr);
45 ierr = VecStashGetInfo_Private(&vec->bstash,bnstash,breallocs);CHKERRQ(ierr);
46 PetscFunctionReturn(0);
47 }
48
49 /*@
50 VecSetLocalToGlobalMapping - Sets a local numbering to global numbering used
51 by the routine VecSetValuesLocal() to allow users to insert vector entries
52 using a local (per-processor) numbering.
53
54 Logically Collective on Vec
55
56 Input Parameters:
57 + x - vector
58 - mapping - mapping created with ISLocalToGlobalMappingCreate() or ISLocalToGlobalMappingCreateIS()
59
60 Notes:
61 All vectors obtained with VecDuplicate() from this vector inherit the same mapping.
62
63 Level: intermediate
64
65 seealso: VecAssemblyBegin(), VecAssemblyEnd(), VecSetValues(), VecSetValuesLocal(),
66 VecSetLocalToGlobalMapping(), VecSetValuesBlockedLocal()
67 @*/
VecSetLocalToGlobalMapping(Vec x,ISLocalToGlobalMapping mapping)68 PetscErrorCode VecSetLocalToGlobalMapping(Vec x,ISLocalToGlobalMapping mapping)
69 {
70 PetscErrorCode ierr;
71
72 PetscFunctionBegin;
73 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
74 PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,2);
75
76 if (x->ops->setlocaltoglobalmapping) {
77 ierr = (*x->ops->setlocaltoglobalmapping)(x,mapping);CHKERRQ(ierr);
78 } else {
79 ierr = PetscLayoutSetISLocalToGlobalMapping(x->map,mapping);CHKERRQ(ierr);
80 }
81 PetscFunctionReturn(0);
82 }
83
84 /*@
85 VecGetLocalToGlobalMapping - Gets the local-to-global numbering set by VecSetLocalToGlobalMapping()
86
87 Not Collective
88
89 Input Parameter:
90 . X - the vector
91
92 Output Parameter:
93 . mapping - the mapping
94
95 Level: advanced
96
97
98 .seealso: VecSetValuesLocal()
99 @*/
VecGetLocalToGlobalMapping(Vec X,ISLocalToGlobalMapping * mapping)100 PetscErrorCode VecGetLocalToGlobalMapping(Vec X,ISLocalToGlobalMapping *mapping)
101 {
102 PetscFunctionBegin;
103 PetscValidHeaderSpecific(X,VEC_CLASSID,1);
104 PetscValidType(X,1);
105 PetscValidPointer(mapping,2);
106 *mapping = X->map->mapping;
107 PetscFunctionReturn(0);
108 }
109
110 /*@
111 VecAssemblyBegin - Begins assembling the vector. This routine should
112 be called after completing all calls to VecSetValues().
113
114 Collective on Vec
115
116 Input Parameter:
117 . vec - the vector
118
119 Level: beginner
120
121 .seealso: VecAssemblyEnd(), VecSetValues()
122 @*/
VecAssemblyBegin(Vec vec)123 PetscErrorCode VecAssemblyBegin(Vec vec)
124 {
125 PetscErrorCode ierr;
126
127 PetscFunctionBegin;
128 PetscValidHeaderSpecific(vec,VEC_CLASSID,1);
129 PetscValidType(vec,1);
130 ierr = VecStashViewFromOptions(vec,NULL,"-vec_view_stash");CHKERRQ(ierr);
131 ierr = PetscLogEventBegin(VEC_AssemblyBegin,vec,0,0,0);CHKERRQ(ierr);
132 if (vec->ops->assemblybegin) {
133 ierr = (*vec->ops->assemblybegin)(vec);CHKERRQ(ierr);
134 }
135 ierr = PetscLogEventEnd(VEC_AssemblyBegin,vec,0,0,0);CHKERRQ(ierr);
136 ierr = PetscObjectStateIncrease((PetscObject)vec);CHKERRQ(ierr);
137 PetscFunctionReturn(0);
138 }
139
140 /*@
141 VecAssemblyEnd - Completes assembling the vector. This routine should
142 be called after VecAssemblyBegin().
143
144 Collective on Vec
145
146 Input Parameter:
147 . vec - the vector
148
149 Options Database Keys:
150 + -vec_view - Prints vector in ASCII format
151 . -vec_view ::ascii_matlab - Prints vector in ASCII MATLAB format to stdout
152 . -vec_view matlab:filename - Prints vector in MATLAB format to matlaboutput.mat
153 . -vec_view draw - Activates vector viewing using drawing tools
154 . -display <name> - Sets display name (default is host)
155 . -draw_pause <sec> - Sets number of seconds to pause after display
156 - -vec_view socket - Activates vector viewing using a socket
157
158 Level: beginner
159
160 .seealso: VecAssemblyBegin(), VecSetValues()
161 @*/
VecAssemblyEnd(Vec vec)162 PetscErrorCode VecAssemblyEnd(Vec vec)
163 {
164 PetscErrorCode ierr;
165
166 PetscFunctionBegin;
167 PetscValidHeaderSpecific(vec,VEC_CLASSID,1);
168 ierr = PetscLogEventBegin(VEC_AssemblyEnd,vec,0,0,0);CHKERRQ(ierr);
169 PetscValidType(vec,1);
170 if (vec->ops->assemblyend) {
171 ierr = (*vec->ops->assemblyend)(vec);CHKERRQ(ierr);
172 }
173 ierr = PetscLogEventEnd(VEC_AssemblyEnd,vec,0,0,0);CHKERRQ(ierr);
174 ierr = VecViewFromOptions(vec,NULL,"-vec_view");CHKERRQ(ierr);
175 PetscFunctionReturn(0);
176 }
177
178 /*@
179 VecPointwiseMax - Computes the componentwise maximum w_i = max(x_i, y_i).
180
181 Logically Collective on Vec
182
183 Input Parameters:
184 . x, y - the vectors
185
186 Output Parameter:
187 . w - the result
188
189 Level: advanced
190
191 Notes:
192 any subset of the x, y, and w may be the same vector.
193 For complex numbers compares only the real part
194
195 .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
196 @*/
VecPointwiseMax(Vec w,Vec x,Vec y)197 PetscErrorCode VecPointwiseMax(Vec w,Vec x,Vec y)
198 {
199 PetscErrorCode ierr;
200
201 PetscFunctionBegin;
202 PetscValidHeaderSpecific(w,VEC_CLASSID,1);
203 PetscValidHeaderSpecific(x,VEC_CLASSID,2);
204 PetscValidHeaderSpecific(y,VEC_CLASSID,3);
205 PetscValidType(w,1);
206 PetscValidType(x,2);
207 PetscValidType(y,3);
208 PetscCheckSameTypeAndComm(x,2,y,3);
209 PetscCheckSameTypeAndComm(y,3,w,1);
210 VecCheckSameSize(w,1,x,2);
211 VecCheckSameSize(w,1,y,3);
212 ierr = VecSetErrorIfLocked(w,1);CHKERRQ(ierr);
213 ierr = (*w->ops->pointwisemax)(w,x,y);CHKERRQ(ierr);
214 ierr = PetscObjectStateIncrease((PetscObject)w);CHKERRQ(ierr);
215 PetscFunctionReturn(0);
216 }
217
218 /*@
219 VecPointwiseMin - Computes the componentwise minimum w_i = min(x_i, y_i).
220
221 Logically Collective on Vec
222
223 Input Parameters:
224 . x, y - the vectors
225
226 Output Parameter:
227 . w - the result
228
229 Level: advanced
230
231 Notes:
232 any subset of the x, y, and w may be the same vector.
233 For complex numbers compares only the real part
234
235 .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
236 @*/
VecPointwiseMin(Vec w,Vec x,Vec y)237 PetscErrorCode VecPointwiseMin(Vec w,Vec x,Vec y)
238 {
239 PetscErrorCode ierr;
240
241 PetscFunctionBegin;
242 PetscValidHeaderSpecific(w,VEC_CLASSID,1);
243 PetscValidHeaderSpecific(x,VEC_CLASSID,2);
244 PetscValidHeaderSpecific(y,VEC_CLASSID,3);
245 PetscValidType(w,1);
246 PetscValidType(x,2);
247 PetscValidType(y,3);
248 PetscCheckSameTypeAndComm(x,2,y,3);
249 PetscCheckSameTypeAndComm(y,3,w,1);
250 VecCheckSameSize(w,1,x,2);
251 VecCheckSameSize(w,1,y,3);
252 ierr = VecSetErrorIfLocked(w,1);CHKERRQ(ierr);
253 ierr = (*w->ops->pointwisemin)(w,x,y);CHKERRQ(ierr);
254 ierr = PetscObjectStateIncrease((PetscObject)w);CHKERRQ(ierr);
255 PetscFunctionReturn(0);
256 }
257
258 /*@
259 VecPointwiseMaxAbs - Computes the componentwise maximum of the absolute values w_i = max(abs(x_i), abs(y_i)).
260
261 Logically Collective on Vec
262
263 Input Parameters:
264 . x, y - the vectors
265
266 Output Parameter:
267 . w - the result
268
269 Level: advanced
270
271 Notes:
272 any subset of the x, y, and w may be the same vector.
273
274 .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMax(), VecMaxPointwiseDivide()
275 @*/
VecPointwiseMaxAbs(Vec w,Vec x,Vec y)276 PetscErrorCode VecPointwiseMaxAbs(Vec w,Vec x,Vec y)
277 {
278 PetscErrorCode ierr;
279
280 PetscFunctionBegin;
281 PetscValidHeaderSpecific(w,VEC_CLASSID,1);
282 PetscValidHeaderSpecific(x,VEC_CLASSID,2);
283 PetscValidHeaderSpecific(y,VEC_CLASSID,3);
284 PetscValidType(w,1);
285 PetscValidType(x,2);
286 PetscValidType(y,3);
287 PetscCheckSameTypeAndComm(x,2,y,3);
288 PetscCheckSameTypeAndComm(y,3,w,1);
289 VecCheckSameSize(w,1,x,2);
290 VecCheckSameSize(w,1,y,3);
291 ierr = VecSetErrorIfLocked(w,1);CHKERRQ(ierr);
292 ierr = (*w->ops->pointwisemaxabs)(w,x,y);CHKERRQ(ierr);
293 ierr = PetscObjectStateIncrease((PetscObject)w);CHKERRQ(ierr);
294 PetscFunctionReturn(0);
295 }
296
297 /*@
298 VecPointwiseDivide - Computes the componentwise division w = x/y.
299
300 Logically Collective on Vec
301
302 Input Parameters:
303 . x, y - the vectors
304
305 Output Parameter:
306 . w - the result
307
308 Level: advanced
309
310 Notes:
311 any subset of the x, y, and w may be the same vector.
312
313 .seealso: VecPointwiseMult(), VecPointwiseMax(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
314 @*/
VecPointwiseDivide(Vec w,Vec x,Vec y)315 PetscErrorCode VecPointwiseDivide(Vec w,Vec x,Vec y)
316 {
317 PetscErrorCode ierr;
318
319 PetscFunctionBegin;
320 PetscValidHeaderSpecific(w,VEC_CLASSID,1);
321 PetscValidHeaderSpecific(x,VEC_CLASSID,2);
322 PetscValidHeaderSpecific(y,VEC_CLASSID,3);
323 PetscValidType(w,1);
324 PetscValidType(x,2);
325 PetscValidType(y,3);
326 PetscCheckSameTypeAndComm(x,2,y,3);
327 PetscCheckSameTypeAndComm(y,3,w,1);
328 VecCheckSameSize(w,1,x,2);
329 VecCheckSameSize(w,1,y,3);
330 ierr = VecSetErrorIfLocked(w,1);CHKERRQ(ierr);
331 ierr = (*w->ops->pointwisedivide)(w,x,y);CHKERRQ(ierr);
332 ierr = PetscObjectStateIncrease((PetscObject)w);CHKERRQ(ierr);
333 PetscFunctionReturn(0);
334 }
335
336
337 /*@
338 VecDuplicate - Creates a new vector of the same type as an existing vector.
339
340 Collective on Vec
341
342 Input Parameters:
343 . v - a vector to mimic
344
345 Output Parameter:
346 . newv - location to put new vector
347
348 Notes:
349 VecDuplicate() DOES NOT COPY the vector entries, but rather allocates storage
350 for the new vector. Use VecCopy() to copy a vector.
351
352 Use VecDestroy() to free the space. Use VecDuplicateVecs() to get several
353 vectors.
354
355 Level: beginner
356
357 .seealso: VecDestroy(), VecDuplicateVecs(), VecCreate(), VecCopy()
358 @*/
VecDuplicate(Vec v,Vec * newv)359 PetscErrorCode VecDuplicate(Vec v,Vec *newv)
360 {
361 PetscErrorCode ierr;
362
363 PetscFunctionBegin;
364 PetscValidHeaderSpecific(v,VEC_CLASSID,1);
365 PetscValidPointer(newv,2);
366 PetscValidType(v,1);
367 ierr = (*v->ops->duplicate)(v,newv);CHKERRQ(ierr);
368 ierr = PetscObjectStateIncrease((PetscObject)*newv);CHKERRQ(ierr);
369 PetscFunctionReturn(0);
370 }
371
372 /*@
373 VecDestroy - Destroys a vector.
374
375 Collective on Vec
376
377 Input Parameters:
378 . v - the vector
379
380 Level: beginner
381
382 .seealso: VecDuplicate(), VecDestroyVecs()
383 @*/
VecDestroy(Vec * v)384 PetscErrorCode VecDestroy(Vec *v)
385 {
386 PetscErrorCode ierr;
387
388 PetscFunctionBegin;
389 if (!*v) PetscFunctionReturn(0);
390 PetscValidHeaderSpecific((*v),VEC_CLASSID,1);
391 if (--((PetscObject)(*v))->refct > 0) {*v = NULL; PetscFunctionReturn(0);}
392
393 ierr = PetscObjectSAWsViewOff((PetscObject)*v);CHKERRQ(ierr);
394 /* destroy the internal part */
395 if ((*v)->ops->destroy) {
396 ierr = (*(*v)->ops->destroy)(*v);CHKERRQ(ierr);
397 }
398 /* destroy the external/common part */
399 ierr = PetscLayoutDestroy(&(*v)->map);CHKERRQ(ierr);
400 ierr = PetscHeaderDestroy(v);CHKERRQ(ierr);
401 PetscFunctionReturn(0);
402 }
403
404 /*@C
405 VecDuplicateVecs - Creates several vectors of the same type as an existing vector.
406
407 Collective on Vec
408
409 Input Parameters:
410 + m - the number of vectors to obtain
411 - v - a vector to mimic
412
413 Output Parameter:
414 . V - location to put pointer to array of vectors
415
416 Notes:
417 Use VecDestroyVecs() to free the space. Use VecDuplicate() to form a single
418 vector.
419
420 Fortran Note:
421 The Fortran interface is slightly different from that given below, it
422 requires one to pass in V a Vec (integer) array of size at least m.
423 See the Fortran chapter of the users manual and petsc/src/vec/vec/examples for details.
424
425 Level: intermediate
426
427 .seealso: VecDestroyVecs(), VecDuplicate(), VecCreate(), VecDuplicateVecsF90()
428 @*/
VecDuplicateVecs(Vec v,PetscInt m,Vec * V[])429 PetscErrorCode VecDuplicateVecs(Vec v,PetscInt m,Vec *V[])
430 {
431 PetscErrorCode ierr;
432
433 PetscFunctionBegin;
434 PetscValidHeaderSpecific(v,VEC_CLASSID,1);
435 PetscValidPointer(V,3);
436 PetscValidType(v,1);
437 ierr = (*v->ops->duplicatevecs)(v,m,V);CHKERRQ(ierr);
438 PetscFunctionReturn(0);
439 }
440
441 /*@C
442 VecDestroyVecs - Frees a block of vectors obtained with VecDuplicateVecs().
443
444 Collective on Vec
445
446 Input Parameters:
447 + vv - pointer to pointer to array of vector pointers, if NULL no vectors are destroyed
448 - m - the number of vectors previously obtained, if zero no vectors are destroyed
449
450 Fortran Note:
451 The Fortran interface is slightly different from that given below.
452 See the Fortran chapter of the users manual
453
454 Level: intermediate
455
456 .seealso: VecDuplicateVecs(), VecDestroyVecsf90()
457 @*/
VecDestroyVecs(PetscInt m,Vec * vv[])458 PetscErrorCode VecDestroyVecs(PetscInt m,Vec *vv[])
459 {
460 PetscErrorCode ierr;
461
462 PetscFunctionBegin;
463 PetscValidPointer(vv,1);
464 if (m < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of vectors %D",m);
465 if (!m || !*vv) {*vv = NULL; PetscFunctionReturn(0);}
466 PetscValidHeaderSpecific(**vv,VEC_CLASSID,1);
467 PetscValidType(**vv,1);
468 ierr = (*(**vv)->ops->destroyvecs)(m,*vv);CHKERRQ(ierr);
469 *vv = NULL;
470 PetscFunctionReturn(0);
471 }
472
473 /*@C
474 VecViewFromOptions - View from Options
475
476 Collective on Vec
477
478 Input Parameters:
479 + A - the vector
480 . obj - Optional object
481 - name - command line option
482
483 Level: intermediate
484 .seealso: Vec, VecView, PetscObjectViewFromOptions(), VecCreate()
485 @*/
VecViewFromOptions(Vec A,PetscObject obj,const char name[])486 PetscErrorCode VecViewFromOptions(Vec A,PetscObject obj,const char name[])
487 {
488 PetscErrorCode ierr;
489
490 PetscFunctionBegin;
491 PetscValidHeaderSpecific(A,VEC_CLASSID,1);
492 ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
493 PetscFunctionReturn(0);
494 }
495
496 /*@C
497 VecView - Views a vector object.
498
499 Collective on Vec
500
501 Input Parameters:
502 + vec - the vector
503 - viewer - an optional visualization context
504
505 Notes:
506 The available visualization contexts include
507 + PETSC_VIEWER_STDOUT_SELF - for sequential vectors
508 . PETSC_VIEWER_STDOUT_WORLD - for parallel vectors created on PETSC_COMM_WORLD
509 - PETSC_VIEWER_STDOUT_(comm) - for parallel vectors created on MPI communicator comm
510
511 You can change the format the vector is printed using the
512 option PetscViewerPushFormat().
513
514 The user can open alternative viewers with
515 + PetscViewerASCIIOpen() - Outputs vector to a specified file
516 . PetscViewerBinaryOpen() - Outputs vector in binary to a
517 specified file; corresponding input uses VecLoad()
518 . PetscViewerDrawOpen() - Outputs vector to an X window display
519 . PetscViewerSocketOpen() - Outputs vector to Socket viewer
520 - PetscViewerHDF5Open() - Outputs vector to HDF5 file viewer
521
522 The user can call PetscViewerPushFormat() to specify the output
523 format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF,
524 PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen). Available formats include
525 + PETSC_VIEWER_DEFAULT - default, prints vector contents
526 . PETSC_VIEWER_ASCII_MATLAB - prints vector contents in MATLAB format
527 . PETSC_VIEWER_ASCII_INDEX - prints vector contents, including indices of vector elements
528 - PETSC_VIEWER_ASCII_COMMON - prints vector contents, using a
529 format common among all vector types
530
531 Notes:
532 You can pass any number of vector objects, or other PETSc objects to the same viewer.
533
534 Notes for binary viewer:
535 If you pass multiple vectors to a binary viewer you can read them back in in the same order
536 with VecLoad().
537
538 If the blocksize of the vector is greater than one then you must provide a unique prefix to
539 the vector with PetscObjectSetOptionsPrefix((PetscObject)vec,"uniqueprefix"); BEFORE calling VecView() on the
540 vector to be stored and then set that same unique prefix on the vector that you pass to VecLoad(). The blocksize
541 information is stored in an ASCII file with the same name as the binary file plus a ".info" appended to the
542 filename. If you copy the binary file, make sure you copy the associated .info file with it.
543
544 See the manual page for VecLoad() on the exact format the binary viewer stores
545 the values in the file.
546
547
548 Notes for HDF5 Viewer:
549 The name of the Vec (given with PetscObjectSetName() is the name that is used
550 for the object in the HDF5 file. If you wish to store the same Vec into multiple
551 datasets in the same file (typically with different values), you must change its
552 name each time before calling the VecView(). To load the same vector,
553 the name of the Vec object passed to VecLoad() must be the same.
554
555 If the block size of the vector is greater than 1 then it is used as the first dimension in the HDF5 array.
556 If the function PetscViewerHDF5SetBaseDimension2()is called then even if the block size is one it will
557 be used as the first dimension in the HDF5 array (that is the HDF5 array will always be two dimensional)
558 See also PetscViewerHDF5SetTimestep() which adds an additional complication to reading and writing Vecs
559 with the HDF5 viewer.
560
561 Level: beginner
562
563
564 .seealso: PetscViewerASCIIOpen(), PetscViewerDrawOpen(), PetscDrawLGCreate(),
565 PetscViewerSocketOpen(), PetscViewerBinaryOpen(), VecLoad(), PetscViewerCreate(),
566 PetscRealView(), PetscScalarView(), PetscIntView(), PetscViewerHDF5SetTimestep()
567 @*/
VecView(Vec vec,PetscViewer viewer)568 PetscErrorCode VecView(Vec vec,PetscViewer viewer)
569 {
570 PetscErrorCode ierr;
571 PetscBool iascii;
572 PetscViewerFormat format;
573 PetscMPIInt size;
574
575 PetscFunctionBegin;
576 PetscValidHeaderSpecific(vec,VEC_CLASSID,1);
577 PetscValidType(vec,1);
578 if (!viewer) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)vec),&viewer);CHKERRQ(ierr);}
579 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
580 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
581 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)vec),&size);CHKERRQ(ierr);
582 if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
583
584 if (vec->stash.n || vec->bstash.n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call VecAssemblyBegin/End() before viewing this vector");
585
586 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
587 if (iascii) {
588 PetscInt rows,bs;
589
590 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)vec,viewer);CHKERRQ(ierr);
591 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
592 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
593 ierr = VecGetSize(vec,&rows);CHKERRQ(ierr);
594 ierr = VecGetBlockSize(vec,&bs);CHKERRQ(ierr);
595 if (bs != 1) {
596 ierr = PetscViewerASCIIPrintf(viewer,"length=%D, bs=%D\n",rows,bs);CHKERRQ(ierr);
597 } else {
598 ierr = PetscViewerASCIIPrintf(viewer,"length=%D\n",rows);CHKERRQ(ierr);
599 }
600 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
601 }
602 }
603 ierr = VecLockReadPush(vec);CHKERRQ(ierr);
604 ierr = PetscLogEventBegin(VEC_View,vec,viewer,0,0);CHKERRQ(ierr);
605 if ((format == PETSC_VIEWER_NATIVE || format == PETSC_VIEWER_LOAD_BALANCE) && vec->ops->viewnative) {
606 ierr = (*vec->ops->viewnative)(vec,viewer);CHKERRQ(ierr);
607 } else {
608 ierr = (*vec->ops->view)(vec,viewer);CHKERRQ(ierr);
609 }
610 ierr = VecLockReadPop(vec);CHKERRQ(ierr);
611 ierr = PetscLogEventEnd(VEC_View,vec,viewer,0,0);CHKERRQ(ierr);
612 PetscFunctionReturn(0);
613 }
614
615 #if defined(PETSC_USE_DEBUG)
616 #include <../src/sys/totalview/tv_data_display.h>
TV_display_type(const struct _p_Vec * v)617 PETSC_UNUSED static int TV_display_type(const struct _p_Vec *v)
618 {
619 const PetscScalar *values;
620 char type[32];
621 PetscErrorCode ierr;
622
623
624 TV_add_row("Local rows", "int", &v->map->n);
625 TV_add_row("Global rows", "int", &v->map->N);
626 TV_add_row("Typename", TV_ascii_string_type, ((PetscObject)v)->type_name);
627 ierr = VecGetArrayRead((Vec)v,&values);CHKERRQ(ierr);
628 ierr = PetscSNPrintf(type,32,"double[%d]",v->map->n);CHKERRQ(ierr);
629 TV_add_row("values",type, values);
630 ierr = VecRestoreArrayRead((Vec)v,&values);CHKERRQ(ierr);
631 return TV_format_OK;
632 }
633 #endif
634
635 /*@
636 VecGetSize - Returns the global number of elements of the vector.
637
638 Not Collective
639
640 Input Parameter:
641 . x - the vector
642
643 Output Parameters:
644 . size - the global length of the vector
645
646 Level: beginner
647
648 .seealso: VecGetLocalSize()
649 @*/
VecGetSize(Vec x,PetscInt * size)650 PetscErrorCode VecGetSize(Vec x,PetscInt *size)
651 {
652 PetscErrorCode ierr;
653
654 PetscFunctionBegin;
655 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
656 PetscValidIntPointer(size,2);
657 PetscValidType(x,1);
658 ierr = (*x->ops->getsize)(x,size);CHKERRQ(ierr);
659 PetscFunctionReturn(0);
660 }
661
662 /*@
663 VecGetLocalSize - Returns the number of elements of the vector stored
664 in local memory.
665
666 Not Collective
667
668 Input Parameter:
669 . x - the vector
670
671 Output Parameter:
672 . size - the length of the local piece of the vector
673
674 Level: beginner
675
676 .seealso: VecGetSize()
677 @*/
VecGetLocalSize(Vec x,PetscInt * size)678 PetscErrorCode VecGetLocalSize(Vec x,PetscInt *size)
679 {
680 PetscErrorCode ierr;
681
682 PetscFunctionBegin;
683 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
684 PetscValidIntPointer(size,2);
685 PetscValidType(x,1);
686 ierr = (*x->ops->getlocalsize)(x,size);CHKERRQ(ierr);
687 PetscFunctionReturn(0);
688 }
689
690 /*@C
691 VecGetOwnershipRange - Returns the range of indices owned by
692 this processor, assuming that the vectors are laid out with the
693 first n1 elements on the first processor, next n2 elements on the
694 second, etc. For certain parallel layouts this range may not be
695 well defined.
696
697 Not Collective
698
699 Input Parameter:
700 . x - the vector
701
702 Output Parameters:
703 + low - the first local element, pass in NULL if not interested
704 - high - one more than the last local element, pass in NULL if not interested
705
706 Note:
707 The high argument is one more than the last element stored locally.
708
709 Fortran: PETSC_NULL_INTEGER should be used instead of NULL
710
711 Level: beginner
712
713
714 .seealso: MatGetOwnershipRange(), MatGetOwnershipRanges(), VecGetOwnershipRanges()
715 @*/
VecGetOwnershipRange(Vec x,PetscInt * low,PetscInt * high)716 PetscErrorCode VecGetOwnershipRange(Vec x,PetscInt *low,PetscInt *high)
717 {
718 PetscFunctionBegin;
719 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
720 PetscValidType(x,1);
721 if (low) PetscValidIntPointer(low,2);
722 if (high) PetscValidIntPointer(high,3);
723 if (low) *low = x->map->rstart;
724 if (high) *high = x->map->rend;
725 PetscFunctionReturn(0);
726 }
727
728 /*@C
729 VecGetOwnershipRanges - Returns the range of indices owned by EACH processor,
730 assuming that the vectors are laid out with the
731 first n1 elements on the first processor, next n2 elements on the
732 second, etc. For certain parallel layouts this range may not be
733 well defined.
734
735 Not Collective
736
737 Input Parameter:
738 . x - the vector
739
740 Output Parameters:
741 . range - array of length size+1 with the start and end+1 for each process
742
743 Note:
744 The high argument is one more than the last element stored locally.
745
746 Fortran: You must PASS in an array of length size+1
747
748 Level: beginner
749
750
751 .seealso: MatGetOwnershipRange(), MatGetOwnershipRanges(), VecGetOwnershipRange()
752 @*/
VecGetOwnershipRanges(Vec x,const PetscInt * ranges[])753 PetscErrorCode VecGetOwnershipRanges(Vec x,const PetscInt *ranges[])
754 {
755 PetscErrorCode ierr;
756
757 PetscFunctionBegin;
758 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
759 PetscValidType(x,1);
760 ierr = PetscLayoutGetRanges(x->map,ranges);CHKERRQ(ierr);
761 PetscFunctionReturn(0);
762 }
763
764 /*@
765 VecSetOption - Sets an option for controling a vector's behavior.
766
767 Collective on Vec
768
769 Input Parameter:
770 + x - the vector
771 . op - the option
772 - flag - turn the option on or off
773
774 Supported Options:
775 + VEC_IGNORE_OFF_PROC_ENTRIES, which causes VecSetValues() to ignore
776 entries destined to be stored on a separate processor. This can be used
777 to eliminate the global reduction in the VecAssemblyXXXX() if you know
778 that you have only used VecSetValues() to set local elements
779 . VEC_IGNORE_NEGATIVE_INDICES, which means you can pass negative indices
780 in ix in calls to VecSetValues() or VecGetValues(). These rows are simply
781 ignored.
782 - VEC_SUBSET_OFF_PROC_ENTRIES, which causes VecAssemblyBegin() to assume that the off-process
783 entries will always be a subset (possibly equal) of the off-process entries set on the
784 first assembly which had a true VEC_SUBSET_OFF_PROC_ENTRIES and the vector has not
785 changed this flag afterwards. If this assembly is not such first assembly, then this
786 assembly can reuse the communication pattern setup in that first assembly, thus avoiding
787 a global reduction. Subsequent assemblies setting off-process values should use the same
788 InsertMode as the first assembly.
789
790 Developer Note:
791 The InsertMode restriction could be removed by packing the stash messages out of place.
792
793 Level: intermediate
794
795 @*/
VecSetOption(Vec x,VecOption op,PetscBool flag)796 PetscErrorCode VecSetOption(Vec x,VecOption op,PetscBool flag)
797 {
798 PetscErrorCode ierr;
799
800 PetscFunctionBegin;
801 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
802 PetscValidType(x,1);
803 if (x->ops->setoption) {
804 ierr = (*x->ops->setoption)(x,op,flag);CHKERRQ(ierr);
805 }
806 PetscFunctionReturn(0);
807 }
808
809 /* Default routines for obtaining and releasing; */
810 /* may be used by any implementation */
VecDuplicateVecs_Default(Vec w,PetscInt m,Vec * V[])811 PetscErrorCode VecDuplicateVecs_Default(Vec w,PetscInt m,Vec *V[])
812 {
813 PetscErrorCode ierr;
814 PetscInt i;
815
816 PetscFunctionBegin;
817 PetscValidHeaderSpecific(w,VEC_CLASSID,1);
818 PetscValidPointer(V,3);
819 if (m <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"m must be > 0: m = %D",m);
820 ierr = PetscMalloc1(m,V);CHKERRQ(ierr);
821 for (i=0; i<m; i++) {ierr = VecDuplicate(w,*V+i);CHKERRQ(ierr);}
822 PetscFunctionReturn(0);
823 }
824
VecDestroyVecs_Default(PetscInt m,Vec v[])825 PetscErrorCode VecDestroyVecs_Default(PetscInt m,Vec v[])
826 {
827 PetscErrorCode ierr;
828 PetscInt i;
829
830 PetscFunctionBegin;
831 PetscValidPointer(v,1);
832 for (i=0; i<m; i++) {ierr = VecDestroy(&v[i]);CHKERRQ(ierr);}
833 ierr = PetscFree(v);CHKERRQ(ierr);
834 PetscFunctionReturn(0);
835 }
836
837 /*@
838 VecResetArray - Resets a vector to use its default memory. Call this
839 after the use of VecPlaceArray().
840
841 Not Collective
842
843 Input Parameters:
844 . vec - the vector
845
846 Level: developer
847
848 .seealso: VecGetArray(), VecRestoreArray(), VecReplaceArray(), VecPlaceArray()
849
850 @*/
VecResetArray(Vec vec)851 PetscErrorCode VecResetArray(Vec vec)
852 {
853 PetscErrorCode ierr;
854
855 PetscFunctionBegin;
856 PetscValidHeaderSpecific(vec,VEC_CLASSID,1);
857 PetscValidType(vec,1);
858 if (vec->ops->resetarray) {
859 ierr = (*vec->ops->resetarray)(vec);CHKERRQ(ierr);
860 } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot reset array in this type of vector");
861 ierr = PetscObjectStateIncrease((PetscObject)vec);CHKERRQ(ierr);
862 PetscFunctionReturn(0);
863 }
864
865 /*@C
866 VecLoad - Loads a vector that has been stored in binary or HDF5 format
867 with VecView().
868
869 Collective on PetscViewer
870
871 Input Parameters:
872 + vec - the newly loaded vector, this needs to have been created with VecCreate() or
873 some related function before a call to VecLoad().
874 - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
875 HDF5 file viewer, obtained from PetscViewerHDF5Open()
876
877 Level: intermediate
878
879 Notes:
880 Defaults to the standard Seq or MPI Vec, if you want some other type of Vec call VecSetFromOptions()
881 before calling this.
882
883 The input file must contain the full global vector, as
884 written by the routine VecView().
885
886 If the type or size of vec is not set before a call to VecLoad, PETSc
887 sets the type and the local and global sizes. If type and/or
888 sizes are already set, then the same are used.
889
890 If using the binary viewer and the blocksize of the vector is greater than one then you must provide a unique prefix to
891 the vector with PetscObjectSetOptionsPrefix((PetscObject)vec,"uniqueprefix"); BEFORE calling VecView() on the
892 vector to be stored and then set that same unique prefix on the vector that you pass to VecLoad(). The blocksize
893 information is stored in an ASCII file with the same name as the binary file plus a ".info" appended to the
894 filename. If you copy the binary file, make sure you copy the associated .info file with it.
895
896 If using HDF5, you must assign the Vec the same name as was used in the Vec
897 that was stored in the file using PetscObjectSetName(). Otherwise you will
898 get the error message: "Cannot H5DOpen2() with Vec name NAMEOFOBJECT".
899
900 If the HDF5 file contains a two dimensional array the first dimension is treated as the block size
901 in loading the vector. Hence, for example, using Matlab notation h5create('vector.dat','/Test_Vec',[27 1]);
902 will load a vector of size 27 and block size 27 thus resulting in all 27 entries being on the first process of
903 vectors communicator and the rest of the processes having zero entries
904
905 Notes for advanced users when using the binary viewer:
906 Most users should not need to know the details of the binary storage
907 format, since VecLoad() and VecView() completely hide these details.
908 But for anyone who's interested, the standard binary vector storage
909 format is
910 .vb
911 PetscInt VEC_FILE_CLASSID
912 PetscInt number of rows
913 PetscScalar *values of all entries
914 .ve
915
916 In addition, PETSc automatically uses byte swapping to work on all machines; the files
917 are written ALWAYS using big-endian ordering. On small-endian machines the numbers
918 are converted to the small-endian format when they are read in from the file.
919 See PetscBinaryRead() and PetscBinaryWrite() to see how this may be done.
920
921 .seealso: PetscViewerBinaryOpen(), VecView(), MatLoad(), VecLoad()
922 @*/
VecLoad(Vec vec,PetscViewer viewer)923 PetscErrorCode VecLoad(Vec vec, PetscViewer viewer)
924 {
925 PetscErrorCode ierr;
926 PetscBool isbinary,ishdf5,isadios,isadios2;
927 PetscViewerFormat format;
928
929 PetscFunctionBegin;
930 PetscValidHeaderSpecific(vec,VEC_CLASSID,1);
931 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
932 PetscCheckSameComm(vec,1,viewer,2);
933 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
934 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
935 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERADIOS,&isadios);CHKERRQ(ierr);
936 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERADIOS,&isadios2);CHKERRQ(ierr);
937 if (!isbinary && !ishdf5 && !isadios && !isadios2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
938
939 ierr = VecSetErrorIfLocked(vec,1);CHKERRQ(ierr);
940 if (!((PetscObject)vec)->type_name && !vec->ops->create) {
941 ierr = VecSetType(vec, VECSTANDARD);CHKERRQ(ierr);
942 }
943 ierr = PetscLogEventBegin(VEC_Load,viewer,0,0,0);CHKERRQ(ierr);
944 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
945 if (format == PETSC_VIEWER_NATIVE && vec->ops->loadnative) {
946 ierr = (*vec->ops->loadnative)(vec,viewer);CHKERRQ(ierr);
947 } else {
948 ierr = (*vec->ops->load)(vec,viewer);CHKERRQ(ierr);
949 }
950 ierr = PetscLogEventEnd(VEC_Load,viewer,0,0,0);CHKERRQ(ierr);
951 PetscFunctionReturn(0);
952 }
953
954
955 /*@
956 VecReciprocal - Replaces each component of a vector by its reciprocal.
957
958 Logically Collective on Vec
959
960 Input Parameter:
961 . vec - the vector
962
963 Output Parameter:
964 . vec - the vector reciprocal
965
966 Level: intermediate
967
968 .seealso: VecLog(), VecExp(), VecSqrtAbs()
969
970 @*/
VecReciprocal(Vec vec)971 PetscErrorCode VecReciprocal(Vec vec)
972 {
973 PetscErrorCode ierr;
974
975 PetscFunctionBegin;
976 PetscValidHeaderSpecific(vec,VEC_CLASSID,1);
977 PetscValidType(vec,1);
978 if (vec->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
979 if (!vec->ops->reciprocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Vector does not support reciprocal operation");
980 ierr = VecSetErrorIfLocked(vec,1);CHKERRQ(ierr);
981 ierr = (*vec->ops->reciprocal)(vec);CHKERRQ(ierr);
982 ierr = PetscObjectStateIncrease((PetscObject)vec);CHKERRQ(ierr);
983 PetscFunctionReturn(0);
984 }
985
986 /*@C
987 VecSetOperation - Allows user to set a vector operation.
988
989 Logically Collective on Vec
990
991 Input Parameters:
992 + vec - the vector
993 . op - the name of the operation
994 - f - the function that provides the operation.
995
996 Level: advanced
997
998 Usage:
999 $ PetscErrorCode userview(Vec,PetscViewer);
1000 $ ierr = VecCreateMPI(comm,m,M,&x);CHKERRQ(ierr);
1001 $ ierr = VecSetOperation(x,VECOP_VIEW,(void(*)(void))userview);CHKERRQ(ierr);
1002
1003 Notes:
1004 See the file include/petscvec.h for a complete list of matrix
1005 operations, which all have the form VECOP_<OPERATION>, where
1006 <OPERATION> is the name (in all capital letters) of the
1007 user interface routine (e.g., VecView() -> VECOP_VIEW).
1008
1009 This function is not currently available from Fortran.
1010
1011 .seealso: VecCreate(), MatShellSetOperation()
1012 @*/
VecSetOperation(Vec vec,VecOperation op,void (* f)(void))1013 PetscErrorCode VecSetOperation(Vec vec,VecOperation op, void (*f)(void))
1014 {
1015 PetscFunctionBegin;
1016 PetscValidHeaderSpecific(vec,VEC_CLASSID,1);
1017 if (op == VECOP_VIEW && !vec->ops->viewnative) {
1018 vec->ops->viewnative = vec->ops->view;
1019 } else if (op == VECOP_LOAD && !vec->ops->loadnative) {
1020 vec->ops->loadnative = vec->ops->load;
1021 }
1022 (((void(**)(void))vec->ops)[(int)op]) = f;
1023 PetscFunctionReturn(0);
1024 }
1025
1026
1027 /*@
1028 VecStashSetInitialSize - sets the sizes of the vec-stash, that is
1029 used during the assembly process to store values that belong to
1030 other processors.
1031
1032 Not Collective, different processes can have different size stashes
1033
1034 Input Parameters:
1035 + vec - the vector
1036 . size - the initial size of the stash.
1037 - bsize - the initial size of the block-stash(if used).
1038
1039 Options Database Keys:
1040 + -vecstash_initial_size <size> or <size0,size1,...sizep-1>
1041 - -vecstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1>
1042
1043 Level: intermediate
1044
1045 Notes:
1046 The block-stash is used for values set with VecSetValuesBlocked() while
1047 the stash is used for values set with VecSetValues()
1048
1049 Run with the option -info and look for output of the form
1050 VecAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
1051 to determine the appropriate value, MM, to use for size and
1052 VecAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
1053 to determine the value, BMM to use for bsize
1054
1055
1056 .seealso: VecSetBlockSize(), VecSetValues(), VecSetValuesBlocked(), VecStashView()
1057
1058 @*/
VecStashSetInitialSize(Vec vec,PetscInt size,PetscInt bsize)1059 PetscErrorCode VecStashSetInitialSize(Vec vec,PetscInt size,PetscInt bsize)
1060 {
1061 PetscErrorCode ierr;
1062
1063 PetscFunctionBegin;
1064 PetscValidHeaderSpecific(vec,VEC_CLASSID,1);
1065 ierr = VecStashSetInitialSize_Private(&vec->stash,size);CHKERRQ(ierr);
1066 ierr = VecStashSetInitialSize_Private(&vec->bstash,bsize);CHKERRQ(ierr);
1067 PetscFunctionReturn(0);
1068 }
1069
1070 /*@
1071 VecConjugate - Conjugates a vector.
1072
1073 Logically Collective on Vec
1074
1075 Input Parameters:
1076 . x - the vector
1077
1078 Level: intermediate
1079
1080 @*/
VecConjugate(Vec x)1081 PetscErrorCode VecConjugate(Vec x)
1082 {
1083 #if defined(PETSC_USE_COMPLEX)
1084 PetscErrorCode ierr;
1085
1086 PetscFunctionBegin;
1087 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
1088 PetscValidType(x,1);
1089 if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1090 ierr = VecSetErrorIfLocked(x,1);CHKERRQ(ierr);
1091 ierr = (*x->ops->conjugate)(x);CHKERRQ(ierr);
1092 /* we need to copy norms here */
1093 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
1094 PetscFunctionReturn(0);
1095 #else
1096 return(0);
1097 #endif
1098 }
1099
1100 /*@
1101 VecPointwiseMult - Computes the componentwise multiplication w = x*y.
1102
1103 Logically Collective on Vec
1104
1105 Input Parameters:
1106 . x, y - the vectors
1107
1108 Output Parameter:
1109 . w - the result
1110
1111 Level: advanced
1112
1113 Notes:
1114 any subset of the x, y, and w may be the same vector.
1115
1116 .seealso: VecPointwiseDivide(), VecPointwiseMax(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
1117 @*/
VecPointwiseMult(Vec w,Vec x,Vec y)1118 PetscErrorCode VecPointwiseMult(Vec w, Vec x,Vec y)
1119 {
1120 PetscErrorCode ierr;
1121
1122 PetscFunctionBegin;
1123 PetscValidHeaderSpecific(w,VEC_CLASSID,1);
1124 PetscValidHeaderSpecific(x,VEC_CLASSID,2);
1125 PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1126 PetscValidType(w,1);
1127 PetscValidType(x,2);
1128 PetscValidType(y,3);
1129 PetscCheckSameTypeAndComm(x,2,y,3);
1130 PetscCheckSameTypeAndComm(y,3,w,1);
1131 VecCheckSameSize(w,1,x,2);
1132 VecCheckSameSize(w,2,y,3);
1133 ierr = VecSetErrorIfLocked(w,1);CHKERRQ(ierr);
1134 ierr = PetscLogEventBegin(VEC_PointwiseMult,x,y,w,0);CHKERRQ(ierr);
1135 ierr = (*w->ops->pointwisemult)(w,x,y);CHKERRQ(ierr);
1136 ierr = PetscLogEventEnd(VEC_PointwiseMult,x,y,w,0);CHKERRQ(ierr);
1137 ierr = PetscObjectStateIncrease((PetscObject)w);CHKERRQ(ierr);
1138 PetscFunctionReturn(0);
1139 }
1140
1141 /*@
1142 VecSetRandom - Sets all components of a vector to random numbers.
1143
1144 Logically Collective on Vec
1145
1146 Input Parameters:
1147 + x - the vector
1148 - rctx - the random number context, formed by PetscRandomCreate(), or NULL and
1149 it will create one internally.
1150
1151 Output Parameter:
1152 . x - the vector
1153
1154 Example of Usage:
1155 .vb
1156 PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
1157 VecSetRandom(x,rctx);
1158 PetscRandomDestroy(rctx);
1159 .ve
1160
1161 Level: intermediate
1162
1163
1164 .seealso: VecSet(), VecSetValues(), PetscRandomCreate(), PetscRandomDestroy()
1165 @*/
VecSetRandom(Vec x,PetscRandom rctx)1166 PetscErrorCode VecSetRandom(Vec x,PetscRandom rctx)
1167 {
1168 PetscErrorCode ierr;
1169 PetscRandom randObj = NULL;
1170
1171 PetscFunctionBegin;
1172 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
1173 if (rctx) PetscValidHeaderSpecific(rctx,PETSC_RANDOM_CLASSID,2);
1174 PetscValidType(x,1);
1175 if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1176 ierr = VecSetErrorIfLocked(x,1);CHKERRQ(ierr);
1177
1178 if (!rctx) {
1179 MPI_Comm comm;
1180 ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr);
1181 ierr = PetscRandomCreate(comm,&randObj);CHKERRQ(ierr);
1182 ierr = PetscRandomSetFromOptions(randObj);CHKERRQ(ierr);
1183 rctx = randObj;
1184 }
1185
1186 ierr = PetscLogEventBegin(VEC_SetRandom,x,rctx,0,0);CHKERRQ(ierr);
1187 ierr = (*x->ops->setrandom)(x,rctx);CHKERRQ(ierr);
1188 ierr = PetscLogEventEnd(VEC_SetRandom,x,rctx,0,0);CHKERRQ(ierr);
1189
1190 ierr = PetscRandomDestroy(&randObj);CHKERRQ(ierr);
1191 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
1192 PetscFunctionReturn(0);
1193 }
1194
1195 /*@
1196 VecZeroEntries - puts a 0.0 in each element of a vector
1197
1198 Logically Collective on Vec
1199
1200 Input Parameter:
1201 . vec - The vector
1202
1203 Level: beginner
1204
1205 .seealso: VecCreate(), VecSetOptionsPrefix(), VecSet(), VecSetValues()
1206 @*/
VecZeroEntries(Vec vec)1207 PetscErrorCode VecZeroEntries(Vec vec)
1208 {
1209 PetscErrorCode ierr;
1210
1211 PetscFunctionBegin;
1212 ierr = VecSet(vec,0);CHKERRQ(ierr);
1213 PetscFunctionReturn(0);
1214 }
1215
1216 /*
1217 VecSetTypeFromOptions_Private - Sets the type of vector from user options. Defaults to a PETSc sequential vector on one
1218 processor and a PETSc MPI vector on more than one processor.
1219
1220 Collective on Vec
1221
1222 Input Parameter:
1223 . vec - The vector
1224
1225 Level: intermediate
1226
1227 .seealso: VecSetFromOptions(), VecSetType()
1228 */
VecSetTypeFromOptions_Private(PetscOptionItems * PetscOptionsObject,Vec vec)1229 static PetscErrorCode VecSetTypeFromOptions_Private(PetscOptionItems *PetscOptionsObject,Vec vec)
1230 {
1231 PetscBool opt;
1232 VecType defaultType;
1233 char typeName[256];
1234 PetscMPIInt size;
1235 PetscErrorCode ierr;
1236
1237 PetscFunctionBegin;
1238 if (((PetscObject)vec)->type_name) defaultType = ((PetscObject)vec)->type_name;
1239 else {
1240 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)vec), &size);CHKERRQ(ierr);
1241 if (size > 1) defaultType = VECMPI;
1242 else defaultType = VECSEQ;
1243 }
1244
1245 ierr = VecRegisterAll();CHKERRQ(ierr);
1246 ierr = PetscOptionsFList("-vec_type","Vector type","VecSetType",VecList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1247 if (opt) {
1248 ierr = VecSetType(vec, typeName);CHKERRQ(ierr);
1249 } else {
1250 ierr = VecSetType(vec, defaultType);CHKERRQ(ierr);
1251 }
1252 PetscFunctionReturn(0);
1253 }
1254
1255 /*@
1256 VecSetFromOptions - Configures the vector from the options database.
1257
1258 Collective on Vec
1259
1260 Input Parameter:
1261 . vec - The vector
1262
1263 Notes:
1264 To see all options, run your program with the -help option, or consult the users manual.
1265 Must be called after VecCreate() but before the vector is used.
1266
1267 Level: beginner
1268
1269
1270 .seealso: VecCreate(), VecSetOptionsPrefix()
1271 @*/
VecSetFromOptions(Vec vec)1272 PetscErrorCode VecSetFromOptions(Vec vec)
1273 {
1274 PetscErrorCode ierr;
1275
1276 PetscFunctionBegin;
1277 PetscValidHeaderSpecific(vec,VEC_CLASSID,1);
1278
1279 ierr = PetscObjectOptionsBegin((PetscObject)vec);CHKERRQ(ierr);
1280 /* Handle vector type options */
1281 ierr = VecSetTypeFromOptions_Private(PetscOptionsObject,vec);CHKERRQ(ierr);
1282
1283 /* Handle specific vector options */
1284 if (vec->ops->setfromoptions) {
1285 ierr = (*vec->ops->setfromoptions)(PetscOptionsObject,vec);CHKERRQ(ierr);
1286 }
1287
1288 /* process any options handlers added with PetscObjectAddOptionsHandler() */
1289 ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)vec);CHKERRQ(ierr);
1290 ierr = PetscOptionsEnd();CHKERRQ(ierr);
1291 PetscFunctionReturn(0);
1292 }
1293
1294 /*@
1295 VecSetSizes - Sets the local and global sizes, and checks to determine compatibility
1296
1297 Collective on Vec
1298
1299 Input Parameters:
1300 + v - the vector
1301 . n - the local size (or PETSC_DECIDE to have it set)
1302 - N - the global size (or PETSC_DECIDE)
1303
1304 Notes:
1305 n and N cannot be both PETSC_DECIDE
1306 If one processor calls this with N of PETSC_DECIDE then all processors must, otherwise the program will hang.
1307
1308 Level: intermediate
1309
1310 .seealso: VecGetSize(), PetscSplitOwnership()
1311 @*/
VecSetSizes(Vec v,PetscInt n,PetscInt N)1312 PetscErrorCode VecSetSizes(Vec v, PetscInt n, PetscInt N)
1313 {
1314 PetscErrorCode ierr;
1315
1316 PetscFunctionBegin;
1317 PetscValidHeaderSpecific(v, VEC_CLASSID,1);
1318 if (N >= 0) PetscValidLogicalCollectiveInt(v,N,3);
1319 if (N >= 0 && n > N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local size %D cannot be larger than global size %D",n,N);
1320 if ((v->map->n >= 0 || v->map->N >= 0) && (v->map->n != n || v->map->N != N)) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot change/reset vector sizes to %D local %D global after previously setting them to %D local %D global",n,N,v->map->n,v->map->N);
1321 v->map->n = n;
1322 v->map->N = N;
1323 if (v->ops->create) {
1324 ierr = (*v->ops->create)(v);CHKERRQ(ierr);
1325 v->ops->create = NULL;
1326 }
1327 PetscFunctionReturn(0);
1328 }
1329
1330 /*@
1331 VecSetBlockSize - Sets the blocksize for future calls to VecSetValuesBlocked()
1332 and VecSetValuesBlockedLocal().
1333
1334 Logically Collective on Vec
1335
1336 Input Parameter:
1337 + v - the vector
1338 - bs - the blocksize
1339
1340 Notes:
1341 All vectors obtained by VecDuplicate() inherit the same blocksize.
1342
1343 Level: advanced
1344
1345 .seealso: VecSetValuesBlocked(), VecSetLocalToGlobalMapping(), VecGetBlockSize()
1346
1347 @*/
VecSetBlockSize(Vec v,PetscInt bs)1348 PetscErrorCode VecSetBlockSize(Vec v,PetscInt bs)
1349 {
1350 PetscErrorCode ierr;
1351
1352 PetscFunctionBegin;
1353 PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1354 PetscValidLogicalCollectiveInt(v,bs,2);
1355 ierr = PetscLayoutSetBlockSize(v->map,bs);CHKERRQ(ierr);
1356 v->bstash.bs = bs; /* use the same blocksize for the vec's block-stash */
1357 PetscFunctionReturn(0);
1358 }
1359
1360 /*@
1361 VecGetBlockSize - Gets the blocksize for the vector, i.e. what is used for VecSetValuesBlocked()
1362 and VecSetValuesBlockedLocal().
1363
1364 Not Collective
1365
1366 Input Parameter:
1367 . v - the vector
1368
1369 Output Parameter:
1370 . bs - the blocksize
1371
1372 Notes:
1373 All vectors obtained by VecDuplicate() inherit the same blocksize.
1374
1375 Level: advanced
1376
1377 .seealso: VecSetValuesBlocked(), VecSetLocalToGlobalMapping(), VecSetBlockSize()
1378
1379
1380 @*/
VecGetBlockSize(Vec v,PetscInt * bs)1381 PetscErrorCode VecGetBlockSize(Vec v,PetscInt *bs)
1382 {
1383 PetscErrorCode ierr;
1384
1385 PetscFunctionBegin;
1386 PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1387 PetscValidIntPointer(bs,2);
1388 ierr = PetscLayoutGetBlockSize(v->map,bs);CHKERRQ(ierr);
1389 PetscFunctionReturn(0);
1390 }
1391
1392 /*@C
1393 VecSetOptionsPrefix - Sets the prefix used for searching for all
1394 Vec options in the database.
1395
1396 Logically Collective on Vec
1397
1398 Input Parameter:
1399 + v - the Vec context
1400 - prefix - the prefix to prepend to all option names
1401
1402 Notes:
1403 A hyphen (-) must NOT be given at the beginning of the prefix name.
1404 The first character of all runtime options is AUTOMATICALLY the hyphen.
1405
1406 Level: advanced
1407
1408 .seealso: VecSetFromOptions()
1409 @*/
VecSetOptionsPrefix(Vec v,const char prefix[])1410 PetscErrorCode VecSetOptionsPrefix(Vec v,const char prefix[])
1411 {
1412 PetscErrorCode ierr;
1413
1414 PetscFunctionBegin;
1415 PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1416 ierr = PetscObjectSetOptionsPrefix((PetscObject)v,prefix);CHKERRQ(ierr);
1417 PetscFunctionReturn(0);
1418 }
1419
1420 /*@C
1421 VecAppendOptionsPrefix - Appends to the prefix used for searching for all
1422 Vec options in the database.
1423
1424 Logically Collective on Vec
1425
1426 Input Parameters:
1427 + v - the Vec context
1428 - prefix - the prefix to prepend to all option names
1429
1430 Notes:
1431 A hyphen (-) must NOT be given at the beginning of the prefix name.
1432 The first character of all runtime options is AUTOMATICALLY the hyphen.
1433
1434 Level: advanced
1435
1436 .seealso: VecGetOptionsPrefix()
1437 @*/
VecAppendOptionsPrefix(Vec v,const char prefix[])1438 PetscErrorCode VecAppendOptionsPrefix(Vec v,const char prefix[])
1439 {
1440 PetscErrorCode ierr;
1441
1442 PetscFunctionBegin;
1443 PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1444 ierr = PetscObjectAppendOptionsPrefix((PetscObject)v,prefix);CHKERRQ(ierr);
1445 PetscFunctionReturn(0);
1446 }
1447
1448 /*@C
1449 VecGetOptionsPrefix - Sets the prefix used for searching for all
1450 Vec options in the database.
1451
1452 Not Collective
1453
1454 Input Parameter:
1455 . v - the Vec context
1456
1457 Output Parameter:
1458 . prefix - pointer to the prefix string used
1459
1460 Notes:
1461 On the fortran side, the user should pass in a string 'prefix' of
1462 sufficient length to hold the prefix.
1463
1464 Level: advanced
1465
1466 .seealso: VecAppendOptionsPrefix()
1467 @*/
VecGetOptionsPrefix(Vec v,const char * prefix[])1468 PetscErrorCode VecGetOptionsPrefix(Vec v,const char *prefix[])
1469 {
1470 PetscErrorCode ierr;
1471
1472 PetscFunctionBegin;
1473 PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1474 ierr = PetscObjectGetOptionsPrefix((PetscObject)v,prefix);CHKERRQ(ierr);
1475 PetscFunctionReturn(0);
1476 }
1477
1478 /*@
1479 VecSetUp - Sets up the internal vector data structures for the later use.
1480
1481 Collective on Vec
1482
1483 Input Parameters:
1484 . v - the Vec context
1485
1486 Notes:
1487 For basic use of the Vec classes the user need not explicitly call
1488 VecSetUp(), since these actions will happen automatically.
1489
1490 Level: advanced
1491
1492 .seealso: VecCreate(), VecDestroy()
1493 @*/
VecSetUp(Vec v)1494 PetscErrorCode VecSetUp(Vec v)
1495 {
1496 PetscMPIInt size;
1497 PetscErrorCode ierr;
1498
1499 PetscFunctionBegin;
1500 PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1501 if (v->map->n < 0 && v->map->N < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Sizes not set");
1502 if (!((PetscObject)v)->type_name) {
1503 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)v), &size);CHKERRQ(ierr);
1504 if (size == 1) {
1505 ierr = VecSetType(v, VECSEQ);CHKERRQ(ierr);
1506 } else {
1507 ierr = VecSetType(v, VECMPI);CHKERRQ(ierr);
1508 }
1509 }
1510 PetscFunctionReturn(0);
1511 }
1512
1513 /*
1514 These currently expose the PetscScalar/PetscReal in updating the
1515 cached norm. If we push those down into the implementation these
1516 will become independent of PetscScalar/PetscReal
1517 */
1518
1519 /*@
1520 VecCopy - Copies a vector. y <- x
1521
1522 Logically Collective on Vec
1523
1524 Input Parameter:
1525 . x - the vector
1526
1527 Output Parameter:
1528 . y - the copy
1529
1530 Notes:
1531 For default parallel PETSc vectors, both x and y must be distributed in
1532 the same manner; local copies are done.
1533
1534 Developer Notes:
1535 PetscCheckSameTypeAndComm(x,1,y,2) is not used on these vectors because we allow one
1536 of the vectors to be sequential and one to be parallel so long as both have the same
1537 local sizes. This is used in some internal functions in PETSc.
1538
1539 Level: beginner
1540
1541 .seealso: VecDuplicate()
1542 @*/
VecCopy(Vec x,Vec y)1543 PetscErrorCode VecCopy(Vec x,Vec y)
1544 {
1545 PetscBool flgs[4];
1546 PetscReal norms[4] = {0.0,0.0,0.0,0.0};
1547 PetscErrorCode ierr;
1548 PetscInt i;
1549
1550 PetscFunctionBegin;
1551 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
1552 PetscValidHeaderSpecific(y,VEC_CLASSID,2);
1553 PetscValidType(x,1);
1554 PetscValidType(y,2);
1555 if (x == y) PetscFunctionReturn(0);
1556 VecCheckSameLocalSize(x,1,y,2);
1557 if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1558 ierr = VecSetErrorIfLocked(y,2);CHKERRQ(ierr);
1559
1560 #if !defined(PETSC_USE_MIXED_PRECISION)
1561 for (i=0; i<4; i++) {
1562 ierr = PetscObjectComposedDataGetReal((PetscObject)x,NormIds[i],norms[i],flgs[i]);CHKERRQ(ierr);
1563 }
1564 #endif
1565
1566 ierr = PetscLogEventBegin(VEC_Copy,x,y,0,0);CHKERRQ(ierr);
1567 #if defined(PETSC_USE_MIXED_PRECISION)
1568 extern PetscErrorCode VecGetArray(Vec,double**);
1569 extern PetscErrorCode VecRestoreArray(Vec,double**);
1570 extern PetscErrorCode VecGetArray(Vec,float**);
1571 extern PetscErrorCode VecRestoreArray(Vec,float**);
1572 extern PetscErrorCode VecGetArrayRead(Vec,const double**);
1573 extern PetscErrorCode VecRestoreArrayRead(Vec,const double**);
1574 extern PetscErrorCode VecGetArrayRead(Vec,const float**);
1575 extern PetscErrorCode VecRestoreArrayRead(Vec,const float**);
1576 if ((((PetscObject)x)->precision == PETSC_PRECISION_SINGLE) && (((PetscObject)y)->precision == PETSC_PRECISION_DOUBLE)) {
1577 PetscInt i,n;
1578 const float *xx;
1579 double *yy;
1580 ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
1581 ierr = VecGetArray(y,&yy);CHKERRQ(ierr);
1582 ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
1583 for (i=0; i<n; i++) yy[i] = xx[i];
1584 ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
1585 ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr);
1586 } else if ((((PetscObject)x)->precision == PETSC_PRECISION_DOUBLE) && (((PetscObject)y)->precision == PETSC_PRECISION_SINGLE)) {
1587 PetscInt i,n;
1588 float *yy;
1589 const double *xx;
1590 ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
1591 ierr = VecGetArray(y,&yy);CHKERRQ(ierr);
1592 ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
1593 for (i=0; i<n; i++) yy[i] = (float) xx[i];
1594 ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
1595 ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr);
1596 } else {
1597 ierr = (*x->ops->copy)(x,y);CHKERRQ(ierr);
1598 }
1599 #else
1600 ierr = (*x->ops->copy)(x,y);CHKERRQ(ierr);
1601 #endif
1602
1603 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr);
1604 #if !defined(PETSC_USE_MIXED_PRECISION)
1605 for (i=0; i<4; i++) {
1606 if (flgs[i]) {
1607 ierr = PetscObjectComposedDataSetReal((PetscObject)y,NormIds[i],norms[i]);CHKERRQ(ierr);
1608 }
1609 }
1610 #endif
1611
1612 ierr = PetscLogEventEnd(VEC_Copy,x,y,0,0);CHKERRQ(ierr);
1613 PetscFunctionReturn(0);
1614 }
1615
1616 /*@
1617 VecSwap - Swaps the vectors x and y.
1618
1619 Logically Collective on Vec
1620
1621 Input Parameters:
1622 . x, y - the vectors
1623
1624 Level: advanced
1625
1626 @*/
VecSwap(Vec x,Vec y)1627 PetscErrorCode VecSwap(Vec x,Vec y)
1628 {
1629 PetscReal normxs[4]={0.0,0.0,0.0,0.0},normys[4]={0.0,0.0,0.0,0.0};
1630 PetscBool flgxs[4],flgys[4];
1631 PetscErrorCode ierr;
1632 PetscInt i;
1633
1634 PetscFunctionBegin;
1635 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
1636 PetscValidHeaderSpecific(y,VEC_CLASSID,2);
1637 PetscValidType(x,1);
1638 PetscValidType(y,2);
1639 PetscCheckSameTypeAndComm(x,1,y,2);
1640 VecCheckSameSize(x,1,y,2);
1641 if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1642 if (y->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1643 ierr = VecSetErrorIfLocked(x,1);CHKERRQ(ierr);
1644 ierr = VecSetErrorIfLocked(y,2);CHKERRQ(ierr);
1645
1646 ierr = PetscLogEventBegin(VEC_Swap,x,y,0,0);CHKERRQ(ierr);
1647 for (i=0; i<4; i++) {
1648 ierr = PetscObjectComposedDataGetReal((PetscObject)x,NormIds[i],normxs[i],flgxs[i]);CHKERRQ(ierr);
1649 ierr = PetscObjectComposedDataGetReal((PetscObject)y,NormIds[i],normys[i],flgys[i]);CHKERRQ(ierr);
1650 }
1651 ierr = (*x->ops->swap)(x,y);CHKERRQ(ierr);
1652 ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
1653 ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr);
1654 for (i=0; i<4; i++) {
1655 if (flgxs[i]) {
1656 ierr = PetscObjectComposedDataSetReal((PetscObject)y,NormIds[i],normxs[i]);CHKERRQ(ierr);
1657 }
1658 if (flgys[i]) {
1659 ierr = PetscObjectComposedDataSetReal((PetscObject)x,NormIds[i],normys[i]);CHKERRQ(ierr);
1660 }
1661 }
1662 ierr = PetscLogEventEnd(VEC_Swap,x,y,0,0);CHKERRQ(ierr);
1663 PetscFunctionReturn(0);
1664 }
1665
1666 /*
1667 VecStashViewFromOptions - Processes command line options to determine if/how an VecStash object is to be viewed.
1668
1669 Collective on VecStash
1670
1671 Input Parameters:
1672 + obj - the VecStash object
1673 . bobj - optional other object that provides the prefix
1674 - optionname - option to activate viewing
1675
1676 Level: intermediate
1677
1678 Developer Note: This cannot use PetscObjectViewFromOptions() because it takes a Vec as an argument but does not use VecView
1679
1680 */
VecStashViewFromOptions(Vec obj,PetscObject bobj,const char optionname[])1681 PetscErrorCode VecStashViewFromOptions(Vec obj,PetscObject bobj,const char optionname[])
1682 {
1683 PetscErrorCode ierr;
1684 PetscViewer viewer;
1685 PetscBool flg;
1686 PetscViewerFormat format;
1687 char *prefix;
1688
1689 PetscFunctionBegin;
1690 prefix = bobj ? bobj->prefix : ((PetscObject)obj)->prefix;
1691 ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)obj),((PetscObject)obj)->options,prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr);
1692 if (flg) {
1693 ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
1694 ierr = VecStashView(obj,viewer);CHKERRQ(ierr);
1695 ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
1696 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1697 }
1698 PetscFunctionReturn(0);
1699 }
1700
1701 /*@
1702 VecStashView - Prints the entries in the vector stash and block stash.
1703
1704 Collective on Vec
1705
1706 Input Parameters:
1707 + v - the vector
1708 - viewer - the viewer
1709
1710 Level: advanced
1711
1712
1713 .seealso: VecSetBlockSize(), VecSetValues(), VecSetValuesBlocked()
1714
1715 @*/
VecStashView(Vec v,PetscViewer viewer)1716 PetscErrorCode VecStashView(Vec v,PetscViewer viewer)
1717 {
1718 PetscErrorCode ierr;
1719 PetscMPIInt rank;
1720 PetscInt i,j;
1721 PetscBool match;
1722 VecStash *s;
1723 PetscScalar val;
1724
1725 PetscFunctionBegin;
1726 PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1727 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1728 PetscCheckSameComm(v,1,viewer,2);
1729
1730 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&match);CHKERRQ(ierr);
1731 if (!match) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Stash viewer only works with ASCII viewer not %s\n",((PetscObject)v)->type_name);
1732 ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
1733 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRQ(ierr);
1734 s = &v->bstash;
1735
1736 /* print block stash */
1737 ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
1738 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d]Vector Block stash size %D block size %D\n",rank,s->n,s->bs);CHKERRQ(ierr);
1739 for (i=0; i<s->n; i++) {
1740 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D ",rank,s->idx[i]);CHKERRQ(ierr);
1741 for (j=0; j<s->bs; j++) {
1742 val = s->array[i*s->bs+j];
1743 #if defined(PETSC_USE_COMPLEX)
1744 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"(%18.16e %18.16e) ",PetscRealPart(val),PetscImaginaryPart(val));CHKERRQ(ierr);
1745 #else
1746 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%18.16e ",val);CHKERRQ(ierr);
1747 #endif
1748 }
1749 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
1750 }
1751 ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1752
1753 s = &v->stash;
1754
1755 /* print basic stash */
1756 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d]Vector stash size %D\n",rank,s->n);CHKERRQ(ierr);
1757 for (i=0; i<s->n; i++) {
1758 val = s->array[i];
1759 #if defined(PETSC_USE_COMPLEX)
1760 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D (%18.16e %18.16e) ",rank,s->idx[i],PetscRealPart(val),PetscImaginaryPart(val));CHKERRQ(ierr);
1761 #else
1762 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D %18.16e\n",rank,s->idx[i],val);CHKERRQ(ierr);
1763 #endif
1764 }
1765 ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1766 ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
1767 ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
1768 PetscFunctionReturn(0);
1769 }
1770
PetscOptionsGetVec(PetscOptions options,const char prefix[],const char key[],Vec v,PetscBool * set)1771 PetscErrorCode PetscOptionsGetVec(PetscOptions options,const char prefix[],const char key[],Vec v,PetscBool *set)
1772 {
1773 PetscInt i,N,rstart,rend;
1774 PetscErrorCode ierr;
1775 PetscScalar *xx;
1776 PetscReal *xreal;
1777 PetscBool iset;
1778
1779 PetscFunctionBegin;
1780 ierr = VecGetOwnershipRange(v,&rstart,&rend);CHKERRQ(ierr);
1781 ierr = VecGetSize(v,&N);CHKERRQ(ierr);
1782 ierr = PetscCalloc1(N,&xreal);CHKERRQ(ierr);
1783 ierr = PetscOptionsGetRealArray(options,prefix,key,xreal,&N,&iset);CHKERRQ(ierr);
1784 if (iset) {
1785 ierr = VecGetArray(v,&xx);CHKERRQ(ierr);
1786 for (i=rstart; i<rend; i++) xx[i-rstart] = xreal[i];
1787 ierr = VecRestoreArray(v,&xx);CHKERRQ(ierr);
1788 }
1789 ierr = PetscFree(xreal);CHKERRQ(ierr);
1790 if (set) *set = iset;
1791 PetscFunctionReturn(0);
1792 }
1793
1794 /*@
1795 VecGetLayout - get PetscLayout describing vector layout
1796
1797 Not Collective
1798
1799 Input Arguments:
1800 . x - the vector
1801
1802 Output Arguments:
1803 . map - the layout
1804
1805 Level: developer
1806
1807 .seealso: VecGetSizes(), VecGetOwnershipRange(), VecGetOwnershipRanges()
1808 @*/
VecGetLayout(Vec x,PetscLayout * map)1809 PetscErrorCode VecGetLayout(Vec x,PetscLayout *map)
1810 {
1811
1812 PetscFunctionBegin;
1813 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
1814 *map = x->map;
1815 PetscFunctionReturn(0);
1816 }
1817
1818 /*@
1819 VecSetLayout - set PetscLayout describing vector layout
1820
1821 Not Collective
1822
1823 Input Arguments:
1824 + x - the vector
1825 - map - the layout
1826
1827 Notes:
1828 It is normally only valid to replace the layout with a layout known to be equivalent.
1829
1830 Level: developer
1831
1832 .seealso: VecGetLayout(), VecGetSizes(), VecGetOwnershipRange(), VecGetOwnershipRanges()
1833 @*/
VecSetLayout(Vec x,PetscLayout map)1834 PetscErrorCode VecSetLayout(Vec x,PetscLayout map)
1835 {
1836 PetscErrorCode ierr;
1837
1838 PetscFunctionBegin;
1839 PetscValidHeaderSpecific(x,VEC_CLASSID,1);
1840 ierr = PetscLayoutReference(map,&x->map);CHKERRQ(ierr);
1841 PetscFunctionReturn(0);
1842 }
1843
VecSetInf(Vec xin)1844 PetscErrorCode VecSetInf(Vec xin)
1845 {
1846 PetscInt i,n = xin->map->n;
1847 PetscScalar *xx;
1848 PetscScalar zero=0.0,one=1.0,inf=one/zero;
1849 PetscErrorCode ierr;
1850
1851 PetscFunctionBegin;
1852 ierr = VecGetArray(xin,&xx);CHKERRQ(ierr);
1853 for (i=0; i<n; i++) xx[i] = inf;
1854 ierr = VecRestoreArray(xin,&xx);CHKERRQ(ierr);
1855 PetscFunctionReturn(0);
1856 }
1857
1858 /*@
1859 VecBindToCPU - marks a vector to temporarily stay on the CPU and perform computations on the CPU
1860
1861 Input Parameters:
1862 + v - the vector
1863 - flg - bind to the CPU if value of PETSC_TRUE
1864
1865 Level: intermediate
1866 @*/
VecBindToCPU(Vec v,PetscBool flg)1867 PetscErrorCode VecBindToCPU(Vec v,PetscBool flg)
1868 {
1869 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1870 PetscErrorCode ierr;
1871
1872 PetscFunctionBegin;
1873 if (v->boundtocpu == flg) PetscFunctionReturn(0);
1874 v->boundtocpu = flg;
1875 if (v->ops->bindtocpu) {
1876 ierr = (*v->ops->bindtocpu)(v,flg);CHKERRQ(ierr);
1877 }
1878 PetscFunctionReturn(0);
1879 #else
1880 return 0;
1881 #endif
1882 }
1883
1884 /*@C
1885 VecSetPinnedMemoryMin - Set the minimum data size for which pinned memory will be used for host (CPU) allocations.
1886
1887 Logically Collective on Vec
1888
1889 Input Parameters:
1890 + v - the vector
1891 - mbytes - minimum data size in bytes
1892
1893 Options Database Keys:
1894
1895 . -vec_pinned_memory_min <size> - minimum size (in bytes) for an allocation to use pinned memory on host.
1896 Note that this takes a PetscScalar, to accommodate large values;
1897 specifying -1 ensures that pinned memory will never be used.
1898
1899 Level: developer
1900
1901 .seealso: VecGetPinnedMemoryMin()
1902 @*/
VecSetPinnedMemoryMin(Vec v,size_t mbytes)1903 PetscErrorCode VecSetPinnedMemoryMin(Vec v,size_t mbytes)
1904 {
1905 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1906 PetscFunctionBegin;
1907 v->minimum_bytes_pinned_memory = mbytes;
1908 PetscFunctionReturn(0);
1909 #else
1910 return 0;
1911 #endif
1912 }
1913
1914 /*@C
1915 VecGetPinnedMemoryMin - Get the minimum data size for which pinned memory will be used for host (CPU) allocations.
1916
1917 Logically Collective on Vec
1918
1919 Input Parameters:
1920 . v - the vector
1921
1922 Output Parameters:
1923 . mbytes - minimum data size in bytes
1924
1925 Level: developer
1926
1927 .seealso: VecSetPinnedMemoryMin()
1928 @*/
VecGetPinnedMemoryMin(Vec v,size_t * mbytes)1929 PetscErrorCode VecGetPinnedMemoryMin(Vec v,size_t *mbytes)
1930 {
1931 #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1932 PetscFunctionBegin;
1933 *mbytes = v->minimum_bytes_pinned_memory;
1934 PetscFunctionReturn(0);
1935 #else
1936 return 0;
1937 #endif
1938 }
1939
1940 /*@
1941 VecGetOffloadMask - Get the offload mask of a Vec.
1942
1943 Not Collective
1944
1945 Input Parameters:
1946 . v - the vector
1947
1948 Output Parameters:
1949 . mask - corresponding PetscOffloadMask enum value.
1950
1951 Level: intermediate
1952
1953 .seealso: VecCreateSeqCUDA(), VecCreateSeqViennaCL(), VecGetArray(), VecGetType()
1954 @*/
VecGetOffloadMask(Vec v,PetscOffloadMask * mask)1955 PetscErrorCode VecGetOffloadMask(Vec v,PetscOffloadMask* mask)
1956 {
1957 PetscFunctionBegin;
1958 *mask = v->offloadmask;
1959 PetscFunctionReturn(0);
1960 }
1961