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

 14: typedef struct {
 15:   MPI_Comm  comm;
 16:   PetscInt  n,N;         /* local, global vector size */
 17:   PetscInt  rstart,rend; /* local start, local end + 1 */
 18:   PetscInt  *range;      /* the offset of each processor */
 19:   PetscInt  bs;          /* number of elements in each block (generally for multi-component problems) Do NOT multiply above numbers by bs */
 20:   PetscInt  refcnt;      /* MPI Vecs obtained with VecDuplicate() and from MatGetVecs() reuse map of input object */
 21: } PetscMap;

 23: EXTERN PetscErrorCode PetscMapInitialize(MPI_Comm,PetscMap*);
 24: EXTERN PetscErrorCode PetscMapSetUp(PetscMap*);
 25: EXTERN PetscErrorCode PetscMapDestroy(PetscMap*);
 26: EXTERN PetscErrorCode PetscMapCopy(MPI_Comm,PetscMap*,PetscMap*);
 27: EXTERN PetscErrorCode  PetscMapSetLocalSize(PetscMap*,PetscInt);
 28: EXTERN PetscErrorCode  PetscMapGetLocalSize(PetscMap*,PetscInt *);
 29: PetscPolymorphicFunction(PetscMapGetLocalSize,(PetscMap *m),(m,&s),PetscInt,s)
 30: EXTERN PetscErrorCode  PetscMapSetSize(PetscMap*,PetscInt);
 31: EXTERN PetscErrorCode  PetscMapGetSize(PetscMap*,PetscInt *);
 32: PetscPolymorphicFunction(PetscMapGetSize,(PetscMap *m),(m,&s),PetscInt,s)
 33: EXTERN PetscErrorCode  PetscMapSetBlockSize(PetscMap*,PetscInt);
 34: EXTERN PetscErrorCode  PetscMapGetRange(PetscMap*,PetscInt *,PetscInt *);
 35: EXTERN PetscErrorCode  PetscMapGetRanges(PetscMap*,const PetscInt *[]);
 36: EXTERN PetscErrorCode  PetscMapSetSizeBlockSize(PetscMap*,PetscInt);
 37: EXTERN PetscErrorCode  PetscMapGetSizeBlockSize(PetscMap*,PetscInt *);

 39: /* ----------------------------------------------------------------------------*/

 41: typedef struct _VecOps *VecOps;
 42: struct _VecOps {
 43:   PetscErrorCode (*duplicate)(Vec,Vec*);         /* get single vector */
 44:   PetscErrorCode (*duplicatevecs)(Vec,PetscInt,Vec**);     /* get array of vectors */
 45:   PetscErrorCode (*destroyvecs)(Vec[],PetscInt);           /* free array of vectors */
 46:   PetscErrorCode (*dot)(Vec,Vec,PetscScalar*);             /* z = x^H * y */
 47:   PetscErrorCode (*mdot)(Vec,PetscInt,const Vec[],PetscScalar*); /* z[j] = x dot y[j] */
 48:   PetscErrorCode (*norm)(Vec,NormType,PetscReal*);        /* z = sqrt(x^H * x) */
 49:   PetscErrorCode (*tdot)(Vec,Vec,PetscScalar*);             /* x'*y */
 50:   PetscErrorCode (*mtdot)(Vec,PetscInt,const Vec[],PetscScalar*);/* z[j] = x dot y[j] */
 51:   PetscErrorCode (*scale)(Vec,PetscScalar);                 /* x = alpha * x   */
 52:   PetscErrorCode (*copy)(Vec,Vec);                     /* y = x */
 53:   PetscErrorCode (*set)(Vec,PetscScalar);                        /* y = alpha  */
 54:   PetscErrorCode (*swap)(Vec,Vec);                               /* exchange x and y */
 55:   PetscErrorCode (*axpy)(Vec,PetscScalar,Vec);                   /* y = y + alpha * x */
 56:   PetscErrorCode (*axpby)(Vec,PetscScalar,PetscScalar,Vec);      /* y = alpha * x + beta * y*/
 57:   PetscErrorCode (*maxpy)(Vec,PetscInt,const PetscScalar*,Vec*); /* y = y + alpha[j] x[j] */
 58:   PetscErrorCode (*aypx)(Vec,PetscScalar,Vec);                   /* y = x + alpha * y */
 59:   PetscErrorCode (*waxpy)(Vec,PetscScalar,Vec,Vec);         /* w = y + alpha * x */
 60:   PetscErrorCode (*axpbypcz)(Vec,PetscScalar,PetscScalar,PetscScalar,Vec,Vec);   /* z = alpha * x + beta *y + gamma *z*/
 61:   PetscErrorCode (*pointwisemult)(Vec,Vec,Vec);        /* w = x .* y */
 62:   PetscErrorCode (*pointwisedivide)(Vec,Vec,Vec);      /* w = x ./ y */
 63:   PetscErrorCode (*setvalues)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 64:   PetscErrorCode (*assemblybegin)(Vec);                /* start global assembly */
 65:   PetscErrorCode (*assemblyend)(Vec);                  /* end global assembly */
 66:   PetscErrorCode (*getarray)(Vec,PetscScalar**);            /* get data array */
 67:   PetscErrorCode (*getsize)(Vec,PetscInt*);
 68:   PetscErrorCode (*getlocalsize)(Vec,PetscInt*);
 69:   PetscErrorCode (*restorearray)(Vec,PetscScalar**);        /* restore data array */
 70:   PetscErrorCode (*max)(Vec,PetscInt*,PetscReal*);      /* z = max(x); idx=index of max(x) */
 71:   PetscErrorCode (*min)(Vec,PetscInt*,PetscReal*);      /* z = min(x); idx=index of min(x) */
 72:   PetscErrorCode (*setrandom)(Vec,PetscRandom);         /* set y[j] = random numbers */
 73:   PetscErrorCode (*setoption)(Vec,VecOption,PetscTruth);
 74:   PetscErrorCode (*setvaluesblocked)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 75:   PetscErrorCode (*destroy)(Vec);
 76:   PetscErrorCode (*view)(Vec,PetscViewer);
 77:   PetscErrorCode (*placearray)(Vec,const PetscScalar*);     /* place data array */
 78:   PetscErrorCode (*replacearray)(Vec,const PetscScalar*);     /* replace data array */
 79:   PetscErrorCode (*dot_local)(Vec,Vec,PetscScalar*);
 80:   PetscErrorCode (*tdot_local)(Vec,Vec,PetscScalar*);
 81:   PetscErrorCode (*norm_local)(Vec,NormType,PetscReal*);
 82:   PetscErrorCode (*mdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
 83:   PetscErrorCode (*mtdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
 84:   PetscErrorCode (*loadintovector)(PetscViewer,Vec);
 85:   PetscErrorCode (*loadintovectornative)(PetscViewer,Vec);
 86:   PetscErrorCode (*reciprocal)(Vec);
 87:   PetscErrorCode (*viewnative)(Vec,PetscViewer);
 88:   PetscErrorCode (*conjugate)(Vec);
 89:   PetscErrorCode (*setlocaltoglobalmapping)(Vec,ISLocalToGlobalMapping);
 90:   PetscErrorCode (*setvalueslocal)(Vec,PetscInt,const PetscInt *,const PetscScalar *,InsertMode);
 91:   PetscErrorCode (*resetarray)(Vec);      /* vector points to its original array, i.e. undoes any VecPlaceArray() */
 92:   PetscErrorCode (*setfromoptions)(Vec);
 93:   PetscErrorCode (*maxpointwisedivide)(Vec,Vec,PetscReal*);      /* m = max abs(x ./ y) */
 94:   PetscErrorCode (*load)(PetscViewer,const VecType,Vec*);
 95:   PetscErrorCode (*pointwisemax)(Vec,Vec,Vec);
 96:   PetscErrorCode (*pointwisemaxabs)(Vec,Vec,Vec);
 97:   PetscErrorCode (*pointwisemin)(Vec,Vec,Vec);
 98:   PetscErrorCode (*getvalues)(Vec,PetscInt,const PetscInt[],PetscScalar[]);
 99:   PetscErrorCode (*sqrt)(Vec);
100:   PetscErrorCode (*abs)(Vec);
101:   PetscErrorCode (*exp)(Vec);
102:   PetscErrorCode (*log)(Vec);
103:   PetscErrorCode (*shift)(Vec);
104:   PetscErrorCode (*create)(Vec);
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      rmax;                   /* maximum message length */
132:   PetscInt      *nprocs;                /* tmp data used both during scatterbegin and end */
133:   PetscInt      nprocessed;             /* number of messages already processed */
134:   PetscTruth    donotstash;
135:   PetscTruth    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:   PetscMap               *map;
143:   void                   *data;     /* implementation-specific data */
144:   ISLocalToGlobalMapping mapping;   /* mapping used in VecSetValuesLocal() */
145:   ISLocalToGlobalMapping bmapping;  /* mapping used in VecSetValuesBlockedLocal() */
146:   PetscTruth             array_gotten;
147:   VecStash               stash,bstash; /* used for storing off-proc values during assembly */
148:   PetscTruth             petscnative;  /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
149: };

151: #define VecGetArray(x,a)     ((x)->petscnative ? (*(a) = *((PetscScalar **)(x)->data),0) : VecGetArray_Private((x),(a)))
152: #define VecRestoreArray(x,a) ((x)->petscnative ? PetscObjectStateIncrease((PetscObject)x) : VecRestoreArray_Private((x),(a)))

154: /*
155:      Common header shared by array based vectors, 
156:    currently Vec_Seq and Vec_MPI
157: */
158: #define VECHEADER                          \
159:   PetscScalar *array;                      \
160:   PetscScalar *array_allocated;                        /* if the array was allocated by PETSc this is its pointer */  \
161:   PetscScalar *unplacedarray;                           /* if one called VecPlaceArray(), this is where it stashed the original */

163: /* Default obtain and release vectors; can be used by any implementation */
164: EXTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
165: EXTERN PetscErrorCode VecDestroyVecs_Default(Vec [],PetscInt);
166: EXTERN PetscErrorCode VecLoadIntoVector_Default(PetscViewer,Vec);


170: /* --------------------------------------------------------------------*/
171: /*                                                                     */
172: /* Defines the data structures used in the Vec Scatter operations      */

174: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
175:                VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
176:                VEC_SCATTER_MPI_TOONE} VecScatterType;

178: /* 
179:    These scatters are for the purely local case.
180: */
181: typedef struct {
182:   VecScatterType type;
183:   PetscInt       n;                    /* number of components to scatter */
184:   PetscInt       *vslots;              /* locations of components */
185:   /*
186:        The next three fields are used in parallel scatters, they contain 
187:        optimization in the special case that the "to" vector and the "from" 
188:        vector are the same, so one only needs copy components that truly 
189:        copies instead of just y[idx[i]] = y[jdx[i]] where idx[i] == jdx[i].
190:   */
191:   PetscTruth     nonmatching_computed;
192:   PetscInt       n_nonmatching;        /* number of "from"s  != "to"s */
193:   PetscInt       *slots_nonmatching;   /* locations of "from"s  != "to"s */
194:   PetscTruth     is_copy;
195:   PetscInt       copy_start;   /* local scatter is a copy starting at copy_start */
196:   PetscInt       copy_length;
197: } VecScatter_Seq_General;

199: typedef struct {
200:   VecScatterType type;
201:   PetscInt       n;
202:   PetscInt       first;
203:   PetscInt       step;
204: } VecScatter_Seq_Stride;

206: /*
207:    This scatter is for a global vector copied (completely) to each processor (or all to one)
208: */
209: typedef struct {
210:   VecScatterType type;
211:   PetscMPIInt    *count;        /* elements of vector on each processor */
212:   PetscMPIInt    *displx;
213:   PetscScalar    *work1;
214:   PetscScalar    *work2;
215: } VecScatter_MPI_ToAll;

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

250: struct _p_VecScatter {
251:   PETSCHEADER(int);
252:   PetscInt       to_n,from_n;
253:   PetscTruth     inuse;                /* prevents corruption from mixing two scatters */
254:   PetscTruth     beginandendtogether;  /* indicates that the scatter begin and end  function are called together, VecScatterEnd()
255:                                           is then treated as a nop */
256:   PetscTruth     packtogether;         /* packs all the messages before sending, same with receive */
257:   PetscTruth     reproduce;            /* always receive the ghost points in the same order of processes */
258:   PetscErrorCode (*begin)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
259:   PetscErrorCode (*end)(VecScatter,Vec,Vec,InsertMode,ScatterMode);
260:   PetscErrorCode (*copy)(VecScatter,VecScatter);
261:   PetscErrorCode (*destroy)(VecScatter);
262:   PetscErrorCode (*view)(VecScatter,PetscViewer);
263:   void           *fromdata,*todata;
264: };

266: EXTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
267: EXTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
268: EXTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
269: EXTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
270: EXTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
271: EXTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
272: EXTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
273: EXTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);

