Actual source code: vecstash.c

  2: #include <private/vecimpl.h>

  4: #define DEFAULT_STASH_SIZE   100

  6: /*
  7:   VecStashCreate_Private - Creates a stash,currently used for all the parallel 
  8:   matrix implementations. The stash is where elements of a matrix destined 
  9:   to be stored on other processors are kept until matrix assembly is done.

 11:   This is a simple minded stash. Simply adds entries to end of stash.

 13:   Input Parameters:
 14:   comm - communicator, required for scatters.
 15:   bs   - stash block size. used when stashing blocks of values

 17:   Output Parameters:
 18:   stash    - the newly created stash
 19: */
 22: PetscErrorCode VecStashCreate_Private(MPI_Comm comm,PetscInt bs,VecStash *stash)
 23: {
 25:   PetscInt       max,*opt,nopt;
 26:   PetscBool      flg;

 29:   /* Require 2 tags, get the second using PetscCommGetNewTag() */
 30:   stash->comm = comm;
 31:   PetscCommGetNewTag(stash->comm,&stash->tag1);
 32:   PetscCommGetNewTag(stash->comm,&stash->tag2);
 33:   MPI_Comm_size(stash->comm,&stash->size);
 34:   MPI_Comm_rank(stash->comm,&stash->rank);

 36:   nopt = stash->size;
 37:   PetscMalloc(nopt*sizeof(PetscInt),&opt);
 38:   PetscOptionsGetIntArray(PETSC_NULL,"-vecstash_initial_size",opt,&nopt,&flg);
 39:   if (flg) {
 40:     if (nopt == 1)                max = opt[0];
 41:     else if (nopt == stash->size) max = opt[stash->rank];
 42:     else if (stash->rank < nopt)  max = opt[stash->rank];
 43:     else                          max = 0; /* use default */
 44:     stash->umax = max;
 45:   } else {
 46:     stash->umax = 0;
 47:   }
 48:   PetscFree(opt);

 50:   if (bs <= 0) bs = 1;

 52:   stash->bs       = bs;
 53:   stash->nmax     = 0;
 54:   stash->oldnmax  = 0;
 55:   stash->n        = 0;
 56:   stash->reallocs = -1;
 57:   stash->idx      = 0;
 58:   stash->array    = 0;

 60:   stash->send_waits  = 0;
 61:   stash->recv_waits  = 0;
 62:   stash->send_status = 0;
 63:   stash->nsends      = 0;
 64:   stash->nrecvs      = 0;
 65:   stash->svalues     = 0;
 66:   stash->rvalues     = 0;
 67:   stash->rmax        = 0;
 68:   stash->nprocs      = 0;
 69:   stash->nprocessed  = 0;
 70:   stash->donotstash  = PETSC_FALSE;
 71:   stash->ignorenegidx  = PETSC_FALSE;
 72:   return(0);
 73: }

 75: /* 
 76:    VecStashDestroy_Private - Destroy the stash
 77: */
 80: PetscErrorCode VecStashDestroy_Private(VecStash *stash)
 81: {

 85:   PetscFree2(stash->array,stash->idx);
 86:   PetscFree(stash->bowners);
 87:   return(0);
 88: }

 90: /* 
 91:    VecStashScatterEnd_Private - This is called as the fial stage of
 92:    scatter. The final stages of message passing is done here, and
 93:    all the memory used for message passing is cleanedu up. This
 94:    routine also resets the stash, and deallocates the memory used
 95:    for the stash. It also keeps track of the current memory usage
 96:    so that the same value can be used the next time through.
 97: */
