Actual source code: pdvec.c

  1: #define PETSCVEC_DLL
  2: /*
  3:      Code for some of the parallel vector primatives.
  4: */
 5:  #include ../src/vec/vec/impls/mpi/pvecimpl.h
  6: #if defined(PETSC_HAVE_PNETCDF)
  8: #include "pnetcdf.h"
 10: #endif

 14: PetscErrorCode VecDestroy_MPI(Vec v)
 15: {
 16:   Vec_MPI        *x = (Vec_MPI*)v->data;

 20: #if defined(PETSC_USE_LOG)
 21:   PetscLogObjectState((PetscObject)v,"Length=%D",v->map->N);
 22: #endif  
 23:   PetscFree(x->array_allocated);

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

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

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

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

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

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

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

248:       VecGetLocalSize(xin, &nLen);
249:       n    = PetscMPIIntCast(nLen);
250:       VecGetBlockSize(xin, &bs);
251:       if ((bs < 1) || (bs > 3)) {
252:         SETERRQ1(PETSC_ERR_ARG_WRONGSTATE, "PCICE can only handle up to 3D objects, but vector dimension is %d", bs);
253:       }
254:       PetscViewerASCIIPrintf(viewer,"%D\n", xin->map->N/bs);
255:       for (i=0; i<n/bs; i++) {
256:         PetscViewerASCIIPrintf(viewer,"%7D   ", vertexCount++);
257:         for (b=0; b<bs; b++) {
258:           if (b > 0) {
259:             PetscViewerASCIIPrintf(viewer," ");
260:           }
261: #if !defined(PETSC_USE_COMPLEX)
262:           PetscViewerASCIIPrintf(viewer,"% 12.5E",xarray[i*bs+b]);
263: #endif
264:         }
265:         PetscViewerASCIIPrintf(viewer,"\n");
266:       }
267:       for (j=1; j<size; j++) {
268:         MPI_Recv(values,(PetscMPIInt)len,MPIU_SCALAR,j,tag,((PetscObject)xin)->comm,&status);
269:         MPI_Get_count(&status,MPIU_SCALAR,&n);
270:         for (i=0; i<n/bs; i++) {
271:           PetscViewerASCIIPrintf(viewer,"%7D   ", vertexCount++);
272:           for (b=0; b<bs; b++) {
273:             if (b > 0) {
274:               PetscViewerASCIIPrintf(viewer," ");
275:             }
276: #if !defined(PETSC_USE_COMPLEX)
277:             PetscViewerASCIIPrintf(viewer,"% 12.5E",values[i*bs+b]);
278: #endif
279:           }
280:           PetscViewerASCIIPrintf(viewer,"\n");
281:         }
282:       }
283:     } else if (format == PETSC_VIEWER_ASCII_PYLITH) {
284:       PetscInt bs, b, vertexCount = 1;

286:       VecGetLocalSize(xin, &nLen);
287:       n    = PetscMPIIntCast(nLen);
288:       VecGetBlockSize(xin, &bs);
289:       if (bs != 3) {
290:         SETERRQ1(PETSC_ERR_ARG_WRONGSTATE, "PyLith can only handle 3D objects, but vector dimension is %d", bs);
291:       }
292:       PetscViewerASCIIPrintf(viewer,"#\n");
293:       PetscViewerASCIIPrintf(viewer,"#  Node      X-coord           Y-coord           Z-coord\n");
294:       PetscViewerASCIIPrintf(viewer,"#\n");
295:       for (i=0; i<n/bs; i++) {
296:         PetscViewerASCIIPrintf(viewer,"%7D ", vertexCount++);
297:         for (b=0; b<bs; b++) {
298:           if (b > 0) {
299:             PetscViewerASCIIPrintf(viewer," ");
300:           }
301: #if !defined(PETSC_USE_COMPLEX)
302:           PetscViewerASCIIPrintf(viewer,"% 16.8E",xarray[i*bs+b]);
303: #endif
304:         }
305:         PetscViewerASCIIPrintf(viewer,"\n");
306:       }
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:         for (i=0; i<n/bs; i++) {
311:           PetscViewerASCIIPrintf(viewer,"%7D ", vertexCount++);
312:           for (b=0; b<bs; b++) {
313:             if (b > 0) {
314:               PetscViewerASCIIPrintf(viewer," ");
315:             }
316: #if !defined(PETSC_USE_COMPLEX)
317:             PetscViewerASCIIPrintf(viewer,"% 16.8E",values[i*bs+b]);
318: #endif
319:           }
320:           PetscViewerASCIIPrintf(viewer,"\n");
321:         }
322:       }
323:     } else {
324:       if (format != PETSC_VIEWER_ASCII_COMMON) {PetscViewerASCIIPrintf(viewer,"Process [%d]\n",rank);}
325:       cnt = 0;
326:       for (i=0; i<xin->map->n; i++) {
327:         if (format == PETSC_VIEWER_ASCII_INDEX) {
328:           PetscViewerASCIIPrintf(viewer,"%D: ",cnt++);
329:         }
330: #if defined(PETSC_USE_COMPLEX)
331:         if (PetscImaginaryPart(xarray[i]) > 0.0) {
332:           PetscViewerASCIIPrintf(viewer,"%G + %G i\n",PetscRealPart(xarray[i]),PetscImaginaryPart(xarray[i]));
333:         } else if (PetscImaginaryPart(xarray[i]) < 0.0) {
334:           PetscViewerASCIIPrintf(viewer,"%G - %G i\n",PetscRealPart(xarray[i]),-PetscImaginaryPart(xarray[i]));
335:         } else {
336:           PetscViewerASCIIPrintf(viewer,"%G\n",PetscRealPart(xarray[i]));
337:         }
338: #else
339:         PetscViewerASCIIPrintf(viewer,"%G\n",xarray[i]);
340: #endif
341:       }
342:       /* receive and print messages */
343:       for (j=1; j<size; j++) {
344:         MPI_Recv(values,(PetscMPIInt)len,MPIU_SCALAR,j,tag,((PetscObject)xin)->comm,&status);
345:         MPI_Get_count(&status,MPIU_SCALAR,&n);
346:         if (format != PETSC_VIEWER_ASCII_COMMON) {
347:           PetscViewerASCIIPrintf(viewer,"Process [%d]\n",j);
348:         }
349:         for (i=0; i<n; i++) {
350:           if (format == PETSC_VIEWER_ASCII_INDEX) {
351:             PetscViewerASCIIPrintf(viewer,"%D: ",cnt++);
352:           }
353: #if defined(PETSC_USE_COMPLEX)
354:           if (PetscImaginaryPart(values[i]) > 0.0) {
355:             PetscViewerASCIIPrintf(viewer,"%G + %G i\n",PetscRealPart(values[i]),PetscImaginaryPart(values[i]));
356:           } else if (PetscImaginaryPart(values[i]) < 0.0) {
357:             PetscViewerASCIIPrintf(viewer,"%G - %G i\n",PetscRealPart(values[i]),-PetscImaginaryPart(values[i]));
358:           } else {
359:             PetscViewerASCIIPrintf(viewer,"%G\n",PetscRealPart(values[i]));
360:           }
361: #else
362:           PetscViewerASCIIPrintf(viewer,"%G\n",values[i]);
363: #endif
364:         }
365:       }
366:     }
367:     PetscFree(values);
368:   } else {
369:     /* send values */
370:     MPI_Send(xarray,xin->map->n,MPIU_SCALAR,0,tag,((PetscObject)xin)->comm);
371:   }
372:   PetscViewerFlush(viewer);
373:   VecRestoreArray(xin,&xarray);
374:   return(0);
375: }

