Actual source code: petsc-vecimpl.h

petsc-3.5.4 2015-05-23
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>


 16: /* ----------------------------------------------------------------------------*/

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

 92: /*
 93:     The stash is used to temporarily store inserted vec values that
 94:   belong to another processor. During the assembly phase the stashed
 95:   values are moved to the correct processor and
 96: */

 98: typedef struct {
 99:   PetscInt      nmax;                   /* maximum stash size */
100:   PetscInt      umax;                   /* max stash size user wants */
101:   PetscInt      oldnmax;                /* the nmax value used previously */
102:   PetscInt      n;                      /* stash size */
103:   PetscInt      bs;                     /* block size of the stash */
104:   PetscInt      reallocs;               /* preserve the no of mallocs invoked */
105:   PetscInt      *idx;                   /* global row numbers in stash */
106:   PetscScalar   *array;                 /* array to hold stashed values */
107:   /* The following variables are used for communication */
108:   MPI_Comm      comm;
109:   PetscMPIInt   size,rank;
110:   PetscMPIInt   tag1,tag2;
111:   MPI_Request   *send_waits;            /* array of send requests */
112:   MPI_Request   *recv_waits;            /* array of receive requests */
113:   MPI_Status    *send_status;           /* array of send status */
114:   PetscInt      nsends,nrecvs;          /* numbers of sends and receives */
115:   PetscScalar   *svalues,*rvalues;      /* sending and receiving data */
116:   PetscInt      *sindices,*rindices;
117:   PetscInt      rmax;                   /* maximum message length */
118:   PetscInt      *nprocs;                /* tmp data used both during scatterbegin and end */
119:   PetscInt      nprocessed;             /* number of messages already processed */
120:   PetscBool     donotstash;
121:   PetscBool     ignorenegidx;           /* ignore negative indices passed into VecSetValues/VetGetValues */
122:   InsertMode    insertmode;
123:   PetscInt      *bowners;
124: } VecStash;

126: struct _p_Vec {
127:   PETSCHEADER(struct _VecOps);
128:   PetscLayout            map;
129:   void                   *data;     /* implementation-specific data */
130:   PetscBool              array_gotten;
131:   VecStash               stash,bstash; /* used for storing off-proc values during assembly */
132:   PetscBool              petscnative;  /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
133: #if defined(PETSC_HAVE_CUSP)
134:   PetscCUSPFlag          valid_GPU_array;    /* indicates where the most recently modified vector data is (GPU or CPU) */
135:   void                   *spptr; /* if we're using CUSP, then this is the special pointer to the array on the GPU */
136: #endif
137: #if defined(PETSC_HAVE_VIENNACL)
138:   PetscViennaCLFlag      valid_GPU_array;    /* indicates where the most recently modified vector data is (GPU or CPU) */
139:   void                   *spptr; /* if we're using ViennaCL, then this is the special pointer to the array on the GPU */
140: #endif
141: };

143: PETSC_EXTERN PetscLogEvent VEC_View, VEC_Max, VEC_Min, VEC_DotBarrier, VEC_Dot, VEC_MDotBarrier, VEC_MDot, VEC_TDot, VEC_MTDot;
144: PETSC_EXTERN PetscLogEvent VEC_Norm, VEC_Normalize, VEC_Scale, VEC_Copy, VEC_Set, VEC_AXPY, VEC_AYPX, VEC_WAXPY, VEC_MAXPY;
145: PETSC_EXTERN PetscLogEvent VEC_AssemblyEnd, VEC_PointwiseMult, VEC_SetValues, VEC_Load, VEC_ScatterBarrier, VEC_ScatterBegin, VEC_ScatterEnd;
146: PETSC_EXTERN PetscLogEvent VEC_SetRandom, VEC_ReduceArithmetic, VEC_ReduceBarrier, VEC_ReduceCommunication;
147: PETSC_EXTERN PetscLogEvent VEC_ReduceBegin,VEC_ReduceEnd;
148: PETSC_EXTERN PetscLogEvent VEC_Swap, VEC_AssemblyBegin, VEC_NormBarrier, VEC_DotNormBarrier, VEC_DotNorm, VEC_AXPBYPCZ, VEC_Ops;
149: PETSC_EXTERN PetscLogEvent VEC_CUSPCopyToGPU, VEC_CUSPCopyFromGPU;
150: PETSC_EXTERN PetscLogEvent VEC_CUSPCopyToGPUSome, VEC_CUSPCopyFromGPUSome;
151: PETSC_EXTERN PetscLogEvent VEC_ViennaCLCopyToGPU,     VEC_ViennaCLCopyFromGPU;

