Actual source code: pdvec.c

petsc-3.3-p7 2013-05-11
  2: /*
  3:      Code for some of the parallel vector primatives.
  4: */
  5: #include <../src/vec/vec/impls/mpi/pvecimpl.h>   /*I  "petscvec.h"   I*/

  9: PetscErrorCode VecDestroy_MPI(Vec v)
 10: {
 11:   Vec_MPI        *x = (Vec_MPI*)v->data;

 15: #if defined(PETSC_USE_LOG)
 16:   PetscLogObjectState((PetscObject)v,"Length=%D",v->map->N);
 17: #endif
 18:   if (!x) return(0);
 19:   PetscFree(x->array_allocated);

 21:   /* Destroy local representation of vector if it exists */
 22:   if (x->localrep) {
 23:     VecDestroy(&x->localrep);
 24:     VecScatterDestroy(&x->localupdate);
 25:   }
 26:   /* Destroy the stashes: note the order - so that the tags are freed properly */
 27:   VecStashDestroy_Private(&v->bstash);
 28:   VecStashDestroy_Private(&v->stash);
 29:   PetscFree(v->data);
 30:   return(0);
 31: }

 35: PetscErrorCode VecView_MPI_ASCII(Vec xin,PetscViewer viewer)
 36: {
 37:   PetscErrorCode    ierr;
 38:   PetscInt          i,work = xin->map->n,cnt,len,nLen;
 39:   PetscMPIInt       j,n = 0,size,rank,tag = ((PetscObject)viewer)->tag;
 40:   MPI_Status        status;
 41:   PetscScalar       *values;
 42:   const PetscScalar *xarray;
 43:   const char        *name;
 44:   PetscViewerFormat format;

 47:   VecGetArrayRead(xin,&xarray);
 48:   /* determine maximum message to arrive */
 49:   MPI_Comm_rank(((PetscObject)xin)->comm,&rank);
 50:   MPI_Reduce(&work,&len,1,MPIU_INT,MPI_MAX,0,((PetscObject)xin)->comm);
 51:   MPI_Comm_size(((PetscObject)xin)->comm,&size);

 53:   if (!rank) {
 54:     PetscMalloc(len*sizeof(PetscScalar),&values);
 55:     PetscViewerGetFormat(viewer,&format);
 56:     /*
 57:         MATLAB format and ASCII format are very similar except
 58:         MATLAB uses %18.16e format while ASCII uses %g
 59:     */
 60:     if (format == PETSC_VIEWER_ASCII_MATLAB) {
 61:       PetscObjectGetName((PetscObject)xin,&name);
 62:       PetscViewerASCIIPrintf(viewer,"%s = [\n",name);
 63:       for (i=0; i<xin->map->n; i++) {
 64: #if defined(PETSC_USE_COMPLEX)
 65:         if (PetscImaginaryPart(xarray[i]) > 0.0) {
 66:           PetscViewerASCIIPrintf(viewer,"%18.16e + %18.16ei\n",PetscRealPart(xarray[i]),PetscImaginaryPart(xarray[i]));
 67:         } else if (PetscImaginaryPart(xarray[i]) < 0.0) {
 68:           PetscViewerASCIIPrintf(viewer,"%18.16e - %18.16ei\n",PetscRealPart(xarray[i]),-PetscImaginaryPart(xarray[i]));
 69:         } else {
 70:           PetscViewerASCIIPrintf(viewer,"%18.16e\n",PetscRealPart(xarray[i]));
 71:         }
 72: #else
 73:         PetscViewerASCIIPrintf(viewer,"%18.16e\n",xarray[i]);
 74: #endif
 75:       }
 76:       /* receive and print messages */
 77:       for (j=1; j<size; j++) {
 78:         MPI_Recv(values,(PetscMPIInt)len,MPIU_SCALAR,j,tag,((PetscObject)xin)->comm,&status);
 79:         MPI_Get_count(&status,MPIU_SCALAR,&n);
 80:         for (i=0; i<n; i++) {
 81: #if defined(PETSC_USE_COMPLEX)
 82:           if (PetscImaginaryPart(values[i]) > 0.0) {
 83:             PetscViewerASCIIPrintf(viewer,"%18.16e + %18.16e i\n",PetscRealPart(values[i]),PetscImaginaryPart(values[i]));
 84:           } else if (PetscImaginaryPart(values[i]) < 0.0) {
 85:             PetscViewerASCIIPrintf(viewer,"%18.16e - %18.16e i\n",PetscRealPart(values[i]),-PetscImaginaryPart(values[i]));
 86:           } else {
 87:             PetscViewerASCIIPrintf(viewer,"%18.16e\n",PetscRealPart(values[i]));
 88:           }
 89: #else
 90:           PetscViewerASCIIPrintf(viewer,"%18.16e\n",values[i]);
 91: #endif
 92:         }
 93:       }
 94:       PetscViewerASCIIPrintf(viewer,"];\n");

 96:     } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
 97:       for (i=0; i<xin->map->n; i++) {
 98: #if defined(PETSC_USE_COMPLEX)
 99:         PetscViewerASCIIPrintf(viewer,"%18.16e %18.16e\n",PetscRealPart(xarray[i]),PetscImaginaryPart(xarray[i]));
100: #else
101:         PetscViewerASCIIPrintf(viewer,"%18.16e\n",xarray[i]);
102: #endif
103:       }
104:       /* receive and print messages */
105:       for (j=1; j<size; j++) {
106:         MPI_Recv(values,(PetscMPIInt)len,MPIU_SCALAR,j,tag,((PetscObject)xin)->comm,&status);
107:         MPI_Get_count(&status,MPIU_SCALAR,&n);
108:         for (i=0; i<n; i++) {
109: #if defined(PETSC_USE_COMPLEX)
110:           PetscViewerASCIIPrintf(viewer,"%18.16e %18.16e\n",PetscRealPart(values[i]),PetscImaginaryPart(values[i]));
111: #else
112:           PetscViewerASCIIPrintf(viewer,"%18.16e\n",values[i]);
113: #endif
114:         }
115:       }
116:     } else if (format == PETSC_VIEWER_ASCII_VTK || format == PETSC_VIEWER_ASCII_VTK_CELL) {
117:       /*
118:         state 0: No header has been output
119:         state 1: Only POINT_DATA has been output
120:         state 2: Only CELL_DATA has been output
121:         state 3: Output both, POINT_DATA last
122:         state 4: Output both, CELL_DATA last
123:       */
124:       static PetscInt stateId = -1;
125:       int outputState = 0;
126:       PetscBool  hasState;
127:       int doOutput = 0;
128:       PetscInt bs, b;

130:       if (stateId < 0) {
131:         PetscObjectComposedDataRegister(&stateId);
132:       }
133:       PetscObjectComposedDataGetInt((PetscObject) viewer, stateId, outputState, hasState);
134:       if (!hasState) {
135:         outputState = 0;
136:       }
137:       PetscObjectGetName((PetscObject) xin, &name);
138:       VecGetLocalSize(xin, &nLen);
139:       n    = PetscMPIIntCast(nLen);
140:       VecGetBlockSize(xin, &bs);
141:       if (format == PETSC_VIEWER_ASCII_VTK) {
142:         if (outputState == 0) {
143:           outputState = 1;
144:           doOutput = 1;
145:         } else if (outputState == 1) {
146:           doOutput = 0;
147:         } else if (outputState == 2) {
148:           outputState = 3;
149:           doOutput = 1;
150:         } else if (outputState == 3) {
151:           doOutput = 0;
152:         } else if (outputState == 4) {
153:           SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Tried to output POINT_DATA again after intervening CELL_DATA");
154:         }
155:         if (doOutput) {
156:           PetscViewerASCIIPrintf(viewer, "POINT_DATA %d\n", xin->map->N/bs);
157:         }
158:       } else {
159:         if (outputState == 0) {
160:           outputState = 2;
161:           doOutput = 1;
162:         } else if (outputState == 1) {
163:           outputState = 4;
164:           doOutput = 1;
165:         } else if (outputState == 2) {
166:           doOutput = 0;
167:         } else if (outputState == 3) {
168:           SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Tried to output CELL_DATA again after intervening POINT_DATA");
169:         } else if (outputState == 4) {
170:           doOutput = 0;
171:         }
172:         if (doOutput) {
173:           PetscViewerASCIIPrintf(viewer, "CELL_DATA %d\n", xin->map->N/bs);
174:         }
175:       }
176:       PetscObjectComposedDataSetInt((PetscObject) viewer, stateId, outputState);
177:       if (name) {
178:         if (bs == 3) {
179:           PetscViewerASCIIPrintf(viewer, "VECTORS %s double\n", name);
180:         } else {
181:           PetscViewerASCIIPrintf(viewer, "SCALARS %s double %d\n", name, bs);
182:         }
183:       } else {
184:         PetscViewerASCIIPrintf(viewer, "SCALARS scalars double %d\n", bs);
185:       }
186:       if (bs != 3) {
187:         PetscViewerASCIIPrintf(viewer, "LOOKUP_TABLE default\n");
188:       }
189:       for (i=0; i<n/bs; i++) {
190:         for (b=0; b<bs; b++) {
191:           if (b > 0) {
192:             PetscViewerASCIIPrintf(viewer," ");
193:           }
194:           PetscViewerASCIIPrintf(viewer,"%G",PetscRealPart(xarray[i*bs+b]));
195:         }
196:         PetscViewerASCIIPrintf(viewer,"\n");
197:       }
198:       for (j=1; j<size; j++) {
199:         MPI_Recv(values,(PetscMPIInt)len,MPIU_SCALAR,j,tag,((PetscObject)xin)->comm,&status);
200:         MPI_Get_count(&status,MPIU_SCALAR,&n);
201:         for (i=0; i<n/bs; i++) {
202:           for (b=0; b<bs; b++) {
203:             if (b > 0) {
204:               PetscViewerASCIIPrintf(viewer," ");
205:             }
206:             PetscViewerASCIIPrintf(viewer,"%G",PetscRealPart(values[i*bs+b]));
207:           }
208:           PetscViewerASCIIPrintf(viewer,"\n");
209:         }
210:       }
211:     } else if (format == PETSC_VIEWER_ASCII_VTK_COORDS) {
212:       PetscInt bs, b;

214:       VecGetLocalSize(xin, &nLen);
215:       n    = PetscMPIIntCast(nLen);
216:       VecGetBlockSize(xin, &bs);
217:       if ((bs < 1) || (bs > 3)) {
218:         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "VTK can only handle 3D objects, but vector dimension is %d", bs);
219:       }
220:       for (i=0; i<n/bs; i++) {
221:         for (b=0; b<bs; b++) {
222:           if (b > 0) {
223:             PetscViewerASCIIPrintf(viewer," ");
224:           }
225:           PetscViewerASCIIPrintf(viewer,"%G",PetscRealPart(xarray[i*bs+b]));
226:         }
227:         for (b=bs; b<3; b++) {
228:           PetscViewerASCIIPrintf(viewer," 0.0");
229:         }
230:         PetscViewerASCIIPrintf(viewer,"\n");
231:       }
232:       for (j=1; j<size; j++) {
233:         MPI_Recv(values,(PetscMPIInt)len,MPIU_SCALAR,j,tag,((PetscObject)xin)->comm,&status);
234:         MPI_Get_count(&status,MPIU_SCALAR,&n);
235:         for (i=0; i<n/bs; i++) {
236:           for (b=0; b<bs; b++) {
237:             if (b > 0) {
238:               PetscViewerASCIIPrintf(viewer," ");
239:             }
240:             PetscViewerASCIIPrintf(viewer,"%G",PetscRealPart(values[i*bs+b]));
241:           }
242:           for (b=bs; b<3; b++) {
243:             PetscViewerASCIIPrintf(viewer," 0.0");
244:           }
245:           PetscViewerASCIIPrintf(viewer,"\n");
246:         }
247:       }
248:     } else if (format == PETSC_VIEWER_ASCII_PCICE) {
249:       PetscInt bs, b, vertexCount = 1;

251:       VecGetLocalSize(xin, &nLen);
252:       n    = PetscMPIIntCast(nLen);
253:       VecGetBlockSize(xin, &bs);
254:       if ((bs < 1) || (bs > 3)) {
255:         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "PCICE can only handle up to 3D objects, but vector dimension is %d", bs);
256:       }
257:       PetscViewerASCIIPrintf(viewer,"%D\n", xin->map->N/bs);
258:       for (i=0; i<n/bs; i++) {
259:         PetscViewerASCIIPrintf(viewer,"%7D   ", vertexCount++);
260:         for (b=0; b<bs; b++) {
261:           if (b > 0) {
262:             PetscViewerASCIIPrintf(viewer," ");
263:           }
264: #if !defined(PETSC_USE_COMPLEX)
265:           PetscViewerASCIIPrintf(viewer,"% 12.5E",xarray[i*bs+b]);
266: #endif
267:         }
268:         PetscViewerASCIIPrintf(viewer,"\n");
269:       }
270:       for (j=1; j<size; j++) {
271:         MPI_Recv(values,(PetscMPIInt)len,MPIU_SCALAR,j,tag,((PetscObject)xin)->comm,&status);
272:         MPI_Get_count(&status,MPIU_SCALAR,&n);
273:         for (i=0; i<n/bs; i++) {
274:           PetscViewerASCIIPrintf(viewer,"%7D   ", vertexCount++);
275:           for (b=0; b<bs; b++) {
276:             if (b > 0) {
277:               PetscViewerASCIIPrintf(viewer," ");
278:             }
279: #if !defined(PETSC_USE_COMPLEX)
280:             PetscViewerASCIIPrintf(viewer,"% 12.5E",values[i*bs+b]);
281: #endif
282:           }
283:           PetscViewerASCIIPrintf(viewer,"\n");
284:         }
285:       }
286:     } else {
287:       PetscObjectPrintClassNamePrefixType((PetscObject)xin,viewer,"Vector Object");
288:       if (format != PETSC_VIEWER_ASCII_COMMON) {PetscViewerASCIIPrintf(viewer,"Process [%d]\n",rank);}
289:       cnt = 0;
290:       for (i=0; i<xin->map->n; i++) {
291:         if (format == PETSC_VIEWER_ASCII_INDEX) {
292:           PetscViewerASCIIPrintf(viewer,"%D: ",cnt++);
293:         }
294: #if defined(PETSC_USE_COMPLEX)
295:         if (PetscImaginaryPart(xarray[i]) > 0.0) {
296:           PetscViewerASCIIPrintf(viewer,"%G + %G i\n",PetscRealPart(xarray[i]),PetscImaginaryPart(xarray[i]));
297:         } else if (PetscImaginaryPart(xarray[i]) < 0.0) {
298:           PetscViewerASCIIPrintf(viewer,"%G - %G i\n",PetscRealPart(xarray[i]),-PetscImaginaryPart(xarray[i]));
299:         } else {
300:           PetscViewerASCIIPrintf(viewer,"%G\n",PetscRealPart(xarray[i]));
301:         }
302: #else
303:         PetscViewerASCIIPrintf(viewer,"%G\n",xarray[i]);
304: #endif
305:       }
306:       /* receive and print messages */
307:       for (j=1; j<size; j++) {
308:         MPI_Recv(values,(PetscMPIInt)len,MPIU_SCALAR,j,tag,((PetscObject)xin)->comm,&status);
309:         MPI_Get_count(&status,MPIU_SCALAR,&n);
310:         if (format != PETSC_VIEWER_ASCII_COMMON) {
311:           PetscViewerASCIIPrintf(viewer,"Process [%d]\n",j);
312:         }
313:         for (i=0; i<n; i++) {
314:           if (format == PETSC_VIEWER_ASCII_INDEX) {
315:             PetscViewerASCIIPrintf(viewer,"%D: ",cnt++);
316:           }
317: #if defined(PETSC_USE_COMPLEX)
318:           if (PetscImaginaryPart(values[i]) > 0.0) {
319:             PetscViewerASCIIPrintf(viewer,"%G + %G i\n",PetscRealPart(values[i]),PetscImaginaryPart(values[i]));
320:           } else if (PetscImaginaryPart(values[i]) < 0.0) {
321:             PetscViewerASCIIPrintf(viewer,"%G - %G i\n",PetscRealPart(values[i]),-PetscImaginaryPart(values[i]));
322:           } else {
323:             PetscViewerASCIIPrintf(viewer,"%G\n",PetscRealPart(values[i]));
324:           }
325: #else
326:           PetscViewerASCIIPrintf(viewer,"%G\n",values[i]);
327: #endif
328:         }
329:       }
330:     }
331:     PetscFree(values);
332:   } else {
333:     PetscViewerGetFormat(viewer,&format);
334:     if (format == PETSC_VIEWER_ASCII_MATLAB) {
335:       /* this may be a collective operation so make sure everyone calls it */
336:       PetscObjectGetName((PetscObject)xin,&name);
337:     }
338:     /* send values */
339:     MPI_Send((void*)xarray,xin->map->n,MPIU_SCALAR,0,tag,((PetscObject)xin)->comm);
340:   }
341:   PetscViewerFlush(viewer);
342:   VecRestoreArrayRead(xin,&xarray);
343:   return(0);
344: }

