Actual source code: vector.c
petsc-3.5.1 2014-07-24
2: /*
3: Provides the interface functions for vector operations that do NOT have PetscScalar/PetscReal in the signature
4: These are the vector functions the user calls.
5: */
6: #include <petsc-private/vecimpl.h> /*I "petscvec.h" I*/
8: /* Logging support */
9: PetscClassId VEC_CLASSID;
10: PetscLogEvent VEC_View, VEC_Max, VEC_Min, VEC_DotBarrier, VEC_Dot, VEC_MDotBarrier, VEC_MDot, VEC_TDot;
11: PetscLogEvent VEC_Norm, VEC_Normalize, VEC_Scale, VEC_Copy, VEC_Set, VEC_AXPY, VEC_AYPX, VEC_WAXPY;
12: PetscLogEvent VEC_MTDot, VEC_NormBarrier, VEC_MAXPY, VEC_Swap, VEC_AssemblyBegin, VEC_ScatterBegin, VEC_ScatterEnd;
13: PetscLogEvent VEC_AssemblyEnd, VEC_PointwiseMult, VEC_SetValues, VEC_Load, VEC_ScatterBarrier;
14: PetscLogEvent VEC_SetRandom, VEC_ReduceArithmetic, VEC_ReduceBarrier, VEC_ReduceCommunication,VEC_ReduceBegin,VEC_ReduceEnd,VEC_Ops;
15: PetscLogEvent VEC_DotNormBarrier, VEC_DotNorm, VEC_AXPBYPCZ, VEC_CUSPCopyFromGPU, VEC_CUSPCopyToGPU;
16: PetscLogEvent VEC_CUSPCopyFromGPUSome, VEC_CUSPCopyToGPUSome;
17: PetscLogEvent VEC_ViennaCLCopyFromGPU, VEC_ViennaCLCopyToGPU;
19: extern PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
22: /*@
23: VecStashGetInfo - Gets how many values are currently in the vector stash, i.e. need
24: to be communicated to other processors during the VecAssemblyBegin/End() process
26: Not collective
28: Input Parameter:
29: . vec - the vector
31: Output Parameters:
32: + nstash - the size of the stash
33: . reallocs - the number of additional mallocs incurred.
34: . bnstash - the size of the block stash
35: - breallocs - the number of additional mallocs incurred.in the block stash
37: Level: advanced
39: .seealso: VecAssemblyBegin(), VecAssemblyEnd(), Vec, VecStashSetInitialSize(), VecStashView()
41: @*/
42: PetscErrorCode VecStashGetInfo(Vec vec,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
43: {
47: VecStashGetInfo_Private(&vec->stash,nstash,reallocs);
48: VecStashGetInfo_Private(&vec->bstash,bnstash,breallocs);
49: return(0);
50: }
54: /*@
55: VecSetLocalToGlobalMapping - Sets a local numbering to global numbering used
56: by the routine VecSetValuesLocal() to allow users to insert vector entries
57: using a local (per-processor) numbering.
59: Logically Collective on Vec
61: Input Parameters:
62: + x - vector
63: - mapping - mapping created with ISLocalToGlobalMappingCreate() or ISLocalToGlobalMappingCreateIS()
65: Notes:
66: All vectors obtained with VecDuplicate() from this vector inherit the same mapping.
68: Level: intermediate
70: Concepts: vector^setting values with local numbering
72: seealso: VecAssemblyBegin(), VecAssemblyEnd(), VecSetValues(), VecSetValuesLocal(),
73: VecSetLocalToGlobalMapping(), VecSetValuesBlockedLocal()
74: @*/
75: PetscErrorCode VecSetLocalToGlobalMapping(Vec x,ISLocalToGlobalMapping mapping)
76: {
83: if (x->ops->setlocaltoglobalmapping) {
84: (*x->ops->setlocaltoglobalmapping)(x,mapping);
85: } else {
86: PetscLayoutSetISLocalToGlobalMapping(x->map,mapping);
87: }
88: return(0);
89: }
93: /*@
94: VecGetLocalToGlobalMapping - Gets the local-to-global numbering set by VecSetLocalToGlobalMapping()
96: Not Collective
98: Input Parameter:
99: . X - the vector
101: Output Parameter:
102: . mapping - the mapping
104: Level: advanced
106: Concepts: vectors^local to global mapping
107: Concepts: local to global mapping^for vectors
109: .seealso: VecSetValuesLocal()
110: @*/
111: PetscErrorCode VecGetLocalToGlobalMapping(Vec X,ISLocalToGlobalMapping *mapping)
112: {
117: *mapping = X->map->mapping;
118: return(0);
119: }
123: /*@
124: VecAssemblyBegin - Begins assembling the vector. This routine should
125: be called after completing all calls to VecSetValues().
127: Collective on Vec
129: Input Parameter:
130: . vec - the vector
132: Level: beginner
134: Concepts: assembly^vectors
136: .seealso: VecAssemblyEnd(), VecSetValues()
137: @*/
138: PetscErrorCode VecAssemblyBegin(Vec vec)
139: {
145: VecStashViewFromOptions(vec,NULL,"-vec_view_stash");
146: PetscLogEventBegin(VEC_AssemblyBegin,vec,0,0,0);
147: if (vec->ops->assemblybegin) {
148: (*vec->ops->assemblybegin)(vec);
149: }
150: PetscLogEventEnd(VEC_AssemblyBegin,vec,0,0,0);
151: PetscObjectStateIncrease((PetscObject)vec);
152: return(0);
153: }
157: /*@
158: VecAssemblyEnd - Completes assembling the vector. This routine should
159: be called after VecAssemblyBegin().
161: Collective on Vec
163: Input Parameter:
164: . vec - the vector
166: Options Database Keys:
167: + -vec_view - Prints vector in ASCII format
168: . -vec_view ::ascii_matlab - Prints vector in ASCII MATLAB format to stdout
169: . -vec_view matlab:filename - Prints vector in MATLAB format to matlaboutput.mat
170: . -vec_view draw - Activates vector viewing using drawing tools
171: . -display <name> - Sets display name (default is host)
172: . -draw_pause <sec> - Sets number of seconds to pause after display
173: - -vec_view socket - Activates vector viewing using a socket
175: Level: beginner
177: .seealso: VecAssemblyBegin(), VecSetValues()
178: @*/
179: PetscErrorCode VecAssemblyEnd(Vec vec)
180: {
185: PetscLogEventBegin(VEC_AssemblyEnd,vec,0,0,0);
187: if (vec->ops->assemblyend) {
188: (*vec->ops->assemblyend)(vec);
189: }
190: PetscLogEventEnd(VEC_AssemblyEnd,vec,0,0,0);
191: VecViewFromOptions(vec,NULL,"-vec_view");
192: return(0);
193: }
197: /*@
198: VecPointwiseMax - Computes the componentwise maximum w_i = max(x_i, y_i).
200: Logically Collective on Vec
202: Input Parameters:
203: . x, y - the vectors
205: Output Parameter:
206: . w - the result
208: Level: advanced
210: Notes: any subset of the x, y, and w may be the same vector.
211: For complex numbers compares only the real part
213: Concepts: vector^pointwise multiply
215: .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
216: @*/
217: PetscErrorCode VecPointwiseMax(Vec w,Vec x,Vec y)
218: {
230: if (x->map->N != y->map->N || x->map->N != w->map->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths");
231: if (x->map->n != y->map->n || x->map->n != w->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");
233: (*w->ops->pointwisemax)(w,x,y);
234: PetscObjectStateIncrease((PetscObject)w);
235: return(0);
236: }
241: /*@
242: VecPointwiseMin - Computes the componentwise minimum w_i = min(x_i, y_i).
244: Logically Collective on Vec
246: Input Parameters:
247: . x, y - the vectors
249: Output Parameter:
250: . w - the result
252: Level: advanced
254: Notes: any subset of the x, y, and w may be the same vector.
255: For complex numbers compares only the real part
257: Concepts: vector^pointwise multiply
259: .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
260: @*/
261: PetscErrorCode VecPointwiseMin(Vec w,Vec x,Vec y)
262: {
274: if (x->map->N != y->map->N || x->map->N != w->map->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths");
275: if (x->map->n != y->map->n || x->map->n != w->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");
277: (*w->ops->pointwisemin)(w,x,y);
278: PetscObjectStateIncrease((PetscObject)w);
279: return(0);
280: }
284: /*@
285: VecPointwiseMaxAbs - Computes the componentwise maximum of the absolute values w_i = max(abs(x_i), abs(y_i)).
287: Logically Collective on Vec
289: Input Parameters:
290: . x, y - the vectors
292: Output Parameter:
293: . w - the result
295: Level: advanced
297: Notes: any subset of the x, y, and w may be the same vector.
299: Concepts: vector^pointwise multiply
301: .seealso: VecPointwiseDivide(), VecPointwiseMult(), VecPointwiseMin(), VecPointwiseMax(), VecMaxPointwiseDivide()
302: @*/
303: PetscErrorCode VecPointwiseMaxAbs(Vec w,Vec x,Vec y)
304: {
316: if (x->map->N != y->map->N || x->map->N != w->map->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths");
317: if (x->map->n != y->map->n || x->map->n != w->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");
319: (*w->ops->pointwisemaxabs)(w,x,y);
320: PetscObjectStateIncrease((PetscObject)w);
321: return(0);
322: }
326: /*@
327: VecPointwiseDivide - Computes the componentwise division w = x/y.
329: Logically Collective on Vec
331: Input Parameters:
332: . x, y - the vectors
334: Output Parameter:
335: . w - the result
337: Level: advanced
339: Notes: any subset of the x, y, and w may be the same vector.
341: Concepts: vector^pointwise divide
343: .seealso: VecPointwiseMult(), VecPointwiseMax(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
344: @*/
345: PetscErrorCode VecPointwiseDivide(Vec w,Vec x,Vec y)
346: {
358: if (x->map->N != y->map->N || x->map->N != w->map->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths");
359: if (x->map->n != y->map->n || x->map->n != w->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");
361: (*w->ops->pointwisedivide)(w,x,y);
362: PetscObjectStateIncrease((PetscObject)w);
363: return(0);
364: }
369: /*@
370: VecDuplicate - Creates a new vector of the same type as an existing vector.
372: Collective on Vec
374: Input Parameters:
375: . v - a vector to mimic
377: Output Parameter:
378: . newv - location to put new vector
380: Notes:
381: VecDuplicate() DOES NOT COPY the vector entries, but rather allocates storage
382: for the new vector. Use VecCopy() to copy a vector.
384: Use VecDestroy() to free the space. Use VecDuplicateVecs() to get several
385: vectors.
387: Level: beginner
389: .seealso: VecDestroy(), VecDuplicateVecs(), VecCreate(), VecCopy()
390: @*/
391: PetscErrorCode VecDuplicate(Vec v,Vec *newv)
392: {
399: (*v->ops->duplicate)(v,newv);
400: PetscObjectStateIncrease((PetscObject)*newv);
401: return(0);
402: }
406: /*@
407: VecDestroy - Destroys a vector.
409: Collective on Vec
411: Input Parameters:
412: . v - the vector
414: Level: beginner
416: .seealso: VecDuplicate(), VecDestroyVecs()
417: @*/
418: PetscErrorCode VecDestroy(Vec *v)
419: {
423: if (!*v) return(0);
425: if (--((PetscObject)(*v))->refct > 0) {*v = 0; return(0);}
427: PetscObjectSAWsViewOff((PetscObject)*v);
428: /* destroy the internal part */
429: if ((*v)->ops->destroy) {
430: (*(*v)->ops->destroy)(*v);
431: }
432: /* destroy the external/common part */
433: PetscLayoutDestroy(&(*v)->map);
434: PetscHeaderDestroy(v);
435: return(0);
436: }
440: /*@C
441: VecDuplicateVecs - Creates several vectors of the same type as an existing vector.
443: Collective on Vec
445: Input Parameters:
446: + m - the number of vectors to obtain
447: - v - a vector to mimic
449: Output Parameter:
450: . V - location to put pointer to array of vectors
452: Notes:
453: Use VecDestroyVecs() to free the space. Use VecDuplicate() to form a single
454: vector.
456: Fortran Note:
457: The Fortran interface is slightly different from that given below, it
458: requires one to pass in V a Vec (integer) array of size at least m.
459: See the Fortran chapter of the users manual and petsc/src/vec/vec/examples for details.
461: Level: intermediate
463: .seealso: VecDestroyVecs(), VecDuplicate(), VecCreate(), VecDuplicateVecsF90()
464: @*/
465: PetscErrorCode VecDuplicateVecs(Vec v,PetscInt m,Vec *V[])
466: {
473: (*v->ops->duplicatevecs)(v, m,V);
474: return(0);
475: }
479: /*@C
480: VecDestroyVecs - Frees a block of vectors obtained with VecDuplicateVecs().
482: Collective on Vec
484: Input Parameters:
485: + vv - pointer to pointer to array of vector pointers
486: - m - the number of vectors previously obtained
488: Fortran Note:
489: The Fortran interface is slightly different from that given below.
490: See the Fortran chapter of the users manual
492: Level: intermediate
494: .seealso: VecDuplicateVecs(), VecDestroyVecsf90()
495: @*/
496: PetscErrorCode VecDestroyVecs(PetscInt m,Vec *vv[])
497: {
502: if (!*vv) return(0);
505: if (m < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of vectors %D",m);
506: (*(**vv)->ops->destroyvecs)(m,*vv);
507: *vv = 0;
508: return(0);
509: }
513: /*@C
514: VecView - Views a vector object.
516: Collective on Vec
518: Input Parameters:
519: + vec - the vector
520: - viewer - an optional visualization context
522: Notes:
523: The available visualization contexts include
524: + PETSC_VIEWER_STDOUT_SELF - standard output (default)
525: - PETSC_VIEWER_STDOUT_WORLD - synchronized standard
526: output where only the first processor opens
527: the file. All other processors send their
528: data to the first processor to print.
530: You can change the format the vector is printed using the
531: option PetscViewerSetFormat().
533: The user can open alternative visualization contexts with
534: + PetscViewerASCIIOpen() - Outputs vector to a specified file
535: . PetscViewerBinaryOpen() - Outputs vector in binary to a
536: specified file; corresponding input uses VecLoad()
537: . PetscViewerDrawOpen() - Outputs vector to an X window display
538: - PetscViewerSocketOpen() - Outputs vector to Socket viewer
540: The user can call PetscViewerSetFormat() to specify the output
541: format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF,
542: PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen). Available formats include
543: + PETSC_VIEWER_DEFAULT - default, prints vector contents
544: . PETSC_VIEWER_ASCII_MATLAB - prints vector contents in MATLAB format
545: . PETSC_VIEWER_ASCII_INDEX - prints vector contents, including indices of vector elements
546: - PETSC_VIEWER_ASCII_COMMON - prints vector contents, using a
547: format common among all vector types
549: Notes for HDF5 Viewer: 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 vector to the HDF5 viewer (with different values,
551: obviously) several times, you must change its name each time before calling the VecView(). The name you use
552: here should equal the name that you use in the Vec object that you use with VecLoad().
554: See the manual page for VecLoad() on the exact format the binary viewer stores
555: the values in the file.
557: Level: beginner
559: Concepts: vector^printing
560: Concepts: vector^saving to disk
562: .seealso: PetscViewerASCIIOpen(), PetscViewerDrawOpen(), PetscDrawLGCreate(),
563: PetscViewerSocketOpen(), PetscViewerBinaryOpen(), VecLoad(), PetscViewerCreate(),
564: PetscRealView(), PetscScalarView(), PetscIntView()
565: @*/
566: PetscErrorCode VecView(Vec vec,PetscViewer viewer)
567: {
568: PetscErrorCode ierr;
569: PetscBool iascii;
570: PetscViewerFormat format;
575: if (!viewer) {
576: PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)vec),&viewer);
577: }
580: if (vec->stash.n || vec->bstash.n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call VecAssemblyBegin/End() before viewing this vector");
582: PetscLogEventBegin(VEC_View,vec,viewer,0,0);
583: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
584: if (iascii) {
585: PetscInt rows,bs;
587: PetscObjectPrintClassNamePrefixType((PetscObject)vec,viewer);
588: PetscViewerGetFormat(viewer,&format);
589: if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
590: PetscViewerASCIIPushTab(viewer);
591: VecGetSize(vec,&rows);
592: VecGetBlockSize(vec,&bs);
593: if (bs != 1) {
594: PetscViewerASCIIPrintf(viewer,"length=%D, bs=%D\n",rows,bs);
595: } else {
596: PetscViewerASCIIPrintf(viewer,"length=%D\n",rows);
597: }
598: PetscViewerASCIIPopTab(viewer);
599: }
600: }
601: (*vec->ops->view)(vec,viewer);
602: PetscLogEventEnd(VEC_View,vec,viewer,0,0);
603: return(0);
604: }
606: #if defined(PETSC_USE_DEBUG)
607: #include <../src/sys/totalview/tv_data_display.h>
608: PETSC_UNUSED static int TV_display_type(const struct _p_Vec *v)
609: {
610: const PetscScalar *values;
611: char type[32];
612: PetscErrorCode ierr;
615: TV_add_row("Local rows", "int", &v->map->n);
616: TV_add_row("Global rows", "int", &v->map->N);
617: TV_add_row("Typename", TV_ascii_string_type, ((PetscObject)v)->type_name);
618: VecGetArrayRead((Vec)v,&values);
619: PetscSNPrintf(type,32,"double[%d]",v->map->n);
620: TV_add_row("values",type, values);
621: VecRestoreArrayRead((Vec)v,&values);
622: return TV_format_OK;
623: }
624: #endif
628: /*@
629: VecGetSize - Returns the global number of elements of the vector.
631: Not Collective
633: Input Parameter:
634: . x - the vector
636: Output Parameters:
637: . size - the global length of the vector
639: Level: beginner
641: Concepts: vector^local size
643: .seealso: VecGetLocalSize()
644: @*/
645: PetscErrorCode VecGetSize(Vec x,PetscInt *size)
646: {
653: (*x->ops->getsize)(x,size);
654: return(0);
655: }
659: /*@
660: VecGetLocalSize - Returns the number of elements of the vector stored
661: in local memory. This routine may be implementation dependent, so use
662: with care.
664: Not Collective
666: Input Parameter:
667: . x - the vector
669: Output Parameter:
670: . size - the length of the local piece of the vector
672: Level: beginner
674: Concepts: vector^size
676: .seealso: VecGetSize()
677: @*/
678: PetscErrorCode VecGetLocalSize(Vec x,PetscInt *size)
679: {
686: (*x->ops->getlocalsize)(x,size);
687: return(0);
688: }
692: /*@C
693: VecGetOwnershipRange - Returns the range of indices owned by
694: this processor, assuming that the vectors are laid out with the
695: first n1 elements on the first processor, next n2 elements on the
696: second, etc. For certain parallel layouts this range may not be
697: well defined.
699: Not Collective
701: Input Parameter:
702: . x - the vector
704: Output Parameters:
705: + low - the first local element, pass in NULL if not interested
706: - high - one more than the last local element, pass in NULL if not interested
708: Note:
709: The high argument is one more than the last element stored locally.
711: Fortran: NULL_INTEGER should be used instead of NULL
713: Level: beginner
715: Concepts: ownership^of vectors
716: Concepts: vector^ownership of elements
718: .seealso: MatGetOwnershipRange(), MatGetOwnershipRanges(), VecGetOwnershipRanges()
719: @*/
720: PetscErrorCode VecGetOwnershipRange(Vec x,PetscInt *low,PetscInt *high)
721: {
727: if (low) *low = x->map->rstart;
728: if (high) *high = x->map->rend;
729: return(0);
730: }
734: /*@C
735: VecGetOwnershipRanges - Returns the range of indices owned by EACH processor,
736: assuming that the vectors are laid out with the
737: first n1 elements on the first processor, next n2 elements on the
738: second, etc. For certain parallel layouts this range may not be
739: well defined.
741: Not Collective
743: Input Parameter:
744: . x - the vector
746: Output Parameters:
747: . range - array of length size+1 with the start and end+1 for each process
749: Note:
750: The high argument is one more than the last element stored locally.
752: Fortran: You must PASS in an array of length size+1
754: Level: beginner
756: Concepts: ownership^of vectors
757: Concepts: vector^ownership of elements
759: .seealso: MatGetOwnershipRange(), MatGetOwnershipRanges(), VecGetOwnershipRange()
760: @*/
761: PetscErrorCode VecGetOwnershipRanges(Vec x,const PetscInt *ranges[])
762: {
768: PetscLayoutGetRanges(x->map,ranges);
769: return(0);
770: }
774: /*@
775: VecSetOption - Sets an option for controling a vector's behavior.
777: Collective on Vec
779: Input Parameter:
780: + x - the vector
781: . op - the option
782: - flag - turn the option on or off
784: Supported Options:
785: + VEC_IGNORE_OFF_PROC_ENTRIES, which causes VecSetValues() to ignore
786: entries destined to be stored on a separate processor. This can be used
787: to eliminate the global reduction in the VecAssemblyXXXX() if you know
788: that you have only used VecSetValues() to set local elements
789: . VEC_IGNORE_NEGATIVE_INDICES, which means you can pass negative indices
790: in ix in calls to VecSetValues() or VecGetValues(). These rows are simply
791: ignored.
793: Level: intermediate
795: @*/
796: PetscErrorCode VecSetOption(Vec x,VecOption op,PetscBool flag)
797: {
803: if (x->ops->setoption) {
804: (*x->ops->setoption)(x,op,flag);
805: }
806: return(0);
807: }
811: /* Default routines for obtaining and releasing; */
812: /* may be used by any implementation */
813: PetscErrorCode VecDuplicateVecs_Default(Vec w,PetscInt m,Vec *V[])
814: {
816: PetscInt i;
821: if (m <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"m must be > 0: m = %D",m);
822: PetscMalloc1(m,V);
823: for (i=0; i<m; i++) {VecDuplicate(w,*V+i);}
824: return(0);
825: }
829: PetscErrorCode VecDestroyVecs_Default(PetscInt m,Vec v[])
830: {
832: PetscInt i;
836: if (m <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"m must be > 0: m = %D",m);
837: for (i=0; i<m; i++) {VecDestroy(&v[i]);}
838: PetscFree(v);
839: return(0);
840: }
844: /*@
845: VecResetArray - Resets a vector to use its default memory. Call this
846: after the use of VecPlaceArray().
848: Not Collective
850: Input Parameters:
851: . vec - the vector
853: Level: developer
855: .seealso: VecGetArray(), VecRestoreArray(), VecReplaceArray(), VecPlaceArray()
857: @*/
858: PetscErrorCode VecResetArray(Vec vec)
859: {
865: if (vec->ops->resetarray) {
866: (*vec->ops->resetarray)(vec);
867: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot reset array in this type of vector");
868: PetscObjectStateIncrease((PetscObject)vec);
869: return(0);
870: }
874: /*@C
875: VecLoad - Loads a vector that has been stored in binary or HDF5 format
876: with VecView().
878: Collective on PetscViewer
880: Input Parameters:
881: + newvec - the newly loaded vector, this needs to have been created with VecCreate() or
882: some related function before a call to VecLoad().
883: - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
884: HDF5 file viewer, obtained from PetscViewerHDF5Open()
886: Level: intermediate
888: Notes:
889: Defaults to the standard Seq or MPI Vec, if you want some other type of Vec call VecSetFromOptions()
890: before calling this.
892: The input file must contain the full global vector, as
893: written by the routine VecView().
895: If the type or size of newvec is not set before a call to VecLoad, PETSc
896: sets the type and the local and global sizes.If type and/or
897: sizes are already set, then the same are used.
899: IF using HDF5, you must assign the Vec the same name as was used in the Vec
900: that was stored in the file using PetscObjectSetName(). Otherwise you will
901: get the error message: "Cannot H5DOpen2() with Vec name NAMEOFOBJECT"
903: Notes for advanced users:
904: Most users should not need to know the details of the binary storage
905: format, since VecLoad() and VecView() completely hide these details.
906: But for anyone who's interested, the standard binary matrix storage
907: format is
908: .vb
909: int VEC_FILE_CLASSID
910: int number of rows
911: PetscScalar *values of all entries
912: .ve
914: In addition, PETSc automatically does the byte swapping for
915: machines that store the bytes reversed, e.g. DEC alpha, freebsd,
916: linux, Windows and the paragon; thus if you write your own binary
917: read/write routines you have to swap the bytes; see PetscBinaryRead()
918: and PetscBinaryWrite() to see how this may be done.
920: Concepts: vector^loading from file
922: .seealso: PetscViewerBinaryOpen(), VecView(), MatLoad(), VecLoad()
923: @*/
924: PetscErrorCode VecLoad(Vec newvec, PetscViewer viewer)
925: {
927: PetscBool isbinary,ishdf5;
932: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
933: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
934: if (!isbinary && !ishdf5) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
936: PetscLogEventBegin(VEC_Load,viewer,0,0,0);
937: if (!((PetscObject)newvec)->type_name && !newvec->ops->create) {
938: VecSetType(newvec, VECSTANDARD);
939: }
940: (*newvec->ops->load)(newvec,viewer);
941: PetscLogEventEnd(VEC_Load,viewer,0,0,0);
942: return(0);
943: }
948: /*@
949: VecReciprocal - Replaces each component of a vector by its reciprocal.
951: Logically Collective on Vec
953: Input Parameter:
954: . vec - the vector
956: Output Parameter:
957: . vec - the vector reciprocal
959: Level: intermediate
961: Concepts: vector^reciprocal
963: .seealso: VecLog(), VecExp(), VecSqrtAbs()
965: @*/
966: PetscErrorCode VecReciprocal(Vec vec)
967: {
973: if (vec->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
974: if (!vec->ops->reciprocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Vector does not support reciprocal operation");
975: (*vec->ops->reciprocal)(vec);
976: PetscObjectStateIncrease((PetscObject)vec);
977: return(0);
978: }
982: /*@C
983: VecSetOperation - Allows user to set a vector operation.
985: Logically Collective on Vec
987: Input Parameters:
988: + vec - the vector
989: . op - the name of the operation
990: - f - the function that provides the operation.
992: Level: advanced
994: Usage:
995: $ PetscErrorCode userview(Vec,PetscViewer);
996: $ VecCreateMPI(comm,m,M,&x);
997: $ VecSetOperation(x,VECOP_VIEW,(void(*)(void))userview);
999: Notes:
1000: See the file include/petscvec.h for a complete list of matrix
1001: operations, which all have the form VECOP_<OPERATION>, where
1002: <OPERATION> is the name (in all capital letters) of the
1003: user interface routine (e.g., VecView() -> VECOP_VIEW).
1005: This function is not currently available from Fortran.
1007: .keywords: vector, set, operation
1009: .seealso: VecCreate(), MatShellSetOperation()
1010: @*/
1011: PetscErrorCode VecSetOperation(Vec vec,VecOperation op, void (*f)(void))
1012: {
1015: (((void(**)(void))vec->ops)[(int)op]) = f;
1016: return(0);
1017: }
1022: /*@
1023: VecStashSetInitialSize - sets the sizes of the vec-stash, that is
1024: used during the assembly process to store values that belong to
1025: other processors.
1027: Not Collective, different processes can have different size stashes
1029: Input Parameters:
1030: + vec - the vector
1031: . size - the initial size of the stash.
1032: - bsize - the initial size of the block-stash(if used).
1034: Options Database Keys:
1035: + -vecstash_initial_size <size> or <size0,size1,...sizep-1>
1036: - -vecstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1>
1038: Level: intermediate
1040: Notes:
1041: The block-stash is used for values set with VecSetValuesBlocked() while
1042: the stash is used for values set with VecSetValues()
1044: Run with the option -info and look for output of the form
1045: VecAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
1046: to determine the appropriate value, MM, to use for size and
1047: VecAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
1048: to determine the value, BMM to use for bsize
1050: Concepts: vector^stash
1051: Concepts: stash^vector
1053: .seealso: VecSetBlockSize(), VecSetValues(), VecSetValuesBlocked(), VecStashView()
1055: @*/
1056: PetscErrorCode VecStashSetInitialSize(Vec vec,PetscInt size,PetscInt bsize)
1057: {
1062: VecStashSetInitialSize_Private(&vec->stash,size);
1063: VecStashSetInitialSize_Private(&vec->bstash,bsize);
1064: return(0);
1065: }
1069: /*@
1070: VecConjugate - Conjugates a vector.
1072: Logically Collective on Vec
1074: Input Parameters:
1075: . x - the vector
1077: Level: intermediate
1079: Concepts: vector^conjugate
1081: @*/
1082: PetscErrorCode VecConjugate(Vec x)
1083: {
1084: #if defined(PETSC_USE_COMPLEX)
1090: if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1091: (*x->ops->conjugate)(x);
1092: /* we need to copy norms here */
1093: PetscObjectStateIncrease((PetscObject)x);
1094: return(0);
1095: #else
1096: return(0);
1097: #endif
1098: }
1102: /*@
1103: VecPointwiseMult - Computes the componentwise multiplication w = x*y.
1105: Logically Collective on Vec
1107: Input Parameters:
1108: . x, y - the vectors
1110: Output Parameter:
1111: . w - the result
1113: Level: advanced
1115: Notes: any subset of the x, y, and w may be the same vector.
1117: Concepts: vector^pointwise multiply
1119: .seealso: VecPointwiseDivide(), VecPointwiseMax(), VecPointwiseMin(), VecPointwiseMaxAbs(), VecMaxPointwiseDivide()
1120: @*/
1121: PetscErrorCode VecPointwiseMult(Vec w, Vec x,Vec y)
1122: {
1134: if (x->map->n != y->map->n || x->map->n != w->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");
1136: PetscLogEventBegin(VEC_PointwiseMult,x,y,w,0);
1137: (*w->ops->pointwisemult)(w,x,y);
1138: PetscLogEventEnd(VEC_PointwiseMult,x,y,w,0);
1139: PetscObjectStateIncrease((PetscObject)w);
1140: return(0);
1141: }
1145: /*@
1146: VecSetRandom - Sets all components of a vector to random numbers.
1148: Logically Collective on Vec
1150: Input Parameters:
1151: + x - the vector
1152: - rctx - the random number context, formed by PetscRandomCreate(), or NULL and
1153: it will create one internally.
1155: Output Parameter:
1156: . x - the vector
1158: Example of Usage:
1159: .vb
1160: PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
1161: VecSetRandom(x,rctx);
1162: PetscRandomDestroy(rctx);
1163: .ve
1165: Level: intermediate
1167: Concepts: vector^setting to random
1168: Concepts: random^vector
1170: .seealso: VecSet(), VecSetValues(), PetscRandomCreate(), PetscRandomDestroy()
1171: @*/
1172: PetscErrorCode VecSetRandom(Vec x,PetscRandom rctx)
1173: {
1175: PetscRandom randObj = NULL;
1181: if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1183: if (!rctx) {
1184: MPI_Comm comm;
1185: PetscObjectGetComm((PetscObject)x,&comm);
1186: PetscRandomCreate(comm,&randObj);
1187: PetscRandomSetFromOptions(randObj);
1188: rctx = randObj;
1189: }
1191: PetscLogEventBegin(VEC_SetRandom,x,rctx,0,0);
1192: (*x->ops->setrandom)(x,rctx);
1193: PetscLogEventEnd(VEC_SetRandom,x,rctx,0,0);
1195: PetscRandomDestroy(&randObj);
1196: PetscObjectStateIncrease((PetscObject)x);
1197: return(0);
1198: }
1202: /*@
1203: VecZeroEntries - puts a 0.0 in each element of a vector
1205: Logically Collective on Vec
1207: Input Parameter:
1208: . vec - The vector
1210: Level: beginner
1212: Developer Note: This routine does not need to exist since the exact functionality is obtained with
1213: VecSet(vec,0); I guess someone added it to mirror the functionality of MatZeroEntries() but Mat is nothing
1214: like a Vec (one is an operator and one is an element of a vector space, yeah yeah dual blah blah blah) so
1215: this routine should not exist.
1217: .keywords: Vec, set, options, database
1218: .seealso: VecCreate(), VecSetOptionsPrefix(), VecSet(), VecSetValues()
1219: @*/
1220: PetscErrorCode VecZeroEntries(Vec vec)
1221: {
1225: VecSet(vec,0);
1226: return(0);
1227: }
1231: /*
1232: VecSetTypeFromOptions_Private - Sets the type of vector from user options. Defaults to a PETSc sequential vector on one
1233: processor and a PETSc MPI vector on more than one processor.
1235: Collective on Vec
1237: Input Parameter:
1238: . vec - The vector
1240: Level: intermediate
1242: .keywords: Vec, set, options, database, type
1243: .seealso: VecSetFromOptions(), VecSetType()
1244: */
1245: static PetscErrorCode VecSetTypeFromOptions_Private(Vec vec)
1246: {
1247: PetscBool opt;
1248: VecType defaultType;
1249: char typeName[256];
1250: PetscMPIInt size;
1254: if (((PetscObject)vec)->type_name) defaultType = ((PetscObject)vec)->type_name;
1255: else {
1256: MPI_Comm_size(PetscObjectComm((PetscObject)vec), &size);
1257: if (size > 1) defaultType = VECMPI;
1258: else defaultType = VECSEQ;
1259: }
1261: if (!VecRegisterAllCalled) {VecRegisterAll();}
1262: PetscOptionsFList("-vec_type","Vector type","VecSetType",VecList,defaultType,typeName,256,&opt);
1263: if (opt) {
1264: VecSetType(vec, typeName);
1265: } else {
1266: VecSetType(vec, defaultType);
1267: }
1268: return(0);
1269: }
1273: /*@
1274: VecSetFromOptions - Configures the vector from the options database.
1276: Collective on Vec
1278: Input Parameter:
1279: . vec - The vector
1281: Notes: To see all options, run your program with the -help option, or consult the users manual.
1282: Must be called after VecCreate() but before the vector is used.
1284: Level: beginner
1286: Concepts: vectors^setting options
1287: Concepts: vectors^setting type
1289: .keywords: Vec, set, options, database
1290: .seealso: VecCreate(), VecSetOptionsPrefix()
1291: @*/
1292: PetscErrorCode VecSetFromOptions(Vec vec)
1293: {
1299: PetscObjectOptionsBegin((PetscObject)vec);
1300: /* Handle vector type options */
1301: VecSetTypeFromOptions_Private(vec);
1303: /* Handle specific vector options */
1304: if (vec->ops->setfromoptions) {
1305: (*vec->ops->setfromoptions)(vec);
1306: }
1308: /* process any options handlers added with PetscObjectAddOptionsHandler() */
1309: PetscObjectProcessOptionsHandlers((PetscObject)vec);
1310: PetscOptionsEnd();
1311: return(0);
1312: }
1316: /*@
1317: VecSetSizes - Sets the local and global sizes, and checks to determine compatibility
1319: Collective on Vec
1321: Input Parameters:
1322: + v - the vector
1323: . n - the local size (or PETSC_DECIDE to have it set)
1324: - N - the global size (or PETSC_DECIDE)
1326: Notes:
1327: n and N cannot be both PETSC_DECIDE
1328: If one processor calls this with N of PETSC_DECIDE then all processors must, otherwise the program will hang.
1330: Level: intermediate
1332: .seealso: VecGetSize(), PetscSplitOwnership()
1333: @*/
1334: PetscErrorCode VecSetSizes(Vec v, PetscInt n, PetscInt N)
1335: {
1341: 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);
1342: 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);
1343: v->map->n = n;
1344: v->map->N = N;
1345: if (v->ops->create) {
1346: (*v->ops->create)(v);
1347: v->ops->create = 0;
1348: }
1349: return(0);
1350: }
1354: /*@
1355: VecSetBlockSize - Sets the blocksize for future calls to VecSetValuesBlocked()
1356: and VecSetValuesBlockedLocal().
1358: Logically Collective on Vec
1360: Input Parameter:
1361: + v - the vector
1362: - bs - the blocksize
1364: Notes:
1365: All vectors obtained by VecDuplicate() inherit the same blocksize.
1367: Level: advanced
1369: .seealso: VecSetValuesBlocked(), VecSetLocalToGlobalMapping(), VecGetBlockSize()
1371: Concepts: block size^vectors
1372: @*/
1373: PetscErrorCode VecSetBlockSize(Vec v,PetscInt bs)
1374: {
1379: if (bs < 0 || bs == v->map->bs) return(0);
1381: PetscLayoutSetBlockSize(v->map,bs);
1382: v->bstash.bs = bs; /* use the same blocksize for the vec's block-stash */
1383: return(0);
1384: }
1388: /*@
1389: VecGetBlockSize - Gets the blocksize for the vector, i.e. what is used for VecSetValuesBlocked()
1390: and VecSetValuesBlockedLocal().
1392: Not Collective
1394: Input Parameter:
1395: . v - the vector
1397: Output Parameter:
1398: . bs - the blocksize
1400: Notes:
1401: All vectors obtained by VecDuplicate() inherit the same blocksize.
1403: Level: advanced
1405: .seealso: VecSetValuesBlocked(), VecSetLocalToGlobalMapping(), VecSetBlockSize()
1407: Concepts: vector^block size
1408: Concepts: block^vector
1410: @*/
1411: PetscErrorCode VecGetBlockSize(Vec v,PetscInt *bs)
1412: {
1418: PetscLayoutGetBlockSize(v->map,bs);
1419: return(0);
1420: }
1424: /*@C
1425: VecSetOptionsPrefix - Sets the prefix used for searching for all
1426: Vec options in the database.
1428: Logically Collective on Vec
1430: Input Parameter:
1431: + v - the Vec context
1432: - prefix - the prefix to prepend to all option names
1434: Notes:
1435: A hyphen (-) must NOT be given at the beginning of the prefix name.
1436: The first character of all runtime options is AUTOMATICALLY the hyphen.
1438: Level: advanced
1440: .keywords: Vec, set, options, prefix, database
1442: .seealso: VecSetFromOptions()
1443: @*/
1444: PetscErrorCode VecSetOptionsPrefix(Vec v,const char prefix[])
1445: {
1450: PetscObjectSetOptionsPrefix((PetscObject)v,prefix);
1451: return(0);
1452: }
1456: /*@C
1457: VecAppendOptionsPrefix - Appends to the prefix used for searching for all
1458: Vec options in the database.
1460: Logically Collective on Vec
1462: Input Parameters:
1463: + v - the Vec context
1464: - prefix - the prefix to prepend to all option names
1466: Notes:
1467: A hyphen (-) must NOT be given at the beginning of the prefix name.
1468: The first character of all runtime options is AUTOMATICALLY the hyphen.
1470: Level: advanced
1472: .keywords: Vec, append, options, prefix, database
1474: .seealso: VecGetOptionsPrefix()
1475: @*/
1476: PetscErrorCode VecAppendOptionsPrefix(Vec v,const char prefix[])
1477: {
1482: PetscObjectAppendOptionsPrefix((PetscObject)v,prefix);
1483: return(0);
1484: }
1488: /*@C
1489: VecGetOptionsPrefix - Sets the prefix used for searching for all
1490: Vec options in the database.
1492: Not Collective
1494: Input Parameter:
1495: . v - the Vec context
1497: Output Parameter:
1498: . prefix - pointer to the prefix string used
1500: Notes: On the fortran side, the user should pass in a string 'prefix' of
1501: sufficient length to hold the prefix.
1503: Level: advanced
1505: .keywords: Vec, get, options, prefix, database
1507: .seealso: VecAppendOptionsPrefix()
1508: @*/
1509: PetscErrorCode VecGetOptionsPrefix(Vec v,const char *prefix[])
1510: {
1515: PetscObjectGetOptionsPrefix((PetscObject)v,prefix);
1516: return(0);
1517: }
1521: /*@
1522: VecSetUp - Sets up the internal vector data structures for the later use.
1524: Collective on Vec
1526: Input Parameters:
1527: . v - the Vec context
1529: Notes:
1530: For basic use of the Vec classes the user need not explicitly call
1531: VecSetUp(), since these actions will happen automatically.
1533: Level: advanced
1535: .keywords: Vec, setup
1537: .seealso: VecCreate(), VecDestroy()
1538: @*/
1539: PetscErrorCode VecSetUp(Vec v)
1540: {
1541: PetscMPIInt size;
1546: if (!((PetscObject)v)->type_name) {
1547: MPI_Comm_size(PetscObjectComm((PetscObject)v), &size);
1548: if (size == 1) {
1549: VecSetType(v, VECSEQ);
1550: } else {
1551: VecSetType(v, VECMPI);
1552: }
1553: }
1554: return(0);
1555: }
1557: /*
1558: These currently expose the PetscScalar/PetscReal in updating the
1559: cached norm. If we push those down into the implementation these
1560: will become independent of PetscScalar/PetscReal
1561: */
1565: /*@
1566: VecCopy - Copies a vector. y <- x
1568: Logically Collective on Vec
1570: Input Parameter:
1571: . x - the vector
1573: Output Parameter:
1574: . y - the copy
1576: Notes:
1577: For default parallel PETSc vectors, both x and y must be distributed in
1578: the same manner; local copies are done.
1580: Level: beginner
1582: .seealso: VecDuplicate()
1583: @*/
1584: PetscErrorCode VecCopy(Vec x,Vec y)
1585: {
1586: PetscBool flgs[4];
1587: PetscReal norms[4] = {0.0,0.0,0.0,0.0};
1589: PetscInt i;
1596: if (x == y) return(0);
1597: if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1598: if (x->map->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths %d != %d", x->map->n, y->map->n);
1600: #if !defined(PETSC_USE_MIXED_PRECISION)
1601: for (i=0; i<4; i++) {
1602: PetscObjectComposedDataGetReal((PetscObject)x,NormIds[i],norms[i],flgs[i]);
1603: }
1604: #endif
1606: PetscLogEventBegin(VEC_Copy,x,y,0,0);
1607: #if defined(PETSC_USE_MIXED_PRECISION)
1608: extern PetscErrorCode VecGetArray(Vec,double**);
1609: extern PetscErrorCode VecRestoreArray(Vec,double**);
1610: extern PetscErrorCode VecGetArray(Vec,float**);
1611: extern PetscErrorCode VecRestoreArray(Vec,float**);
1612: extern PetscErrorCode VecGetArrayRead(Vec,const double**);
1613: extern PetscErrorCode VecRestoreArrayRead(Vec,const double**);
1614: extern PetscErrorCode VecGetArrayRead(Vec,const float**);
1615: extern PetscErrorCode VecRestoreArrayRead(Vec,const float**);
1616: if ((((PetscObject)x)->precision == PETSC_PRECISION_SINGLE) && (((PetscObject)y)->precision == PETSC_PRECISION_DOUBLE)) {
1617: PetscInt i,n;
1618: const float *xx;
1619: double *yy;
1620: VecGetArrayRead(x,&xx);
1621: VecGetArray(y,&yy);
1622: VecGetLocalSize(x,&n);
1623: for (i=0; i<n; i++) yy[i] = xx[i];
1624: VecRestoreArrayRead(x,&xx);
1625: VecRestoreArray(y,&yy);
1626: } else if ((((PetscObject)x)->precision == PETSC_PRECISION_DOUBLE) && (((PetscObject)y)->precision == PETSC_PRECISION_SINGLE)) {
1627: PetscInt i,n;
1628: float *yy;
1629: const double *xx;
1630: VecGetArrayRead(x,&xx);
1631: VecGetArray(y,&yy);
1632: VecGetLocalSize(x,&n);
1633: for (i=0; i<n; i++) yy[i] = (float) xx[i];
1634: VecRestoreArrayRead(x,&xx);
1635: VecRestoreArray(y,&yy);
1636: } else {
1637: (*x->ops->copy)(x,y);
1638: }
1639: #else
1640: (*x->ops->copy)(x,y);
1641: #endif
1643: PetscObjectStateIncrease((PetscObject)y);
1644: #if !defined(PETSC_USE_MIXED_PRECISION)
1645: for (i=0; i<4; i++) {
1646: if (flgs[i]) {
1647: PetscObjectComposedDataSetReal((PetscObject)y,NormIds[i],norms[i]);
1648: }
1649: }
1650: #endif
1652: PetscLogEventEnd(VEC_Copy,x,y,0,0);
1653: return(0);
1654: }
1658: /*@
1659: VecSwap - Swaps the vectors x and y.
1661: Logically Collective on Vec
1663: Input Parameters:
1664: . x, y - the vectors
1666: Level: advanced
1668: Concepts: vector^swapping values
1670: @*/
1671: PetscErrorCode VecSwap(Vec x,Vec y)
1672: {
1673: PetscReal normxs[4]={0.0,0.0,0.0,0.0},normys[4]={0.0,0.0,0.0,0.0};
1674: PetscBool flgxs[4],flgys[4];
1676: PetscInt i;
1684: if (x->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1685: if (y->stash.insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled vector");
1686: if (x->map->N != y->map->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths");
1687: if (x->map->n != y->map->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths");
1689: PetscLogEventBegin(VEC_Swap,x,y,0,0);
1690: for (i=0; i<4; i++) {
1691: PetscObjectComposedDataGetReal((PetscObject)x,NormIds[i],normxs[i],flgxs[i]);
1692: PetscObjectComposedDataGetReal((PetscObject)y,NormIds[i],normys[i],flgys[i]);
1693: }
1694: (*x->ops->swap)(x,y);
1695: PetscObjectStateIncrease((PetscObject)x);
1696: PetscObjectStateIncrease((PetscObject)y);
1697: for (i=0; i<4; i++) {
1698: if (flgxs[i]) {
1699: PetscObjectComposedDataSetReal((PetscObject)y,NormIds[i],normxs[i]);
1700: }
1701: if (flgys[i]) {
1702: PetscObjectComposedDataSetReal((PetscObject)x,NormIds[i],normys[i]);
1703: }
1704: }
1705: PetscLogEventEnd(VEC_Swap,x,y,0,0);
1706: return(0);
1707: }
1711: /*
1712: VecStashViewFromOptions - Processes command line options to determine if/how an VecStash object is to be viewed.
1714: Collective on VecStash
1716: Input Parameters:
1717: + obj - the VecStash object
1718: . prefix - prefix to use for viewing, or NULL to use prefix of 'mat'
1719: - optionname - option to activate viewing
1721: Level: intermediate
1723: Developer Note: This cannot use PetscObjectViewFromOptions() because it takes a Vec as an argument but does not use VecView
1725: */
1726: PetscErrorCode VecStashViewFromOptions(Vec obj,const char prefix[],const char optionname[])
1727: {
1728: PetscErrorCode ierr;
1729: PetscViewer viewer;
1730: PetscBool flg;
1731: static PetscBool incall = PETSC_FALSE;
1732: PetscViewerFormat format;
1735: if (incall) return(0);
1736: incall = PETSC_TRUE;
1737: PetscOptionsGetViewer(PetscObjectComm((PetscObject)obj),prefix,optionname,&viewer,&format,&flg);
1738: if (flg) {
1739: PetscViewerPushFormat(viewer,format);
1740: VecStashView(obj,viewer);
1741: PetscViewerPopFormat(viewer);
1742: PetscViewerDestroy(&viewer);
1743: }
1744: incall = PETSC_FALSE;
1745: return(0);
1746: }
1750: /*@
1751: VecStashView - Prints the entries in the vector stash and block stash.
1753: Collective on Vec
1755: Input Parameters:
1756: + v - the vector
1757: - viewer - the viewer
1759: Level: advanced
1761: Concepts: vector^stash
1762: Concepts: stash^vector
1764: .seealso: VecSetBlockSize(), VecSetValues(), VecSetValuesBlocked()
1766: @*/
1767: PetscErrorCode VecStashView(Vec v,PetscViewer viewer)
1768: {
1770: PetscMPIInt rank;
1771: PetscInt i,j;
1772: PetscBool match;
1773: VecStash *s;
1774: PetscScalar val;
1781: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&match);
1782: if (!match) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Stash viewer only works with ASCII viewer not %s\n",((PetscObject)v)->type_name);
1783: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
1784: MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);
1785: s = &v->bstash;
1787: /* print block stash */
1788: PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);
1789: PetscViewerASCIISynchronizedPrintf(viewer,"[%d]Vector Block stash size %D block size %D\n",rank,s->n,s->bs);
1790: for (i=0; i<s->n; i++) {
1791: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D ",rank,s->idx[i]);
1792: for (j=0; j<s->bs; j++) {
1793: val = s->array[i*s->bs+j];
1794: #if defined(PETSC_USE_COMPLEX)
1795: PetscViewerASCIISynchronizedPrintf(viewer,"(%18.16e %18.16e) ",PetscRealPart(val),PetscImaginaryPart(val));
1796: #else
1797: PetscViewerASCIISynchronizedPrintf(viewer,"%18.16e ",val);
1798: #endif
1799: }
1800: PetscViewerASCIISynchronizedPrintf(viewer,"\n");
1801: }
1802: PetscViewerFlush(viewer);
1804: s = &v->stash;
1806: /* print basic stash */
1807: PetscViewerASCIISynchronizedPrintf(viewer,"[%d]Vector stash size %D\n",rank,s->n);
1808: for (i=0; i<s->n; i++) {
1809: val = s->array[i];
1810: #if defined(PETSC_USE_COMPLEX)
1811: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D (%18.16e %18.16e) ",rank,s->idx[i],PetscRealPart(val),PetscImaginaryPart(val));
1812: #else
1813: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Element %D %18.16e\n",rank,s->idx[i],val);
1814: #endif
1815: }
1816: PetscViewerFlush(viewer);
1817: PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);
1819: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1820: return(0);
1821: }
1825: PetscErrorCode PetscOptionsVec(const char key[],const char text[],const char man[],Vec v,PetscBool *set)
1826: {
1827: PetscInt i,N,rstart,rend;
1829: PetscScalar *xx;
1830: PetscReal *xreal;
1831: PetscBool iset;
1834: VecGetOwnershipRange(v,&rstart,&rend);
1835: VecGetSize(v,&N);
1836: PetscCalloc1(N,&xreal);
1837: PetscOptionsRealArray(key,text,man,xreal,&N,&iset);
1838: if (iset) {
1839: VecGetArray(v,&xx);
1840: for (i=rstart; i<rend; i++) xx[i-rstart] = xreal[i];
1841: VecRestoreArray(v,&xx);
1842: }
1843: PetscFree(xreal);
1844: if (set) *set = iset;
1845: return(0);
1846: }
1850: /*@
1851: VecGetLayout - get PetscLayout describing vector layout
1853: Not Collective
1855: Input Arguments:
1856: . x - the vector
1858: Output Arguments:
1859: . map - the layout
1861: Level: developer
1863: .seealso: VecGetSizes(), VecGetOwnershipRange(), VecGetOwnershipRanges()
1864: @*/
1865: PetscErrorCode VecGetLayout(Vec x,PetscLayout *map)
1866: {
1870: *map = x->map;
1871: return(0);
1872: }
1876: /*@
1877: VecSetLayout - set PetscLayout describing vector layout
1879: Not Collective
1881: Input Arguments:
1882: + x - the vector
1883: - map - the layout
1885: Notes:
1886: It is normally only valid to replace the layout with a layout known to be equivalent.
1888: Level: developer
1890: .seealso: VecGetLayout(), VecGetSizes(), VecGetOwnershipRange(), VecGetOwnershipRanges()
1891: @*/
1892: PetscErrorCode VecSetLayout(Vec x,PetscLayout map)
1893: {
1898: PetscLayoutReference(map,&x->map);
1899: return(0);
1900: }