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 *[]);
 46: EXTERN PetscErrorCode  PetscLayoutSetSizeBlockSize(PetscLayout,PetscInt);
 47: EXTERN PetscErrorCode  PetscLayoutGetSizeBlockSize(PetscLayout,PetscInt *);

 49: /* ----------------------------------------------------------------------------*/

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

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

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

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

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

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

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


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:   PetscTruth     nonmatching_computed;
203:   PetscInt       n_nonmatching;        /* number of "from"s  != "to"s */
204:   PetscInt       *slots_nonmatching;   /* locations of "from"s  != "to"s */
205:   PetscTruth     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:   PetscTruth             use_readyreceiver;
242:   PetscInt               bs;
243:   PetscTruth             sendfirst;
244:   PetscTruth             contiq;
245:   /* for MPI_Alltoallv() approach */
246:   PetscTruth             use_alltoallv;
247:   PetscMPIInt            *counts,*displs;
248:   /* for MPI_Alltoallw() approach */
249:   PetscTruth             use_alltoallw;
250: #if defined(PETSC_HAVE_MPI_ALLTOALLW)
251:   PetscMPIInt            *wcounts,*wdispls;
252:   MPI_Datatype           *types;
253: #endif
254:   PetscTruth             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:   PetscTruth     inuse;                /* prevents corruption from mixing two scatters */
265:   PetscTruth     beginandendtogether;  /* indicates that the scatter begin and end  function are called together, VecScatterEnd()
266:                                           is then treated as a nop */
267:   PetscTruth     packtogether;         /* packs all the messages before sending, same with receive */
268:   PetscTruth     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: };

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

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

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

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

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

330: EXTERN PetscErrorCode VecReciprocal_Default(Vec);


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

346: #endif