379: PetscErrorCode VecView_MPI_Binary(Vec xin,PetscViewer viewer)
380: {
382:   PetscMPIInt    rank,size,mesgsize,tag = ((PetscObject)viewer)->tag, mesglen;
383:   PetscInt       len,n = xin->map->n,j,tr[2];
384:   int            fdes;
385:   MPI_Status     status;
386:   PetscScalar    *values,*xarray;
387:   FILE           *file;
388: #if defined(PETSC_HAVE_MPIIO)
389:   PetscTruth     isMPIIO;
390: #endif

393:   VecGetArray(xin,&xarray);
394:   PetscViewerBinaryGetDescriptor(viewer,&fdes);

396:   /* determine maximum message to arrive */
397:   MPI_Comm_rank(((PetscObject)xin)->comm,&rank);
398:   MPI_Comm_size(((PetscObject)xin)->comm,&size);

400:   tr[0] = VEC_FILE_COOKIE;
401:   tr[1] = xin->map->N;
402:   PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT,PETSC_FALSE);

404: #if defined(PETSC_HAVE_MPIIO)
405:   PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);
406:   if (!isMPIIO) {
407: #endif
408: #if defined(PETSC_HANDSHAKE_IO)
409:     PetscInt message_count = 256;
410: #endif
411:     if (!rank) {
412:       PetscBinaryWrite(fdes,xarray,xin->map->n,PETSC_SCALAR,PETSC_FALSE);
413: 
414:       len = 0;
415:       for (j=1; j<size; j++) len = PetscMax(len,xin->map->range[j+1]-xin->map->range[j]);
416:       PetscMalloc((len+1)*sizeof(PetscScalar),&values);
417:       mesgsize = PetscMPIIntCast(len);
418:       /* receive and save messages */
419:       for (j=1; j<size; j++) {
420: #if defined(PETSC_HANDSHAKE_IO)
421:         if (j >= message_count) {
422:           message_count += 256;
423:           MPI_Bcast(&message_count,1,MPIU_INT,0,((PetscObject)xin)->comm);
424:         }
425: #endif
426:         MPI_Recv(values,mesgsize,MPIU_SCALAR,j,tag,((PetscObject)xin)->comm,&status);
427:         MPI_Get_count(&status,MPIU_SCALAR,&mesglen);
428:         n = (PetscInt)mesglen;
429:         PetscBinaryWrite(fdes,values,n,PETSC_SCALAR,PETSC_FALSE);
430:       }
431: #if defined(PETSC_HANDSHAKE_IO)
432:       message_count = 0;
433:       MPI_Bcast(&message_count,1,MPIU_INT,0,((PetscObject)xin)->comm);
434: #endif

436:       PetscFree(values);
437:     } else {
438: #if defined(PETSC_HANDSHAKE_IO)
439:       while (1) {
440:         if (rank < message_count) break;
441:         MPI_Bcast(&message_count,1,MPIU_INT,0,((PetscObject)xin)->comm);
442:       }
443: #endif
444:       /* send values */
445:       mesgsize = PetscMPIIntCast(xin->map->n);
446:       MPI_Send(xarray,mesgsize,MPIU_SCALAR,0,tag,((PetscObject)xin)->comm);
447: #if defined(PETSC_HANDSHAKE_IO)
448:       while (1) {
449:         if (message_count == 0) break;
450:         MPI_Bcast(&message_count,1,MPIU_INT,0,((PetscObject)xin)->comm);
451:       }
452: #endif
453:     }
454: #if defined(PETSC_HAVE_MPIIO)
455:   } else {
456:     MPI_Offset   off;
457:     MPI_File     mfdes;
458:     PetscMPIInt  gsizes[1],lsizes[1],lstarts[1];
459:     MPI_Datatype view;

461:     gsizes[0]  = PetscMPIIntCast(xin->map->N);
462:     lsizes[0]  = PetscMPIIntCast(n);
463:     lstarts[0] = PetscMPIIntCast(xin->map->rstart);
464:     MPI_Type_create_subarray(1,gsizes,lsizes,lstarts,MPI_ORDER_FORTRAN,MPIU_SCALAR,&view);
465:     MPI_Type_commit(&view);

467:     PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);
468:     PetscViewerBinaryGetMPIIOOffset(viewer,&off);
469:     MPIU_File_write_all(mfdes,xarray,lsizes[0],MPIU_SCALAR,MPI_STATUS_IGNORE);
470:     PetscViewerBinaryAddMPIIOOffset(viewer,xin->map->N*sizeof(PetscScalar));
471:     MPI_Type_free(&view);
472:   }
473: #endif