348: PetscErrorCode VecView_MPI_Binary(Vec xin,PetscViewer viewer)
349: {
350:   PetscErrorCode    ierr;
351:   PetscMPIInt       rank,size,mesgsize,tag = ((PetscObject)viewer)->tag, mesglen;
352:   PetscInt          len,n = xin->map->n,j,tr[2];
353:   int               fdes;
354:   MPI_Status        status;
355:   PetscScalar       *values;
356:   const PetscScalar *xarray;
357:   FILE              *file;
358: #if defined(PETSC_HAVE_MPIIO)
359:   PetscBool         isMPIIO;
360: #endif
361:   PetscBool         skipHeader;
362:   PetscInt          message_count,flowcontrolcount;

365:   VecGetArrayRead(xin,&xarray);
366:   PetscViewerBinaryGetDescriptor(viewer,&fdes);
367:   PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);

369:   /* determine maximum message to arrive */
370:   MPI_Comm_rank(((PetscObject)xin)->comm,&rank);
371:   MPI_Comm_size(((PetscObject)xin)->comm,&size);

373:   if (!skipHeader) {
374:     tr[0] = VEC_FILE_CLASSID;
375:     tr[1] = xin->map->N;
376:     PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT,PETSC_FALSE);
377:   }

379: #if defined(PETSC_HAVE_MPIIO)
380:   PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);
381:   if (!isMPIIO) {
382: #endif
383:     PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
384:     if (!rank) {
385:       PetscBinaryWrite(fdes,(void*)xarray,xin->map->n,PETSC_SCALAR,PETSC_FALSE);

387:       len = 0;
388:       for (j=1; j<size; j++) len = PetscMax(len,xin->map->range[j+1]-xin->map->range[j]);
389:       PetscMalloc(len*sizeof(PetscScalar),&values);
390:       mesgsize = PetscMPIIntCast(len);
391:       /* receive and save messages */
392:       for (j=1; j<size; j++) {
393:         PetscViewerFlowControlStepMaster(viewer,j,message_count,flowcontrolcount);
394:         MPI_Recv(values,mesgsize,MPIU_SCALAR,j,tag,((PetscObject)xin)->comm,&status);
395:         MPI_Get_count(&status,MPIU_SCALAR,&mesglen);
396:         n = (PetscInt)mesglen;
397:         PetscBinaryWrite(fdes,values,n,PETSC_SCALAR,PETSC_TRUE);
398:       }
399:       PetscViewerFlowControlEndMaster(viewer,message_count);
400:       PetscFree(values);
401:     } else {
402:       PetscViewerFlowControlStepWorker(viewer,rank,message_count);
403:       mesgsize = PetscMPIIntCast(xin->map->n);
404:       MPI_Send((void*)xarray,mesgsize,MPIU_SCALAR,0,tag,((PetscObject)xin)->comm);
405:       PetscViewerFlowControlEndWorker(viewer,message_count);
406:     }
407: #if defined(PETSC_HAVE_MPIIO)
408:   } else {
409:     MPI_Offset   off;
410:     MPI_File     mfdes;
411:     PetscMPIInt  gsizes[1],lsizes[1],lstarts[1];
412:     MPI_Datatype view;

414:     gsizes[0]  = PetscMPIIntCast(xin->map->N);
415:     lsizes[0]  = PetscMPIIntCast(n);
416:     lstarts[0] = PetscMPIIntCast(xin->map->rstart);
417:     MPI_Type_create_subarray(1,gsizes,lsizes,lstarts,MPI_ORDER_FORTRAN,MPIU_SCALAR,&view);
418:     MPI_Type_commit(&view);

420:     PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);
421:     PetscViewerBinaryGetMPIIOOffset(viewer,&off);
422:     MPIU_File_write_all(mfdes,(void*)xarray,lsizes[0],MPIU_SCALAR,MPI_STATUS_IGNORE);
423:     PetscViewerBinaryAddMPIIOOffset(viewer,xin->map->N*sizeof(PetscScalar));
424:     MPI_Type_free(&view);
425:   }
426: #endif

