Actual source code: vecstash.c
1: #define PETSCVEC_DLL
3: #include private/vecimpl.h
5: #define DEFAULT_STASH_SIZE 100
7: /*
8: VecStashCreate_Private - Creates a stash,currently used for all the parallel
9: matrix implementations. The stash is where elements of a matrix destined
10: to be stored on other processors are kept until matrix assembly is done.
12: This is a simple minded stash. Simply adds entries to end of stash.
14: Input Parameters:
15: comm - communicator, required for scatters.
16: bs - stash block size. used when stashing blocks of values
18: Output Parameters:
19: stash - the newly created stash
20: */
23: PetscErrorCode VecStashCreate_Private(MPI_Comm comm,PetscInt bs,VecStash *stash)
24: {
26: PetscInt max,*opt,nopt;
27: PetscTruth flg;
30: /* Require 2 tags, get the second using PetscCommGetNewTag() */
31: stash->comm = comm;
32: PetscCommGetNewTag(stash->comm,&stash->tag1);
33: PetscCommGetNewTag(stash->comm,&stash->tag2);
34: MPI_Comm_size(stash->comm,&stash->size);
35: MPI_Comm_rank(stash->comm,&stash->rank);
37: nopt = stash->size;
38: PetscMalloc(nopt*sizeof(PetscInt),&opt);
39: PetscOptionsGetIntArray(PETSC_NULL,"-vecstash_initial_size",opt,&nopt,&flg);
40: if (flg) {
41: if (nopt == 1) max = opt[0];
42: else if (nopt == stash->size) max = opt[stash->rank];
43: else if (stash->rank < nopt) max = opt[stash->rank];
44: else max = 0; /* use default */
45: stash->umax = max;
46: } else {
47: stash->umax = 0;
48: }
49: PetscFree(opt);
51: if (bs <= 0) bs = 1;
53: stash->bs = bs;
54: stash->nmax = 0;
55: stash->oldnmax = 0;
56: stash->n = 0;
57: stash->reallocs = -1;
58: stash->idx = 0;
59: stash->array = 0;
61: stash->send_waits = 0;
62: stash->recv_waits = 0;
63: stash->send_status = 0;
64: stash->nsends = 0;
65: stash->nrecvs = 0;
66: stash->svalues = 0;
67: stash->rvalues = 0;
68: stash->rmax = 0;
69: stash->nprocs = 0;
70: stash->nprocessed = 0;
71: stash->donotstash = PETSC_FALSE;
72: return(0);
73: }
75: /*
76: VecStashDestroy_Private - Destroy the stash
77: */
80: PetscErrorCode VecStashDestroy_Private(VecStash *stash)
81: {
85: PetscFree(stash->array);
86: stash->array = 0;
87: PetscFree(stash->bowners);
88: return(0);
89: }
91: /*
92: VecStashScatterEnd_Private - This is called as the fial stage of
93: scatter. The final stages of message passing is done here, and
94: all the memory used for message passing is cleanedu up. This
95: routine also resets the stash, and deallocates the memory used
96: for the stash. It also keeps track of the current memory usage
97: so that the same value can be used the next time through.
98: */
101: PetscErrorCode VecStashScatterEnd_Private(VecStash *stash)
102: {
104: PetscInt nsends=stash->nsends,oldnmax;
105: MPI_Status *send_status;
108: /* wait on sends */
109: if (nsends) {
110: PetscMalloc(2*nsends*sizeof(MPI_Status),&send_status);
111: MPI_Waitall(2*nsends,stash->send_waits,send_status);
112: PetscFree(send_status);
113: }
115: /* Now update nmaxold to be app 10% more than max n, this way the
116: wastage of space is reduced the next time this stash is used.
117: Also update the oldmax, only if it increases */
118: if (stash->n) {
119: oldnmax = ((PetscInt)(stash->n * 1.1) + 5)*stash->bs;
120: if (oldnmax > stash->oldnmax) stash->oldnmax = oldnmax;
121: }
123: stash->nmax = 0;
124: stash->n = 0;
125: stash->reallocs = -1;
126: stash->rmax = 0;
127: stash->nprocessed = 0;
129: PetscFree(stash->array);
130: stash->array = 0;
131: stash->idx = 0;
132: PetscFree(stash->send_waits);
133: stash->send_waits = 0;
134: PetscFree(stash->recv_waits);
135: stash->recv_waits = 0;
136: PetscFree(stash->svalues);
137: stash->svalues = 0;
138: PetscFree(stash->rvalues);
139: stash->rvalues = 0;
140: PetscFree(stash->nprocs);
141: stash->nprocs = 0;
142: return(0);
143: }
145: /*
146: VecStashGetInfo_Private - Gets the relavant statistics of the stash
148: Input Parameters:
149: stash - the stash
150: nstash - the size of the stash
151: reallocs - the number of additional mallocs incurred.
152:
153: */
156: PetscErrorCode VecStashGetInfo_Private(VecStash *stash,PetscInt *nstash,PetscInt *reallocs)
157: {
160: if (nstash) *nstash = stash->n*stash->bs;
161: if (reallocs) {
162: if (stash->reallocs < 0) *reallocs = 0;
163: else *reallocs = stash->reallocs;
164: }
165: return(0);
166: }
169: /*
170: VecStashSetInitialSize_Private - Sets the initial size of the stash
172: Input Parameters:
173: stash - the stash
174: max - the value that is used as the max size of the stash.
175: this value is used while allocating memory. It specifies
176: the number of vals stored, even with the block-stash
177: */
180: PetscErrorCode VecStashSetInitialSize_Private(VecStash *stash,PetscInt max)
181: {
183: stash->umax = max;
184: return(0);
185: }
187: /* VecStashExpand_Private - Expand the stash. This function is called
188: when the space in the stash is not sufficient to add the new values
189: being inserted into the stash.
190:
191: Input Parameters:
192: stash - the stash
193: incr - the minimum increase requested
194:
195: Notes:
196: This routine doubles the currently used memory.
197: */
200: PetscErrorCode VecStashExpand_Private(VecStash *stash,PetscInt incr)
201: {
203: PetscInt *n_idx,newnmax,bs=stash->bs;
204: PetscScalar *n_array;
207: /* allocate a larger stash. */
208: if (!stash->oldnmax && !stash->nmax) { /* new stash */
209: if (stash->umax) newnmax = stash->umax/bs;
210: else newnmax = DEFAULT_STASH_SIZE/bs;
211: } else if (!stash->nmax) { /* resuing stash */
212: if (stash->umax > stash->oldnmax) newnmax = stash->umax/bs;
213: else newnmax = stash->oldnmax/bs;
214: } else newnmax = stash->nmax*2;
216: if (newnmax < (stash->nmax + incr)) newnmax += 2*incr;
218: PetscMalloc((newnmax)*(sizeof(PetscInt)+bs*sizeof(PetscScalar)),&n_array);
219: n_idx = (PetscInt*)(n_array + bs*newnmax);
220: PetscMemcpy(n_array,stash->array,bs*stash->nmax*sizeof(PetscScalar));
221: PetscMemcpy(n_idx,stash->idx,stash->nmax*sizeof(PetscInt));
222: PetscFree(stash->array);
223: stash->array = n_array;
224: stash->idx = n_idx;
225: stash->nmax = newnmax;
226: stash->reallocs++;
227: return(0);
228: }
229: /*
230: VecStashScatterBegin_Private - Initiates the transfer of values to the
231: correct owners. This function goes through the stash, and check the
232: owners of each stashed value, and sends the values off to the owner
233: processors.
235: Input Parameters:
236: stash - the stash
237: owners - an array of size 'no-of-procs' which gives the ownership range
238: for each node.
240: Notes: The 'owners' array in the cased of the blocked-stash has the
241: ranges specified blocked global indices, and for the regular stash in
242: the proper global indices.
243: */
246: PetscErrorCode VecStashScatterBegin_Private(VecStash *stash,PetscInt *owners)
247: {
249: PetscMPIInt size = stash->size,tag1=stash->tag1,tag2=stash->tag2;
250: PetscInt *owner,*start,*nprocs,nsends,nreceives;
251: PetscInt nmax,count,*sindices,*rindices,i,j,idx,bs=stash->bs,lastidx;
252: PetscScalar *rvalues,*svalues;
253: MPI_Comm comm = stash->comm;
254: MPI_Request *send_waits,*recv_waits;
258: /* first count number of contributors to each processor */
259: PetscMalloc(2*size*sizeof(PetscInt),&nprocs);
260: PetscMemzero(nprocs,2*size*sizeof(PetscInt));
261: PetscMalloc((stash->n+1)*sizeof(PetscInt),&owner);
263: j = 0;
264: lastidx = -1;
265: for (i=0; i<stash->n; i++) {
266: /* if indices are NOT locally sorted, need to start search at the beginning */
267: if (lastidx > (idx = stash->idx[i])) j = 0;
268: lastidx = idx;
269: for (; j<size; j++) {
270: if (idx >= owners[j] && idx < owners[j+1]) {
271: nprocs[2*j]++; nprocs[2*j+1] = 1; owner[i] = j; break;
272: }
273: }
274: }
275: nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
276:
277: /* inform other processors of number of messages and max length*/
278: PetscMaxSum(comm,nprocs,&nmax,&nreceives);
280: /* post receives:
281: since we don't know how long each individual message is we
282: allocate the largest needed buffer for each receive. Potentially
283: this is a lot of wasted space.
284: */
285: PetscMalloc((nreceives+1)*(nmax+1)*(bs*sizeof(PetscScalar)+sizeof(PetscInt)),&rvalues);
286: rindices = (PetscInt*)(rvalues + bs*nreceives*nmax);
287: PetscMalloc((nreceives+1)*2*sizeof(MPI_Request),&recv_waits);
288: for (i=0,count=0; i<nreceives; i++) {
289: MPI_Irecv(rvalues+bs*nmax*i,bs*nmax,MPIU_SCALAR,MPI_ANY_SOURCE,tag1,comm,recv_waits+count++);
290: MPI_Irecv(rindices+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag2,comm,recv_waits+count++);
291: }
293: /* do sends:
294: 1) starts[i] gives the starting index in svalues for stuff going to
295: the ith processor
296: */
297: PetscMalloc((stash->n+1)*(bs*sizeof(PetscScalar)+sizeof(PetscInt)),&svalues);
298: sindices = (PetscInt*)(svalues + bs*stash->n);
299: PetscMalloc(2*(nsends+1)*sizeof(MPI_Request),&send_waits);
300: PetscMalloc(size*sizeof(PetscInt),&start);
301: /* use 2 sends the first with all_v, the next with all_i */
302: start[0] = 0;
303: for (i=1; i<size; i++) {
304: start[i] = start[i-1] + nprocs[2*i-2];
305: }
306: for (i=0; i<stash->n; i++) {
307: j = owner[i];
308: if (bs == 1) {
309: svalues[start[j]] = stash->array[i];
310: } else {
311: PetscMemcpy(svalues+bs*start[j],stash->array+bs*i,bs*sizeof(PetscScalar));
312: }
313: sindices[start[j]] = stash->idx[i];
314: start[j]++;
315: }
316: start[0] = 0;
317: for (i=1; i<size; i++) { start[i] = start[i-1] + nprocs[2*i-2];}
318: for (i=0,count=0; i<size; i++) {
319: if (nprocs[2*i+1]) {
320: MPI_Isend(svalues+bs*start[i],bs*nprocs[2*i],MPIU_SCALAR,i,tag1,comm,send_waits+count++);
321: MPI_Isend(sindices+start[i],nprocs[2*i],MPIU_INT,i,tag2,comm,send_waits+count++);
322: }
323: }
324: PetscFree(owner);
325: PetscFree(start);
326: /* This memory is reused in scatter end for a different purpose*/
327: for (i=0; i<2*size; i++) nprocs[i] = -1;
328: stash->nprocs = nprocs;
330: stash->svalues = svalues; stash->rvalues = rvalues;
331: stash->nsends = nsends; stash->nrecvs = nreceives;
332: stash->send_waits = send_waits; stash->recv_waits = recv_waits;
333: stash->rmax = nmax;
334: return(0);
335: }
337: /*
338: VecStashScatterGetMesg_Private - This function waits on the receives posted
339: in the function VecStashScatterBegin_Private() and returns one message at
340: a time to the calling function. If no messages are left, it indicates this
341: by setting flg = 0, else it sets flg = 1.
343: Input Parameters:
344: stash - the stash
346: Output Parameters:
347: nvals - the number of entries in the current message.
348: rows - an array of row indices (or blocked indices) corresponding to the values
349: cols - an array of columnindices (or blocked indices) corresponding to the values
350: vals - the values
351: flg - 0 indicates no more message left, and the current call has no values associated.
352: 1 indicates that the current call successfully received a message, and the
353: other output parameters nvals,rows,cols,vals are set appropriately.
354: */
357: PetscErrorCode VecStashScatterGetMesg_Private(VecStash *stash,PetscMPIInt *nvals,PetscInt **rows,PetscScalar **vals,PetscInt *flg)
358: {
360: PetscMPIInt i;
361: PetscInt *flg_v;
362: PetscInt i1,i2,*rindices,bs=stash->bs;
363: MPI_Status recv_status;
364: PetscTruth match_found = PETSC_FALSE;
368: *flg = 0; /* When a message is discovered this is reset to 1 */
369: /* Return if no more messages to process */
370: if (stash->nprocessed == stash->nrecvs) { return(0); }
372: flg_v = stash->nprocs;
373: /* If a matching pair of receieves are found, process them, and return the data to
374: the calling function. Until then keep receiving messages */
375: while (!match_found) {
376: MPI_Waitany(2*stash->nrecvs,stash->recv_waits,&i,&recv_status);
377: /* Now pack the received message into a structure which is useable by others */
378: if (i % 2) {
379: MPI_Get_count(&recv_status,MPIU_INT,nvals);
380: flg_v[2*recv_status.MPI_SOURCE+1] = i/2;
381: } else {
382: MPI_Get_count(&recv_status,MPIU_SCALAR,nvals);
383: flg_v[2*recv_status.MPI_SOURCE] = i/2;
384: *nvals = *nvals/bs;
385: }
386:
387: /* Check if we have both the messages from this proc */
388: i1 = flg_v[2*recv_status.MPI_SOURCE];
389: i2 = flg_v[2*recv_status.MPI_SOURCE+1];
390: if (i1 != -1 && i2 != -1) {
391: rindices = (PetscInt*)(stash->rvalues + bs*stash->rmax*stash->nrecvs);
392: *rows = rindices + i2*stash->rmax;
393: *vals = stash->rvalues + i1*bs*stash->rmax;
394: *flg = 1;
395: stash->nprocessed ++;
396: match_found = PETSC_TRUE;
397: }
398: }
399: return(0);
400: }