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:    It is in the public include directories so that VecGetArray() (which is public) can be defined
  7: */

  9: #ifndef __VECIMPL_H

 12:  #include petscvec.h

 15: typedef struct {
 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 */
 20: } PetscMap;

 22: EXTERN PetscErrorCode PetscMapInitialize(MPI_Comm,PetscMap*);
 23: EXTERN PetscErrorCode PetscMapCopy(MPI_Comm,PetscMap*,PetscMap*);
 24: EXTERN PetscErrorCode  PetscMapSetLocalSize(PetscMap*,PetscInt);
 25: EXTERN PetscErrorCode  PetscMapGetLocalSize(PetscMap*,PetscInt *);
 26: PetscPolymorphicFunction(PetscMapGetLocalSize,(PetscMap *m),(m,&s),PetscInt,s)
 27: EXTERN PetscErrorCode  PetscMapSetSize(PetscMap*,PetscInt);
 28: EXTERN PetscErrorCode  PetscMapGetSize(PetscMap*,PetscInt *);
 29: PetscPolymorphicFunction(PetscMapGetSize,(PetscMap *m),(m,&s),PetscInt,s)
 30: EXTERN PetscErrorCode  PetscMapSetBlockSize(PetscMap*,PetscInt);
 31: EXTERN PetscErrorCode  PetscMapGetLocalRange(PetscMap*,PetscInt *,PetscInt *);
 32: EXTERN PetscErrorCode  PetscMapGetGlobalRange(PetscMap*,const PetscInt *[]);
 33: EXTERN PetscErrorCode  PetscMapSetSizeBlockSize(PetscMap*,PetscInt);
 34: EXTERN PetscErrorCode  PetscMapGetSizeBlockSize(PetscMap*,PetscInt *);

 36: /* ----------------------------------------------------------------------------*/

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

 96: /* 
 97:     The stash is used to temporarily store inserted vec values that 
 98:   belong to another processor. During the assembly phase the stashed 
 99:   values are moved to the correct processor and 
100: */

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

128: struct _p_Vec {
129:   PETSCHEADER(struct _VecOps);
130:   PetscMap               map;
131:   void                   *data;     /* implementation-specific data */
132:   ISLocalToGlobalMapping mapping;   /* mapping used in VecSetValuesLocal() */
133:   ISLocalToGlobalMapping bmapping;  /* mapping used in VecSetValuesBlockedLocal() */
134:   PetscTruth             array_gotten;
135:   VecStash               stash,bstash; /* used for storing off-proc values during assembly */
136:   PetscTruth             petscnative;  /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
137: };

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

142: /*
143:      Common header shared by array based vectors, 
144:    currently Vec_Seq and Vec_MPI
145: */
146: #define VECHEADER                          \
147:   PetscScalar *array;                      \
148:   PetscScalar *array_allocated;                        /* if the array was allocated by PETSc this is its pointer */  \
149:   PetscScalar *unplacedarray;                           /* if one called VecPlaceArray(), this is where it stashed the original */

151: /* Default obtain and release vectors; can be used by any implementation */
152: EXTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
153: EXTERN PetscErrorCode VecDestroyVecs_Default(Vec [],PetscInt);
154: EXTERN PetscErrorCode VecLoadIntoVector_Default(PetscViewer,Vec);


158: /* --------------------------------------------------------------------*/
159: /*                                                                     */
160: /* Defines the data structures used in the Vec Scatter operations      */

162: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
163:                VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
164:                VEC_SCATTER_MPI_TOONE} VecScatterType;

166: /* 
167:    These scatters are for the purely local case.
168: */
169: typedef struct {
170:   VecScatterType type;
171:   PetscInt       n;                    /* number of components to scatter */
172:   PetscInt       *vslots;              /* locations of components */
173:   /*
174:        The next three fields are used in parallel scatters, they contain 
175:        optimization in the special case that the "to" vector and the "from" 
176:        vector are the same, so one only needs copy components that truly 
177:        copies instead of just y[idx[i]] = y[jdx[i]] where idx[i] == jdx[i].
178:   */
179:   PetscTruth     nonmatching_computed;
180:   PetscInt       n_nonmatching;        /* number of "from"s  != "to"s */
181:   PetscInt       *slots_nonmatching;   /* locations of "from"s  != "to"s */
182:   PetscTruth     is_copy;
183:   PetscInt       copy_start;   /* local scatter is a copy starting at copy_start */
184:   PetscInt       copy_length;
185: } VecScatter_Seq_General;