428:   VecRestoreArrayRead(xin,&xarray);
429:   if (!rank) {
430:     PetscViewerBinaryGetInfoPointer(viewer,&file);
431:     if (file) {
432:       if (((PetscObject)xin)->prefix) {
433:         PetscFPrintf(PETSC_COMM_SELF,file,"-%svecload_block_size %D\n",((PetscObject)xin)->prefix,xin->map->bs);
434:       } else {
435:         PetscFPrintf(PETSC_COMM_SELF,file,"-vecload_block_size %D\n",xin->map->bs);
436:       }
437:     }
438:   }
439:   return(0);
440: }

444: PetscErrorCode VecView_MPI_Draw_LG(Vec xin,PetscViewer viewer)
445: {
446:   PetscDraw         draw;
447:   PetscBool         isnull;
448:   PetscErrorCode    ierr;

450: #if defined(PETSC_USE_64BIT_INDICES)
452:   PetscViewerDrawGetDraw(viewer,0,&draw);
453:   PetscDrawIsNull(draw,&isnull);
454:   if (isnull) return(0);
455:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported with 64 bit integers");
456: #else
457:   PetscMPIInt       size,rank;
458:   int               i,N = xin->map->N,*lens;
459:   PetscReal         *xx,*yy;
460:   PetscDrawLG       lg;
461:   const PetscScalar *xarray;

464:   PetscViewerDrawGetDraw(viewer,0,&draw);
465:   PetscDrawIsNull(draw,&isnull);
466:   if (isnull) return(0);

468:   VecGetArrayRead(xin,&xarray);
469:   PetscViewerDrawGetDrawLG(viewer,0,&lg);
470:   PetscDrawCheckResizedWindow(draw);
471:   MPI_Comm_rank(((PetscObject)xin)->comm,&rank);
472:   MPI_Comm_size(((PetscObject)xin)->comm,&size);
473:   if (!rank) {
474:     PetscDrawLGReset(lg);
475:     PetscMalloc(2*(N+1)*sizeof(PetscReal),&xx);
476:     for (i=0; i<N; i++) {xx[i] = (PetscReal) i;}
477:     yy   = xx + N;
478:     PetscMalloc(size*sizeof(PetscInt),&lens);
479:     for (i=0; i<size; i++) {
480:       lens[i] = xin->map->range[i+1] - xin->map->range[i];
481:     }
482: #if !defined(PETSC_USE_COMPLEX)
483:     MPI_Gatherv((void*)xarray,xin->map->n,MPIU_REAL,yy,lens,xin->map->range,MPIU_REAL,0,((PetscObject)xin)->comm);
484: #else
485:     {
486:       PetscReal *xr;
487:       PetscMalloc((xin->map->n+1)*sizeof(PetscReal),&xr);
488:       for (i=0; i<xin->map->n; i++) {
489:         xr[i] = PetscRealPart(xarray[i]);
490:       }
491:       MPI_Gatherv(xr,xin->map->n,MPIU_REAL,yy,lens,xin->map->range,MPIU_REAL,0,((PetscObject)xin)->comm);
492:       PetscFree(xr);
493:     }
494: #endif
495:     PetscFree(lens);
496:     PetscDrawLGAddPoints(lg,N,&xx,&yy);
497:     PetscFree(xx);
498:   } else {
499: #if !defined(PETSC_USE_COMPLEX)
500:     MPI_Gatherv((void*)xarray,xin->map->n,MPIU_REAL,0,0,0,MPIU_REAL,0,((PetscObject)xin)->comm);
501: #else
502:     {
503:       PetscReal *xr;
504:       PetscMalloc((xin->map->n+1)*sizeof(PetscReal),&xr);
505:       for (i=0; i<xin->map->n; i++) {
506:         xr[i] = PetscRealPart(xarray[i]);
507:       }
508:       MPI_Gatherv(xr,xin->map->n,MPIU_REAL,0,0,0,MPIU_REAL,0,((PetscObject)xin)->comm);
509:       PetscFree(xr);
510:     }
511: #endif
512:   }
513:   PetscDrawLGDraw(lg);
514:   PetscDrawSynchronizedFlush(draw);
515:   VecRestoreArrayRead(xin,&xarray);
516: #endif
517:   return(0);
518: }

