Actual source code: vecimpl.h

petsc-main 2021-04-20
Report Typos and Errors

  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);
 17: PETSC_EXTERN MPI_Op MPIU_MAXINDEX_OP, MPIU_MININDEX_OP;

 19: /* ----------------------------------------------------------------------------*/

 21: typedef struct _VecOps *VecOps;
 22: struct _VecOps {
 23:   PetscErrorCode (*duplicate)(Vec,Vec*);         /* get single vector */
 24:   PetscErrorCode (*duplicatevecs)(Vec,PetscInt,Vec**);     /* get array of vectors */
 25:   PetscErrorCode (*destroyvecs)(PetscInt,Vec[]);           /* free array of vectors */
 26:   PetscErrorCode (*dot)(Vec,Vec,PetscScalar*);             /* z = x^H * y */
 27:   PetscErrorCode (*mdot)(Vec,PetscInt,const Vec[],PetscScalar*); /* z[j] = x dot y[j] */
 28:   PetscErrorCode (*norm)(Vec,NormType,PetscReal*);        /* z = sqrt(x^H * x) */
 29:   PetscErrorCode (*tdot)(Vec,Vec,PetscScalar*);             /* x'*y */
 30:   PetscErrorCode (*mtdot)(Vec,PetscInt,const Vec[],PetscScalar*);/* z[j] = x dot y[j] */
 31:   PetscErrorCode (*scale)(Vec,PetscScalar);                 /* x = alpha * x   */
 32:   PetscErrorCode (*copy)(Vec,Vec);                     /* y = x */
 33:   PetscErrorCode (*set)(Vec,PetscScalar);                        /* y = alpha  */
 34:   PetscErrorCode (*swap)(Vec,Vec);                               /* exchange x and y */
 35:   PetscErrorCode (*axpy)(Vec,PetscScalar,Vec);                   /* y = y + alpha * x */
 36:   PetscErrorCode (*axpby)(Vec,PetscScalar,PetscScalar,Vec);      /* y = alpha * x + beta * y*/
 37:   PetscErrorCode (*maxpy)(Vec,PetscInt,const PetscScalar*,Vec*); /* y = y + alpha[j] x[j] */
 38:   PetscErrorCode (*aypx)(Vec,PetscScalar,Vec);                   /* y = x + alpha * y */
 39:   PetscErrorCode (*waxpy)(Vec,PetscScalar,Vec,Vec);         /* w = y + alpha * x */
 40:   PetscErrorCode (*axpbypcz)(Vec,PetscScalar,PetscScalar,PetscScalar,Vec,Vec);   /* z = alpha * x + beta *y + gamma *z*/
 41:   PetscErrorCode (*pointwisemult)(Vec,Vec,Vec);        /* w = x .* y */
 42:   PetscErrorCode (*pointwisedivide)(Vec,Vec,Vec);      /* w = x ./ y */
 43:   PetscErrorCode (*setvalues)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 44:   PetscErrorCode (*assemblybegin)(Vec);                /* start global assembly */
 45:   PetscErrorCode (*assemblyend)(Vec);                  /* end global assembly */
 46:   PetscErrorCode (*getarray)(Vec,PetscScalar**);            /* get data array */
 47:   PetscErrorCode (*getsize)(Vec,PetscInt*);
 48:   PetscErrorCode (*getlocalsize)(Vec,PetscInt*);
 49:   PetscErrorCode (*restorearray)(Vec,PetscScalar**);        /* restore data array */
 50:   PetscErrorCode (*max)(Vec,PetscInt*,PetscReal*);      /* z = max(x); idx=index of max(x) */
 51:   PetscErrorCode (*min)(Vec,PetscInt*,PetscReal*);      /* z = min(x); idx=index of min(x) */
 52:   PetscErrorCode (*setrandom)(Vec,PetscRandom);         /* set y[j] = random numbers */
 53:   PetscErrorCode (*setoption)(Vec,VecOption,PetscBool);
 54:   PetscErrorCode (*setvaluesblocked)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 55:   PetscErrorCode (*destroy)(Vec);
 56:   PetscErrorCode (*view)(Vec,PetscViewer);
 57:   PetscErrorCode (*placearray)(Vec,const PetscScalar*);     /* place data array */
 58:   PetscErrorCode (*replacearray)(Vec,const PetscScalar*);     /* replace data array */
 59:   PetscErrorCode (*dot_local)(Vec,Vec,PetscScalar*);
 60:   PetscErrorCode (*tdot_local)(Vec,Vec,PetscScalar*);
 61:   PetscErrorCode (*norm_local)(Vec,NormType,PetscReal*);
 62:   PetscErrorCode (*mdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
 63:   PetscErrorCode (*mtdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
 64:   PetscErrorCode (*load)(Vec,PetscViewer);
 65:   PetscErrorCode (*reciprocal)(Vec);
 66:   PetscErrorCode (*conjugate)(Vec);
 67:   PetscErrorCode (*setlocaltoglobalmapping)(Vec,ISLocalToGlobalMapping);
 68:   PetscErrorCode (*setvalueslocal)(Vec,PetscInt,const PetscInt *,const PetscScalar *,InsertMode);
 69:   PetscErrorCode (*resetarray)(Vec);      /* vector points to its original array, i.e. undoes any VecPlaceArray() */
 70:   PetscErrorCode (*setfromoptions)(PetscOptionItems*,Vec);
 71:   PetscErrorCode (*maxpointwisedivide)(Vec,Vec,PetscReal*);      /* m = max abs(x ./ y) */
 72:   PetscErrorCode (*pointwisemax)(Vec,Vec,Vec);
 73:   PetscErrorCode (*pointwisemaxabs)(Vec,Vec,Vec);
 74:   PetscErrorCode (*pointwisemin)(Vec,Vec,Vec);
 75:   PetscErrorCode (*getvalues)(Vec,PetscInt,const PetscInt[],PetscScalar[]);
 76:   PetscErrorCode (*sqrt)(Vec);
 77:   PetscErrorCode (*abs)(Vec);
 78:   PetscErrorCode (*exp)(Vec);
 79:   PetscErrorCode (*log)(Vec);
 80:   PetscErrorCode (*shift)(Vec,PetscScalar);
 81:   PetscErrorCode (*create)(Vec);
 82:   PetscErrorCode (*stridegather)(Vec,PetscInt,Vec,InsertMode);
 83:   PetscErrorCode (*stridescatter)(Vec,PetscInt,Vec,InsertMode);
 84:   PetscErrorCode (*dotnorm2)(Vec,Vec,PetscScalar*,PetscScalar*);
 85:   PetscErrorCode (*getsubvector)(Vec,IS,Vec*);
 86:   PetscErrorCode (*restoresubvector)(Vec,IS,Vec*);
 87:   PetscErrorCode (*getarrayread)(Vec,const PetscScalar**);
 88:   PetscErrorCode (*restorearrayread)(Vec,const PetscScalar**);
 89:   PetscErrorCode (*stridesubsetgather)(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
 90:   PetscErrorCode (*stridesubsetscatter)(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
 91:   PetscErrorCode (*viewnative)(Vec,PetscViewer);
 92:   PetscErrorCode (*loadnative)(Vec,PetscViewer);
 93:   PetscErrorCode (*getlocalvector)(Vec,Vec);
 94:   PetscErrorCode (*restorelocalvector)(Vec,Vec);
 95:   PetscErrorCode (*getlocalvectorread)(Vec,Vec);
 96:   PetscErrorCode (*restorelocalvectorread)(Vec,Vec);
 97:   PetscErrorCode (*bindtocpu)(Vec,PetscBool);
 98:   PetscErrorCode (*getarraywrite)(Vec,PetscScalar**);
 99:   PetscErrorCode (*restorearraywrite)(Vec,PetscScalar**);
100:   PetscErrorCode (*getarrayandmemtype)(Vec,PetscScalar**,PetscMemType*);
101:   PetscErrorCode (*getarrayreadandmemtype)(Vec,const PetscScalar**,PetscMemType*);
102:   PetscErrorCode (*restorearrayandmemtype)(Vec,PetscScalar**);
103:   PetscErrorCode (*restorearrayreadandmemtype)(Vec,const PetscScalar**);
104:   PetscErrorCode (*concatenate)(PetscInt,const Vec[],Vec*,IS*[]);
105: };

107: /*
108:     The stash is used to temporarily store inserted vec values that
109:   belong to another processor. During the assembly phase the stashed
110:   values are moved to the correct processor and
111: */

113: typedef struct {
114:   PetscInt      nmax;                   /* maximum stash size */
115:   PetscInt      umax;                   /* max stash size user wants */
116:   PetscInt      oldnmax;                /* the nmax value used previously */
117:   PetscInt      n;                      /* stash size */
118:   PetscInt      bs;                     /* block size of the stash */
119:   PetscInt      reallocs;               /* preserve the no of mallocs invoked */
120:   PetscInt      *idx;                   /* global row numbers in stash */
121:   PetscScalar   *array;                 /* array to hold stashed values */
122:   /* The following variables are used for communication */
123:   MPI_Comm      comm;
124:   PetscMPIInt   size,rank;
125:   PetscMPIInt   tag1,tag2;
126:   MPI_Request   *send_waits;            /* array of send requests */
127:   MPI_Request   *recv_waits;            /* array of receive requests */
128:   MPI_Status    *send_status;           /* array of send status */
129:   PetscInt      nsends,nrecvs;          /* numbers of sends and receives */
130:   PetscScalar   *svalues,*rvalues;      /* sending and receiving data */
131:   PetscInt      *sindices,*rindices;
132:   PetscInt      rmax;                   /* maximum message length */
133:   PetscInt      *nprocs;                /* tmp data used both during scatterbegin and end */
134:   PetscInt      nprocessed;             /* number of messages already processed */
135:   PetscBool     donotstash;
136:   PetscBool     ignorenegidx;           /* ignore negative indices passed into VecSetValues/VetGetValues */
137:   InsertMode    insertmode;
138:   PetscInt      *bowners;
139: } VecStash;

141: struct _p_Vec {
142:   PETSCHEADER(struct _VecOps);
143:   PetscLayout            map;
144:   void                   *data;     /* implementation-specific data */
145:   PetscBool              array_gotten;
146:   VecStash               stash,bstash; /* used for storing off-proc values during assembly */
147:   PetscBool              petscnative;  /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
148:   PetscInt               lock;         /* lock state. vector can be free (=0), locked for read (>0) or locked for write(<0) */
149:   PetscOffloadMask       offloadmask;  /* a mask which indicates where the valid vector data is (GPU, CPU or both) */
150: #if defined(PETSC_HAVE_DEVICE)
151:   void                   *spptr; /* this is the special pointer to the array on the GPU */
152:   PetscBool              boundtocpu;
153:   size_t                 minimum_bytes_pinned_memory; /* minimum data size in bytes for which pinned memory will be allocated */
154:   PetscBool              pinned_memory; /* PETSC_TRUE if the current host allocation has been made from pinned memory. */
155: #endif
156:   char                   *defaultrandtype;
157: };

159: PETSC_EXTERN PetscLogEvent VEC_SetRandom;
160: PETSC_EXTERN PetscLogEvent VEC_View;
161: PETSC_EXTERN PetscLogEvent VEC_Max;
162: PETSC_EXTERN PetscLogEvent VEC_Min;
163: PETSC_EXTERN PetscLogEvent VEC_Dot;
164: PETSC_EXTERN PetscLogEvent VEC_MDot;
165: PETSC_EXTERN PetscLogEvent VEC_TDot;
166: PETSC_EXTERN PetscLogEvent VEC_MTDot;
167: PETSC_EXTERN PetscLogEvent VEC_Norm;
168: PETSC_EXTERN PetscLogEvent VEC_Normalize;
169: PETSC_EXTERN PetscLogEvent VEC_Scale;
170: PETSC_EXTERN PetscLogEvent VEC_Copy;
171: PETSC_EXTERN PetscLogEvent VEC_Set;
172: PETSC_EXTERN PetscLogEvent VEC_AXPY;
173: PETSC_EXTERN PetscLogEvent VEC_AYPX;
174: PETSC_EXTERN PetscLogEvent VEC_WAXPY;
175: PETSC_EXTERN PetscLogEvent VEC_MAXPY;
176: PETSC_EXTERN PetscLogEvent VEC_AssemblyEnd;
177: PETSC_EXTERN PetscLogEvent VEC_PointwiseMult;
178: PETSC_EXTERN PetscLogEvent VEC_SetValues;
179: PETSC_EXTERN PetscLogEvent VEC_Load;
180: PETSC_EXTERN PetscLogEvent VEC_ScatterBegin;
181: PETSC_EXTERN PetscLogEvent VEC_ScatterEnd;
182: PETSC_EXTERN PetscLogEvent VEC_ReduceArithmetic;
183: PETSC_EXTERN PetscLogEvent VEC_ReduceCommunication;
184: PETSC_EXTERN PetscLogEvent VEC_ReduceBegin;
185: PETSC_EXTERN PetscLogEvent VEC_ReduceEnd;
186: PETSC_EXTERN PetscLogEvent VEC_Swap;
187: PETSC_EXTERN PetscLogEvent VEC_AssemblyBegin;
188: PETSC_EXTERN PetscLogEvent VEC_DotNorm2;
189: PETSC_EXTERN PetscLogEvent VEC_AXPBYPCZ;
190: PETSC_EXTERN PetscLogEvent VEC_Ops;
191: PETSC_EXTERN PetscLogEvent VEC_ViennaCLCopyToGPU;
192: PETSC_EXTERN PetscLogEvent VEC_ViennaCLCopyFromGPU;
193: PETSC_EXTERN PetscLogEvent VEC_CUDACopyToGPU;
194: PETSC_EXTERN PetscLogEvent VEC_CUDACopyFromGPU;
195: PETSC_EXTERN PetscLogEvent VEC_CUDACopyToGPUSome;
196: PETSC_EXTERN PetscLogEvent VEC_CUDACopyFromGPUSome;
197: PETSC_EXTERN PetscLogEvent VEC_HIPCopyToGPU;
198: PETSC_EXTERN PetscLogEvent VEC_HIPCopyFromGPU;
199: PETSC_EXTERN PetscLogEvent VEC_HIPCopyToGPUSome;
200: PETSC_EXTERN PetscLogEvent VEC_HIPCopyFromGPUSome;

202: PETSC_EXTERN PetscErrorCode VecView_Seq(Vec,PetscViewer);
203: #if defined(PETSC_HAVE_VIENNACL)
204: PETSC_EXTERN PetscErrorCode VecViennaCLAllocateCheckHost(Vec v);
205: PETSC_EXTERN PetscErrorCode VecViennaCLCopyFromGPU(Vec v);
206: #endif
207: #if defined(PETSC_HAVE_CUDA)
208: PETSC_EXTERN PetscErrorCode VecCUDAAllocateCheckHost(Vec v);
209: PETSC_EXTERN PetscErrorCode VecCUDACopyFromGPU(Vec v);
210: #endif
211: #if defined(PETSC_HAVE_HIP)
212: PETSC_EXTERN PetscErrorCode VecHIPAllocateCheckHost(Vec v);
213: PETSC_EXTERN PetscErrorCode VecHIPCopyFromGPU(Vec v);
214: #endif


217: /*
218:      Common header shared by array based vectors,
219:    currently Vec_Seq and Vec_MPI
220: */
221: #define VECHEADER                          \
222:   PetscScalar *array;                      \
223:   PetscScalar *array_allocated;                        /* if the array was allocated by PETSc this is its pointer */  \
224:   PetscScalar *unplacedarray;                           /* if one called VecPlaceArray(), this is where it stashed the original */

226: /* Lock a vector for exclusive read&write access */
227: #if defined(PETSC_USE_DEBUG)
228: PETSC_INTERN PetscErrorCode VecLockWriteSet_Private(Vec,PetscBool);
229: #else
230: #define VecLockWriteSet_Private(x,flg) 0
231: #endif

233: /* Default obtain and release vectors; can be used by any implementation */
234: PETSC_EXTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
235: PETSC_EXTERN PetscErrorCode VecDestroyVecs_Default(PetscInt,Vec []);
236: PETSC_EXTERN PetscErrorCode VecView_Binary(Vec, PetscViewer);
237: PETSC_EXTERN PetscErrorCode VecLoad_Binary(Vec, PetscViewer);
238: PETSC_EXTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);

240: PETSC_EXTERN PetscInt  NormIds[7];  /* map from NormType to IDs used to cache/retreive values of norms */

242: PETSC_INTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
243: PETSC_INTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
244: PETSC_EXTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
245: PETSC_INTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
246: PETSC_INTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
247: PETSC_INTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
248: PETSC_INTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
249: PETSC_INTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);
250: PETSC_INTERN PetscErrorCode VecStashSortCompress_Private(VecStash*);
251: PETSC_INTERN PetscErrorCode VecStashGetOwnerList_Private(VecStash*,PetscLayout,PetscMPIInt*,PetscMPIInt**);

253: /*
254:   VecStashValue_Private - inserts a single value into the stash.

256:   Input Parameters:
257:   stash  - the stash
258:   idx    - the global of the inserted value
259:   values - the value inserted
260: */
261: PETSC_STATIC_INLINE PetscErrorCode VecStashValue_Private(VecStash *stash,PetscInt row,PetscScalar value)
262: {
264:   /* Check and see if we have sufficient memory */
265:   if (((stash)->n + 1) > (stash)->nmax) {
266:     VecStashExpand_Private(stash,1);
267:   }
268:   (stash)->idx[(stash)->n]   = row;
269:   (stash)->array[(stash)->n] = value;
270:   (stash)->n++;
271:   return 0;
272: }

274: /*
275:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash.

277:   Input Parameters:
278:   stash  - the stash
279:   idx    - the global block index
280:   values - the values inserted
281: */
282: PETSC_STATIC_INLINE PetscErrorCode VecStashValuesBlocked_Private(VecStash *stash,PetscInt row,PetscScalar *values)
283: {
284:   PetscInt       jj,stash_bs=(stash)->bs;
285:   PetscScalar    *array;
287:   if (((stash)->n+1) > (stash)->nmax) {
288:     VecStashExpand_Private(stash,1);
289:   }
290:   array = (stash)->array + stash_bs*(stash)->n;
291:   (stash)->idx[(stash)->n]   = row;
292:   for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];}
293:   (stash)->n++;
294:   return 0;
295: }

