Actual source code: vecimpl.h


  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;
 18: PETSC_EXTERN MPI_Op MPIU_MININDEX_OP;

 20: /* ----------------------------------------------------------------------------*/

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

353: #endif