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.
  5: */

  7: #ifndef __VECIMPL_H

 10:  #include petscvec.h

 12: struct _PetscMapOps {
 13:   PetscErrorCode (*setfromoptions)(PetscMap);
 14:   PetscErrorCode (*destroy)(PetscMap);
 15: };

 17: struct _p_PetscMap {
 18:   PETSCHEADER(struct _PetscMapOps)
 19:   PetscInt  n,N;         /* local, global vector size */
 20:   PetscInt  rstart,rend; /* local start, local end + 1 */
 21:   PetscInt  *range;       /* the offset of each processor */
 22: };

 24: /* ----------------------------------------------------------------------------*/

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

 78: /* 
 79:     The stash is used to temporarily store inserted vec values that 
 80:   belong to another processor. During the assembly phase the stashed 
 81:   values are moved to the correct processor and 
 82: */

 84: typedef struct {
 85:   PetscInt      nmax;                   /* maximum stash size */
 86:   PetscInt      umax;                   /* max stash size user wants */
 87:   PetscInt      oldnmax;                /* the nmax value used previously */
 88:   PetscInt      n;                      /* stash size */
 89:   PetscInt      bs;                     /* block size of the stash */
 90:   PetscInt      reallocs;               /* preserve the no of mallocs invoked */
 91:   PetscInt      *idx;                   /* global row numbers in stash */
 92:   PetscScalar   *array;                 /* array to hold stashed values */
 93:   /* The following variables are used for communication */
 94:   MPI_Comm      comm;
 95:   PetscMPIInt   size,rank;
 96:   PetscMPIInt   tag1,tag2;
 97:   MPI_Request   *send_waits;            /* array of send requests */
 98:   MPI_Request   *recv_waits;            /* array of receive requests */
 99:   MPI_Status    *send_status;           /* array of send status */
100:   PetscInt      nsends,nrecvs;          /* numbers of sends and receives */
101:   PetscScalar   *svalues,*rvalues;      /* sending and receiving data */
102:   PetscInt      rmax;                   /* maximum message length */
103:   PetscInt      *nprocs;                /* tmp data used both during scatterbegin and end */
104:   PetscInt      nprocessed;             /* number of messages already processed */
105:   PetscTruth    donotstash;
106:   InsertMode    insertmode;
107:   PetscInt      *bowners;
108: } VecStash;

110: struct _p_Vec {
111:   PETSCHEADER(struct _VecOps)
112:   PetscMap               map;
113:   void                   *data;     /* implementation-specific data */
114:   PetscInt               N,n;      /* global, local vector size */
115:   PetscInt               bs;
116:   ISLocalToGlobalMapping mapping;   /* mapping used in VecSetValuesLocal() */
117:   ISLocalToGlobalMapping bmapping;  /* mapping used in VecSetValuesBlockedLocal() */
118:   PetscTruth             array_gotten;
119:   VecStash               stash,bstash; /* used for storing off-proc values during assembly */
120:   PetscTruth             petscnative;  /* means the ->data starts with VECHEADER and can use VecGetArrayFast()*/
121: };

123: #define VecGetArray(x,a)     ((x)->petscnative ? (*(a) = *((PetscScalar **)(x)->data),0) : VecGetArray_Private((x),(a)))
124: #define VecRestoreArray(x,a) ((x)->petscnative ? PetscObjectIncreaseState((PetscObject)x) : VecRestoreArray_Private((x),(a)))

126: /*
127:      Common header shared by array based vectors, 
128:    currently Vec_Seq and Vec_MPI
129: */
130: #define VECHEADER                         \
131:   PetscScalar *array;                          \
132:   PetscScalar *array_allocated;            

134: /* Default obtain and release vectors; can be used by any implementation */
135: EXTERN PetscErrorCode VecDuplicateVecs_Default(Vec,PetscInt,Vec *[]);
136: EXTERN PetscErrorCode VecDestroyVecs_Default(Vec [],PetscInt);
137: EXTERN PetscErrorCode VecLoadIntoVector_Default(PetscViewer,Vec);

139: /* --------------------------------------------------------------------*/
140: /*                                                                     */
141: /* Defines the data structures used in the Vec Scatter operations      */

143: typedef enum { VEC_SCATTER_SEQ_GENERAL,VEC_SCATTER_SEQ_STRIDE,
144:                VEC_SCATTER_MPI_GENERAL,VEC_SCATTER_MPI_TOALL,
145:                VEC_SCATTER_MPI_TOONE} VecScatterType;

147: /* 
148:    These scatters are for the purely local case.
149: */
150: typedef struct {
151:   VecScatterType type;
152:   PetscInt       n;                    /* number of components to scatter */
153:   PetscInt       *slots;               /* locations of components */
154:   /*
155:        The next three fields are used in parallel scatters, they contain 
156:        optimization in the special case that the "to" vector and the "from" 
157:        vector are the same, so one only needs copy components that truly 
158:        copies instead of just y[idx[i]] = y[jdx[i]] where idx[i] == jdx[i].
159:   */
160:   PetscTruth     nonmatching_computed;
161:   PetscInt       n_nonmatching;        /* number of "from"s  != "to"s */
162:   PetscInt       *slots_nonmatching;   /* locations of "from"s  != "to"s */
163:   PetscTruth     is_copy;
164:   PetscInt       copy_start;   /* local scatter is a copy starting at copy_start */
165:   PetscInt       copy_length;
166: } VecScatter_Seq_General;