297: PETSC_INTERN PetscErrorCode VecStrideGather_Default(Vec,PetscInt,Vec,InsertMode);
298: PETSC_INTERN PetscErrorCode VecStrideScatter_Default(Vec,PetscInt,Vec,InsertMode);
299: PETSC_INTERN PetscErrorCode VecReciprocal_Default(Vec);
300: PETSC_INTERN PetscErrorCode VecStrideSubSetGather_Default(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
301: PETSC_INTERN PetscErrorCode VecStrideSubSetScatter_Default(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);

303: #if defined(PETSC_HAVE_MATLAB_ENGINE)
304: PETSC_EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
305: PETSC_EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
306: #endif

308: PETSC_EXTERN PetscErrorCode PetscSectionGetField_Internal(PetscSection, PetscSection, Vec, PetscInt, PetscInt, PetscInt, IS *, Vec *);
309: PETSC_EXTERN PetscErrorCode PetscSectionRestoreField_Internal(PetscSection, PetscSection, Vec, PetscInt, PetscInt, PetscInt, IS *, Vec *);

311: #define VecCheckSameLocalSize(x,ar1,y,ar2) do { \
312:     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); \
313:   } while (0)

315: #define VecCheckSameSize(x,ar1,y,ar2) do { \
316:     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); \
317:     VecCheckSameLocalSize(x,ar1,y,ar2); \
318:   } while (0)