522: PetscErrorCode  VecView_MPI_Draw(Vec xin,PetscViewer viewer)
523: {
524:   PetscErrorCode    ierr;
525:   PetscMPIInt       rank,size,tag = ((PetscObject)viewer)->tag;
526:   PetscInt          i,start,end;
527:   MPI_Status        status;
528:   PetscReal         coors[4],ymin,ymax,xmin,xmax,tmp;
529:   PetscDraw         draw;
530:   PetscBool         isnull;
531:   PetscDrawAxis     axis;
532:   const PetscScalar *xarray;

535:   PetscViewerDrawGetDraw(viewer,0,&draw);
536:   PetscDrawIsNull(draw,&isnull); if (isnull) return(0);

538:   VecGetArrayRead(xin,&xarray);
539:   PetscDrawCheckResizedWindow(draw);
540:   xmin = 1.e20; xmax = -1.e20;
541:   for (i=0; i<xin->map->n; i++) {
542: #if defined(PETSC_USE_COMPLEX)
543:     if (PetscRealPart(xarray[i]) < xmin) xmin = PetscRealPart(xarray[i]);
544:     if (PetscRealPart(xarray[i]) > xmax) xmax = PetscRealPart(xarray[i]);
545: #else
546:     if (xarray[i] < xmin) xmin = xarray[i];
547:     if (xarray[i] > xmax) xmax = xarray[i];
548: #endif
549:   }
550:   if (xmin + 1.e-10 > xmax) {
551:     xmin -= 1.e-5;
552:     xmax += 1.e-5;
553:   }
554:   MPI_Reduce(&xmin,&ymin,1,MPIU_REAL,MPIU_MIN,0,((PetscObject)xin)->comm);
555:   MPI_Reduce(&xmax,&ymax,1,MPIU_REAL,MPIU_MAX,0,((PetscObject)xin)->comm);
556:   MPI_Comm_size(((PetscObject)xin)->comm,&size);
557:   MPI_Comm_rank(((PetscObject)xin)->comm,&rank);
558:   PetscDrawAxisCreate(draw,&axis);
559:   PetscLogObjectParent(draw,axis);
560:   if (!rank) {
561:     PetscDrawClear(draw);
562:     PetscDrawFlush(draw);
563:     PetscDrawAxisSetLimits(axis,0.0,(double)xin->map->N,ymin,ymax);
564:     PetscDrawAxisDraw(axis);
565:     PetscDrawGetCoordinates(draw,coors,coors+1,coors+2,coors+3);
566:   }
567:   PetscDrawAxisDestroy(&axis);
568:   MPI_Bcast(coors,4,MPIU_REAL,0,((PetscObject)xin)->comm);
569:   if (rank) {PetscDrawSetCoordinates(draw,coors[0],coors[1],coors[2],coors[3]);}
570:   /* draw local part of vector */
571:   VecGetOwnershipRange(xin,&start,&end);
572:   if (rank < size-1) { /*send value to right */
573:     MPI_Send((void*)&xarray[xin->map->n-1],1,MPIU_REAL,rank+1,tag,((PetscObject)xin)->comm);
574:   }
575:   for (i=1; i<xin->map->n; i++) {
576: #if !defined(PETSC_USE_COMPLEX)
577:     PetscDrawLine(draw,(PetscReal)(i-1+start),xarray[i-1],(PetscReal)(i+start),
578:                    xarray[i],PETSC_DRAW_RED);
579: #else
580:     PetscDrawLine(draw,(PetscReal)(i-1+start),PetscRealPart(xarray[i-1]),(PetscReal)(i+start),
581:                    PetscRealPart(xarray[i]),PETSC_DRAW_RED);
582: #endif
583:   }
584:   if (rank) { /* receive value from right */
585:     MPI_Recv(&tmp,1,MPIU_REAL,rank-1,tag,((PetscObject)xin)->comm,&status);
586: #if !defined(PETSC_USE_COMPLEX)
587:     PetscDrawLine(draw,(PetscReal)start-1,tmp,(PetscReal)start,xarray[0],PETSC_DRAW_RED);
588: #else
589:     PetscDrawLine(draw,(PetscReal)start-1,tmp,(PetscReal)start,PetscRealPart(xarray[0]),PETSC_DRAW_RED);
590: #endif
591:   }
592:   PetscDrawSynchronizedFlush(draw);
593:   PetscDrawPause(draw);
594:   VecRestoreArrayRead(xin,&xarray);
595:   return(0);
596: }