475:   VecRestoreArray(xin,&xarray);
476:   if (!rank) {
477:     PetscViewerBinaryGetInfoPointer(viewer,&file);
478:     if (file && xin->map->bs > 1) {
479:       if (((PetscObject)xin)->prefix) {
480:         PetscFPrintf(PETSC_COMM_SELF,file,"-%svecload_block_size %D\n",((PetscObject)xin)->prefix,xin->map->bs);
481:       } else {
482:         PetscFPrintf(PETSC_COMM_SELF,file,"-vecload_block_size %D\n",xin->map->bs);
483:       }
484:     }
485:   }
486:   return(0);
487: }

491: PetscErrorCode VecView_MPI_Draw_LG(Vec xin,PetscViewer viewer)
492: {
493:   PetscDraw      draw;
494:   PetscTruth     isnull;

497: #if defined(PETSC_USE_64BIT_INDICES)
499:   PetscViewerDrawGetDraw(viewer,0,&draw);
500:   PetscDrawIsNull(draw,&isnull);
501:   if (isnull) return(0);
502:   SETERRQ(PETSC_ERR_SUP,"Not supported with 64 bit integers");
503: #else
504:   PetscMPIInt    size,rank;
505:   int            i,N = xin->map->N,*lens;
506:   PetscReal      *xx,*yy;
507:   PetscDrawLG    lg;
508:   PetscScalar    *xarray;

511:   PetscViewerDrawGetDraw(viewer,0,&draw);
512:   PetscDrawIsNull(draw,&isnull);
513:   if (isnull) return(0);

515:   VecGetArray(xin,&xarray);
516:   PetscViewerDrawGetDrawLG(viewer,0,&lg);
517:   PetscDrawCheckResizedWindow(draw);
518:   MPI_Comm_rank(((PetscObject)xin)->comm,&rank);
519:   MPI_Comm_size(((PetscObject)xin)->comm,&size);
520:   if (!rank) {
521:     PetscDrawLGReset(lg);
522:     PetscMalloc(2*(N+1)*sizeof(PetscReal),&xx);
523:     for (i=0; i<N; i++) {xx[i] = (PetscReal) i;}
524:     yy   = xx + N;
525:     PetscMalloc(size*sizeof(PetscInt),&lens);
526:     for (i=0; i<size; i++) {
527:       lens[i] = xin->map->range[i+1] - xin->map->range[i];
528:     }
529: #if !defined(PETSC_USE_COMPLEX)
530:     MPI_Gatherv(xarray,xin->map->n,MPIU_REAL,yy,lens,xin->map->range,MPIU_REAL,0,((PetscObject)xin)->comm);
531: #else
532:     {
533:       PetscReal *xr;
534:       PetscMalloc((xin->map->n+1)*sizeof(PetscReal),&xr);
535:       for (i=0; i<xin->map->n; i++) {
536:         xr[i] = PetscRealPart(xarray[i]);
537:       }
538:       MPI_Gatherv(xr,xin->map->n,MPIU_REAL,yy,lens,xin->map->range,MPIU_REAL,0,((PetscObject)xin)->comm);
539:       PetscFree(xr);
540:     }
541: #endif
542:     PetscFree(lens);
543:     PetscDrawLGAddPoints(lg,N,&xx,&yy);
544:     PetscFree(xx);
545:   } else {
546: #if !defined(PETSC_USE_COMPLEX)
547:     MPI_Gatherv(xarray,xin->map->n,MPIU_REAL,0,0,0,MPIU_REAL,0,((PetscObject)xin)->comm);
548: #else
549:     {
550:       PetscReal *xr;
551:       PetscMalloc((xin->map->n+1)*sizeof(PetscReal),&xr);
552:       for (i=0; i<xin->map->n; i++) {
553:         xr[i] = PetscRealPart(xarray[i]);
554:       }
555:       MPI_Gatherv(xr,xin->map->n,MPIU_REAL,0,0,0,MPIU_REAL,0,((PetscObject)xin)->comm);
556:       PetscFree(xr);
557:     }
558: #endif
559:   }
560:   PetscDrawLGDraw(lg);
561:   PetscDrawSynchronizedFlush(draw);
562:   VecRestoreArray(xin,&xarray);
563: #endif
564:   return(0);
565: }