100: PetscErrorCode VecStashScatterEnd_Private(VecStash *stash)
101: {
103:   PetscInt       nsends=stash->nsends,oldnmax;
104:   MPI_Status     *send_status;

107:   /* wait on sends */
108:   if (nsends) {
109:     PetscMalloc(2*nsends*sizeof(MPI_Status),&send_status);
110:     MPI_Waitall(2*nsends,stash->send_waits,send_status);
111:     PetscFree(send_status);
112:   }

114:   /* Now update nmaxold to be app 10% more than max n, this way the
115:      wastage of space is reduced the next time this stash is used.
116:      Also update the oldmax, only if it increases */
117:   if (stash->n) {
118:     oldnmax  = ((PetscInt)(stash->n * 1.1) + 5)*stash->bs;
119:     if (oldnmax > stash->oldnmax) stash->oldnmax = oldnmax;
120:   }

122:   stash->nmax       = 0;
123:   stash->n          = 0;
124:   stash->reallocs   = -1;
125:   stash->rmax       = 0;
126:   stash->nprocessed = 0;

128:   PetscFree2(stash->array,stash->idx);
129:   stash->array = 0;
130:   stash->idx   = 0;
131:   PetscFree(stash->send_waits);
132:   PetscFree(stash->recv_waits);
133:   PetscFree2(stash->svalues,stash->sindices);
134:   PetscFree2(stash->rvalues,stash->rindices);
135:   PetscFree(stash->nprocs);
136:   return(0);
137: }

139: /* 
140:    VecStashGetInfo_Private - Gets the relavant statistics of the stash

142:    Input Parameters:
143:    stash    - the stash
144:    nstash   - the size of the stash
145:    reallocs - the number of additional mallocs incurred.
146:    
147: */
150: PetscErrorCode VecStashGetInfo_Private(VecStash *stash,PetscInt *nstash,PetscInt *reallocs)
151: {

154:   if (nstash)  *nstash   = stash->n*stash->bs;
155:   if (reallocs) {
156:     if (stash->reallocs < 0) *reallocs = 0;
157:     else                     *reallocs = stash->reallocs;
158:   }
159:   return(0);
160: }


163: /* 
164:    VecStashSetInitialSize_Private - Sets the initial size of the stash

166:    Input Parameters:
167:    stash  - the stash
168:    max    - the value that is used as the max size of the stash. 
169:             this value is used while allocating memory. It specifies
170:             the number of vals stored, even with the block-stash
171: */
174: PetscErrorCode VecStashSetInitialSize_Private(VecStash *stash,PetscInt max)
175: {
177:   stash->umax = max;
178:   return(0);
179: }