153: #if defined(PETSC_HAVE_CUSP)
154: PETSC_EXTERN PetscErrorCode VecCUSPAllocateCheckHost(Vec v);
155: PETSC_EXTERN PetscErrorCode VecCUSPCopyFromGPU(Vec v);
156: #endif

158: #if defined(PETSC_HAVE_VIENNACL)
159: PETSC_EXTERN PetscErrorCode VecViennaCLAllocateCheckHost(Vec v);
160: PETSC_EXTERN PetscErrorCode VecViennaCLCopyFromGPU(Vec v);
161: #endif


164: /*
165:      Common header shared by array based vectors,
166:    currently Vec_Seq and Vec_MPI
167: */
168: #define VECHEADER                          \
169:   PetscScalar *array;                      \
170:   PetscScalar *array_allocated;                        /* if the array was allocated by PETSc this is its pointer */  \
171:   PetscScalar *unplacedarray;                           /* if one called VecPlaceArray(), this is where it stashed the original */

173: /* Default obtain and release vectors; can be used by any implementation */
174: PETSC_INTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
175: PETSC_INTERN PetscErrorCode VecDestroyVecs_Default(PetscInt,Vec []);
176: PETSC_INTERN PetscErrorCode VecLoad_Binary(Vec, PetscViewer);
177: PETSC_EXTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);

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

181: /* --------------------------------------------------------------------*/
182: /*                                                                     */
183: /* Defines the data structures used in the Vec Scatter operations      */

185: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
186:                VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
187:                VEC_SCATTER_MPI_TOONE} VecScatterType;

189: /*
190:    These scatters are for the purely local case.
191: */
192: typedef struct {
193:   VecScatterType type;
194:   PetscInt       n;                    /* number of components to scatter */
195:   PetscInt       *vslots;              /* locations of components */
196:   /*
197:        The next three fields are used in parallel scatters, they contain
198:        optimization in the special case that the "to" vector and the "from"
199:        vector are the same, so one only needs copy components that truly
200:        copies instead of just y[idx[i]] = y[jdx[i]] where idx[i] == jdx[i].
201:   */
202:   PetscBool      nonmatching_computed;
203:   PetscInt       n_nonmatching;        /* number of "from"s  != "to"s */
204:   PetscInt       *slots_nonmatching;   /* locations of "from"s  != "to"s */
205:   PetscBool      is_copy;
206:   PetscInt       copy_start;   /* local scatter is a copy starting at copy_start */
207:   PetscInt       copy_length;
208: } VecScatter_Seq_General;

210: typedef struct {
211:   VecScatterType type;
212:   PetscInt       n;
213:   PetscInt       first;
214:   PetscInt       step;
215: } VecScatter_Seq_Stride;

217: /*
218:    This scatter is for a global vector copied (completely) to each processor (or all to one)
219: */
220: typedef struct {
221:   VecScatterType type;
222:   PetscMPIInt    *count;        /* elements of vector on each processor */
223:   PetscMPIInt    *displx;
224:   PetscScalar    *work1;
225:   PetscScalar    *work2;
226: } VecScatter_MPI_ToAll;