568: /* I am assuming this is Extern 'C' because it is dynamically loaded.  If not, we can remove the DLLEXPORT tag */
571: PetscErrorCode  VecView_MPI_Draw(Vec xin,PetscViewer viewer)
572: {
574:   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
575:   PetscInt       i,start,end;
576:   MPI_Status     status;
577:   PetscReal      coors[4],ymin,ymax,xmin,xmax,tmp;
578:   PetscDraw      draw;
579:   PetscTruth     isnull;
580:   PetscDrawAxis  axis;
581:   PetscScalar    *xarray;

584:   PetscViewerDrawGetDraw(viewer,0,&draw);
585:   PetscDrawIsNull(draw,&isnull); if (isnull) return(0);

587:   VecGetArray(xin,&xarray);
588:   PetscDrawCheckResizedWindow(draw);
589:   xmin = 1.e20; xmax = -1.e20;
590:   for (i=0; i<xin->map->n; i++) {
591: #if defined(PETSC_USE_COMPLEX)
592:     if (PetscRealPart(xarray[i]) < xmin) xmin = PetscRealPart(xarray[i]);
593:     if (PetscRealPart(xarray[i]) > xmax) xmax = PetscRealPart(xarray[i]);
594: #else
595:     if (xarray[i] < xmin) xmin = xarray[i];
596:     if (xarray[i] > xmax) xmax = xarray[i];
597: #endif
598:   }
599:   if (xmin + 1.e-10 > xmax) {
600:     xmin -= 1.e-5;
601:     xmax += 1.e-5;
602:   }
603:   MPI_Reduce(&xmin,&ymin,1,MPIU_REAL,MPI_MIN,0,((PetscObject)xin)->comm);
604:   MPI_Reduce(&xmax,&ymax,1,MPIU_REAL,MPI_MAX,0,((PetscObject)xin)->comm);
605:   MPI_Comm_size(((PetscObject)xin)->comm,&size);
606:   MPI_Comm_rank(((PetscObject)xin)->comm,&rank);
607:   PetscDrawAxisCreate(draw,&axis);
608:   PetscLogObjectParent(draw,axis);
609:   if (!rank) {
610:     PetscDrawClear(draw);
611:     PetscDrawFlush(draw);
612:     PetscDrawAxisSetLimits(axis,0.0,(double)xin->map->N,ymin,ymax);
613:     PetscDrawAxisDraw(axis);
614:     PetscDrawGetCoordinates(draw,coors,coors+1,coors+2,coors+3);
615:   }
616:   PetscDrawAxisDestroy(axis);
617:   MPI_Bcast(coors,4,MPIU_REAL,0,((PetscObject)xin)->comm);
618:   if (rank) {PetscDrawSetCoordinates(draw,coors[0],coors[1],coors[2],coors[3]);}
619:   /* draw local part of vector */
620:   VecGetOwnershipRange(xin,&start,&end);
621:   if (rank < size-1) { /*send value to right */
622:     MPI_Send(&xarray[xin->map->n-1],1,MPIU_REAL,rank+1,tag,((PetscObject)xin)->comm);
623:   }
624:   for (i=1; i<xin->map->n; i++) {
625: #if !defined(PETSC_USE_COMPLEX)
626:     PetscDrawLine(draw,(PetscReal)(i-1+start),xarray[i-1],(PetscReal)(i+start),
627:                    xarray[i],PETSC_DRAW_RED);
628: #else
629:     PetscDrawLine(draw,(PetscReal)(i-1+start),PetscRealPart(xarray[i-1]),(PetscReal)(i+start),
630:                    PetscRealPart(xarray[i]),PETSC_DRAW_RED);
631: #endif
632:   }
633:   if (rank) { /* receive value from right */
634:     MPI_Recv(&tmp,1,MPIU_REAL,rank-1,tag,((PetscObject)xin)->comm,&status);
635: #if !defined(PETSC_USE_COMPLEX)
636:     PetscDrawLine(draw,(PetscReal)start-1,tmp,(PetscReal)start,xarray[0],PETSC_DRAW_RED);
637: #else
638:     PetscDrawLine(draw,(PetscReal)start-1,tmp,(PetscReal)start,PetscRealPart(xarray[0]),PETSC_DRAW_RED);
639: #endif
640:   }
641:   PetscDrawSynchronizedFlush(draw);
642:   PetscDrawPause(draw);
643:   VecRestoreArray(xin,&xarray);
644:   return(0);
645: }