275: /*
276:   VecStashValue_Private - inserts a single value into the stash.

278:   Input Parameters:
279:   stash  - the stash
280:   idx    - the global of the inserted value
281:   values - the value inserted
282: */
283: PETSC_STATIC_INLINE PetscErrorCode VecStashValue_Private(VecStash *stash,PetscInt row,PetscScalar value)
284: {
286:   /* Check and see if we have sufficient memory */
287:   if (((stash)->n + 1) > (stash)->nmax) {
288:     VecStashExpand_Private(stash,1);
289:   }
290:   (stash)->idx[(stash)->n]   = row;
291:   (stash)->array[(stash)->n] = value;
292:   (stash)->n++;
293:   return 0;
294: }

296: /*
297:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash. 

299:   Input Parameters:
300:   stash  - the stash
301:   idx    - the global block index
302:   values - the values inserted
303: */
304: PETSC_STATIC_INLINE PetscErrorCode VecStashValuesBlocked_Private(VecStash *stash,PetscInt row,PetscScalar *values)
305: {
306:   PetscInt       jj,stash_bs=(stash)->bs;
307:   PetscScalar    *array;
309:   if (((stash)->n+1) > (stash)->nmax) {
310:     VecStashExpand_Private(stash,1);
311:   }
312:   array = (stash)->array + stash_bs*(stash)->n;
313:   (stash)->idx[(stash)->n]   = row;
314:   for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];}
315:   (stash)->n++;
316:   return 0;
317: }

319: EXTERN PetscErrorCode VecReciprocal_Default(Vec);


327: #if defined(PETSC_HAVE_MATLAB_ENGINE)
329: EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
330: EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
332: #endif

335: #endif