Actual source code: bvec2.c
petsc-3.8.0 2017-09-26
2: /*
3: Implements the sequential vectors.
4: */
6: #include <../src/vec/vec/impls/dvecimpl.h>
7: #include <../src/vec/vec/impls/mpi/pvecimpl.h> /* For VecView_MPI_HDF5 */
8: #include <petsc/private/glvisviewerimpl.h>
9: #include <petsc/private/glvisvecimpl.h>
10: #include <petscblaslapack.h>
12: #if defined(PETSC_HAVE_HDF5)
13: extern PetscErrorCode VecView_MPI_HDF5(Vec,PetscViewer);
14: #endif
16: PetscErrorCode VecPointwiseMax_Seq(Vec win,Vec xin,Vec yin)
17: {
19: PetscInt n = win->map->n,i;
20: PetscScalar *ww,*xx,*yy; /* cannot make xx or yy const since might be ww */
23: VecGetArrayRead(xin,(const PetscScalar**)&xx);
24: VecGetArrayRead(yin,(const PetscScalar**)&yy);
25: VecGetArray(win,&ww);
27: for (i=0; i<n; i++) ww[i] = PetscMax(PetscRealPart(xx[i]),PetscRealPart(yy[i]));
29: VecRestoreArrayRead(xin,(const PetscScalar**)&xx);
30: VecRestoreArrayRead(yin,(const PetscScalar**)&yy);
31: VecRestoreArray(win,&ww);
32: PetscLogFlops(n);
33: return(0);
34: }
36: PetscErrorCode VecPointwiseMin_Seq(Vec win,Vec xin,Vec yin)
37: {
39: PetscInt n = win->map->n,i;
40: PetscScalar *ww,*xx,*yy; /* cannot make xx or yy const since might be ww */
43: VecGetArrayRead(xin,(const PetscScalar**)&xx);
44: VecGetArrayRead(yin,(const PetscScalar**)&yy);
45: VecGetArray(win,&ww);
47: for (i=0; i<n; i++) ww[i] = PetscMin(PetscRealPart(xx[i]),PetscRealPart(yy[i]));
49: VecRestoreArrayRead(xin,(const PetscScalar**)&xx);
50: VecRestoreArrayRead(yin,(const PetscScalar**)&yy);
51: VecRestoreArray(win,&ww);
52: PetscLogFlops(n);
53: return(0);
54: }
56: PetscErrorCode VecPointwiseMaxAbs_Seq(Vec win,Vec xin,Vec yin)
57: {
59: PetscInt n = win->map->n,i;
60: PetscScalar *ww,*xx,*yy; /* cannot make xx or yy const since might be ww */
63: VecGetArrayRead(xin,(const PetscScalar**)&xx);
64: VecGetArrayRead(yin,(const PetscScalar**)&yy);
65: VecGetArray(win,&ww);
67: for (i=0; i<n; i++) ww[i] = PetscMax(PetscAbsScalar(xx[i]),PetscAbsScalar(yy[i]));
69: PetscLogFlops(n);
70: VecRestoreArrayRead(xin,(const PetscScalar**)&xx);
71: VecRestoreArrayRead(yin,(const PetscScalar**)&yy);
72: VecRestoreArray(win,&ww);
73: return(0);
74: }
76: #include <../src/vec/vec/impls/seq/ftn-kernels/fxtimesy.h>
78: PetscErrorCode VecPointwiseMult_Seq(Vec win,Vec xin,Vec yin)
79: {
81: PetscInt n = win->map->n,i;
82: PetscScalar *ww,*xx,*yy; /* cannot make xx or yy const since might be ww */
85: VecGetArrayRead(xin,(const PetscScalar**)&xx);
86: VecGetArrayRead(yin,(const PetscScalar**)&yy);
87: VecGetArray(win,&ww);
88: if (ww == xx) {
89: for (i=0; i<n; i++) ww[i] *= yy[i];
90: } else if (ww == yy) {
91: for (i=0; i<n; i++) ww[i] *= xx[i];
92: } else {
93: #if defined(PETSC_USE_FORTRAN_KERNEL_XTIMESY)
94: fortranxtimesy_(xx,yy,ww,&n);
95: #else
96: for (i=0; i<n; i++) ww[i] = xx[i] * yy[i];
97: #endif
98: }
99: VecRestoreArrayRead(xin,(const PetscScalar**)&xx);
100: VecRestoreArrayRead(yin,(const PetscScalar**)&yy);
101: VecRestoreArray(win,&ww);
102: PetscLogFlops(n);
103: return(0);
104: }
106: PetscErrorCode VecPointwiseDivide_Seq(Vec win,Vec xin,Vec yin)
107: {
109: PetscInt n = win->map->n,i;
110: PetscScalar *ww,*xx,*yy; /* cannot make xx or yy const since might be ww */
113: VecGetArrayRead(xin,(const PetscScalar**)&xx);
114: VecGetArrayRead(yin,(const PetscScalar**)&yy);
115: VecGetArray(win,&ww);
117: for (i=0; i<n; i++) ww[i] = xx[i] / yy[i];
119: PetscLogFlops(n);
120: VecRestoreArrayRead(xin,(const PetscScalar**)&xx);
121: VecRestoreArrayRead(yin,(const PetscScalar**)&yy);
122: VecRestoreArray(win,&ww);
123: return(0);
124: }
126: PetscErrorCode VecSetRandom_Seq(Vec xin,PetscRandom r)
127: {
129: PetscInt n = xin->map->n,i;
130: PetscScalar *xx;
133: VecGetArray(xin,&xx);
134: for (i=0; i<n; i++) {PetscRandomGetValue(r,&xx[i]);}
135: VecRestoreArray(xin,&xx);
136: return(0);
137: }
139: PetscErrorCode VecGetSize_Seq(Vec vin,PetscInt *size)
140: {
142: *size = vin->map->n;
143: return(0);
144: }
148: PetscErrorCode VecConjugate_Seq(Vec xin)
149: {
150: PetscScalar *x;
151: PetscInt n = xin->map->n;
155: VecGetArray(xin,&x);
156: while (n-->0) {
157: *x = PetscConj(*x);
158: x++;
159: }
160: VecRestoreArray(xin,&x);
161: return(0);
162: }
164: PetscErrorCode VecResetArray_Seq(Vec vin)
165: {
166: Vec_Seq *v = (Vec_Seq*)vin->data;
169: v->array = v->unplacedarray;
170: v->unplacedarray = 0;
171: return(0);
172: }
174: PetscErrorCode VecCopy_Seq(Vec xin,Vec yin)
175: {
176: PetscScalar *ya;
177: const PetscScalar *xa;
178: PetscErrorCode ierr;
181: if (xin != yin) {
182: VecGetArrayRead(xin,&xa);
183: VecGetArray(yin,&ya);
184: PetscMemcpy(ya,xa,xin->map->n*sizeof(PetscScalar));
185: VecRestoreArrayRead(xin,&xa);
186: VecRestoreArray(yin,&ya);
187: }
188: return(0);
189: }
191: PetscErrorCode VecSwap_Seq(Vec xin,Vec yin)
192: {
193: PetscScalar *ya, *xa;
195: PetscBLASInt one = 1,bn;
198: if (xin != yin) {
199: PetscBLASIntCast(xin->map->n,&bn);
200: VecGetArray(xin,&xa);
201: VecGetArray(yin,&ya);
202: PetscStackCallBLAS("BLASswap",BLASswap_(&bn,xa,&one,ya,&one));
203: VecRestoreArray(xin,&xa);
204: VecRestoreArray(yin,&ya);
205: }
206: return(0);
207: }
209: #include <../src/vec/vec/impls/seq/ftn-kernels/fnorm.h>
211: PetscErrorCode VecNorm_Seq(Vec xin,NormType type,PetscReal *z)
212: {
213: const PetscScalar *xx;
214: PetscErrorCode ierr;
215: PetscInt n = xin->map->n;
216: PetscBLASInt one = 1, bn;
219: PetscBLASIntCast(n,&bn);
220: if (type == NORM_2 || type == NORM_FROBENIUS) {
221: VecGetArrayRead(xin,&xx);
222: #if defined(PETSC_USE_REAL___FP16)
223: *z = BLASnrm2_(&bn,xx,&one);
224: #else
225: *z = PetscRealPart(BLASdot_(&bn,xx,&one,xx,&one));
226: *z = PetscSqrtReal(*z);
227: #endif
228: VecRestoreArrayRead(xin,&xx);
229: PetscLogFlops(PetscMax(2.0*n-1,0.0));
230: } else if (type == NORM_INFINITY) {
231: PetscInt i;
232: PetscReal max = 0.0,tmp;
234: VecGetArrayRead(xin,&xx);
235: for (i=0; i<n; i++) {
236: if ((tmp = PetscAbsScalar(*xx)) > max) max = tmp;
237: /* check special case of tmp == NaN */
238: if (tmp != tmp) {max = tmp; break;}
239: xx++;
240: }
241: VecRestoreArrayRead(xin,&xx);
242: *z = max;
243: } else if (type == NORM_1) {
244: #if defined(PETSC_USE_COMPLEX)
245: PetscReal tmp = 0.0;
246: PetscInt i;
247: #endif
248: VecGetArrayRead(xin,&xx);
249: #if defined(PETSC_USE_COMPLEX)
250: /* BLASasum() returns the nonstandard 1 norm of the 1 norm of the complex entries so we provide a custom loop instead */
251: for (i=0; i<n; i++) {
252: tmp += PetscAbsScalar(xx[i]);
253: }
254: *z = tmp;
255: #else
256: PetscStackCallBLAS("BLASasum",*z = BLASasum_(&bn,xx,&one));
257: #endif
258: VecRestoreArrayRead(xin,&xx);
259: PetscLogFlops(PetscMax(n-1.0,0.0));
260: } else if (type == NORM_1_AND_2) {
261: VecNorm_Seq(xin,NORM_1,z);
262: VecNorm_Seq(xin,NORM_2,z+1);
263: }
264: return(0);
265: }
267: PetscErrorCode VecView_Seq_ASCII(Vec xin,PetscViewer viewer)
268: {
269: PetscErrorCode ierr;
270: PetscInt i,n = xin->map->n;
271: const char *name;
272: PetscViewerFormat format;
273: const PetscScalar *xv;
276: VecGetArrayRead(xin,&xv);
277: PetscViewerGetFormat(viewer,&format);
278: if (format == PETSC_VIEWER_ASCII_MATLAB) {
279: PetscObjectGetName((PetscObject)xin,&name);
280: PetscViewerASCIIPrintf(viewer,"%s = [\n",name);
281: for (i=0; i<n; i++) {
282: #if defined(PETSC_USE_COMPLEX)
283: if (PetscImaginaryPart(xv[i]) > 0.0) {
284: PetscViewerASCIIPrintf(viewer,"%18.16e + %18.16ei\n",(double)PetscRealPart(xv[i]),(double)PetscImaginaryPart(xv[i]));
285: } else if (PetscImaginaryPart(xv[i]) < 0.0) {
286: PetscViewerASCIIPrintf(viewer,"%18.16e - %18.16ei\n",(double)PetscRealPart(xv[i]),-(double)PetscImaginaryPart(xv[i]));
287: } else {
288: PetscViewerASCIIPrintf(viewer,"%18.16e\n",(double)PetscRealPart(xv[i]));
289: }
290: #else
291: PetscViewerASCIIPrintf(viewer,"%18.16e\n",(double) xv[i]);
292: #endif
293: }
294: PetscViewerASCIIPrintf(viewer,"];\n");
295: } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
296: for (i=0; i<n; i++) {
297: #if defined(PETSC_USE_COMPLEX)
298: PetscViewerASCIIPrintf(viewer,"%18.16e %18.16e\n",(double)PetscRealPart(xv[i]),(double)PetscImaginaryPart(xv[i]));
299: #else
300: PetscViewerASCIIPrintf(viewer,"%18.16e\n",(double)xv[i]);
301: #endif
302: }
303: } else if (format == PETSC_VIEWER_ASCII_VTK || format == PETSC_VIEWER_ASCII_VTK_CELL) {
304: /*
305: state 0: No header has been output
306: state 1: Only POINT_DATA has been output
307: state 2: Only CELL_DATA has been output
308: state 3: Output both, POINT_DATA last
309: state 4: Output both, CELL_DATA last
310: */
311: static PetscInt stateId = -1;
312: int outputState = 0;
313: PetscBool hasState;
314: int doOutput = 0;
315: PetscInt bs, b;
317: if (stateId < 0) {
318: PetscObjectComposedDataRegister(&stateId);
319: }
320: PetscObjectComposedDataGetInt((PetscObject) viewer, stateId, outputState, hasState);
321: if (!hasState) outputState = 0;
322: PetscObjectGetName((PetscObject) xin, &name);
323: VecGetBlockSize(xin, &bs);
324: if ((bs < 1) || (bs > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "VTK can only handle 3D objects, but vector dimension is %d", bs);
325: if (format == PETSC_VIEWER_ASCII_VTK) {
326: if (outputState == 0) {
327: outputState = 1;
328: doOutput = 1;
329: } else if (outputState == 1) doOutput = 0;
330: else if (outputState == 2) {
331: outputState = 3;
332: doOutput = 1;
333: } else if (outputState == 3) doOutput = 0;
334: else if (outputState == 4) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Tried to output POINT_DATA again after intervening CELL_DATA");
336: if (doOutput) {
337: PetscViewerASCIIPrintf(viewer, "POINT_DATA %d\n", n/bs);
338: }
339: } else {
340: if (outputState == 0) {
341: outputState = 2;
342: doOutput = 1;
343: } else if (outputState == 1) {
344: outputState = 4;
345: doOutput = 1;
346: } else if (outputState == 2) doOutput = 0;
347: else if (outputState == 3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Tried to output CELL_DATA again after intervening POINT_DATA");
348: else if (outputState == 4) doOutput = 0;
350: if (doOutput) {
351: PetscViewerASCIIPrintf(viewer, "CELL_DATA %d\n", n);
352: }
353: }
354: PetscObjectComposedDataSetInt((PetscObject) viewer, stateId, outputState);
355: if (name) {
356: if (bs == 3) {
357: PetscViewerASCIIPrintf(viewer, "VECTORS %s double\n", name);
358: } else {
359: PetscViewerASCIIPrintf(viewer, "SCALARS %s double %d\n", name, bs);
360: }
361: } else {
362: PetscViewerASCIIPrintf(viewer, "SCALARS scalars double %d\n", bs);
363: }
364: if (bs != 3) {
365: PetscViewerASCIIPrintf(viewer, "LOOKUP_TABLE default\n");
366: }
367: for (i=0; i<n/bs; i++) {
368: for (b=0; b<bs; b++) {
369: if (b > 0) {
370: PetscViewerASCIIPrintf(viewer," ");
371: }
372: #if !defined(PETSC_USE_COMPLEX)
373: PetscViewerASCIIPrintf(viewer,"%g",(double)xv[i*bs+b]);
374: #endif
375: }
376: PetscViewerASCIIPrintf(viewer,"\n");
377: }
378: } else if (format == PETSC_VIEWER_ASCII_VTK_COORDS) {
379: PetscInt bs, b;
381: VecGetBlockSize(xin, &bs);
382: if ((bs < 1) || (bs > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "VTK can only handle 3D objects, but vector dimension is %d", bs);
383: for (i=0; i<n/bs; i++) {
384: for (b=0; b<bs; b++) {
385: if (b > 0) {
386: PetscViewerASCIIPrintf(viewer," ");
387: }
388: #if !defined(PETSC_USE_COMPLEX)
389: PetscViewerASCIIPrintf(viewer,"%g",(double)xv[i*bs+b]);
390: #endif
391: }
392: for (b=bs; b<3; b++) {
393: PetscViewerASCIIPrintf(viewer," 0.0");
394: }
395: PetscViewerASCIIPrintf(viewer,"\n");
396: }
397: } else if (format == PETSC_VIEWER_ASCII_PCICE) {
398: PetscInt bs, b;
400: VecGetBlockSize(xin, &bs);
401: if ((bs < 1) || (bs > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "PCICE can only handle up to 3D objects, but vector dimension is %d", bs);
402: PetscViewerASCIIPrintf(viewer,"%D\n", xin->map->N/bs);
403: for (i=0; i<n/bs; i++) {
404: PetscViewerASCIIPrintf(viewer,"%7D ", i+1);
405: for (b=0; b<bs; b++) {
406: if (b > 0) {
407: PetscViewerASCIIPrintf(viewer," ");
408: }
409: #if !defined(PETSC_USE_COMPLEX)
410: PetscViewerASCIIPrintf(viewer,"% 12.5E",(double)xv[i*bs+b]);
411: #endif
412: }
413: PetscViewerASCIIPrintf(viewer,"\n");
414: }
415: } else if (format == PETSC_VIEWER_ASCII_GLVIS) {
416: /* GLVis ASCII visualization/dump: this function mimicks mfem::GridFunction::Save() */
417: const PetscScalar *array;
418: PetscInt i,n,vdim, ordering = 1; /* mfem::FiniteElementSpace::Ordering::byVDIM */
419: PetscContainer glvis_container;
420: PetscViewerGLVisVecInfo glvis_vec_info;
421: PetscViewerGLVisInfo glvis_info;
422: PetscErrorCode ierr;
424: /* mfem::FiniteElementSpace::Save() */
425: VecGetBlockSize(xin,&vdim);
426: PetscViewerASCIIPrintf(viewer,"FiniteElementSpace\n");
427: PetscObjectQuery((PetscObject)xin,"_glvis_info_container",(PetscObject*)&glvis_container);
428: if (!glvis_container) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_PLIB,"Missing GLVis container");
429: PetscContainerGetPointer(glvis_container,(void**)&glvis_vec_info);
430: PetscViewerASCIIPrintf(viewer,"%s\n",glvis_vec_info->fec_type);
431: PetscViewerASCIIPrintf(viewer,"VDim: %d\n",vdim);
432: PetscViewerASCIIPrintf(viewer,"Ordering: %d\n",ordering);
433: PetscViewerASCIIPrintf(viewer,"\n");
434: /* mfem::Vector::Print() */
435: PetscObjectQuery((PetscObject)viewer,"_glvis_info_container",(PetscObject*)&glvis_container);
436: if (!glvis_container) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_PLIB,"Missing GLVis container");
437: PetscContainerGetPointer(glvis_container,(void**)&glvis_info);
438: if (glvis_info->enabled) {
439: VecGetLocalSize(xin,&n);
440: VecGetArrayRead(xin,&array);
441: for (i=0;i<n;i++) {
442: PetscViewerASCIIPrintf(viewer,"%g\n",(double)PetscRealPart(array[i]));
443: }
444: VecRestoreArrayRead(xin,&array);
445: }
446: } else if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
447: /* No info */
448: } else {
449: for (i=0; i<n; i++) {
450: if (format == PETSC_VIEWER_ASCII_INDEX) {
451: PetscViewerASCIIPrintf(viewer,"%D: ",i);
452: }
453: #if defined(PETSC_USE_COMPLEX)
454: if (PetscImaginaryPart(xv[i]) > 0.0) {
455: PetscViewerASCIIPrintf(viewer,"%g + %g i\n",(double)PetscRealPart(xv[i]),(double)PetscImaginaryPart(xv[i]));
456: } else if (PetscImaginaryPart(xv[i]) < 0.0) {
457: PetscViewerASCIIPrintf(viewer,"%g - %g i\n",(double)PetscRealPart(xv[i]),-(double)PetscImaginaryPart(xv[i]));
458: } else {
459: PetscViewerASCIIPrintf(viewer,"%g\n",(double)PetscRealPart(xv[i]));
460: }
461: #else
462: PetscViewerASCIIPrintf(viewer,"%g\n",(double)xv[i]);
463: #endif
464: }
465: }
466: PetscViewerFlush(viewer);
467: VecRestoreArrayRead(xin,&xv);
468: return(0);
469: }
471: #include <petscdraw.h>
472: PetscErrorCode VecView_Seq_Draw_LG(Vec xin,PetscViewer v)
473: {
474: PetscDraw draw;
475: PetscBool isnull;
476: PetscDrawLG lg;
477: PetscErrorCode ierr;
478: PetscInt i,c,bs = PetscAbs(xin->map->bs),n = xin->map->n/bs;
479: const PetscScalar *xv;
480: PetscReal *xx,*yy;
481: int colors[] = {PETSC_DRAW_RED};
484: PetscViewerDrawGetDraw(v,0,&draw);
485: PetscDrawIsNull(draw,&isnull);
486: if (isnull) return(0);
488: PetscMalloc2(n,&xx,n,&yy);
489: VecGetArrayRead(xin,&xv);
490: for (c=0; c<bs; c++) {
491: PetscViewerDrawGetDrawLG(v,c,&lg);
492: PetscDrawLGReset(lg);
493: PetscDrawLGSetDimension(lg,1);
494: PetscDrawLGSetColors(lg,colors);
495: for (i=0; i<n; i++) {
496: xx[i] = (PetscReal)i;
497: yy[i] = PetscRealPart(xv[c + i*bs]);
498: }
499: PetscDrawLGAddPoints(lg,n,&xx,&yy);
500: PetscDrawLGDraw(lg);
501: PetscDrawLGSave(lg);
502: }
503: VecRestoreArrayRead(xin,&xv);
504: PetscFree2(xx,yy);
505: return(0);
506: }
508: PetscErrorCode VecView_Seq_Draw(Vec xin,PetscViewer v)
509: {
510: PetscErrorCode ierr;
511: PetscDraw draw;
512: PetscBool isnull;
515: PetscViewerDrawGetDraw(v,0,&draw);
516: PetscDrawIsNull(draw,&isnull);
517: if (isnull) return(0);
518: PetscViewerPushFormat(v,PETSC_VIEWER_DRAW_LG);
519: VecView_Seq_Draw_LG(xin,v);
520: PetscViewerPopFormat(v);
521: return(0);
522: }
524: PetscErrorCode VecView_Seq_Binary(Vec xin,PetscViewer viewer)
525: {
526: PetscErrorCode ierr;
527: int fdes;
528: PetscInt n = xin->map->n,classid=VEC_FILE_CLASSID;
529: FILE *file;
530: const PetscScalar *xv;
531: #if defined(PETSC_HAVE_MPIIO)
532: PetscBool isMPIIO;
533: #endif
534: PetscBool skipHeader;
535: PetscViewerFormat format;
538: /* Write vector header */
539: PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);
540: if (!skipHeader) {
541: PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);
542: PetscViewerBinaryWrite(viewer,&n,1,PETSC_INT,PETSC_FALSE);
543: }
545: /* Write vector contents */
546: #if defined(PETSC_HAVE_MPIIO)
547: PetscViewerBinaryGetUseMPIIO(viewer,&isMPIIO);
548: if (!isMPIIO) {
549: #endif
550: PetscViewerBinaryGetDescriptor(viewer,&fdes);
551: VecGetArrayRead(xin,&xv);
552: PetscBinaryWrite(fdes,(void*)xv,n,PETSC_SCALAR,PETSC_FALSE);
553: VecRestoreArrayRead(xin,&xv);
554: PetscViewerGetFormat(viewer,&format);
555: if (format == PETSC_VIEWER_BINARY_MATLAB) {
556: MPI_Comm comm;
557: FILE *info;
558: const char *name;
560: PetscObjectGetName((PetscObject)xin,&name);
561: PetscObjectGetComm((PetscObject)viewer,&comm);
562: PetscViewerBinaryGetInfoPointer(viewer,&info);
563: PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");
564: PetscFPrintf(comm,info,"#$$ Set.%s = PetscBinaryRead(fd);\n",name);
565: PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");
566: }
567: #if defined(PETSC_HAVE_MPIIO)
568: } else {
569: MPI_Offset off;
570: MPI_File mfdes;
571: PetscMPIInt lsize;
573: PetscMPIIntCast(n,&lsize);
574: PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);
575: PetscViewerBinaryGetMPIIOOffset(viewer,&off);
576: MPI_File_set_view(mfdes,off,MPIU_SCALAR,MPIU_SCALAR,(char*)"native",MPI_INFO_NULL);
577: VecGetArrayRead(xin,&xv);
578: MPIU_File_write_all(mfdes,(void*)xv,lsize,MPIU_SCALAR,MPI_STATUS_IGNORE);
579: VecRestoreArrayRead(xin,&xv);
580: PetscViewerBinaryAddMPIIOOffset(viewer,n*sizeof(PetscScalar));
581: }
582: #endif
584: PetscViewerBinaryGetInfoPointer(viewer,&file);
585: if (file) {
586: if (((PetscObject)xin)->prefix) {
587: PetscFPrintf(PETSC_COMM_SELF,file,"-%svecload_block_size %D\n",((PetscObject)xin)->prefix,PetscAbs(xin->map->bs));
588: } else {
589: PetscFPrintf(PETSC_COMM_SELF,file,"-vecload_block_size %D\n",PetscAbs(xin->map->bs));
590: }
591: }
592: return(0);
593: }
595: #if defined(PETSC_HAVE_MATLAB_ENGINE)
596: #include <petscmatlab.h>
597: #include <mat.h> /* MATLAB include file */
598: PetscErrorCode VecView_Seq_Matlab(Vec vec,PetscViewer viewer)
599: {
600: PetscErrorCode ierr;
601: PetscInt n;
602: const PetscScalar *array;
605: VecGetLocalSize(vec,&n);
606: PetscObjectName((PetscObject)vec);
607: VecGetArrayRead(vec,&array);
608: PetscViewerMatlabPutArray(viewer,n,1,array,((PetscObject)vec)->name);
609: VecRestoreArrayRead(vec,&array);
610: return(0);
611: }
612: #endif
614: PETSC_EXTERN PetscErrorCode VecView_Seq(Vec xin,PetscViewer viewer)
615: {
617: PetscBool isdraw,iascii,issocket,isbinary;
618: #if defined(PETSC_HAVE_MATHEMATICA)
619: PetscBool ismathematica;
620: #endif
621: #if defined(PETSC_HAVE_MATLAB_ENGINE)
622: PetscBool ismatlab;
623: #endif
624: #if defined(PETSC_HAVE_HDF5)
625: PetscBool ishdf5;
626: #endif
627: PetscBool isglvis;
630: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
631: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
632: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
633: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
634: #if defined(PETSC_HAVE_MATHEMATICA)
635: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERMATHEMATICA,&ismathematica);
636: #endif
637: #if defined(PETSC_HAVE_HDF5)
638: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
639: #endif
640: #if defined(PETSC_HAVE_MATLAB_ENGINE)
641: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERMATLAB,&ismatlab);
642: #endif
643: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERGLVIS,&isglvis);
645: if (isdraw) {
646: VecView_Seq_Draw(xin,viewer);
647: } else if (iascii) {
648: VecView_Seq_ASCII(xin,viewer);
649: } else if (isbinary) {
650: VecView_Seq_Binary(xin,viewer);
651: #if defined(PETSC_HAVE_MATHEMATICA)
652: } else if (ismathematica) {
653: PetscViewerMathematicaPutVector(viewer,xin);
654: #endif
655: #if defined(PETSC_HAVE_HDF5)
656: } else if (ishdf5) {
657: VecView_MPI_HDF5(xin,viewer); /* Reusing VecView_MPI_HDF5 ... don't want code duplication*/
658: #endif
659: #if defined(PETSC_HAVE_MATLAB_ENGINE)
660: } else if (ismatlab) {
661: VecView_Seq_Matlab(xin,viewer);
662: #endif
663: } else if (isglvis) {
664: VecView_GLVis(xin,viewer);
665: }
666: return(0);
667: }
669: PetscErrorCode VecGetValues_Seq(Vec xin,PetscInt ni,const PetscInt ix[],PetscScalar y[])
670: {
671: const PetscScalar *xx;
672: PetscInt i;
673: PetscErrorCode ierr;
676: VecGetArrayRead(xin,&xx);
677: for (i=0; i<ni; i++) {
678: if (xin->stash.ignorenegidx && ix[i] < 0) continue;
679: #if defined(PETSC_USE_DEBUG)
680: if (ix[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D cannot be negative",ix[i]);
681: if (ix[i] >= xin->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D to large maximum allowed %D",ix[i],xin->map->n);
682: #endif
683: y[i] = xx[ix[i]];
684: }
685: VecRestoreArrayRead(xin,&xx);
686: return(0);
687: }
689: PetscErrorCode VecSetValues_Seq(Vec xin,PetscInt ni,const PetscInt ix[],const PetscScalar y[],InsertMode m)
690: {
691: PetscScalar *xx;
692: PetscInt i;
696: VecGetArray(xin,&xx);
697: if (m == INSERT_VALUES) {
698: for (i=0; i<ni; i++) {
699: if (xin->stash.ignorenegidx && ix[i] < 0) continue;
700: #if defined(PETSC_USE_DEBUG)
701: if (ix[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D cannot be negative",ix[i]);
702: 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);
703: #endif
704: xx[ix[i]] = y[i];
705: }
706: } else {
707: for (i=0; i<ni; i++) {
708: if (xin->stash.ignorenegidx && ix[i] < 0) continue;
709: #if defined(PETSC_USE_DEBUG)
710: if (ix[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D cannot be negative",ix[i]);
711: 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);
712: #endif
713: xx[ix[i]] += y[i];
714: }
715: }
716: VecRestoreArray(xin,&xx);
717: return(0);
718: }
720: PetscErrorCode VecSetValuesBlocked_Seq(Vec xin,PetscInt ni,const PetscInt ix[],const PetscScalar yin[],InsertMode m)
721: {
722: PetscScalar *xx,*y = (PetscScalar*)yin;
723: PetscInt i,bs,start,j;
726: /*
727: For optimization could treat bs = 2, 3, 4, 5 as special cases with loop unrolling
728: */
730: VecGetBlockSize(xin,&bs);
731: VecGetArray(xin,&xx);
732: if (m == INSERT_VALUES) {
733: for (i=0; i<ni; i++) {
734: start = bs*ix[i];
735: if (start < 0) continue;
736: #if defined(PETSC_USE_DEBUG)
737: if (start >= xin->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D maximum %D",start,xin->map->n);
738: #endif
739: for (j=0; j<bs; j++) xx[start+j] = y[j];
740: y += bs;
741: }
742: } else {
743: for (i=0; i<ni; i++) {
744: start = bs*ix[i];
745: if (start < 0) continue;
746: #if defined(PETSC_USE_DEBUG)
747: if (start >= xin->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %D maximum %D",start,xin->map->n);
748: #endif
749: for (j=0; j<bs; j++) xx[start+j] += y[j];
750: y += bs;
751: }
752: }
753: VecRestoreArray(xin,&xx);
754: return(0);
755: }
757: PetscErrorCode VecDestroy_Seq(Vec v)
758: {
759: Vec_Seq *vs = (Vec_Seq*)v->data;
763: #if defined(PETSC_USE_LOG)
764: PetscLogObjectState((PetscObject)v,"Length=%D",v->map->n);
765: #endif
766: PetscFree(vs->array_allocated);
767: PetscFree(v->data);
768: return(0);
769: }
771: PetscErrorCode VecSetOption_Seq(Vec v,VecOption op,PetscBool flag)
772: {
774: if (op == VEC_IGNORE_NEGATIVE_INDICES) v->stash.ignorenegidx = flag;
775: return(0);
776: }
778: PetscErrorCode VecDuplicate_Seq(Vec win,Vec *V)
779: {
783: VecCreate(PetscObjectComm((PetscObject)win),V);
784: VecSetSizes(*V,win->map->n,win->map->n);
785: VecSetType(*V,((PetscObject)win)->type_name);
786: PetscLayoutReference(win->map,&(*V)->map);
787: PetscObjectListDuplicate(((PetscObject)win)->olist,&((PetscObject)(*V))->olist);
788: PetscFunctionListDuplicate(((PetscObject)win)->qlist,&((PetscObject)(*V))->qlist);
790: (*V)->ops->view = win->ops->view;
791: (*V)->stash.ignorenegidx = win->stash.ignorenegidx;
792: return(0);
793: }
795: static struct _VecOps DvOps = {VecDuplicate_Seq, /* 1 */
796: VecDuplicateVecs_Default,
797: VecDestroyVecs_Default,
798: VecDot_Seq,
799: VecMDot_Seq,
800: VecNorm_Seq,
801: VecTDot_Seq,
802: VecMTDot_Seq,
803: VecScale_Seq,
804: VecCopy_Seq, /* 10 */
805: VecSet_Seq,
806: VecSwap_Seq,
807: VecAXPY_Seq,
808: VecAXPBY_Seq,
809: VecMAXPY_Seq,
810: VecAYPX_Seq,
811: VecWAXPY_Seq,
812: VecAXPBYPCZ_Seq,
813: VecPointwiseMult_Seq,
814: VecPointwiseDivide_Seq,
815: VecSetValues_Seq, /* 20 */
816: 0,0,
817: 0,
818: VecGetSize_Seq,
819: VecGetSize_Seq,
820: 0,
821: VecMax_Seq,
822: VecMin_Seq,
823: VecSetRandom_Seq,
824: VecSetOption_Seq, /* 30 */
825: VecSetValuesBlocked_Seq,
826: VecDestroy_Seq,
827: VecView_Seq,
828: VecPlaceArray_Seq,
829: VecReplaceArray_Seq,
830: VecDot_Seq,
831: VecTDot_Seq,
832: VecNorm_Seq,
833: VecMDot_Seq,
834: VecMTDot_Seq, /* 40 */
835: VecLoad_Default,
836: VecReciprocal_Default,
837: VecConjugate_Seq,
838: 0,
839: 0,
840: VecResetArray_Seq,
841: 0,
842: VecMaxPointwiseDivide_Seq,
843: VecPointwiseMax_Seq,
844: VecPointwiseMaxAbs_Seq,
845: VecPointwiseMin_Seq,
846: VecGetValues_Seq,
847: 0,
848: 0,
849: 0,
850: 0,
851: 0,
852: 0,
853: VecStrideGather_Default,
854: VecStrideScatter_Default,
855: 0,
856: 0,
857: 0,
858: 0,
859: 0,
860: VecStrideSubSetGather_Default,
861: VecStrideSubSetScatter_Default,
862: 0,
863: 0
864: };
867: /*
868: This is called by VecCreate_Seq() (i.e. VecCreateSeq()) and VecCreateSeqWithArray()
869: */
870: PetscErrorCode VecCreate_Seq_Private(Vec v,const PetscScalar array[])
871: {
872: Vec_Seq *s;
876: PetscNewLog(v,&s);
877: PetscMemcpy(v->ops,&DvOps,sizeof(DvOps));
879: v->data = (void*)s;
880: v->petscnative = PETSC_TRUE;
881: s->array = (PetscScalar*)array;
882: s->array_allocated = 0;
884: PetscLayoutSetUp(v->map);
885: PetscObjectChangeTypeName((PetscObject)v,VECSEQ);
886: #if defined(PETSC_HAVE_MATLAB_ENGINE)
887: PetscObjectComposeFunction((PetscObject)v,"PetscMatlabEnginePut_C",VecMatlabEnginePut_Default);
888: PetscObjectComposeFunction((PetscObject)v,"PetscMatlabEngineGet_C",VecMatlabEngineGet_Default);
889: #endif
890: return(0);
891: }
893: /*@C
894: VecCreateSeqWithArray - Creates a standard,sequential array-style vector,
895: where the user provides the array space to store the vector values.
897: Collective on MPI_Comm
899: Input Parameter:
900: + comm - the communicator, should be PETSC_COMM_SELF
901: . bs - the block size
902: . n - the vector length
903: - array - memory where the vector elements are to be stored.
905: Output Parameter:
906: . V - the vector
908: Notes:
909: Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the
910: same type as an existing vector.
912: If the user-provided array is NULL, then VecPlaceArray() can be used
913: at a later stage to SET the array for storing the vector values.
915: PETSc does NOT free the array when the vector is destroyed via VecDestroy().
916: The user should not free the array until the vector is destroyed.
918: Level: intermediate
920: Concepts: vectors^creating with array
922: .seealso: VecCreateMPIWithArray(), VecCreate(), VecDuplicate(), VecDuplicateVecs(),
923: VecCreateGhost(), VecCreateSeq(), VecPlaceArray()
924: @*/
925: PetscErrorCode VecCreateSeqWithArray(MPI_Comm comm,PetscInt bs,PetscInt n,const PetscScalar array[],Vec *V)
926: {
928: PetscMPIInt size;
931: VecCreate(comm,V);
932: VecSetSizes(*V,n,n);
933: VecSetBlockSize(*V,bs);
934: MPI_Comm_size(comm,&size);
935: if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot create VECSEQ on more than one process");
936: VecCreate_Seq_Private(*V,array);
937: return(0);
938: }