648: #if defined(PETSC_HAVE_MATLAB_ENGINE)
651: PetscErrorCode VecView_MPI_Matlab(Vec xin,PetscViewer viewer)
652: {
654:   PetscMPIInt    rank,size,*lens;
655:   PetscInt       i,N = xin->map->N;
656:   PetscScalar    *xx,*xarray;

659:   VecGetArray(xin,&xarray);
660:   MPI_Comm_rank(((PetscObject)xin)->comm,&rank);
661:   MPI_Comm_size(((PetscObject)xin)->comm,&size);
662:   if (!rank) {
663:     PetscMalloc((N+1)*sizeof(PetscScalar),&xx);
664:     PetscMalloc(size*sizeof(PetscMPIInt),&lens);
665:     for (i=0; i<size; i++) {
666:       lens[i] = xin->map->range[i+1] - xin->map->range[i];
667:     }
668:     MPI_Gatherv(xarray,xin->map->n,MPIU_SCALAR,xx,lens,xin->map->range,MPIU_SCALAR,0,((PetscObject)xin)->comm);
669:     PetscFree(lens);

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

674:     PetscFree(xx);
675:   } else {
676:     MPI_Gatherv(xarray,xin->map->n,MPIU_SCALAR,0,0,0,MPIU_SCALAR,0,((PetscObject)xin)->comm);
677:   }
678:   VecRestoreArray(xin,&xarray);
679:   return(0);
680: }
681: #endif