598: #if defined(PETSC_HAVE_MATLAB_ENGINE)
601: PetscErrorCode VecView_MPI_Matlab(Vec xin,PetscViewer viewer)
602: {
603:   PetscErrorCode    ierr;
604:   PetscMPIInt       rank,size,*lens;
605:   PetscInt          i,N = xin->map->N;
606:   const PetscScalar *xarray;
607:   PetscScalar       *xx;

610:   VecGetArrayRead(xin,&xarray);
611:   MPI_Comm_rank(((PetscObject)xin)->comm,&rank);
612:   MPI_Comm_size(((PetscObject)xin)->comm,&size);
613:   if (!rank) {
614:     PetscMalloc(N*sizeof(PetscScalar),&xx);
615:     PetscMalloc(size*sizeof(PetscMPIInt),&lens);
616:     for (i=0; i<size; i++) {
617:       lens[i] = xin->map->range[i+1] - xin->map->range[i];
618:     }
619:     MPI_Gatherv((void*)xarray,xin->map->n,MPIU_SCALAR,xx,lens,xin->map->range,MPIU_SCALAR,0,((PetscObject)xin)->comm);
620:     PetscFree(lens);

622:     PetscObjectName((PetscObject)xin);
623:     PetscViewerMatlabPutArray(viewer,N,1,xx,((PetscObject)xin)->name);

625:     PetscFree(xx);
626:   } else {
627:     MPI_Gatherv((void*)xarray,xin->map->n,MPIU_SCALAR,0,0,0,MPIU_SCALAR,0,((PetscObject)xin)->comm);
628:   }
629:   VecRestoreArrayRead(xin,&xarray);
630:   return(0);
631: }
632: #endif