181: /* VecStashExpand_Private - Expand the stash. This function is called
182:    when the space in the stash is not sufficient to add the new values
183:    being inserted into the stash.
184:    
185:    Input Parameters:
186:    stash - the stash
187:    incr  - the minimum increase requested
188:    
189:    Notes: 
190:    This routine doubles the currently used memory. 
191: */
194: PetscErrorCode VecStashExpand_Private(VecStash *stash,PetscInt incr)
195: {
197:   PetscInt       *n_idx,newnmax,bs=stash->bs;
198:   PetscScalar    *n_array;

201:   /* allocate a larger stash. */
202:   if (!stash->oldnmax && !stash->nmax) { /* new stash */
203:     if (stash->umax)                  newnmax = stash->umax/bs;
204:     else                              newnmax = DEFAULT_STASH_SIZE/bs;
205:   } else if (!stash->nmax) { /* resuing stash */
206:     if (stash->umax > stash->oldnmax) newnmax = stash->umax/bs;
207:     else                              newnmax = stash->oldnmax/bs;
208:   } else                              newnmax = stash->nmax*2;

210:   if (newnmax  < (stash->nmax + incr)) newnmax += 2*incr;

212:   PetscMalloc2(bs*newnmax,PetscScalar,&n_array,newnmax,PetscInt,&n_idx);
213:   PetscMemcpy(n_array,stash->array,bs*stash->nmax*sizeof(PetscScalar));
214:   PetscMemcpy(n_idx,stash->idx,stash->nmax*sizeof(PetscInt));
215:   PetscFree2(stash->array,stash->idx);
216:   stash->array   = n_array;
217:   stash->idx     = n_idx;
218:   stash->nmax    = newnmax;
219:   stash->reallocs++;
220:   return(0);
221: }
222: /*
223:   VecStashScatterBegin_Private - Initiates the transfer of values to the
224:   correct owners. This function goes through the stash, and check the
225:   owners of each stashed value, and sends the values off to the owner
226:   processors.

228:   Input Parameters:
229:   stash  - the stash
230:   owners - an array of size 'no-of-procs' which gives the ownership range
231:            for each node.

233:   Notes: The 'owners' array in the cased of the blocked-stash has the 
234:   ranges specified blocked global indices, and for the regular stash in
235:   the proper global indices.
236: */
239: PetscErrorCode VecStashScatterBegin_Private(VecStash *stash,PetscInt *owners)
240: {
242:   PetscMPIInt    size = stash->size,tag1=stash->tag1,tag2=stash->tag2;
243:   PetscInt       *owner,*start,*nprocs,nsends,nreceives;
244:   PetscInt       nmax,count,*sindices,*rindices,i,j,idx,bs=stash->bs,lastidx;
245:   PetscScalar    *rvalues,*svalues;
246:   MPI_Comm       comm = stash->comm;
247:   MPI_Request    *send_waits,*recv_waits;


251:   /*  first count number of contributors to each processor */
252:   PetscMalloc(2*size*sizeof(PetscInt),&nprocs);
253:   PetscMemzero(nprocs,2*size*sizeof(PetscInt));
254:   PetscMalloc(stash->n*sizeof(PetscInt),&owner);

256:   j       = 0;
257:   lastidx = -1;
258:   for (i=0; i<stash->n; i++) {
259:     /* if indices are NOT locally sorted, need to start search at the beginning */
260:     if (lastidx > (idx = stash->idx[i])) j = 0;
261:     lastidx = idx;
262:     for (; j<size; j++) {
263:       if (idx >= owners[j] && idx < owners[j+1]) {
264:         nprocs[2*j]++; nprocs[2*j+1] = 1; owner[i] = j; break;
265:       }
266:     }
267:   }
268:   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
269: 
270:   /* inform other processors of number of messages and max length*/
271:   PetscMaxSum(comm,nprocs,&nmax,&nreceives);

273:   /* post receives: 
274:      since we don't know how long each individual message is we 
275:      allocate the largest needed buffer for each receive. Potentially 
276:      this is a lot of wasted space.
277:   */
278:   PetscMalloc2(nreceives*nmax*bs,PetscScalar,&rvalues,nreceives*nmax,PetscInt,&rindices);
279:   PetscMalloc(2*nreceives*sizeof(MPI_Request),&recv_waits);
280:   for (i=0,count=0; i<nreceives; i++) {
281:     MPI_Irecv(rvalues+bs*nmax*i,bs*nmax,MPIU_SCALAR,MPI_ANY_SOURCE,tag1,comm,recv_waits+count++);
282:     MPI_Irecv(rindices+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag2,comm,recv_waits+count++);
283:   }

285:   /* do sends:
286:       1) starts[i] gives the starting index in svalues for stuff going to 
287:          the ith processor
288:   */
289:   PetscMalloc2(stash->n*bs,PetscScalar,&svalues,stash->n,PetscInt,&sindices);
290:   PetscMalloc(2*nsends*sizeof(MPI_Request),&send_waits);
291:   PetscMalloc(size*sizeof(PetscInt),&start);
292:   /* use 2 sends the first with all_v, the next with all_i */
293:   start[0] = 0;
294:   for (i=1; i<size; i++) {
295:     start[i] = start[i-1] + nprocs[2*i-2];
296:   }
297:   for (i=0; i<stash->n; i++) {
298:     j = owner[i];
299:     if (bs == 1) {
300:       svalues[start[j]] = stash->array[i];
301:     } else {
302:       PetscMemcpy(svalues+bs*start[j],stash->array+bs*i,bs*sizeof(PetscScalar));
303:     }
304:     sindices[start[j]]  = stash->idx[i];
305:     start[j]++;
306:   }
307:   start[0] = 0;
308:   for (i=1; i<size; i++) { start[i] = start[i-1] + nprocs[2*i-2];}
309:   for (i=0,count=0; i<size; i++) {
310:     if (nprocs[2*i+1]) {
311:       MPI_Isend(svalues+bs*start[i],bs*nprocs[2*i],MPIU_SCALAR,i,tag1,comm,send_waits+count++);
312:       MPI_Isend(sindices+start[i],nprocs[2*i],MPIU_INT,i,tag2,comm,send_waits+count++);
313:     }
314:   }
315:   PetscFree(owner);
316:   PetscFree(start);
317:   /* This memory is reused in scatter end  for a different purpose*/
318:   for (i=0; i<2*size; i++) nprocs[i] = -1;
319:   stash->nprocs      = nprocs;

321:   stash->svalues    = svalues;
322:   stash->sindices   = sindices;
323:   stash->rvalues    = rvalues;
324:   stash->rindices   = rindices;
325:   stash->nsends     = nsends;
326:   stash->nrecvs     = nreceives;
327:   stash->send_waits = send_waits;
328:   stash->recv_waits = recv_waits;
329:   stash->rmax       = nmax;
330:   return(0);
331: }