683: #if defined(PETSC_HAVE_PNETCDF)
686: PetscErrorCode VecView_MPI_Netcdf(Vec xin,PetscViewer v)
687: {
689:   int         n = xin->map->n,ncid,xdim,xdim_num=1,xin_id,xstart;
690:   PetscScalar *xarray;

693:   VecGetArray(xin,&xarray);
694:   PetscViewerNetcdfGetID(v,&ncid);
695:   if (ncid < 0) SETERRQ(PETSC_ERR_ORDER,"First call PetscViewerNetcdfOpen to create NetCDF dataset");
696:   /* define dimensions */
697:   ncmpi_def_dim(ncid,"PETSc_Vector_Global_Size",xin->map->N,&xdim);
698:   /* define variables */
699:   ncmpi_def_var(ncid,"PETSc_Vector_MPI",NC_DOUBLE,xdim_num,&xdim,&xin_id);
700:   /* leave define mode */
701:   ncmpi_enddef(ncid);
702:   /* store the vector */
703:   VecGetOwnershipRange(xin,&xstart,PETSC_NULL);
704:   ncmpi_put_vara_double_all(ncid,xin_id,(const MPI_Offset*)&xstart,(const MPI_Offset*)&n,xarray);
705:   VecRestoreArray(xin,&xarray);
706:   return(0);
707: }
708: #endif

710: #if defined(PETSC_HAVE_HDF5)

714: PetscErrorCode VecView_MPI_HDF5(Vec xin, PetscViewer viewer)
715: {
716:   hid_t         filespace; /* file dataspace identifier */
717:   hid_t                plist_id;  /* property list identifier */
718:   hid_t         dset_id;   /* dataset identifier */
719:   hid_t         memspace;  /* memory dataspace identifier */
720:   hid_t         file_id;
721:   herr_t        status;
722:   /* PetscInt       bs        = xin->map->bs > 0 ? xin->map->bs : 1; */
723:   int            rank      = 1; /* Could have rank 2 for blocked vectors */
724:   hsize_t        dims[1]   = {xin->map->N};
725:   hsize_t        count[1]  = {xin->map->n};
726:   hsize_t        offset[1];
727:   PetscInt       low;
728:   PetscScalar    *x;
730:   const char     *vecname;

732:   PetscViewerHDF5GetFileId(viewer, &file_id);

734:   /* Create the dataspace for the dataset */
735:   filespace = H5Screate_simple(rank, dims, NULL);
736:   if (filespace == -1) SETERRQ(PETSC_ERR_LIB,"Cannot H5Screate_simple()");

738:   /* Create the dataset with default properties and close filespace */
739:   PetscObjectGetName((PetscObject)xin,&vecname);
740: #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
741:   dset_id = H5Dcreate2(file_id, vecname, H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
742: #else
743:   dset_id = H5Dcreate(file_id, vecname, H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT);
744: #endif
745:   if (dset_id == -1) SETERRQ(PETSC_ERR_LIB,"Cannot H5Dcreate2()");
746:   status = H5Sclose(filespace);CHKERRQ(status);

748:   /* Each process defines a dataset and writes it to the hyperslab in the file */
749:   memspace = H5Screate_simple(rank, count, NULL);
750:   if (memspace == -1) SETERRQ(PETSC_ERR_LIB,"Cannot H5Screate_simple()");

752:   /* Select hyperslab in the file */
753:   VecGetOwnershipRange(xin, &low, PETSC_NULL);
754:   offset[0] = low;
755:   filespace = H5Dget_space(dset_id);
756:   if (filespace == -1) SETERRQ(PETSC_ERR_LIB,"Cannot H5Dget_space()");
757:   status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);

759:   /* Create property list for collective dataset write */
760:   plist_id = H5Pcreate(H5P_DATASET_XFER);
761:   if (plist_id == -1) SETERRQ(PETSC_ERR_LIB,"Cannot H5Pcreate()");
762: #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
763:   status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
764: #endif
765:   /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */

767:   VecGetArray(xin, &x);
768:   status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace, plist_id, x);CHKERRQ(status);
769:   status = H5Fflush(file_id, H5F_SCOPE_GLOBAL);CHKERRQ(status);
770:   VecRestoreArray(xin, &x);

772:   /* Close/release resources */
773:   status = H5Pclose(plist_id);CHKERRQ(status);
774:   status = H5Sclose(filespace);CHKERRQ(status);
775:   status = H5Sclose(memspace);CHKERRQ(status);
776:   status = H5Dclose(dset_id);CHKERRQ(status);
777:   return(0);
778: }
779: #endif

