Actual source code: vecimpl.h
petsc-main 2021-03-04
2: /*
3: This private file should not be included in users' code.
4: Defines the fields shared by all vector implementations.
6: */
8: #ifndef __VECIMPL_H
11: #include <petscvec.h>
12: #include <petsc/private/petscimpl.h>
13: #include <petscviewer.h>
15: PETSC_EXTERN PetscBool VecRegisterAllCalled;
16: PETSC_EXTERN PetscErrorCode VecRegisterAll(void);
18: /* ----------------------------------------------------------------------------*/
20: typedef struct _VecOps *VecOps;
21: struct _VecOps {
22: PetscErrorCode (*duplicate)(Vec,Vec*); /* get single vector */
23: PetscErrorCode (*duplicatevecs)(Vec,PetscInt,Vec**); /* get array of vectors */
24: PetscErrorCode (*destroyvecs)(PetscInt,Vec[]); /* free array of vectors */
25: PetscErrorCode (*dot)(Vec,Vec,PetscScalar*); /* z = x^H * y */
26: PetscErrorCode (*mdot)(Vec,PetscInt,const Vec[],PetscScalar*); /* z[j] = x dot y[j] */
27: PetscErrorCode (*norm)(Vec,NormType,PetscReal*); /* z = sqrt(x^H * x) */
28: PetscErrorCode (*tdot)(Vec,Vec,PetscScalar*); /* x'*y */
29: PetscErrorCode (*mtdot)(Vec,PetscInt,const Vec[],PetscScalar*);/* z[j] = x dot y[j] */
30: PetscErrorCode (*scale)(Vec,PetscScalar); /* x = alpha * x */
31: PetscErrorCode (*copy)(Vec,Vec); /* y = x */
32: PetscErrorCode (*set)(Vec,PetscScalar); /* y = alpha */
33: PetscErrorCode (*swap)(Vec,Vec); /* exchange x and y */
34: PetscErrorCode (*axpy)(Vec,PetscScalar,Vec); /* y = y + alpha * x */
35: PetscErrorCode (*axpby)(Vec,PetscScalar,PetscScalar,Vec); /* y = alpha * x + beta * y*/
36: PetscErrorCode (*maxpy)(Vec,PetscInt,const PetscScalar*,Vec*); /* y = y + alpha[j] x[j] */
37: PetscErrorCode (*aypx)(Vec,PetscScalar,Vec); /* y = x + alpha * y */
38: PetscErrorCode (*waxpy)(Vec,PetscScalar,Vec,Vec); /* w = y + alpha * x */
39: PetscErrorCode (*axpbypcz)(Vec,PetscScalar,PetscScalar,PetscScalar,Vec,Vec); /* z = alpha * x + beta *y + gamma *z*/
40: PetscErrorCode (*pointwisemult)(Vec,Vec,Vec); /* w = x .* y */
41: PetscErrorCode (*pointwisedivide)(Vec,Vec,Vec); /* w = x ./ y */
42: PetscErrorCode (*setvalues)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
43: PetscErrorCode (*assemblybegin)(Vec); /* start global assembly */
44: PetscErrorCode (*assemblyend)(Vec); /* end global assembly */
45: PetscErrorCode (*getarray)(Vec,PetscScalar**); /* get data array */
46: PetscErrorCode (*getsize)(Vec,PetscInt*);
47: PetscErrorCode (*getlocalsize)(Vec,PetscInt*);
48: PetscErrorCode (*restorearray)(Vec,PetscScalar**); /* restore data array */
49: PetscErrorCode (*max)(Vec,PetscInt*,PetscReal*); /* z = max(x); idx=index of max(x) */
50: PetscErrorCode (*min)(Vec,PetscInt*,PetscReal*); /* z = min(x); idx=index of min(x) */
51: PetscErrorCode (*setrandom)(Vec,PetscRandom); /* set y[j] = random numbers */
52: PetscErrorCode (*setoption)(Vec,VecOption,PetscBool);
53: PetscErrorCode (*setvaluesblocked)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
54: PetscErrorCode (*destroy)(Vec);
55: PetscErrorCode (*view)(Vec,PetscViewer);
56: PetscErrorCode (*placearray)(Vec,const PetscScalar*); /* place data array */
57: PetscErrorCode (*replacearray)(Vec,const PetscScalar*); /* replace data array */
58: PetscErrorCode (*dot_local)(Vec,Vec,PetscScalar*);
59: PetscErrorCode (*tdot_local)(Vec,Vec,PetscScalar*);
60: PetscErrorCode (*norm_local)(Vec,NormType,PetscReal*);
61: PetscErrorCode (*mdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
62: PetscErrorCode (*mtdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
63: PetscErrorCode (*load)(Vec,PetscViewer);
64: PetscErrorCode (*reciprocal)(Vec);
65: PetscErrorCode (*conjugate)(Vec);
66: PetscErrorCode (*setlocaltoglobalmapping)(Vec,ISLocalToGlobalMapping);
67: PetscErrorCode (*setvalueslocal)(Vec,PetscInt,const PetscInt *,const PetscScalar *,InsertMode);
68: PetscErrorCode (*resetarray)(Vec); /* vector points to its original array, i.e. undoes any VecPlaceArray() */
69: PetscErrorCode (*setfromoptions)(PetscOptionItems*,Vec);
70: PetscErrorCode (*maxpointwisedivide)(Vec,Vec,PetscReal*); /* m = max abs(x ./ y) */
71: PetscErrorCode (*pointwisemax)(Vec,Vec,Vec);
72: PetscErrorCode (*pointwisemaxabs)(Vec,Vec,Vec);
73: PetscErrorCode (*pointwisemin)(Vec,Vec,Vec);
74: PetscErrorCode (*getvalues)(Vec,PetscInt,const PetscInt[],PetscScalar[]);
75: PetscErrorCode (*sqrt)(Vec);
76: PetscErrorCode (*abs)(Vec);
77: PetscErrorCode (*exp)(Vec);
78: PetscErrorCode (*log)(Vec);
79: PetscErrorCode (*shift)(Vec,PetscScalar);
80: PetscErrorCode (*create)(Vec);
81: PetscErrorCode (*stridegather)(Vec,PetscInt,Vec,InsertMode);
82: PetscErrorCode (*stridescatter)(Vec,PetscInt,Vec,InsertMode);
83: PetscErrorCode (*dotnorm2)(Vec,Vec,PetscScalar*,PetscScalar*);
84: PetscErrorCode (*getsubvector)(Vec,IS,Vec*);
85: PetscErrorCode (*restoresubvector)(Vec,IS,Vec*);
86: PetscErrorCode (*getarrayread)(Vec,const PetscScalar**);
87: PetscErrorCode (*restorearrayread)(Vec,const PetscScalar**);
88: PetscErrorCode (*stridesubsetgather)(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
89: PetscErrorCode (*stridesubsetscatter)(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
90: PetscErrorCode (*viewnative)(Vec,PetscViewer);
91: PetscErrorCode (*loadnative)(Vec,PetscViewer);
92: PetscErrorCode (*getlocalvector)(Vec,Vec);
93: PetscErrorCode (*restorelocalvector)(Vec,Vec);
94: PetscErrorCode (*getlocalvectorread)(Vec,Vec);
95: PetscErrorCode (*restorelocalvectorread)(Vec,Vec);
96: PetscErrorCode (*bindtocpu)(Vec,PetscBool);
97: PetscErrorCode (*getarraywrite)(Vec,PetscScalar**);
98: PetscErrorCode (*restorearraywrite)(Vec,PetscScalar**);
99: PetscErrorCode (*getarrayandmemtype)(Vec,PetscScalar**,PetscMemType*);
100: PetscErrorCode (*getarrayreadandmemtype)(Vec,const PetscScalar**,PetscMemType*);
101: PetscErrorCode (*restorearrayandmemtype)(Vec,PetscScalar**);
102: PetscErrorCode (*restorearrayreadandmemtype)(Vec,const PetscScalar**);
103: PetscErrorCode (*concatenate)(PetscInt,const Vec[],Vec*,IS*[]);
104: };
106: /*
107: The stash is used to temporarily store inserted vec values that
108: belong to another processor. During the assembly phase the stashed
109: values are moved to the correct processor and
110: */
112: typedef struct {
113: PetscInt nmax; /* maximum stash size */
114: PetscInt umax; /* max stash size user wants */
115: PetscInt oldnmax; /* the nmax value used previously */
116: PetscInt n; /* stash size */
117: PetscInt bs; /* block size of the stash */
118: PetscInt reallocs; /* preserve the no of mallocs invoked */
119: PetscInt *idx; /* global row numbers in stash */
120: PetscScalar *array; /* array to hold stashed values */
121: /* The following variables are used for communication */
122: MPI_Comm comm;
123: PetscMPIInt size,rank;
124: PetscMPIInt tag1,tag2;
125: MPI_Request *send_waits; /* array of send requests */
126: MPI_Request *recv_waits; /* array of receive requests */
127: MPI_Status *send_status; /* array of send status */
128: PetscInt nsends,nrecvs; /* numbers of sends and receives */
129: PetscScalar *svalues,*rvalues; /* sending and receiving data */
130: PetscInt *sindices,*rindices;
131: PetscInt rmax; /* maximum message length */
132: PetscInt *nprocs; /* tmp data used both during scatterbegin and end */
133: PetscInt nprocessed; /* number of messages already processed */
134: PetscBool donotstash;
135: PetscBool ignorenegidx; /* ignore negative indices passed into VecSetValues/VetGetValues */
136: InsertMode insertmode;
137: PetscInt *bowners;
138: } VecStash;
140: struct _p_Vec {
141: PETSCHEADER(struct _VecOps);
142: PetscLayout map;
143: void *data; /* implementation-specific data */
144: PetscBool array_gotten;
145: VecStash stash,bstash; /* used for storing off-proc values during assembly */
146: PetscBool petscnative; /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
147: PetscInt lock; /* lock state. vector can be free (=0), locked for read (>0) or locked for write(<0) */
148: PetscOffloadMask offloadmask; /* a mask which indicates where the valid vector data is (GPU, CPU or both) */
149: #if defined(PETSC_HAVE_DEVICE)
150: void *spptr; /* this is the special pointer to the array on the GPU */
151: PetscBool boundtocpu;
152: size_t minimum_bytes_pinned_memory; /* minimum data size in bytes for which pinned memory will be allocated */
153: PetscBool pinned_memory; /* PETSC_TRUE if the current host allocation has been made from pinned memory. */
154: #endif
155: char *defaultrandtype;
156: };
158: PETSC_EXTERN PetscLogEvent VEC_SetRandom;
159: PETSC_EXTERN PetscLogEvent VEC_View;
160: PETSC_EXTERN PetscLogEvent VEC_Max;
161: PETSC_EXTERN PetscLogEvent VEC_Min;
162: PETSC_EXTERN PetscLogEvent VEC_Dot;
163: PETSC_EXTERN PetscLogEvent VEC_MDot;
164: PETSC_EXTERN PetscLogEvent VEC_TDot;
165: PETSC_EXTERN PetscLogEvent VEC_MTDot;
166: PETSC_EXTERN PetscLogEvent VEC_Norm;
167: PETSC_EXTERN PetscLogEvent VEC_Normalize;
168: PETSC_EXTERN PetscLogEvent VEC_Scale;
169: PETSC_EXTERN PetscLogEvent VEC_Copy;
170: PETSC_EXTERN PetscLogEvent VEC_Set;
171: PETSC_EXTERN PetscLogEvent VEC_AXPY;
172: PETSC_EXTERN PetscLogEvent VEC_AYPX;
173: PETSC_EXTERN PetscLogEvent VEC_WAXPY;
174: PETSC_EXTERN PetscLogEvent VEC_MAXPY;
175: PETSC_EXTERN PetscLogEvent VEC_AssemblyEnd;
176: PETSC_EXTERN PetscLogEvent VEC_PointwiseMult;
177: PETSC_EXTERN PetscLogEvent VEC_SetValues;
178: PETSC_EXTERN PetscLogEvent VEC_Load;
179: PETSC_EXTERN PetscLogEvent VEC_ScatterBegin;
180: PETSC_EXTERN PetscLogEvent VEC_ScatterEnd;
181: PETSC_EXTERN PetscLogEvent VEC_ReduceArithmetic;
182: PETSC_EXTERN PetscLogEvent VEC_ReduceCommunication;
183: PETSC_EXTERN PetscLogEvent VEC_ReduceBegin;
184: PETSC_EXTERN PetscLogEvent VEC_ReduceEnd;
185: PETSC_EXTERN PetscLogEvent VEC_Swap;
186: PETSC_EXTERN PetscLogEvent VEC_AssemblyBegin;
187: PETSC_EXTERN PetscLogEvent VEC_DotNorm2;
188: PETSC_EXTERN PetscLogEvent VEC_AXPBYPCZ;
189: PETSC_EXTERN PetscLogEvent VEC_Ops;
190: PETSC_EXTERN PetscLogEvent VEC_ViennaCLCopyToGPU;
191: PETSC_EXTERN PetscLogEvent VEC_ViennaCLCopyFromGPU;
192: PETSC_EXTERN PetscLogEvent VEC_CUDACopyToGPU;
193: PETSC_EXTERN PetscLogEvent VEC_CUDACopyFromGPU;
194: PETSC_EXTERN PetscLogEvent VEC_CUDACopyToGPUSome;
195: PETSC_EXTERN PetscLogEvent VEC_CUDACopyFromGPUSome;
196: PETSC_EXTERN PetscLogEvent VEC_HIPCopyToGPU;
197: PETSC_EXTERN PetscLogEvent VEC_HIPCopyFromGPU;
198: PETSC_EXTERN PetscLogEvent VEC_HIPCopyToGPUSome;
199: PETSC_EXTERN PetscLogEvent VEC_HIPCopyFromGPUSome;
201: PETSC_EXTERN PetscErrorCode VecView_Seq(Vec,PetscViewer);
202: #if defined(PETSC_HAVE_VIENNACL)
203: PETSC_EXTERN PetscErrorCode VecViennaCLAllocateCheckHost(Vec v);
204: PETSC_EXTERN PetscErrorCode VecViennaCLCopyFromGPU(Vec v);
205: #endif
206: #if defined(PETSC_HAVE_CUDA)
207: PETSC_EXTERN PetscErrorCode VecCUDAAllocateCheckHost(Vec v);
208: PETSC_EXTERN PetscErrorCode VecCUDACopyFromGPU(Vec v);
209: #endif
210: #if defined(PETSC_HAVE_HIP)
211: PETSC_EXTERN PetscErrorCode VecHIPAllocateCheckHost(Vec v);
212: PETSC_EXTERN PetscErrorCode VecHIPCopyFromGPU(Vec v);
213: #endif
216: /*
217: Common header shared by array based vectors,
218: currently Vec_Seq and Vec_MPI
219: */
220: #define VECHEADER \
221: PetscScalar *array; \
222: PetscScalar *array_allocated; /* if the array was allocated by PETSc this is its pointer */ \
223: PetscScalar *unplacedarray; /* if one called VecPlaceArray(), this is where it stashed the original */
225: /* Lock a vector for exclusive read&write access */
226: #if defined(PETSC_USE_DEBUG)
227: PETSC_INTERN PetscErrorCode VecLockWriteSet_Private(Vec,PetscBool);
228: #else
229: #define VecLockWriteSet_Private(x,flg) 0
230: #endif
232: /* Default obtain and release vectors; can be used by any implementation */
233: PETSC_EXTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
234: PETSC_EXTERN PetscErrorCode VecDestroyVecs_Default(PetscInt,Vec []);
235: PETSC_EXTERN PetscErrorCode VecView_Binary(Vec, PetscViewer);
236: PETSC_EXTERN PetscErrorCode VecLoad_Binary(Vec, PetscViewer);
237: PETSC_EXTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);
239: PETSC_EXTERN PetscInt NormIds[7]; /* map from NormType to IDs used to cache/retreive values of norms */
241: PETSC_INTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
242: PETSC_INTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
243: PETSC_EXTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
244: PETSC_INTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
245: PETSC_INTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
246: PETSC_INTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
247: PETSC_INTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
248: PETSC_INTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);
249: PETSC_INTERN PetscErrorCode VecStashSortCompress_Private(VecStash*);
250: PETSC_INTERN PetscErrorCode VecStashGetOwnerList_Private(VecStash*,PetscLayout,PetscMPIInt*,PetscMPIInt**);
252: /*
253: VecStashValue_Private - inserts a single value into the stash.
255: Input Parameters:
256: stash - the stash
257: idx - the global of the inserted value
258: values - the value inserted
259: */
260: PETSC_STATIC_INLINE PetscErrorCode VecStashValue_Private(VecStash *stash,PetscInt row,PetscScalar value)
261: {
263: /* Check and see if we have sufficient memory */
264: if (((stash)->n + 1) > (stash)->nmax) {
265: VecStashExpand_Private(stash,1);
266: }
267: (stash)->idx[(stash)->n] = row;
268: (stash)->array[(stash)->n] = value;
269: (stash)->n++;
270: return 0;
271: }
273: /*
274: VecStashValuesBlocked_Private - inserts 1 block of values into the stash.
276: Input Parameters:
277: stash - the stash
278: idx - the global block index
279: values - the values inserted
280: */
281: PETSC_STATIC_INLINE PetscErrorCode VecStashValuesBlocked_Private(VecStash *stash,PetscInt row,PetscScalar *values)
282: {
283: PetscInt jj,stash_bs=(stash)->bs;
284: PetscScalar *array;
286: if (((stash)->n+1) > (stash)->nmax) {
287: VecStashExpand_Private(stash,1);
288: }
289: array = (stash)->array + stash_bs*(stash)->n;
290: (stash)->idx[(stash)->n] = row;
291: for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];}
292: (stash)->n++;
293: return 0;
294: }
296: PETSC_INTERN PetscErrorCode VecStrideGather_Default(Vec,PetscInt,Vec,InsertMode);
297: PETSC_INTERN PetscErrorCode VecStrideScatter_Default(Vec,PetscInt,Vec,InsertMode);
298: PETSC_INTERN PetscErrorCode VecReciprocal_Default(Vec);
299: PETSC_INTERN PetscErrorCode VecStrideSubSetGather_Default(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
300: PETSC_INTERN PetscErrorCode VecStrideSubSetScatter_Default(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
302: #if defined(PETSC_HAVE_MATLAB_ENGINE)
303: PETSC_EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
304: PETSC_EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
305: #endif
307: PETSC_EXTERN PetscErrorCode PetscSectionGetField_Internal(PetscSection, PetscSection, Vec, PetscInt, PetscInt, PetscInt, IS *, Vec *);
308: PETSC_EXTERN PetscErrorCode PetscSectionRestoreField_Internal(PetscSection, PetscSection, Vec, PetscInt, PetscInt, PetscInt, IS *, Vec *);
310: #define VecCheckSameLocalSize(x,ar1,y,ar2) do { \
311: if ((x)->map->n != (y)->map->n) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector local lengths parameter # %d local size %D != parameter # %d local size %D", ar1,(x)->map->n, ar2,(y)->map->n); \
312: } while (0)
314: #define VecCheckSameSize(x,ar1,y,ar2) do { \
315: if ((x)->map->N != (y)->map->N) SETERRQ4(PetscObjectComm((PetscObject)x),PETSC_ERR_ARG_INCOMP,"Incompatible vector global lengths parameter # %d global size %D != parameter # %d global size %D", ar1,(x)->map->N, ar2,(y)->map->N); \
316: VecCheckSameLocalSize(x,ar1,y,ar2); \
317: } while (0)
319: #define VecCheckLocalSize(x,ar1,n) do { \
320: if ((x)->map->n != n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incorrect vector local size: parameter # %d local size %D != %D",ar1,(x)->map->n,n); \
321: } while (0)
323: #define VecCheckSize(x,ar1,n,N) do { \
324: if ((x)->map->N != N) SETERRQ3(PetscObjectComm((PetscObject)x),PETSC_ERR_ARG_INCOMP,"Incorrect vector global size: parameter # %d global size %D != %D",ar1,(x)->map->N, N); \
325: VecCheckLocalSize(x,ar1,n); \
326: } while (0)
328: typedef struct _VecTaggerOps *VecTaggerOps;
329: struct _VecTaggerOps {
330: PetscErrorCode (*create) (VecTagger);
331: PetscErrorCode (*destroy) (VecTagger);
332: PetscErrorCode (*setfromoptions) (PetscOptionItems*,VecTagger);
333: PetscErrorCode (*setup) (VecTagger);
334: PetscErrorCode (*view) (VecTagger,PetscViewer);
335: PetscErrorCode (*computeboxes) (VecTagger,Vec,PetscInt *,VecTaggerBox **);
336: PetscErrorCode (*computeis) (VecTagger,Vec,IS *);
337: };
338: struct _p_VecTagger {
339: PETSCHEADER(struct _VecTaggerOps);
340: void *data;
341: PetscInt blocksize;
342: PetscBool invert;
343: PetscBool setupcalled;
344: };
346: PETSC_EXTERN PetscBool VecTaggerRegisterAllCalled;
347: PETSC_EXTERN PetscErrorCode VecTaggerRegisterAll(void);
348: PETSC_EXTERN PetscErrorCode VecTaggerComputeIS_FromBoxes(VecTagger,Vec,IS*);
349: PETSC_EXTERN PetscMPIInt Petsc_Reduction_keyval;
351: #endif