333: /* 
334:    VecStashScatterGetMesg_Private - This function waits on the receives posted 
335:    in the function VecStashScatterBegin_Private() and returns one message at 
336:    a time to the calling function. If no messages are left, it indicates this
337:    by setting flg = 0, else it sets flg = 1.

339:    Input Parameters:
340:    stash - the stash

342:    Output Parameters:
343:    nvals - the number of entries in the current message.
344:    rows  - an array of row indices (or blocked indices) corresponding to the values
345:    cols  - an array of columnindices (or blocked indices) corresponding to the values
346:    vals  - the values
347:    flg   - 0 indicates no more message left, and the current call has no values associated.
348:            1 indicates that the current call successfully received a message, and the
349:              other output parameters nvals,rows,cols,vals are set appropriately.
350: */
353: PetscErrorCode VecStashScatterGetMesg_Private(VecStash *stash,PetscMPIInt *nvals,PetscInt **rows,PetscScalar **vals,PetscInt *flg)
354: {
356:   PetscMPIInt    i = 0; /* dummy value so MPI-Uni doesn't think it is not set */
357:   PetscInt       *flg_v;
358:   PetscInt       i1,i2,bs=stash->bs;
359:   MPI_Status     recv_status;
360:   PetscBool      match_found = PETSC_FALSE;


364:   *flg = 0; /* When a message is discovered this is reset to 1 */
365:   /* Return if no more messages to process */
366:   if (stash->nprocessed == stash->nrecvs) { return(0); }

368:   flg_v = stash->nprocs;
369:   /* If a matching pair of receieves are found, process them, and return the data to
370:      the calling function. Until then keep receiving messages */
371:   while (!match_found) {
372:     MPI_Waitany(2*stash->nrecvs,stash->recv_waits,&i,&recv_status);
373:     /* Now pack the received message into a structure which is useable by others */
374:     if (i % 2) {
375:       MPI_Get_count(&recv_status,MPIU_INT,nvals);
376:       flg_v[2*recv_status.MPI_SOURCE+1] = i/2;
377:     } else {
378:       MPI_Get_count(&recv_status,MPIU_SCALAR,nvals);
379:       flg_v[2*recv_status.MPI_SOURCE] = i/2;
380:       *nvals = *nvals/bs;
381:     }
382: 
383:     /* Check if we have both the messages from this proc */
384:     i1 = flg_v[2*recv_status.MPI_SOURCE];
385:     i2 = flg_v[2*recv_status.MPI_SOURCE+1];
386:     if (i1 != -1 && i2 != -1) {
387:       *rows       = stash->rindices + i2*stash->rmax;
388:       *vals       = stash->rvalues + i1*bs*stash->rmax;
389:       *flg        = 1;
390:       stash->nprocessed ++;
391:       match_found = PETSC_TRUE;
392:     }
393:   }
394:   return(0);
395: }