783: PetscErrorCode VecView_MPI(Vec xin,PetscViewer viewer)
784: {
786:   PetscTruth     iascii,isbinary,isdraw;
787: #if defined(PETSC_HAVE_MATHEMATICA)
788:   PetscTruth     ismathematica;
789: #endif
790: #if defined(PETSC_HAVE_NETCDF)
791:   PetscTruth     isnetcdf;
792: #endif
793: #if defined(PETSC_HAVE_HDF5)
794:   PetscTruth     ishdf5;
795: #endif
796: #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE)
797:   PetscTruth     ismatlab;
798: #endif

801:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
802:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
803:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
804: #if defined(PETSC_HAVE_MATHEMATICA)
805:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_MATHEMATICA,&ismathematica);
806: #endif
807: #if defined(PETSC_HAVE_NETCDF)
808:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_NETCDF,&isnetcdf);
809: #endif
810: #if defined(PETSC_HAVE_HDF5)
811:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_HDF5,&ishdf5);
812: #endif
813: #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE)
814:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_MATLAB,&ismatlab);
815: #endif
816:   if (iascii){
817:     VecView_MPI_ASCII(xin,viewer);
818:   } else if (isbinary) {
819:     VecView_MPI_Binary(xin,viewer);
820:   } else if (isdraw) {
821:     PetscViewerFormat format;

823:     PetscViewerGetFormat(viewer,&format);
824:     if (format == PETSC_VIEWER_DRAW_LG) {
825:       VecView_MPI_Draw_LG(xin,viewer);
826:     } else {
827:       VecView_MPI_Draw(xin,viewer);
828:     }
829: #if defined(PETSC_HAVE_MATHEMATICA)
830:   } else if (ismathematica) {
831:     PetscViewerMathematicaPutVector(viewer,xin);
832: #endif
833: #if defined(PETSC_HAVE_NETCDF)
834:   } else if (isnetcdf) {
835:     VecView_MPI_Netcdf(xin,viewer);
836: #endif
837: #if defined(PETSC_HAVE_HDF5)
838:   } else if (ishdf5) {
839:     VecView_MPI_HDF5(xin,viewer);
840: #endif
841: #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE)
842:   } else if (ismatlab) {
843:     VecView_MPI_Matlab(xin,viewer);
844: #endif
845:   } else {
846:     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for this object",((PetscObject)viewer)->type_name);
847:   }
848:   return(0);
849: }

853: PetscErrorCode VecGetSize_MPI(Vec xin,PetscInt *N)
854: {
856:   *N = xin->map->N;
857:   return(0);
858: }

862: PetscErrorCode VecGetValues_MPI(Vec xin,PetscInt ni,const PetscInt ix[],PetscScalar y[])
863: {
864:   Vec_MPI     *x = (Vec_MPI *)xin->data;
865:   PetscScalar *xx = x->array;
866:   PetscInt    i,tmp,start = xin->map->range[xin->stash.rank];

869:   for (i=0; i<ni; i++) {
870:     if (xin->stash.ignorenegidx && ix[i] < 0) continue;
871:     tmp = ix[i] - start;
872: #if defined(PETSC_USE_DEBUG)
873:     if (tmp < 0 || tmp >= xin->map->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Can only get local values, trying %D",ix[i]);
874: #endif
875:     y[i] = xx[tmp];
876:   }
877:   return(0);
878: }

882: PetscErrorCode VecSetValues_MPI(Vec xin,PetscInt ni,const PetscInt ix[],const PetscScalar y[],InsertMode addv)
883: {
885:   PetscMPIInt    rank = xin->stash.rank;
886:   PetscInt       *owners = xin->map->range,start = owners[rank];
887:   PetscInt       end = owners[rank+1],i,row;
888:   PetscScalar    *xx;

891: #if defined(PETSC_USE_DEBUG)
892:   if (xin->stash.insertmode == INSERT_VALUES && addv == ADD_VALUES) {
893:    SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"You have already inserted values; you cannot now add");
894:   } else if (xin->stash.insertmode == ADD_VALUES && addv == INSERT_VALUES) {
895:    SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"You have already added values; you cannot now insert");
896:   }
897: #endif
898:   VecGetArray(xin,&xx);
899:   xin->stash.insertmode = addv;