168: typedef struct {
169:   VecScatterType type;
170:   PetscInt       n;
171:   PetscInt       first;
172:   PetscInt       step;
173: } VecScatter_Seq_Stride;

175: /*
176:    This scatter is for a global vector copied (completely) to each processor (or all to one)
177: */
178: typedef struct {
179:   VecScatterType type;
180:   PetscMPIInt    *count;        /* elements of vector on each processor */
181:   PetscScalar    *work1;
182:   PetscScalar    *work2;
183: } VecScatter_MPI_ToAll;

185: /*
186:    This is the general parallel scatter
187: */
188: typedef struct {
189:   VecScatterType         type;
190:   PetscInt               n;        /* number of processors to send/receive */
191:   PetscInt               *starts;  /* starting point in indices and values for each proc*/
192:   PetscInt               *indices; /* list of all components sent or received */
193:   PetscMPIInt            *procs;   /* processors we are communicating with in scatter */
194:   MPI_Request            *requests,*rev_requests;
195:   PetscScalar            *values;  /* buffer for all sends or receives */
196:   VecScatter_Seq_General local;    /* any part that happens to be local */
197:   MPI_Status             *sstatus,*rstatus;
198:   PetscTruth             use_readyreceiver;
199:   PetscInt               bs;
200:   PetscTruth             sendfirst;
201: } VecScatter_MPI_General;

203: struct _p_VecScatter {
204:   PETSCHEADER(int)
205:   PetscInt       to_n,from_n;
206:   PetscTruth     inuse;   /* prevents corruption from mixing two scatters */
207:   PetscTruth     beginandendtogether;         /* indicates that the scatter begin and end
208:                                           function are called together, VecScatterEnd()
209:                                           is then treated as a nop */
210:   PetscTruth     packtogether; /* packs all the messages before sending, same with receive */
211:   PetscErrorCode (*postrecvs)(Vec,Vec,InsertMode,ScatterMode,VecScatter);
212:   PetscErrorCode (*begin)(Vec,Vec,InsertMode,ScatterMode,VecScatter);
213:   PetscErrorCode (*end)(Vec,Vec,InsertMode,ScatterMode,VecScatter);
214:   PetscErrorCode (*copy)(VecScatter,VecScatter);
215:   PetscErrorCode (*destroy)(VecScatter);
216:   PetscErrorCode (*view)(VecScatter,PetscViewer);
217:   void           *fromdata,*todata;
218: };

220: EXTERN PetscErrorCode VecStashCreate_Private(MPI_Comm,PetscInt,VecStash*);
221: EXTERN PetscErrorCode VecStashDestroy_Private(VecStash*);
222: EXTERN PetscErrorCode VecStashExpand_Private(VecStash*,PetscInt);
223: EXTERN PetscErrorCode VecStashScatterEnd_Private(VecStash*);
224: EXTERN PetscErrorCode VecStashSetInitialSize_Private(VecStash*,PetscInt);
225: EXTERN PetscErrorCode VecStashGetInfo_Private(VecStash*,PetscInt*,PetscInt*);
226: EXTERN PetscErrorCode VecStashScatterBegin_Private(VecStash*,PetscInt*);
227: EXTERN PetscErrorCode VecStashScatterGetMesg_Private(VecStash*,PetscMPIInt*,PetscInt**,PetscScalar**,PetscInt*);

229: /* 
230:    The following are implemented as macros to avoid the function
231:    call overhead.

235: */

237: /*
238:   VecStashValue_Private - inserts a single value into the stash.

240:   Input Parameters:
241:   stash  - the stash
242:   idx    - the global of the inserted value
243:   values - the value inserted
244: */
245: #define VecStashValue_Private(stash,row,value) \
246: {  \
247:   /* Check and see if we have sufficient memory */ \
248:   if (((stash)->n + 1) > (stash)->nmax) { \
249:     VecStashExpand_Private(stash,1); \
250:   } \
251:   (stash)->idx[(stash)->n]   = row; \
252:   (stash)->array[(stash)->n] = value; \
253:   (stash)->n++; \
254: }

256: /*
257:   VecStashValuesBlocked_Private - inserts 1 block of values into the stash. 

259:   Input Parameters:
260:   stash  - the stash
261:   idx    - the global block index
262:   values - the values inserted
263: */
264: #define VecStashValuesBlocked_Private(stash,row,values) \
265: { \
266:   PetscInt    jj,stash_bs=(stash)->bs; \
267:   PetscScalar *array; \
268:   if (((stash)->n+1) > (stash)->nmax) { \
269:     VecStashExpand_Private(stash,1); \
270:   } \
271:   array = (stash)->array + stash_bs*(stash)->n; \
272:   (stash)->idx[(stash)->n]   = row; \
273:   for (jj=0; jj<stash_bs; jj++) { array[jj] = values[jj];} \
274:   (stash)->n++; \
275: }

277: EXTERN PetscErrorCode VecReciprocal_Default(Vec);

279: #if defined(PETSC_HAVE_MATLAB) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
281: EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject,void*);
282: EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject,void*);
284: #endif


287: #endif