187: typedef struct {
188:   VecScatterType type;
189:   PetscInt       n;
190:   PetscInt       first;
191:   PetscInt       step;
192: } VecScatter_Seq_Stride;

194: /*
195:    This scatter is for a global vector copied (completely) to each processor (or all to one)
196: */
197: typedef struct {
198:   VecScatterType type;
199:   PetscMPIInt    *count;        /* elements of vector on each processor */
200:   PetscScalar    *work1;
201:   PetscScalar    *work2;
202: } VecScatter_MPI_ToAll;

204: /*
205:    This is the general parallel scatter
206: */
207: typedef struct {
208:   VecScatterType         type;
209:   PetscInt               n;        /* number of processors to send/receive */
210:   PetscInt               *starts;  /* starting point in indices and values for each proc*/
211:   PetscInt               *indices; /* list of all components sent or received */
212:   PetscMPIInt            *procs;   /* processors we are communicating with in scatter */
213:   MPI_Request            *requests,*rev_requests;
214:   PetscScalar            *values;  /* buffer for all sends or receives */
215:   VecScatter_Seq_General local;    /* any part that happens to be local */
216:   MPI_Status             *sstatus,*rstatus;
217:   PetscTruth             use_readyreceiver;
218:   PetscInt               bs;
219:   PetscTruth             sendfirst;
220:   PetscTruth             contiq;
221:   /* for MPI_Alltoallv() approach */
222:   PetscTruth             use_alltoallv;
223:   PetscMPIInt            *counts,*displs;
224:   /* for MPI_Alltoallw() approach */
225:   PetscTruth             use_alltoallw;
226:   PetscMPIInt            *wcounts,*wdispls;
227:   MPI_Datatype           *types;
228: } VecScatter_MPI_General;

230: struct _p_VecScatter {
231:   PETSCHEADER(int);
232:   PetscInt       to_n,from_n;
233:   PetscTruth     inuse;                /* prevents corruption from mixing two scatters */
234:   PetscTruth     beginandendtogether;  /* indicates that the scatter begin and end  function are called together, VecScatterEnd()
235:                                           is then treated as a nop */
236:   PetscTruth     packtogether;         /* packs all the messages before sending, same with receive */
237:   PetscErrorCode (*begin)(Vec,Vec,InsertMode,ScatterMode,VecScatter);
238:   PetscErrorCode (*end)(Vec,Vec,InsertMode,ScatterMode,VecScatter);
239:   PetscErrorCode (*copy)(VecScatter,VecScatter);
240:   PetscErrorCode (*destroy)(VecScatter);
241:   PetscErrorCode (*view)(VecScatter,PetscViewer);
242:   void           *fromdata,*todata;
243: };

245: EXTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
246: EXTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
247: EXTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
248: EXTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
249: EXTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
250: EXTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
251: EXTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
252: EXTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);

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: #define VecStashValue_Private(stash,row,value) \
263: {  \
264:   /* Check and see if we have sufficient memory */ \
265:   if (((stash)->n + 1) > (stash)->nmax) { \
266:     VecStashExpand_Private(stash,1); \
267:   } \
268:   (stash)->idx[(stash)->n]   = row; \
269:   (stash)->array[(stash)->n] = value; \
270:   (stash)->n++; \
271: }

273: /*
274:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash. 

276:   Input Parameters:
277:   stash  - the stash
278:   idx    - the global block index
279:   values - the values inserted
280: */
281: #define VecStashValuesBlocked_Private(stash,row,values) \
282: { \
283:   PetscInt    jj,stash_bs=(stash)->bs; \
284:   PetscScalar *array; \
285:   if (((stash)->n+1) > (stash)->nmax) { \
286:     VecStashExpand_Private(stash,1); \
287:   } \
288:   array = (stash)->array + stash_bs*(stash)->n; \
289:   (stash)->idx[(stash)->n]   = row; \
290:   for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];} \
291:   (stash)->n++; \
292: }

294: EXTERN PetscErrorCode VecReciprocal_Default(Vec);


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

310: #endif