320: #define VecCheckLocalSize(x,ar1,n) do { \
321:     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); \
322:   } while (0)

324: #define VecCheckSize(x,ar1,n,N) do { \
325:     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); \
326:     VecCheckLocalSize(x,ar1,n); \
327:   } while (0)

329: typedef struct _VecTaggerOps *VecTaggerOps;
330: struct _VecTaggerOps {
331:   PetscErrorCode (*create) (VecTagger);
332:   PetscErrorCode (*destroy) (VecTagger);
333:   PetscErrorCode (*setfromoptions) (PetscOptionItems*,VecTagger);
334:   PetscErrorCode (*setup) (VecTagger);
335:   PetscErrorCode (*view) (VecTagger,PetscViewer);
336:   PetscErrorCode (*computeboxes) (VecTagger,Vec,PetscInt *,VecTaggerBox **);
337:   PetscErrorCode (*computeis) (VecTagger,Vec,IS *);
338: };
339: struct _p_VecTagger {
340:   PETSCHEADER(struct _VecTaggerOps);
341:   void      *data;
342:   PetscInt  blocksize;
343:   PetscBool invert;
344:   PetscBool setupcalled;
345: };

347: PETSC_EXTERN PetscBool      VecTaggerRegisterAllCalled;
348: PETSC_EXTERN PetscErrorCode VecTaggerRegisterAll(void);
349: PETSC_EXTERN PetscErrorCode VecTaggerComputeIS_FromBoxes(VecTagger,Vec,IS*);
350: PETSC_EXTERN PetscMPIInt Petsc_Reduction_keyval;

352: #endif