634: #if defined(PETSC_HAVE_HDF5)
637: PetscErrorCode VecView_MPI_HDF5(Vec xin, PetscViewer viewer)
638: {
639:   /* TODO: It looks like we can remove the H5Sclose(filespace) and H5Dget_space(dset_id). Why do we do this? */
640:   hid_t             filespace; /* file dataspace identifier */
641:   hid_t             chunkspace; /* chunk dataset property identifier */
642:   hid_t                    plist_id;  /* property list identifier */
643:   hid_t             dset_id;   /* dataset identifier */
644:   hid_t             memspace;  /* memory dataspace identifier */
645:   hid_t             file_id;
646:   hid_t             group;
647:   hid_t             scalartype; /* scalar type (H5T_NATIVE_FLOAT or H5T_NATIVE_DOUBLE) */
648:   herr_t            status;
649:   PetscInt          bs = xin->map->bs > 0 ? xin->map->bs : 1;
650:   hsize_t           dim;
651:   hsize_t           maxDims[4], dims[4], chunkDims[4], count[4],offset[4];
652:   PetscInt          timestep;
653:   PetscInt          low;
654:   const PetscScalar *x;
655:   const char        *vecname;
656:   PetscErrorCode    ierr;

659:   PetscViewerHDF5OpenGroup(viewer, &file_id, &group);
660:   PetscViewerHDF5GetTimestep(viewer, &timestep);

662:   /* Create the dataspace for the dataset.
663:    *
664:    * dims - holds the current dimensions of the dataset
665:    *
666:    * maxDims - holds the maximum dimensions of the dataset (unlimited
667:    * for the number of time steps with the current dimensions for the
668:    * other dimensions; so only additional time steps can be added).
669:    *
670:    * chunkDims - holds the size of a single time step (required to
671:    * permit extending dataset).
672:    */
673:   dim  = 0;
674:   if (timestep >= 0) {
675:     dims[dim]    = timestep+1;
676:     maxDims[dim] = H5S_UNLIMITED;
677:     chunkDims[dim] = 1;
678:     ++dim;
679:   }
680:   dims[dim]    = PetscHDF5IntCast(xin->map->N)/bs;
681:   maxDims[dim] = dims[dim];
682:   chunkDims[dim] = dims[dim];
683:   ++dim;
684:   if (bs >= 1) {
685:     dims[dim]    = bs;
686:     maxDims[dim] = dims[dim];
687:     chunkDims[dim] = dims[dim];
688:     ++dim;
689:   }
690: #if defined(PETSC_USE_COMPLEX)
691:   dims[dim]    = 2;
692:   maxDims[dim] = dims[dim];
693:   chunkDims[dim] = dims[dim];
694:   ++dim;
695: #endif
696:   filespace = H5Screate_simple(dim, dims, maxDims);
697:   if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");

699: #if defined(PETSC_USE_REAL_SINGLE)
700:   scalartype = H5T_NATIVE_FLOAT;
701: #elif defined(PETSC_USE_REAL___FLOAT128)
702: #error "HDF5 output with 128 bit floats not supported."
703: #else
704:   scalartype = H5T_NATIVE_DOUBLE;
705: #endif

707:   /* Create the dataset with default properties and close filespace */
708:   PetscObjectGetName((PetscObject) xin, &vecname);
709:   if (!H5Lexists(group, vecname, H5P_DEFAULT)) {
710:     /* Create chunk */
711:     chunkspace = H5Pcreate(H5P_DATASET_CREATE);
712:     if (chunkspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
713:     status = H5Pset_chunk(chunkspace, dim, chunkDims); CHKERRQ(status);

715: #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
716:     dset_id = H5Dcreate2(group, vecname, scalartype, filespace, H5P_DEFAULT, chunkspace, H5P_DEFAULT);
717: #else
718:     dset_id = H5Dcreate(group, vecname, scalartype, filespace, H5P_DEFAULT);
719: #endif
720:     if (dset_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dcreate2()");
721:     status = H5Pclose(chunkspace);CHKERRQ(status);
722:   } else {
723:     dset_id = H5Dopen2(group, vecname, H5P_DEFAULT);
724:     status = H5Dset_extent(dset_id, dims);CHKERRQ(status);
725:   }
726:   status = H5Sclose(filespace);CHKERRQ(status);

728:   /* Each process defines a dataset and writes it to the hyperslab in the file */
729:   dim = 0;
730:   if (timestep >= 0) {
731:     count[dim] = 1;
732:     ++dim;
733:   }
734:   count[dim] = PetscHDF5IntCast(xin->map->n)/bs;
735:   ++dim;
736:   if (bs >= 1) {
737:     count[dim] = bs;
738:     ++dim;
739:   }
740: #if defined(PETSC_USE_COMPLEX)
741:   count[dim] = 2;
742:   ++dim;
743: #endif
744:   if (xin->map->n > 0) {
745:     memspace = H5Screate_simple(dim, count, NULL);
746:     if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
747:   } else {
748:     /* Can't create dataspace with zero for any dimension, so create null dataspace. */
749:     memspace = H5Screate(H5S_NULL);
750:     if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate()");
751:   }

753:   /* Select hyperslab in the file */
754:   VecGetOwnershipRange(xin, &low, PETSC_NULL);
755:   dim = 0;
756:   if (timestep >= 0) {
757:     offset[dim] = timestep;
758:     ++dim;
759:   }
760:   offset[dim] = PetscHDF5IntCast(low/bs);
761:   ++dim;
762:   if (bs >= 1) {
763:     offset[dim] = 0;
764:     ++dim;
765:   }
766: #if defined(PETSC_USE_COMPLEX)
767:   offset[dim] = 0;
768:   ++dim;
769: #endif
770:   if (xin->map->n > 0) {
771:     filespace = H5Dget_space(dset_id);
772:     if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dget_space()");
773:     status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);
774:   } else {
775:     /* Create null filespace to match null memspace. */
776:     filespace = H5Screate(H5S_NULL);
777:     if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate(H5S_NULL)");
778:   }

780:   /* Create property list for collective dataset write */
781:   plist_id = H5Pcreate(H5P_DATASET_XFER);
782:   if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
783: #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
784:   status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
785: #endif
786:   /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */

788:   VecGetArrayRead(xin, &x);
789:   status = H5Dwrite(dset_id, scalartype, memspace, filespace, plist_id, x);CHKERRQ(status);
790:   status = H5Fflush(file_id, H5F_SCOPE_GLOBAL);CHKERRQ(status);
791:   VecRestoreArrayRead(xin, &x);

793:   /* Close/release resources */
794:   if (group != file_id) {
795:     status = H5Gclose(group);CHKERRQ(status);
796:   }
797:   status = H5Pclose(plist_id);CHKERRQ(status);
798:   status = H5Sclose(filespace);CHKERRQ(status);
799:   status = H5Sclose(memspace);CHKERRQ(status);
800:   status = H5Dclose(dset_id);CHKERRQ(status);
801:   PetscInfo1(xin,"Wrote Vec object with name %s\n",vecname);
802:   return(0);
803: }
804: #endif

808: PetscErrorCode VecView_MPI(Vec xin,PetscViewer viewer)
809: {
811:   PetscBool      iascii,isbinary,isdraw;
812: #if defined(PETSC_HAVE_MATHEMATICA)
813:   PetscBool      ismathematica;
814: #endif
815: #if defined(PETSC_HAVE_HDF5)
816:   PetscBool      ishdf5;
817: #endif
818: #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
819:   PetscBool      ismatlab;
820: #endif

823:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
824:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
825:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
826: #if defined(PETSC_HAVE_MATHEMATICA)
827:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERMATHEMATICA,&ismathematica);
828: #endif
829: #if defined(PETSC_HAVE_HDF5)
830:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
831: #endif
832: #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
833:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERMATLAB,&ismatlab);
834: #endif
835:   if (iascii){
836:     VecView_MPI_ASCII(xin,viewer);
837:   } else if (isbinary) {
838:     VecView_MPI_Binary(xin,viewer);
839:   } else if (isdraw) {
840:     PetscViewerFormat format;

842:     PetscViewerGetFormat(viewer,&format);
843:     if (format == PETSC_VIEWER_DRAW_LG) {
844:       VecView_MPI_Draw_LG(xin,viewer);
845:     } else {
846:       VecView_MPI_Draw(xin,viewer);
847:     }
848: #if defined(PETSC_HAVE_MATHEMATICA)
849:   } else if (ismathematica) {
850:     PetscViewerMathematicaPutVector(viewer,xin);
851: #endif
852: #if defined(PETSC_HAVE_HDF5)
853:   } else if (ishdf5) {
854:     VecView_MPI_HDF5(xin,viewer);
855: #endif
856: #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
857:   } else if (ismatlab) {
858:     VecView_MPI_Matlab(xin,viewer);
859: #endif
860:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for this object",((PetscObject)viewer)->type_name);
861:   return(0);
862: }