901:   if (addv == INSERT_VALUES) {
902:     for (i=0; i<ni; i++) {
903:       if (xin->stash.ignorenegidx && ix[i] < 0) continue;
904: #if defined(PETSC_USE_DEBUG)
905:       if (ix[i] < 0) {
906:         SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D cannot be negative",ix[i]);
907:       }
908: #endif 
909:       if ((row = ix[i]) >= start && row < end) {
910:         xx[row-start] = y[i];
911:       } else if (!xin->stash.donotstash) {
912: #if defined(PETSC_USE_DEBUG)
913:         if (ix[i] >= xin->map->N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D maximum %D",ix[i],xin->map->N);
914: #endif
915:         VecStashValue_Private(&xin->stash,row,y[i]);
916:       }
917:     }
918:   } else {
919:     for (i=0; i<ni; i++) {
920:       if (xin->stash.ignorenegidx && ix[i] < 0) continue;
921: #if defined(PETSC_USE_DEBUG)
922:       if (ix[i] < 0) {
923:         SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D cannot be negative",ix[i]);
924:       }
925: #endif 
926:       if ((row = ix[i]) >= start && row < end) {
927:         xx[row-start] += y[i];
928:       } else if (!xin->stash.donotstash) {
929: #if defined(PETSC_USE_DEBUG)
930:         if (ix[i] > xin->map->N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D maximum %D",ix[i],xin->map->N);
931: #endif        
932:         VecStashValue_Private(&xin->stash,row,y[i]);
933:       }
934:     }
935:   }
936:   VecRestoreArray(xin,&xx);
937:   return(0);
938: }

942: PetscErrorCode VecSetValuesBlocked_MPI(Vec xin,PetscInt ni,const PetscInt ix[],const PetscScalar yin[],InsertMode addv)
943: {
944:   PetscMPIInt    rank = xin->stash.rank;
945:   PetscInt       *owners = xin->map->range,start = owners[rank];
947:   PetscInt       end = owners[rank+1],i,row,bs = xin->map->bs,j;
948:   PetscScalar    *xx,*y = (PetscScalar*)yin;

951:   VecGetArray(xin,&xx);
952: #if defined(PETSC_USE_DEBUG)
953:   if (xin->stash.insertmode == INSERT_VALUES && addv == ADD_VALUES) {
954:    SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"You have already inserted values; you cannot now add");
955:   }
956:   else if (xin->stash.insertmode == ADD_VALUES && addv == INSERT_VALUES) {
957:    SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"You have already added values; you cannot now insert");
958:   }
959: #endif
960:   xin->stash.insertmode = addv;

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

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

1012:   if (xin->stash.donotstash) {
1013:     return(0);
1014:   }

1016:   MPI_Allreduce(&xin->stash.insertmode,&addv,1,MPI_INT,MPI_BOR,comm);
1017:   if (addv == (ADD_VALUES|INSERT_VALUES)) {
1018:     SETERRQ(PETSC_ERR_ARG_NOTSAMETYPE,"Some processors inserted values while others added");
1019:   }
1020:   xin->stash.insertmode = addv; /* in case this processor had no cache */
1021: 
1022:   bs = xin->map->bs;
1023:   MPI_Comm_size(((PetscObject)xin)->comm,&size);
1024:   if (!xin->bstash.bowners && xin->map->bs != -1) {
1025:     PetscMalloc((size+1)*sizeof(PetscInt),&bowners);
1026:     for (i=0; i<size+1; i++){ bowners[i] = owners[i]/bs;}
1027:     xin->bstash.bowners = bowners;
1028:   } else {
1029:     bowners = xin->bstash.bowners;
1030:   }
1031:   VecStashScatterBegin_Private(&xin->stash,owners);
1032:   VecStashScatterBegin_Private(&xin->bstash,bowners);
1033:   VecStashGetInfo_Private(&xin->stash,&nstash,&reallocs);
1034:   PetscInfo2(xin,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
1035:   VecStashGetInfo_Private(&xin->bstash,&nstash,&reallocs);
1036:   PetscInfo2(xin,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);

1038:   return(0);
1039: }

1043: PetscErrorCode VecAssemblyEnd_MPI(Vec vec)
1044: {
1046:   PetscInt       base,i,j,*row,flg,bs;
1047:   PetscMPIInt    n;
1048:   PetscScalar    *val,*vv,*array,*xarray;

1051:   if (!vec->stash.donotstash) {
1052:     VecGetArray(vec,&xarray);
1053:     base = vec->map->range[vec->stash.rank];
1054:     bs   = vec->map->bs;

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

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