228: /*
229:    This is the general parallel scatter
230: */
231: typedef struct {
232:   VecScatterType         type;
233:   PetscInt               n;        /* number of processors to send/receive */
234:   PetscInt               *starts;  /* starting point in indices and values for each proc*/
235:   PetscInt               *indices; /* list of all components sent or received */
236:   PetscMPIInt            *procs;   /* processors we are communicating with in scatter */
237:   MPI_Request            *requests,*rev_requests;
238:   PetscScalar            *values;  /* buffer for all sends or receives */
239:   VecScatter_Seq_General local;    /* any part that happens to be local */
240:   MPI_Status             *sstatus,*rstatus;
241:   PetscBool              use_readyreceiver;
242:   PetscInt               bs;
243:   PetscBool              sendfirst;
244:   PetscBool              contiq;
245:   /* for MPI_Alltoallv() approach */
246:   PetscBool              use_alltoallv;
247:   PetscMPIInt            *counts,*displs;
248:   /* for MPI_Alltoallw() approach */
249:   PetscBool              use_alltoallw;
250: #if defined(PETSC_HAVE_MPI_ALLTOALLW)
251:   PetscMPIInt            *wcounts,*wdispls;
252:   MPI_Datatype           *types;
253: #endif
254:   PetscBool              use_window;
255: #if defined(PETSC_HAVE_MPI_WIN_CREATE)
256:   MPI_Win                window;
257:   PetscInt               *winstarts;    /* displacements in the processes I am putting to */
258: #endif
259: } VecScatter_MPI_General;

261: struct _p_VecScatter {
262:   PETSCHEADER(int);
263:   PetscInt       to_n,from_n;
264:   PetscBool      inuse;                /* prevents corruption from mixing two scatters */
265:   PetscBool      beginandendtogether;  /* indicates that the scatter begin and end  function are called together, VecScatterEnd()
266:                                           is then treated as a nop */
267:   PetscBool      packtogether;         /* packs all the messages before sending, same with receive */
268:   PetscBool      reproduce;            /* always receive the ghost points in the same order of processes */
269:   PetscErrorCode (*begin)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
270:   PetscErrorCode (*end)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
271:   PetscErrorCode (*copy)(VecScatter,VecScatter);
272:   PetscErrorCode (*destroy)(VecScatter);
273:   PetscErrorCode (*view)(VecScatter,PetscViewer);
274:   void           *fromdata,*todata;
275:   void           *spptr;
276: };

278: PETSC_INTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
279: PETSC_INTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
280: PETSC_INTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
281: PETSC_INTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
282: PETSC_INTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
283: PETSC_INTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
284: PETSC_INTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
285: PETSC_INTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);

287: /*
288:   VecStashValue_Private - inserts a single value into the stash.

290:   Input Parameters:
291:   stash  - the stash
292:   idx    - the global of the inserted value
293:   values - the value inserted
294: */
295: PETSC_STATIC_INLINE PetscErrorCode VecStashValue_Private(VecStash *stash,PetscInt row,PetscScalar value)
296: {
298:   /* Check and see if we have sufficient memory */
299:   if (((stash)->n + 1) > (stash)->nmax) {
300:     VecStashExpand_Private(stash,1);
301:   }
302:   (stash)->idx[(stash)->n]   = row;
303:   (stash)->array[(stash)->n] = value;
304:   (stash)->n++;
305:   return 0;
306: }

308: /*
309:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash.

311:   Input Parameters:
312:   stash  - the stash
313:   idx    - the global block index
314:   values - the values inserted
315: */
316: PETSC_STATIC_INLINE PetscErrorCode VecStashValuesBlocked_Private(VecStash *stash,PetscInt row,PetscScalar *values)
317: {
318:   PetscInt       jj,stash_bs=(stash)->bs;
319:   PetscScalar    *array;
321:   if (((stash)->n+1) > (stash)->nmax) {
322:     VecStashExpand_Private(stash,1);
323:   }
324:   array = (stash)->array + stash_bs*(stash)->n;
325:   (stash)->idx[(stash)->n]   = row;
326:   for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];}
327:   (stash)->n++;
328:   return 0;
329: }

331: PETSC_INTERN PetscErrorCode VecStrideGather_Default(Vec,PetscInt,Vec,InsertMode);
332: PETSC_INTERN PetscErrorCode VecStrideScatter_Default(Vec,PetscInt,Vec,InsertMode);
333: PETSC_INTERN PetscErrorCode VecReciprocal_Default(Vec);
334: PETSC_INTERN PetscErrorCode VecStrideSubSetGather_Default(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);
335: PETSC_INTERN PetscErrorCode VecStrideSubSetScatter_Default(Vec,PetscInt,const PetscInt[],const PetscInt[],Vec,InsertMode);

337: #if defined(PETSC_HAVE_MATLAB_ENGINE)
338: PETSC_EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
339: PETSC_EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
340: #endif


343: /* Reset __FUNCT__ in case the user does not define it themselves */

347: #endif