866: PetscErrorCode VecGetSize_MPI(Vec xin,PetscInt *N)
867: {
869:   *N = xin->map->N;
870:   return(0);
871: }

875: PetscErrorCode VecGetValues_MPI(Vec xin,PetscInt ni,const PetscInt ix[],PetscScalar y[])
876: {
877:   const PetscScalar *xx;
878:   PetscInt          i,tmp,start = xin->map->range[xin->stash.rank];
879:   PetscErrorCode    ierr;

882:   VecGetArrayRead(xin,&xx);
883:   for (i=0; i<ni; i++) {
884:     if (xin->stash.ignorenegidx && ix[i] < 0) continue;
885:     tmp = ix[i] - start;
886: #if defined(PETSC_USE_DEBUG)
887:     if (tmp < 0 || tmp >= xin->map->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Can only get local values, trying %D",ix[i]);
888: #endif
889:     y[i] = xx[tmp];
890:   }
891:   VecRestoreArrayRead(xin,&xx);
892:   return(0);
893: }

897: PetscErrorCode VecSetValues_MPI(Vec xin,PetscInt ni,const PetscInt ix[],const PetscScalar y[],InsertMode addv)
898: {
900:   PetscMPIInt    rank = xin->stash.rank;
901:   PetscInt       *owners = xin->map->range,start = owners[rank];
902:   PetscInt       end = owners[rank+1],i,row;
903:   PetscScalar    *xx;

906: #if defined(PETSC_USE_DEBUG)
907:   if (xin->stash.insertmode == INSERT_VALUES && addv == ADD_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"You have already inserted values; you cannot now add");
908:   else if (xin->stash.insertmode == ADD_VALUES && addv == INSERT_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"You have already added values; you cannot now insert");
909: #endif
910:   VecGetArray(xin,&xx);
911:   xin->stash.insertmode = addv;

913:   if (addv == INSERT_VALUES) {
914:     for (i=0; i<ni; i++) {
915:       if (xin->stash.ignorenegidx && ix[i] < 0) continue;
916: #if defined(PETSC_USE_DEBUG)
917:       if (ix[i] < 0) {
918:         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D cannot be negative",ix[i]);
919:       }
920: #endif
921:       if ((row = ix[i]) >= start && row < end) {
922:         xx[row-start] = y[i];
923:       } else if (!xin->stash.donotstash) {
924: #if defined(PETSC_USE_DEBUG)
925:         if (ix[i] >= xin->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D maximum %D",ix[i],xin->map->N);
926: #endif
927:         VecStashValue_Private(&xin->stash,row,y[i]);
928:       }
929:     }
930:   } else {
931:     for (i=0; i<ni; i++) {
932:       if (xin->stash.ignorenegidx && ix[i] < 0) continue;
933: #if defined(PETSC_USE_DEBUG)
934:       if (ix[i] < 0) {
935:         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D cannot be negative",ix[i]);
936:       }
937: #endif
938:       if ((row = ix[i]) >= start && row < end) {
939:         xx[row-start] += y[i];
940:       } else if (!xin->stash.donotstash) {
941: #if defined(PETSC_USE_DEBUG)
942:         if (ix[i] > xin->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D maximum %D",ix[i],xin->map->N);
943: #endif
944:         VecStashValue_Private(&xin->stash,row,y[i]);
945:       }
946:     }
947:   }
948:   VecRestoreArray(xin,&xx);
949:   return(0);
950: }

954: PetscErrorCode VecSetValuesBlocked_MPI(Vec xin,PetscInt ni,const PetscInt ix[],const PetscScalar yin[],InsertMode addv)
955: {
956:   PetscMPIInt    rank = xin->stash.rank;
957:   PetscInt       *owners = xin->map->range,start = owners[rank];
959:   PetscInt       end = owners[rank+1],i,row,bs = xin->map->bs,j;
960:   PetscScalar    *xx,*y = (PetscScalar*)yin;

963:   VecGetArray(xin,&xx);
964: #if defined(PETSC_USE_DEBUG)
965:   if (xin->stash.insertmode == INSERT_VALUES && addv == ADD_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"You have already inserted values; you cannot now add");
966:   else if (xin->stash.insertmode == ADD_VALUES && addv == INSERT_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"You have already added values; you cannot now insert");
967: #endif
968:   xin->stash.insertmode = addv;

970:   if (addv == INSERT_VALUES) {
971:     for (i=0; i<ni; i++) {
972:       if ((row = bs*ix[i]) >= start && row < end) {
973:         for (j=0; j<bs; j++) {
974:           xx[row-start+j] = y[j];
975:         }
976:       } else if (!xin->stash.donotstash) {
977:         if (ix[i] < 0) continue;
978: #if defined(PETSC_USE_DEBUG)
979:         if (ix[i] >= xin->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D max %D",ix[i],xin->map->N);
980: #endif
981:         VecStashValuesBlocked_Private(&xin->bstash,ix[i],y);
982:       }
983:       y += bs;
984:     }
985:   } else {
986:     for (i=0; i<ni; i++) {
987:       if ((row = bs*ix[i]) >= start && row < end) {
988:         for (j=0; j<bs; j++) {
989:           xx[row-start+j] += y[j];
990:         }
991:       } else if (!xin->stash.donotstash) {
992:         if (ix[i] < 0) continue;
993: #if defined(PETSC_USE_DEBUG)
994:         if (ix[i] > xin->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D max %D",ix[i],xin->map->N);
995: #endif
996:         VecStashValuesBlocked_Private(&xin->bstash,ix[i],y);
997:       }
998:       y += bs;
999:     }
1000:   }
1001:   VecRestoreArray(xin,&xx);
1002:   return(0);
1003: }

1005: /*
1006:    Since nsends or nreceives may be zero we add 1 in certain mallocs
1007: to make sure we never malloc an empty one.
1008: */
1011: PetscErrorCode VecAssemblyBegin_MPI(Vec xin)
1012: {
1014:   PetscInt       *owners = xin->map->range,*bowners,i,bs,nstash,reallocs;
1015:   PetscMPIInt    size;
1016:   InsertMode     addv;
1017:   MPI_Comm       comm = ((PetscObject)xin)->comm;

1020:   if (xin->stash.donotstash) {
1021:     return(0);
1022:   }

1024:   MPI_Allreduce(&xin->stash.insertmode,&addv,1,MPI_INT,MPI_BOR,comm);
1025:   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(comm,PETSC_ERR_ARG_NOTSAMETYPE,"Some processors inserted values while others added");
1026:   xin->stash.insertmode = addv; /* in case this processor had no cache */

1028:   bs = xin->map->bs;
1029:   MPI_Comm_size(((PetscObject)xin)->comm,&size);
1030:   if (!xin->bstash.bowners && xin->map->bs != -1) {
1031:     PetscMalloc((size+1)*sizeof(PetscInt),&bowners);
1032:     for (i=0; i<size+1; i++){ bowners[i] = owners[i]/bs;}
1033:     xin->bstash.bowners = bowners;
1034:   } else {
1035:     bowners = xin->bstash.bowners;
1036:   }
1037:   VecStashScatterBegin_Private(&xin->stash,owners);
1038:   VecStashScatterBegin_Private(&xin->bstash,bowners);
1039:   VecStashGetInfo_Private(&xin->stash,&nstash,&reallocs);
1040:   PetscInfo2(xin,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
1041:   VecStashGetInfo_Private(&xin->bstash,&nstash,&reallocs);
1042:   PetscInfo2(xin,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);

1044:   return(0);
1045: }

1049: PetscErrorCode VecAssemblyEnd_MPI(Vec vec)
1050: {
1052:   PetscInt       base,i,j,*row,flg,bs;
1053:   PetscMPIInt    n;
1054:   PetscScalar    *val,*vv,*array,*xarray;

1057:   if (!vec->stash.donotstash) {
1058:     VecGetArray(vec,&xarray);
1059:     base = vec->map->range[vec->stash.rank];
1060:     bs   = vec->map->bs;

1062:     /* Process the stash */
1063:     while (1) {
1064:       VecStashScatterGetMesg_Private(&vec->stash,&n,&row,&val,&flg);
1065:       if (!flg) break;
1066:       if (vec->stash.insertmode == ADD_VALUES) {
1067:         for (i=0; i<n; i++) { xarray[row[i] - base] += val[i]; }
1068:       } else if (vec->stash.insertmode == INSERT_VALUES) {
1069:         for (i=0; i<n; i++) { xarray[row[i] - base] = val[i]; }
1070:       } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Insert mode is not set correctly; corrupted vector");
1071:     }
1072:     VecStashScatterEnd_Private(&vec->stash);

1074:     /* now process the block-stash */
1075:     while (1) {
1076:       VecStashScatterGetMesg_Private(&vec->bstash,&n,&row,&val,&flg);
1077:       if (!flg) break;
1078:       for (i=0; i<n; i++) {
1079:         array = xarray+row[i]*bs-base;
1080:         vv    = val+i*bs;
1081:         if (vec->stash.insertmode == ADD_VALUES) {
1082:           for (j=0; j<bs; j++) { array[j] += vv[j];}
1083:         } else if (vec->stash.insertmode == INSERT_VALUES) {
1084:           for (j=0; j<bs; j++) { array[j] = vv[j]; }
1085:         } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Insert mode is not set correctly; corrupted vector");
1086:       }
1087:     }
1088:     VecStashScatterEnd_Private(&vec->bstash);
1089:     VecRestoreArray(vec,&xarray);
1090:   }
1091:   vec->stash.insertmode = NOT_SET_VALUES;
1092:   return(0);
1093: }