Actual source code: bvec2.c
1: /*$Id: bvec2.c,v 1.202 2001/09/12 03:26:24 bsmith Exp $*/
2: /*
3: Implements the sequential vectors.
4: */
6: #include src/vec/vecimpl.h
7: #include src/vec/impls/dvecimpl.h
8: #include src/inline/dot.h
9: #include petscblaslapack.h
10: #if defined(PETSC_HAVE_PNETCDF)
11: EXTERN_C_BEGIN
12: #include "pnetcdf.h"
13: EXTERN_C_END
14: #endif
15: #if defined(PETSC_HAVE_AMS)
16: EXTERN int PetscViewerAMSGetAMSComm(PetscViewer,AMS_Comm *);
17: #endif
21: int VecNorm_Seq(Vec xin,NormType type,PetscReal* z)
22: {
23: PetscScalar *xx;
24: int n=xin->n,ierr,one = 1;
27: if (type == NORM_2) {
28: VecGetArrayFast(xin,&xx);
29: /*
30: This is because the Fortran BLAS 1 Norm is very slow!
31: */
32: #if defined(PETSC_HAVE_SLOW_NRM2)
33: #if defined(PETSC_USE_FORTRAN_KERNEL_NORM)
34: fortrannormsqr_(xx,&n,z);
35: *z = sqrt(*z);
36: #elif defined(PETSC_USE_UNROLLED_NORM)
37: {
38: PetscReal work = 0.0;
39: switch (n & 0x3) {
40: case 3: work += PetscRealPart(xx[0]*PetscConj(xx[0])); xx++;
41: case 2: work += PetscRealPart(xx[0]*PetscConj(xx[0])); xx++;
42: case 1: work += PetscRealPart(xx[0]*PetscConj(xx[0])); xx++; n -= 4;
43: }
44: while (n>0) {
45: work += PetscRealPart(xx[0]*PetscConj(xx[0])+xx[1]*PetscConj(xx[1])+
46: xx[2]*PetscConj(xx[2])+xx[3]*PetscConj(xx[3]));
47: xx += 4; n -= 4;
48: }
49: *z = sqrt(work);}
50: #else
51: {
52: int i;
53: PetscScalar sum=0.0;
54: for (i=0; i<n; i++) {
55: sum += (xx[i])*(PetscConj(xx[i]));
56: }
57: *z = sqrt(PetscRealPart(sum));
58: }
59: #endif
60: #else
61: *z = BLnrm2_(&n,xx,&one);
62: #endif
63: VecRestoreArrayFast(xin,&xx);
64: PetscLogFlops(2*n-1);
65: } else if (type == NORM_INFINITY) {
66: int i;
67: PetscReal max = 0.0,tmp;
69: VecGetArrayFast(xin,&xx);
70: for (i=0; i<n; i++) {
71: if ((tmp = PetscAbsScalar(*xx)) > max) max = tmp;
72: /* check special case of tmp == NaN */
73: if (tmp != tmp) {max = tmp; break;}
74: xx++;
75: }
76: VecRestoreArrayFast(xin,&xx);
77: *z = max;
78: } else if (type == NORM_1) {
79: VecGetArrayFast(xin,&xx);
80: *z = BLasum_(&n,xx,&one);
81: VecRestoreArrayFast(xin,&xx);
82: PetscLogFlops(n-1);
83: } else if (type == NORM_1_AND_2) {
84: VecNorm_Seq(xin,NORM_1,z);
85: VecNorm_Seq(xin,NORM_2,z+1);
86: }
87: return(0);
88: }
90: #include petscviewer.h
91: #include petscsys.h
95: int VecView_Seq_File(Vec xin,PetscViewer viewer)
96: {
97: Vec_Seq *x = (Vec_Seq *)xin->data;
98: int i,n = xin->n,ierr;
99: char *name;
100: PetscViewerFormat format;
103: PetscViewerGetFormat(viewer,&format);
104: if (format == PETSC_VIEWER_ASCII_MATLAB) {
105: PetscObjectGetName((PetscObject)xin,&name);
106: PetscViewerASCIIPrintf(viewer,"%s = [\n",name);
107: for (i=0; i<n; i++) {
108: #if defined(PETSC_USE_COMPLEX)
109: if (PetscImaginaryPart(x->array[i]) > 0.0) {
110: PetscViewerASCIIPrintf(viewer,"%18.16e + %18.16ei\n",PetscRealPart(x->array[i]),PetscImaginaryPart(x->array[i]));
111: } else if (PetscImaginaryPart(x->array[i]) < 0.0) {
112: PetscViewerASCIIPrintf(viewer,"%18.16e - %18.16ei\n",PetscRealPart(x->array[i]),-PetscImaginaryPart(x->array[i]));
113: } else {
114: PetscViewerASCIIPrintf(viewer,"%18.16e\n",PetscRealPart(x->array[i]));
115: }
116: #else
117: PetscViewerASCIIPrintf(viewer,"%18.16e\n",x->array[i]);
118: #endif
119: }
120: PetscViewerASCIIPrintf(viewer,"];\n");
121: } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
122: for (i=0; i<n; i++) {
123: #if defined(PETSC_USE_COMPLEX)
124: PetscViewerASCIIPrintf(viewer,"%18.16e %18.16e\n",PetscRealPart(x->array[i]),PetscImaginaryPart(x->array[i]));
125: #else
126: PetscViewerASCIIPrintf(viewer,"%18.16e\n",x->array[i]);
127: #endif
128: }
129: } else {
130: for (i=0; i<n; i++) {
131: if (format == PETSC_VIEWER_ASCII_INDEX) {
132: PetscViewerASCIIPrintf(viewer,"%d: ",i);
133: }
134: #if defined(PETSC_USE_COMPLEX)
135: if (PetscImaginaryPart(x->array[i]) > 0.0) {
136: PetscViewerASCIIPrintf(viewer,"%g + %g i\n",PetscRealPart(x->array[i]),PetscImaginaryPart(x->array[i]));
137: } else if (PetscImaginaryPart(x->array[i]) < 0.0) {
138: PetscViewerASCIIPrintf(viewer,"%g - %g i\n",PetscRealPart(x->array[i]),-PetscImaginaryPart(x->array[i]));
139: } else {
140: PetscViewerASCIIPrintf(viewer,"%g\n",PetscRealPart(x->array[i]));
141: }
142: #else
143: PetscViewerASCIIPrintf(viewer,"%g\n",x->array[i]);
144: #endif
145: }
146: }
147: PetscViewerFlush(viewer);
148: return(0);
149: }
153: static int VecView_Seq_Draw_LG(Vec xin,PetscViewer v)
154: {
155: Vec_Seq *x = (Vec_Seq *)xin->data;
156: int i,n = xin->n,ierr;
157: PetscDraw win;
158: PetscReal *xx;
159: PetscDrawLG lg;
162: PetscViewerDrawGetDrawLG(v,0,&lg);
163: PetscDrawLGGetDraw(lg,&win);
164: PetscDrawCheckResizedWindow(win);
165: PetscDrawLGReset(lg);
166: PetscMalloc((n+1)*sizeof(PetscReal),&xx);
167: for (i=0; i<n; i++) {
168: xx[i] = (PetscReal) i;
169: }
170: #if !defined(PETSC_USE_COMPLEX)
171: PetscDrawLGAddPoints(lg,n,&xx,&x->array);
172: #else
173: {
174: PetscReal *yy;
175: PetscMalloc((n+1)*sizeof(PetscReal),&yy);
176: for (i=0; i<n; i++) {
177: yy[i] = PetscRealPart(x->array[i]);
178: }
179: PetscDrawLGAddPoints(lg,n,&xx,&yy);
180: PetscFree(yy);
181: }
182: #endif
183: PetscFree(xx);
184: PetscDrawLGDraw(lg);
185: PetscDrawSynchronizedFlush(win);
186: return(0);
187: }
191: static int VecView_Seq_Draw(Vec xin,PetscViewer v)
192: {
193: int ierr;
194: PetscDraw draw;
195: PetscTruth isnull;
196: PetscViewerFormat format;
199: PetscViewerDrawGetDraw(v,0,&draw);
200: PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
201:
202: PetscViewerGetFormat(v,&format);
203: /*
204: Currently it only supports drawing to a line graph */
205: if (format != PETSC_VIEWER_DRAW_LG) {
206: PetscViewerPushFormat(v,PETSC_VIEWER_DRAW_LG);
207: }
208: VecView_Seq_Draw_LG(xin,v);
209: if (format != PETSC_VIEWER_DRAW_LG) {
210: PetscViewerPopFormat(v);
211: }
213: return(0);
214: }
218: static int VecView_Seq_Binary(Vec xin,PetscViewer viewer)
219: {
220: Vec_Seq *x = (Vec_Seq *)xin->data;
221: int ierr,fdes,n = xin->n,cookie=VEC_FILE_COOKIE;
222: FILE *file;
225: PetscViewerBinaryGetDescriptor(viewer,&fdes);
226: /* Write vector header */
227: PetscBinaryWrite(fdes,&cookie,1,PETSC_INT,0);
228: PetscBinaryWrite(fdes,&n,1,PETSC_INT,0);
230: /* Write vector contents */
231: PetscBinaryWrite(fdes,x->array,n,PETSC_SCALAR,0);
233: PetscViewerBinaryGetInfoPointer(viewer,&file);
234: if (file && xin->bs > 1) {
235: if (xin->prefix) {
236: fprintf(file,"-%s_vecload_block_size %d\n",xin->prefix,xin->bs);
237: } else {
238: fprintf(file,"-vecload_block_size %d\n",xin->bs);
239: }
240: }
241: return(0);
242: }
246: int VecView_Seq_Netcdf(Vec xin,PetscViewer v)
247: {
248: #if defined(PETSC_HAVE_PNETCDF)
249: int n = xin->n,ierr,ncid,xdim,xdim_num=1,xin_id,xstart=0;
250: MPI_Comm comm = xin->comm;
251: PetscScalar *values,*xarray;
254: #if !defined(PETSC_USE_COMPLEX)
255: VecGetArrayFast(xin,&xarray);
256: PetscViewerNetcdfGetID(v,&ncid);
257: if (ncid < 0) SETERRQ(1,"First call PetscViewerNetcdfOpen to create NetCDF dataset");
258: /* define dimensions */
259: ncmpi_def_dim(ncid,"PETSc_Vector_Global_Size",n,&xdim);
260: /* define variables */
261: ncmpi_def_var(ncid,"PETSc_Vector_Seq",NC_DOUBLE,xdim_num,&xdim,&xin_id);
262: /* leave define mode */
263: ncmpi_enddef(ncid);
264: /* store the vector */
265: VecGetOwnershipRange(xin,&xstart,PETSC_NULL);
266: ncmpi_put_vara_double_all(ncid,xin_id,(const size_t*)&xstart,(const size_t*)&n,xarray);
267: #else
268: PetscPrintf(PETSC_COMM_WORLD,"NetCDF viewer not supported for complex numbers\n");
269: #endif
270: return(0);
271: #else
273: SETERRQ(1,"Build PETSc with NetCDF to use this viewer");
274: #endif
275: }
278: int VecView_Seq(Vec xin,PetscViewer viewer)
279: {
280: Vec_Seq *x = (Vec_Seq *)xin->data;
281: int ierr;
282: PetscTruth isdraw,isascii,issocket,isbinary,ismathematica,isnetcdf;
285: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
286: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
287: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);
288: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
289: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_MATHEMATICA,&ismathematica);
290: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_NETCDF,&isnetcdf);
291: if (isdraw){
292: VecView_Seq_Draw(xin,viewer);
293: } else if (isascii){
294: VecView_Seq_File(xin,viewer);
295: } else if (issocket) {
296: PetscViewerSocketPutScalar(viewer,xin->n,1,x->array);
297: } else if (isbinary) {
298: VecView_Seq_Binary(xin,viewer);
299: } else if (ismathematica) {
300: PetscViewerMathematicaPutVector(viewer,xin);
301: #if defined(PETSC_HAVE_PNETCDF_noneed)
302: } else if (isnetcdf) {
303: VecView_Seq_Netcdf(xin,viewer);
304: #endif
305: } else {
306: SETERRQ1(1,"Viewer type %s not supported by this vector object",((PetscObject)viewer)->type_name);
307: }
308: return(0);
309: }
313: int VecSetValues_Seq(Vec xin,int ni,const int ix[],const PetscScalar y[],InsertMode m)
314: {
315: Vec_Seq *x = (Vec_Seq *)xin->data;
316: PetscScalar *xx = x->array;
317: int i;
320: if (m == INSERT_VALUES) {
321: for (i=0; i<ni; i++) {
322: if (ix[i] < 0) continue;
323: #if defined(PETSC_USE_BOPT_g)
324: if (ix[i] >= xin->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %d maximum %d",ix[i],xin->n);
325: #endif
326: xx[ix[i]] = y[i];
327: }
328: } else {
329: for (i=0; i<ni; i++) {
330: if (ix[i] < 0) continue;
331: #if defined(PETSC_USE_BOPT_g)
332: if (ix[i] >= xin->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %d maximum %d",ix[i],xin->n);
333: #endif
334: xx[ix[i]] += y[i];
335: }
336: }
337: return(0);
338: }
342: int VecSetValuesBlocked_Seq(Vec xin,int ni,const int ix[],const PetscScalar yin[],InsertMode m)
343: {
344: Vec_Seq *x = (Vec_Seq *)xin->data;
345: PetscScalar *xx = x->array,*y = (PetscScalar*)yin;
346: int i,bs = xin->bs,start,j;
348: /*
349: For optimization could treat bs = 2, 3, 4, 5 as special cases with loop unrolling
350: */
352: if (m == INSERT_VALUES) {
353: for (i=0; i<ni; i++) {
354: start = bs*ix[i];
355: if (start < 0) continue;
356: #if defined(PETSC_USE_BOPT_g)
357: if (start >= xin->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %d maximum %d",start,xin->n);
358: #endif
359: for (j=0; j<bs; j++) {
360: xx[start+j] = y[j];
361: }
362: y += bs;
363: }
364: } else {
365: for (i=0; i<ni; i++) {
366: start = bs*ix[i];
367: if (start < 0) continue;
368: #if defined(PETSC_USE_BOPT_g)
369: if (start >= xin->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Out of range index value %d maximum %d",start,xin->n);
370: #endif
371: for (j=0; j<bs; j++) {
372: xx[start+j] += y[j];
373: }
374: y += bs;
375: }
376: }
377: return(0);
378: }
383: int VecDestroy_Seq(Vec v)
384: {
385: Vec_Seq *vs = (Vec_Seq*)v->data;
386: int ierr;
390: /* if memory was published with AMS then destroy it */
391: PetscObjectDepublish(v);
393: #if defined(PETSC_USE_LOG)
394: PetscLogObjectState((PetscObject)v,"Length=%d",v->n);
395: #endif
396: if (vs->array_allocated) {PetscFree(vs->array_allocated);}
397: PetscFree(vs);
399: return(0);
400: }
404: static int VecPublish_Seq(PetscObject obj)
405: {
406: #if defined(PETSC_HAVE_AMS)
407: Vec v = (Vec) obj;
408: Vec_Seq *s = (Vec_Seq*)v->data;
409: int ierr,(*f)(AMS_Memory,char *,Vec);
410: #endif
414: #if defined(PETSC_HAVE_AMS)
415: /* if it is already published then return */
416: if (v->amem >=0) return(0);
418: /* if array in vector was not allocated (for example PCSetUp_BJacobi_Singleblock()) then
419: cannot AMS publish the object*/
420: if (!s->array) return(0);
422: PetscObjectPublishBaseBegin(obj);
423: AMS_Memory_add_field((AMS_Memory)v->amem,"values",s->array,v->n,AMS_DOUBLE,AMS_READ,
424: AMS_DISTRIBUTED,AMS_REDUCT_UNDEF);
426: /* if the vector knows its "layout" let it set it*/
427: PetscObjectQueryFunction(obj,"AMSSetFieldBlock_C",(void (**)(void))&f);
428: if (f) {
429: (*f)((AMS_Memory)v->amem,"values",v);
430: }
431: PetscObjectPublishBaseEnd(obj);
432: #endif
434: return(0);
435: }
437: static struct _VecOps DvOps = {VecDuplicate_Seq,
438: VecDuplicateVecs_Default,
439: VecDestroyVecs_Default,
440: VecDot_Seq,
441: VecMDot_Seq,
442: VecNorm_Seq,
443: VecTDot_Seq,
444: VecMTDot_Seq,
445: VecScale_Seq,
446: VecCopy_Seq,
447: VecSet_Seq,
448: VecSwap_Seq,
449: VecAXPY_Seq,
450: VecAXPBY_Seq,
451: VecMAXPY_Seq,
452: VecAYPX_Seq,
453: VecWAXPY_Seq,
454: VecPointwiseMult_Seq,
455: VecPointwiseDivide_Seq,
456: VecSetValues_Seq,0,0,
457: VecGetArray_Seq,
458: VecGetSize_Seq,
459: VecGetSize_Seq,
460: VecRestoreArray_Seq,
461: VecMax_Seq,
462: VecMin_Seq,
463: VecSetRandom_Seq,0,
464: VecSetValuesBlocked_Seq,
465: VecDestroy_Seq,
466: VecView_Seq,
467: VecPlaceArray_Seq,
468: VecReplaceArray_Seq,
469: VecDot_Seq,
470: VecTDot_Seq,
471: VecNorm_Seq,
472: VecLoadIntoVector_Default,
473: VecReciprocal_Default,
474: 0, /* VecViewNative */
475: VecConjugate_Seq,
476: 0,
477: 0,
478: VecResetArray_Seq,
479: 0,
480: VecMaxPointwiseDivide_Seq};
483: /*
484: This is called by VecCreate_Seq() (i.e. VecCreateSeq()) and VecCreateSeqWithArray()
485: */
488: static int VecCreate_Seq_Private(Vec v,const PetscScalar array[])
489: {
490: Vec_Seq *s;
491: int ierr;
494: PetscMemcpy(v->ops,&DvOps,sizeof(DvOps));
495: PetscNew(Vec_Seq,&s);
496: PetscMemzero(s,sizeof(Vec_Seq));
497: v->data = (void*)s;
498: v->bops->publish = VecPublish_Seq;
499: v->n = PetscMax(v->n,v->N);
500: v->N = PetscMax(v->n,v->N);
501: v->petscnative = PETSC_TRUE;
502: s->array = (PetscScalar *)array;
503: s->array_allocated = 0;
504: if (!v->map) {
505: PetscMapCreateMPI(v->comm,v->n,v->N,&v->map);
506: }
507: PetscObjectChangeTypeName((PetscObject)v,VECSEQ);
508: #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
509: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscMatlabEnginePut_C","VecMatlabEnginePut_Default",VecMatlabEnginePut_Default);
510: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscMatlabEngineGet_C","VecMatlabEngineGet_Default",VecMatlabEngineGet_Default);
511: #endif
512: PetscPublishAll(v);
513: return(0);
514: }
518: /*@C
519: VecCreateSeqWithArray - Creates a standard,sequential array-style vector,
520: where the user provides the array space to store the vector values.
522: Collective on MPI_Comm
524: Input Parameter:
525: + comm - the communicator, should be PETSC_COMM_SELF
526: . n - the vector length
527: - array - memory where the vector elements are to be stored.
529: Output Parameter:
530: . V - the vector
532: Notes:
533: Use VecDuplicate() or VecDuplicateVecs() to form additional vectors of the
534: same type as an existing vector.
536: If the user-provided array is PETSC_NULL, then VecPlaceArray() can be used
537: at a later stage to SET the array for storing the vector values.
539: PETSc does NOT free the array when the vector is destroyed via VecDestroy().
540: The user should not free the array until the vector is destroyed.
542: Level: intermediate
544: Concepts: vectors^creating with array
546: .seealso: VecCreateMPIWithArray(), VecCreate(), VecDuplicate(), VecDuplicateVecs(),
547: VecCreateGhost(), VecCreateSeq(), VecPlaceArray()
548: @*/
549: int VecCreateSeqWithArray(MPI_Comm comm,int n,const PetscScalar array[],Vec *V)
550: {
551: int ierr;
554: VecCreate(comm,V);
555: VecSetSizes(*V,n,n);
556: VecCreate_Seq_Private(*V,array);
557: return(0);
558: }
560: EXTERN_C_BEGIN
563: int VecCreate_Seq(Vec V)
564: {
565: Vec_Seq *s;
566: PetscScalar *array;
567: int ierr,n = PetscMax(V->n,V->N);
570: PetscMalloc( ( n > 0 ? n : 1)*sizeof(PetscScalar),&array);
571: PetscMemzero(array,n*sizeof(PetscScalar));
572: VecCreate_Seq_Private(V,array);
573: s = (Vec_Seq*)V->data;
574: s->array_allocated = array;
575: VecSetSerializeType(V,VEC_SER_SEQ_BINARY);
576: return(0);
577: }
581: int VecSerialize_Seq(MPI_Comm comm, Vec *vec, PetscViewer viewer, PetscTruth store)
582: {
583: Vec v;
584: Vec_Seq *x;
585: PetscScalar *array;
586: int fd;
587: int vars;
588: int ierr;
591: PetscViewerBinaryGetDescriptor(viewer, &fd);
592: if (store) {
593: v = *vec;
594: x = (Vec_Seq *) v->data;
595: PetscBinaryWrite(fd, &v->n, 1, PETSC_INT, 0);
596: PetscBinaryWrite(fd, x->array, v->n, PETSC_SCALAR, 0);
597: } else {
598: PetscBinaryRead(fd, &vars, 1, PETSC_INT);
599: VecCreate(comm, &v);
600: VecSetSizes(v, vars, vars);
601: PetscMalloc(vars * sizeof(PetscScalar), &array);
602: PetscBinaryRead(fd, array, vars, PETSC_SCALAR);
603: VecCreate_Seq_Private(v, array);
604: ((Vec_Seq *) v->data)->array_allocated = array;
606: VecAssemblyBegin(v);
607: VecAssemblyEnd(v);
608: *vec = v;
609: }
611: return(0);
612: }
613: EXTERN_C_END
618: int VecDuplicate_Seq(Vec win,Vec *V)
619: {
620: int ierr;
623: VecCreateSeq(win->comm,win->n,V);
624: if (win->mapping) {
625: (*V)->mapping = win->mapping;
626: PetscObjectReference((PetscObject)win->mapping);
627: }
628: if (win->bmapping) {
629: (*V)->bmapping = win->bmapping;
630: PetscObjectReference((PetscObject)win->bmapping);
631: }
632: (*V)->bs = win->bs;
633: PetscOListDuplicate(win->olist,&(*V)->olist);
634: PetscFListDuplicate(win->qlist,&(*V)->qlist);
635: return(0);
636: }