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: /*S
 15:      PetscLayout - defines layout of vectors and matrices across processes (which rows are owned by which processes)

 17:    Level: developer


 20: .seealso:  PetscLayoutCreate(), PetscLayoutDestroy()
 21: S*/
 22: typedef struct _p_PetscLayout* PetscLayout;
 23: struct _p_PetscLayout{
 24:   MPI_Comm  comm;
 25:   PetscInt  n,N;         /* local, global vector size */
 26:   PetscInt  rstart,rend; /* local start, local end + 1 */
 27:   PetscInt  *range;      /* the offset of each processor */
 28:   PetscInt  bs;          /* number of elements in each block (generally for multi-component problems) Do NOT multiply above numbers by bs */
 29:   PetscInt  refcnt;      /* MPI Vecs obtained with VecDuplicate() and from MatGetVecs() reuse map of input object */
 30: };

 32: EXTERN PetscErrorCode PetscLayoutCreate(MPI_Comm,PetscLayout*);
 33: EXTERN PetscErrorCode PetscLayoutSetUp(PetscLayout);
 34: EXTERN PetscErrorCode PetscLayoutDestroy(PetscLayout);
 35: EXTERN PetscErrorCode PetscLayoutCopy(PetscLayout,PetscLayout*);
 36: EXTERN PetscErrorCode  PetscLayoutSetLocalSize(PetscLayout,PetscInt);
 37: EXTERN PetscErrorCode  PetscLayoutGetLocalSize(PetscLayout,PetscInt *);
 38: PetscPolymorphicFunction(PetscLayoutGetLocalSize,(PetscLayout m),(m,&s),PetscInt,s)
 39: EXTERN PetscErrorCode  PetscLayoutSetSize(PetscLayout,PetscInt);
 40: EXTERN PetscErrorCode  PetscLayoutGetSize(PetscLayout,PetscInt *);
 41: PetscPolymorphicFunction(PetscLayoutGetSize,(PetscLayout m),(m,&s),PetscInt,s)
 42: EXTERN PetscErrorCode  PetscLayoutSetBlockSize(PetscLayout,PetscInt);
 43: EXTERN PetscErrorCode  PetscLayoutGetBlockSize(PetscLayout,PetscInt*);
 44: EXTERN PetscErrorCode  PetscLayoutGetRange(PetscLayout,PetscInt *,PetscInt *);
 45: EXTERN PetscErrorCode  PetscLayoutGetRanges(PetscLayout,const PetscInt *[]);

 47: /* ----------------------------------------------------------------------------*/

 49: typedef struct _VecOps *VecOps;
 50: struct _VecOps {
 51:   PetscErrorCode (*duplicate)(Vec,Vec*);         /* get single vector */
 52:   PetscErrorCode (*duplicatevecs)(Vec,PetscInt,Vec**);     /* get array of vectors */
 53:   PetscErrorCode (*destroyvecs)(Vec[],PetscInt);           /* free array of vectors */
 54:   PetscErrorCode (*dot)(Vec,Vec,PetscScalar*);             /* z = x^H * y */
 55:   PetscErrorCode (*mdot)(Vec,PetscInt,const Vec[],PetscScalar*); /* z[j] = x dot y[j] */
 56:   PetscErrorCode (*norm)(Vec,NormType,PetscReal*);        /* z = sqrt(x^H * x) */
 57:   PetscErrorCode (*tdot)(Vec,Vec,PetscScalar*);             /* x'*y */
 58:   PetscErrorCode (*mtdot)(Vec,PetscInt,const Vec[],PetscScalar*);/* z[j] = x dot y[j] */
 59:   PetscErrorCode (*scale)(Vec,PetscScalar);                 /* x = alpha * x   */
 60:   PetscErrorCode (*copy)(Vec,Vec);                     /* y = x */
 61:   PetscErrorCode (*set)(Vec,PetscScalar);                        /* y = alpha  */
 62:   PetscErrorCode (*swap)(Vec,Vec);                               /* exchange x and y */
 63:   PetscErrorCode (*axpy)(Vec,PetscScalar,Vec);                   /* y = y + alpha * x */
 64:   PetscErrorCode (*axpby)(Vec,PetscScalar,PetscScalar,Vec);      /* y = alpha * x + beta * y*/
 65:   PetscErrorCode (*maxpy)(Vec,PetscInt,const PetscScalar*,Vec*); /* y = y + alpha[j] x[j] */
 66:   PetscErrorCode (*aypx)(Vec,PetscScalar,Vec);                   /* y = x + alpha * y */
 67:   PetscErrorCode (*waxpy)(Vec,PetscScalar,Vec,Vec);         /* w = y + alpha * x */
 68:   PetscErrorCode (*axpbypcz)(Vec,PetscScalar,PetscScalar,PetscScalar,Vec,Vec);   /* z = alpha * x + beta *y + gamma *z*/
 69:   PetscErrorCode (*pointwisemult)(Vec,Vec,Vec);        /* w = x .* y */
 70:   PetscErrorCode (*pointwisedivide)(Vec,Vec,Vec);      /* w = x ./ y */
 71:   PetscErrorCode (*setvalues)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 72:   PetscErrorCode (*assemblybegin)(Vec);                /* start global assembly */
 73:   PetscErrorCode (*assemblyend)(Vec);                  /* end global assembly */
 74:   PetscErrorCode (*getarray)(Vec,PetscScalar**);            /* get data array */
 75:   PetscErrorCode (*getsize)(Vec,PetscInt*);
 76:   PetscErrorCode (*getlocalsize)(Vec,PetscInt*);
 77:   PetscErrorCode (*restorearray)(Vec,PetscScalar**);        /* restore data array */
 78:   PetscErrorCode (*max)(Vec,PetscInt*,PetscReal*);      /* z = max(x); idx=index of max(x) */
 79:   PetscErrorCode (*min)(Vec,PetscInt*,PetscReal*);      /* z = min(x); idx=index of min(x) */
 80:   PetscErrorCode (*setrandom)(Vec,PetscRandom);         /* set y[j] = random numbers */
 81:   PetscErrorCode (*setoption)(Vec,VecOption,PetscTruth);
 82:   PetscErrorCode (*setvaluesblocked)(Vec,PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
 83:   PetscErrorCode (*destroy)(Vec);
 84:   PetscErrorCode (*view)(Vec,PetscViewer);
 85:   PetscErrorCode (*placearray)(Vec,const PetscScalar*);     /* place data array */
 86:   PetscErrorCode (*replacearray)(Vec,const PetscScalar*);     /* replace data array */
 87:   PetscErrorCode (*dot_local)(Vec,Vec,PetscScalar*);
 88:   PetscErrorCode (*tdot_local)(Vec,Vec,PetscScalar*);
 89:   PetscErrorCode (*norm_local)(Vec,NormType,PetscReal*);
 90:   PetscErrorCode (*mdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
 91:   PetscErrorCode (*mtdot_local)(Vec,PetscInt,const Vec[],PetscScalar*);
 92:   PetscErrorCode (*loadintovector)(PetscViewer,Vec);
 93:   PetscErrorCode (*loadintovectornative)(PetscViewer,Vec);
 94:   PetscErrorCode (*reciprocal)(Vec);
 95:   PetscErrorCode (*viewnative)(Vec,PetscViewer);
 96:   PetscErrorCode (*conjugate)(Vec);
 97:   PetscErrorCode (*setlocaltoglobalmapping)(Vec,ISLocalToGlobalMapping);
 98:   PetscErrorCode (*setvalueslocal)(Vec,PetscInt,const PetscInt *,const PetscScalar *,InsertMode);
 99:   PetscErrorCode (*resetarray)(Vec);      /* vector points to its original array, i.e. undoes any VecPlaceArray() */
100:   PetscErrorCode (*setfromoptions)(Vec);
101:   PetscErrorCode (*maxpointwisedivide)(Vec,Vec,PetscReal*);      /* m = max abs(x ./ y) */
102:   PetscErrorCode (*load)(PetscViewer,const VecType,Vec*);
103:   PetscErrorCode (*pointwisemax)(Vec,Vec,Vec);
104:   PetscErrorCode (*pointwisemaxabs)(Vec,Vec,Vec);
105:   PetscErrorCode (*pointwisemin)(Vec,Vec,Vec);
106:   PetscErrorCode (*getvalues)(Vec,PetscInt,const PetscInt[],PetscScalar[]);
107:   PetscErrorCode (*sqrt)(Vec);
108:   PetscErrorCode (*abs)(Vec);
109:   PetscErrorCode (*exp)(Vec);
110:   PetscErrorCode (*log)(Vec);
111:   PetscErrorCode (*shift)(Vec);
112:   PetscErrorCode (*create)(Vec);
113: };

115: /* 
116:     The stash is used to temporarily store inserted vec values that 
117:   belong to another processor. During the assembly phase the stashed 
118:   values are moved to the correct processor and 
119: */

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

149: struct _p_Vec {
150:   PETSCHEADER(struct _VecOps);
151:   PetscLayout            map;
152:   void                   *data;     /* implementation-specific data */
153:   ISLocalToGlobalMapping mapping;   /* mapping used in VecSetValuesLocal() */
154:   ISLocalToGlobalMapping bmapping;  /* mapping used in VecSetValuesBlockedLocal() */
155:   PetscTruth             array_gotten;
156:   VecStash               stash,bstash; /* used for storing off-proc values during assembly */
157:   PetscTruth             petscnative;  /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
158: };

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

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

172: /* Default obtain and release vectors; can be used by any implementation */
173: EXTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
174: EXTERN PetscErrorCode VecDestroyVecs_Default(Vec [],PetscInt);
175: EXTERN PetscErrorCode VecLoadIntoVector_Default(PetscViewer,Vec);


179: /* --------------------------------------------------------------------*/
180: /*                                                                     */
181: /* Defines the data structures used in the Vec Scatter operations      */

183: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
184:                VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
185:                VEC_SCATTER_MPI_TOONE} VecScatterType;

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

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

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

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

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

275: EXTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
276: EXTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
277: EXTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
278: EXTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
279: EXTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
280: EXTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
281: EXTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
282: EXTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);

284: /*
285:   VecStashValue_Private - inserts a single value into the stash.

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

305: /*
306:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash. 

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

328: EXTERN PetscErrorCode VecReciprocal_Default(Vec);


336: #if defined(PETSC_HAVE_MATLAB_ENGINE)
338: EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
339: EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
341: #endif

344: #endif