1: /*
2: BV operations involving global communication.
4: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5: SLEPc - Scalable Library for Eigenvalue Problem Computations
6: Copyright (c) 2002-2015, Universitat Politecnica de Valencia, Spain
8: This file is part of SLEPc.
10: SLEPc is free software: you can redistribute it and/or modify it under the
11: terms of version 3 of the GNU Lesser General Public License as published by
12: the Free Software Foundation.
14: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
15: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17: more details.
19: You should have received a copy of the GNU Lesser General Public License
20: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
21: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22: */
24: #include <slepc/private/bvimpl.h> /*I "slepcbv.h" I*/
28: /*
29: BVDot for the particular case of non-standard inner product with
30: matrix B, which is assumed to be symmetric (or complex Hermitian)
31: */
32: PETSC_STATIC_INLINE PetscErrorCode BVDot_Private(BV X,BV Y,Mat M) 33: {
35: PetscObjectId idx,idy;
36: PetscInt i,j,jend,m;
37: PetscScalar *marray;
38: PetscBool symm=PETSC_FALSE;
39: Vec z;
42: MatGetSize(M,&m,NULL);
43: MatDenseGetArray(M,&marray);
44: PetscObjectGetId((PetscObject)X,&idx);
45: PetscObjectGetId((PetscObject)Y,&idy);
46: if (idx==idy) symm=PETSC_TRUE; /* M=X'BX is symmetric */
47: jend = X->k;
48: for (j=X->l;j<jend;j++) {
49: if (symm) Y->k = j+1;
50: BVGetColumn(X,j,&z);
51: (*Y->ops->dotvec)(Y,z,marray+j*m+Y->l);
52: BVRestoreColumn(X,j,&z);
53: if (symm) {
54: for (i=X->l;i<j;i++)
55: marray[j+i*m] = PetscConj(marray[i+j*m]);
56: }
57: }
58: MatDenseRestoreArray(M,&marray);
59: return(0);
60: }
64: /*@
65: BVDot - Computes the 'block-dot' product of two basis vectors objects.
67: Collective on BV 69: Input Parameters:
70: + X, Y - basis vectors
71: - M - Mat object where the result must be placed
73: Output Parameter:
74: . M - the resulting matrix
76: Notes:
77: This is the generalization of VecDot() for a collection of vectors, M = Y^H*X.
78: The result is a matrix M whose entry m_ij is equal to y_i^H x_j (where y_i^H
79: denotes the conjugate transpose of y_i).
81: If a non-standard inner product has been specified with BVSetMatrix(),
82: then the result is M = Y^H*B*X. In this case, both X and Y must have
83: the same associated matrix.
85: On entry, M must be a sequential dense Mat with dimensions m,n at least, where
86: m is the number of active columns of Y and n is the number of active columns of X.
87: Only rows (resp. columns) of M starting from ly (resp. lx) are computed,
88: where ly (resp. lx) is the number of leading columns of Y (resp. X).
90: X and Y need not be different objects.
92: Level: intermediate
94: .seealso: BVDotVec(), BVDotColumn(), BVSetActiveColumns(), BVSetMatrix()
95: @*/
96: PetscErrorCode BVDot(BV X,BV Y,Mat M) 97: {
99: PetscBool match;
100: PetscInt m,n;
107: BVCheckSizes(X,1);
109: BVCheckSizes(Y,2);
112: PetscObjectTypeCompare((PetscObject)M,MATSEQDENSE,&match);
113: if (!match) SETERRQ(PetscObjectComm((PetscObject)X),PETSC_ERR_SUP,"Mat argument must be of type seqdense");
115: MatGetSize(M,&m,&n);
116: if (m<Y->k) SETERRQ2(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_SIZ,"Mat argument has %D rows, should have at least %D",m,Y->k);
117: if (n<X->k) SETERRQ2(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_SIZ,"Mat argument has %D columns, should have at least %D",n,X->k);
118: if (X->n!=Y->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension X %D, Y %D",X->n,Y->n);
119: if (X->matrix!=Y->matrix) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"X and Y must have the same inner product matrix");
120: if (X->l==X->k || Y->l==Y->k) return(0);
122: PetscLogEventBegin(BV_Dot,X,Y,0,0);
123: if (X->matrix) { /* non-standard inner product: cast into dotvec ops */
124: BVDot_Private(X,Y,M);
125: } else {
126: (*X->ops->dot)(X,Y,M);
127: }
128: PetscLogEventEnd(BV_Dot,X,Y,0,0);
129: return(0);
130: }
134: /*@
135: BVDotVec - Computes multiple dot products of a vector against all the
136: column vectors of a BV.
138: Collective on BV and Vec
140: Input Parameters:
141: + X - basis vectors
142: - y - a vector
144: Output Parameter:
145: . m - an array where the result must be placed
147: Notes:
148: This is analogue to VecMDot(), but using BV to represent a collection
149: of vectors. The result is m = X^H*y, so m_i is equal to x_j^H y. Note
150: that here X is transposed as opposed to BVDot().
152: If a non-standard inner product has been specified with BVSetMatrix(),
153: then the result is m = X^H*B*y.
155: The length of array m must be equal to the number of active columns of X
156: minus the number of leading columns, i.e. the first entry of m is the
157: product of the first non-leading column with y.
159: Level: intermediate
161: .seealso: BVDot(), BVDotColumn(), BVSetActiveColumns(), BVSetMatrix()
162: @*/
163: PetscErrorCode BVDotVec(BV X,Vec y,PetscScalar *m)164: {
166: PetscInt n;
172: BVCheckSizes(X,1);
176: VecGetLocalSize(y,&n);
177: if (X->n!=n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension X %D, y %D",X->n,n);
179: PetscLogEventBegin(BV_Dot,X,y,0,0);
180: (*X->ops->dotvec)(X,y,m);
181: PetscLogEventEnd(BV_Dot,X,y,0,0);
182: return(0);
183: }
187: /*@
188: BVDotVecBegin - Starts a split phase dot product computation.
190: Input Parameters:
191: + X - basis vectors
192: . y - a vector
193: - m - an array where the result will go (can be NULL)
195: Note:
196: Each call to BVDotVecBegin() should be paired with a call to BVDotVecEnd().
198: Level: advanced
200: .seealso: BVDotVecEnd(), BVDotVec()
201: @*/
202: PetscErrorCode BVDotVecBegin(BV X,Vec y,PetscScalar *m)203: {
204: PetscErrorCode ierr;
205: PetscInt i,n,nv;
206: PetscSplitReduction *sr;
207: MPI_Comm comm;
213: BVCheckSizes(X,1);
217: VecGetLocalSize(y,&n);
218: if (X->n!=n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension X %D, y %D",X->n,n);
220: if (X->ops->dotvec_begin) {
221: (*X->ops->dotvec_begin)(X,y,m);
222: } else {
223: nv = X->k-X->l;
224: PetscObjectGetComm((PetscObject)X,&comm);
225: PetscSplitReductionGet(comm,&sr);
226: if (sr->state != STATE_BEGIN) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Called before all VecxxxEnd() called");
227: for (i=0;i<nv;i++) {
228: if (sr->numopsbegin+i >= sr->maxops) {
229: PetscSplitReductionExtend(sr);
230: }
231: sr->reducetype[sr->numopsbegin+i] = REDUCE_SUM;
232: sr->invecs[sr->numopsbegin+i] = (void*)X;
233: }
234: PetscLogEventBegin(BV_Dot,X,y,0,0);
235: (*X->ops->dotvec_local)(X,y,sr->lvalues+sr->numopsbegin);
236: sr->numopsbegin += nv;
237: PetscLogEventEnd(BV_Dot,X,y,0,0);
238: }
239: return(0);
240: }
244: /*@
245: BVDotVecEnd - Ends a split phase dot product computation.
247: Input Parameters:
248: + X - basis vectors
249: . y - a vector
250: - m - an array where the result will go
252: Note:
253: Each call to BVDotVecBegin() should be paired with a call to BVDotVecEnd().
255: Level: advanced
257: .seealso: BVDotVecBegin(), BVDotVec()
258: @*/
259: PetscErrorCode BVDotVecEnd(BV X,Vec y,PetscScalar *m)260: {
261: PetscErrorCode ierr;
262: PetscInt i,nv;
263: PetscSplitReduction *sr;
264: MPI_Comm comm;
269: BVCheckSizes(X,1);
271: if (X->ops->dotvec_end) {
272: (*X->ops->dotvec_end)(X,y,m);
273: } else {
274: nv = X->k-X->l;
275: PetscObjectGetComm((PetscObject)X,&comm);
276: PetscSplitReductionGet(comm,&sr);
277: PetscSplitReductionEnd(sr);
279: if (sr->numopsend >= sr->numopsbegin) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Called VecxxxEnd() more times than VecxxxBegin()");
280: if ((void*)X != sr->invecs[sr->numopsend]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Called BVxxxEnd() in a different order or with a different BV than BVxxxBegin()");
281: if (sr->reducetype[sr->numopsend] != REDUCE_SUM) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Wrong type of reduction");
282: for (i=0;i<nv;i++) m[i] = sr->gvalues[sr->numopsend++];
284: /* Finished getting all the results so reset to no outstanding requests */
285: if (sr->numopsend == sr->numopsbegin) {
286: sr->state = STATE_BEGIN;
287: sr->numopsend = 0;
288: sr->numopsbegin = 0;
289: }
290: }
291: return(0);
292: }
296: /*@
297: BVDotColumn - Computes multiple dot products of a column against all the
298: previous columns of a BV.
300: Collective on BV302: Input Parameters:
303: + X - basis vectors
304: - j - the column index
306: Output Parameter:
307: . m - an array where the result must be placed
309: Notes:
310: This operation is equivalent to BVDotVec() but it uses column j of X
311: rather than taking a Vec as an argument. The number of active columns of
312: X is set to j before the computation, and restored afterwards.
313: If X has leading columns specified, then these columns do not participate
314: in the computation. Therefore, the length of array m must be equal to j
315: minus the number of leading columns.
317: Level: advanced
319: .seealso: BVDot(), BVDotVec(), BVSetActiveColumns(), BVSetMatrix()
320: @*/
321: PetscErrorCode BVDotColumn(BV X,PetscInt j,PetscScalar *m)322: {
324: PetscInt ksave;
325: Vec y;
331: BVCheckSizes(X,1);
333: if (j<0) SETERRQ(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_OUTOFRANGE,"Index j must be non-negative");
334: if (j>=X->m) SETERRQ2(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_OUTOFRANGE,"Index j=%D but BV only has %D columns",j,X->m);
336: PetscLogEventBegin(BV_Dot,X,0,0,0);
337: ksave = X->k;
338: X->k = j;
339: BVGetColumn(X,j,&y);
340: (*X->ops->dotvec)(X,y,m);
341: BVRestoreColumn(X,j,&y);
342: X->k = ksave;
343: PetscLogEventEnd(BV_Dot,X,0,0,0);
344: return(0);
345: }
349: /*@
350: BVDotColumnBegin - Starts a split phase dot product computation.
352: Input Parameters:
353: + X - basis vectors
354: - j - the column index
355: - m - an array where the result will go (can be NULL)
357: Note:
358: Each call to BVDotColumnBegin() should be paired with a call to BVDotColumnEnd().
360: Level: advanced
362: .seealso: BVDotColumnEnd(), BVDotColumn()
363: @*/
364: PetscErrorCode BVDotColumnBegin(BV X,PetscInt j,PetscScalar *m)365: {
366: PetscErrorCode ierr;
367: PetscInt i,nv,ksave;
368: PetscSplitReduction *sr;
369: MPI_Comm comm;
370: Vec y;
376: BVCheckSizes(X,1);
378: if (j<0) SETERRQ(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_OUTOFRANGE,"Index j must be non-negative");
379: if (j>=X->m) SETERRQ2(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_OUTOFRANGE,"Index j=%D but BV only has %D columns",j,X->m);
380: ksave = X->k;
381: X->k = j;
382: BVGetColumn(X,j,&y);
384: if (X->ops->dotvec_begin) {
385: (*X->ops->dotvec_begin)(X,y,m);
386: } else {
387: nv = X->k-X->l;
388: PetscObjectGetComm((PetscObject)X,&comm);
389: PetscSplitReductionGet(comm,&sr);
390: if (sr->state != STATE_BEGIN) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Called before all VecxxxEnd() called");
391: for (i=0;i<nv;i++) {
392: if (sr->numopsbegin+i >= sr->maxops) {
393: PetscSplitReductionExtend(sr);
394: }
395: sr->reducetype[sr->numopsbegin+i] = REDUCE_SUM;
396: sr->invecs[sr->numopsbegin+i] = (void*)X;
397: }
398: PetscLogEventBegin(BV_Dot,X,0,0,0);
399: (*X->ops->dotvec_local)(X,y,sr->lvalues+sr->numopsbegin);
400: sr->numopsbegin += nv;
401: PetscLogEventEnd(BV_Dot,X,0,0,0);
402: }
403: BVRestoreColumn(X,j,&y);
404: X->k = ksave;
405: return(0);
406: }
410: /*@
411: BVDotColumnEnd - Ends a split phase dot product computation.
413: Input Parameters:
414: + X - basis vectors
415: - j - the column index
416: - m - an array where the result will go
418: Note:
419: Each call to BVDotColumnBegin() should be paired with a call to BVDotColumnEnd().
421: Level: advanced
423: .seealso: BVDotColumnBegin(), BVDotColumn()
424: @*/
425: PetscErrorCode BVDotColumnEnd(BV X,PetscInt j,PetscScalar *m)426: {
427: PetscErrorCode ierr;
428: PetscInt i,nv,ksave;
429: PetscSplitReduction *sr;
430: MPI_Comm comm;
431: Vec y;
437: BVCheckSizes(X,1);
439: if (j<0) SETERRQ(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_OUTOFRANGE,"Index j must be non-negative");
440: if (j>=X->m) SETERRQ2(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_OUTOFRANGE,"Index j=%D but BV only has %D columns",j,X->m);
441: ksave = X->k;
442: X->k = j;
444: if (X->ops->dotvec_end) {
445: BVGetColumn(X,j,&y);
446: (*X->ops->dotvec_end)(X,y,m);
447: BVRestoreColumn(X,j,&y);
448: } else {
449: nv = X->k-X->l;
450: PetscObjectGetComm((PetscObject)X,&comm);
451: PetscSplitReductionGet(comm,&sr);
452: PetscSplitReductionEnd(sr);
454: if (sr->numopsend >= sr->numopsbegin) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Called VecxxxEnd() more times than VecxxxBegin()");
455: if ((void*)X != sr->invecs[sr->numopsend]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Called BVxxxEnd() in a different order or with a different BV than BVxxxBegin()");
456: if (sr->reducetype[sr->numopsend] != REDUCE_SUM) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Wrong type of reduction");
457: for (i=0;i<nv;i++) m[i] = sr->gvalues[sr->numopsend++];
459: /* Finished getting all the results so reset to no outstanding requests */
460: if (sr->numopsend == sr->numopsbegin) {
461: sr->state = STATE_BEGIN;
462: sr->numopsend = 0;
463: sr->numopsbegin = 0;
464: }
465: }
466: X->k = ksave;
467: return(0);
468: }
472: PETSC_STATIC_INLINE PetscErrorCode BVNorm_Private(BV bv,Vec z,NormType type,PetscReal *val)473: {
475: PetscScalar p;
478: BV_IPMatMult(bv,z);
479: VecDot(bv->Bx,z,&p);
480: BV_SafeSqrt(bv,p,val);
481: return(0);
482: }
486: PETSC_STATIC_INLINE PetscErrorCode BVNorm_Begin_Private(BV bv,Vec z,NormType type,PetscReal *val)487: {
489: PetscScalar p;
492: BV_IPMatMult(bv,z);
493: VecDotBegin(bv->Bx,z,&p);
494: return(0);
495: }
499: PETSC_STATIC_INLINE PetscErrorCode BVNorm_End_Private(BV bv,Vec z,NormType type,PetscReal *val)500: {
502: PetscScalar p;
505: VecDotEnd(bv->Bx,z,&p);
506: BV_SafeSqrt(bv,p,val);
507: return(0);
508: }
512: /*@
513: BVNorm - Computes the matrix norm of the BV.
515: Collective on BV517: Input Parameters:
518: + bv - basis vectors
519: - type - the norm type
521: Output Parameter:
522: . val - the norm
524: Notes:
525: All active columns (except the leading ones) are considered as a matrix.
526: The allowed norms are NORM_1, NORM_FROBENIUS, and NORM_INFINITY.
528: This operation fails if a non-standard inner product has been
529: specified with BVSetMatrix().
531: Level: intermediate
533: .seealso: BVNormVec(), BVNormColumn(), BVSetActiveColumns(), BVSetMatrix()
534: @*/
535: PetscErrorCode BVNorm(BV bv,NormType type,PetscReal *val)536: {
544: BVCheckSizes(bv,1);
546: if (type==NORM_2 || type==NORM_1_AND_2) SETERRQ(PetscObjectComm((PetscObject)bv),PETSC_ERR_SUP,"Requested norm not available");
547: if (bv->matrix) SETERRQ(PetscObjectComm((PetscObject)bv),PETSC_ERR_SUP,"Matrix norm not available for non-standard inner product");
549: PetscLogEventBegin(BV_Norm,bv,0,0,0);
550: (*bv->ops->norm)(bv,-1,type,val);
551: PetscLogEventEnd(BV_Norm,bv,0,0,0);
552: return(0);
553: }
557: /*@
558: BVNormVec - Computes the norm of a given vector.
560: Collective on BV562: Input Parameters:
563: + bv - basis vectors
564: . v - the vector
565: - type - the norm type
567: Output Parameter:
568: . val - the norm
570: Notes:
571: This is the analogue of BVNormColumn() but for a vector that is not in the BV.
572: If a non-standard inner product has been specified with BVSetMatrix(),
573: then the returned value is sqrt(v'*B*v), where B is the inner product
574: matrix (argument 'type' is ignored). Otherwise, VecNorm() is called.
576: Level: developer
578: .seealso: BVNorm(), BVNormColumn(), BVSetMatrix()
579: @*/
580: PetscErrorCode BVNormVec(BV bv,Vec v,NormType type,PetscReal *val)581: {
583: PetscInt n;
591: BVCheckSizes(bv,1);
595: if (type==NORM_1_AND_2) SETERRQ(PetscObjectComm((PetscObject)bv),PETSC_ERR_SUP,"Requested norm not available");
597: PetscLogEventBegin(BV_Norm,bv,0,0,0);
598: if (bv->matrix) { /* non-standard inner product */
599: VecGetLocalSize(v,&n);
600: if (bv->n!=n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension bv %D, v %D",bv->n,n);
601: BVNorm_Private(bv,v,type,val);
602: } else {
603: VecNorm(v,type,val);
604: }
605: PetscLogEventEnd(BV_Norm,bv,0,0,0);
606: return(0);
607: }
611: /*@
612: BVNormVecBegin - Starts a split phase norm computation.
614: Input Parameters:
615: + bv - basis vectors
616: . v - the vector
617: . type - the norm type
618: - val - the norm
620: Note:
621: Each call to BVNormVecBegin() should be paired with a call to BVNormVecEnd().
623: Level: advanced
625: .seealso: BVNormVecEnd(), BVNormVec()
626: @*/
627: PetscErrorCode BVNormVecBegin(BV bv,Vec v,NormType type,PetscReal *val)628: {
630: PetscInt n;
638: BVCheckSizes(bv,1);
642: if (type==NORM_1_AND_2) SETERRQ(PetscObjectComm((PetscObject)bv),PETSC_ERR_SUP,"Requested norm not available");
644: PetscLogEventBegin(BV_Norm,bv,0,0,0);
645: if (bv->matrix) { /* non-standard inner product */
646: VecGetLocalSize(v,&n);
647: if (bv->n!=n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension bv %D, v %D",bv->n,n);
648: BVNorm_Begin_Private(bv,v,type,val);
649: } else {
650: VecNormBegin(v,type,val);
651: }
652: PetscLogEventEnd(BV_Norm,bv,0,0,0);
653: return(0);
654: }
658: /*@
659: BVNormVecEnd - Ends a split phase norm computation.
661: Input Parameters:
662: + bv - basis vectors
663: . v - the vector
664: . type - the norm type
665: - val - the norm
667: Note:
668: Each call to BVNormVecBegin() should be paired with a call to BVNormVecEnd().
670: Level: advanced
672: .seealso: BVNormVecBegin(), BVNormVec()
673: @*/
674: PetscErrorCode BVNormVecEnd(BV bv,Vec v,NormType type,PetscReal *val)675: {
683: BVCheckSizes(bv,1);
685: if (type==NORM_1_AND_2) SETERRQ(PetscObjectComm((PetscObject)bv),PETSC_ERR_SUP,"Requested norm not available");
687: if (bv->matrix) { /* non-standard inner product */
688: BVNorm_End_Private(bv,v,type,val);
689: } else {
690: VecNormEnd(v,type,val);
691: }
692: return(0);
693: }
697: /*@
698: BVNormColumn - Computes the vector norm of a selected column.
700: Collective on BV702: Input Parameters:
703: + bv - basis vectors
704: . j - column number to be used
705: - type - the norm type
707: Output Parameter:
708: . val - the norm
710: Notes:
711: The norm of V[j] is computed (NORM_1, NORM_2, or NORM_INFINITY).
712: If a non-standard inner product has been specified with BVSetMatrix(),
713: then the returned value is sqrt(V[j]'*B*V[j]),
714: where B is the inner product matrix (argument 'type' is ignored).
716: Level: intermediate
718: .seealso: BVNorm(), BVNormVec(), BVSetActiveColumns(), BVSetMatrix()
719: @*/
720: PetscErrorCode BVNormColumn(BV bv,PetscInt j,NormType type,PetscReal *val)721: {
723: Vec z;
731: BVCheckSizes(bv,1);
733: if (j<0 || j>=bv->m) SETERRQ2(PetscObjectComm((PetscObject)bv),PETSC_ERR_ARG_OUTOFRANGE,"Argument j has wrong value %D, the number of columns is %D",j,bv->m);
734: if (type==NORM_1_AND_2) SETERRQ(PetscObjectComm((PetscObject)bv),PETSC_ERR_SUP,"Requested norm not available");
736: PetscLogEventBegin(BV_Norm,bv,0,0,0);
737: if (bv->matrix) { /* non-standard inner product */
738: BVGetColumn(bv,j,&z);
739: BVNorm_Private(bv,z,type,val);
740: BVRestoreColumn(bv,j,&z);
741: } else {
742: (*bv->ops->norm)(bv,j,type,val);
743: }
744: PetscLogEventEnd(BV_Norm,bv,0,0,0);
745: return(0);
746: }
750: /*@
751: BVNormColumnBegin - Starts a split phase norm computation.
753: Input Parameters:
754: + bv - basis vectors
755: . j - column number to be used
756: . type - the norm type
757: - val - the norm
759: Note:
760: Each call to BVNormColumnBegin() should be paired with a call to BVNormColumnEnd().
762: Level: advanced
764: .seealso: BVNormColumnEnd(), BVNormColumn()
765: @*/
766: PetscErrorCode BVNormColumnBegin(BV bv,PetscInt j,NormType type,PetscReal *val)767: {
768: PetscErrorCode ierr;
769: PetscSplitReduction *sr;
770: PetscReal lresult;
771: MPI_Comm comm;
772: Vec z;
780: BVCheckSizes(bv,1);
782: if (j<0 || j>=bv->m) SETERRQ2(PetscObjectComm((PetscObject)bv),PETSC_ERR_ARG_OUTOFRANGE,"Argument j has wrong value %D, the number of columns is %D",j,bv->m);
783: if (type==NORM_1_AND_2) SETERRQ(PetscObjectComm((PetscObject)bv),PETSC_ERR_SUP,"Requested norm not available");
785: PetscLogEventBegin(BV_Norm,bv,0,0,0);
786: BVGetColumn(bv,j,&z);
787: if (bv->matrix) { /* non-standard inner product */
788: BVNorm_Begin_Private(bv,z,type,val);
789: } else if (bv->ops->norm_begin) {
790: (*bv->ops->norm_begin)(bv,j,type,val);
791: } else {
792: PetscObjectGetComm((PetscObject)z,&comm);
793: PetscSplitReductionGet(comm,&sr);
794: if (sr->state != STATE_BEGIN) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Called before all VecxxxEnd() called");
795: if (sr->numopsbegin >= sr->maxops) {
796: PetscSplitReductionExtend(sr);
797: }
798: sr->invecs[sr->numopsbegin] = (void*)bv;
799: (*bv->ops->norm_local)(bv,j,type,&lresult);
800: if (type == NORM_2) lresult = lresult*lresult;
801: else if (type == NORM_MAX) sr->reducetype[sr->numopsbegin] = REDUCE_MAX;
802: else sr->reducetype[sr->numopsbegin] = REDUCE_SUM;
803: sr->lvalues[sr->numopsbegin++] = lresult;
804: }
805: BVRestoreColumn(bv,j,&z);
806: PetscLogEventEnd(BV_Norm,bv,0,0,0);
807: return(0);
808: }
812: /*@
813: BVNormColumnEnd - Ends a split phase norm computation.
815: Input Parameters:
816: + bv - basis vectors
817: . j - column number to be used
818: . type - the norm type
819: - val - the norm
821: Note:
822: Each call to BVNormColumnBegin() should be paired with a call to BVNormColumnEnd().
824: Level: advanced
826: .seealso: BVNormColumnBegin(), BVNormColumn()
827: @*/
828: PetscErrorCode BVNormColumnEnd(BV bv,PetscInt j,NormType type,PetscReal *val)829: {
830: PetscErrorCode ierr;
831: PetscSplitReduction *sr;
832: MPI_Comm comm;
833: Vec z;
841: BVCheckSizes(bv,1);
843: if (type==NORM_1_AND_2) SETERRQ(PetscObjectComm((PetscObject)bv),PETSC_ERR_SUP,"Requested norm not available");
845: BVGetColumn(bv,j,&z);
846: if (bv->matrix) { /* non-standard inner product */
847: BVNorm_End_Private(bv,z,type,val);
848: } else if (bv->ops->norm_end) {
849: (*bv->ops->norm_end)(bv,j,type,val);
850: } else {
851: PetscObjectGetComm((PetscObject)z,&comm);
852: PetscSplitReductionGet(comm,&sr);
853: PetscSplitReductionEnd(sr);
855: if (sr->numopsend >= sr->numopsbegin) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Called VecxxxEnd() more times then VecxxxBegin()");
856: if ((void*)bv != sr->invecs[sr->numopsend]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Called VecxxxEnd() in a different order or with a different vector than VecxxxBegin()");
857: if (sr->reducetype[sr->numopsend] != REDUCE_MAX && type == NORM_MAX) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Called BVNormEnd(,NORM_MAX,) on a reduction started with VecDotBegin() or NORM_1 or NORM_2");
858: *val = PetscRealPart(sr->gvalues[sr->numopsend++]);
859: if (type == NORM_2) *val = PetscSqrtReal(*val);
860: if (sr->numopsend == sr->numopsbegin) {
861: sr->state = STATE_BEGIN;
862: sr->numopsend = 0;
863: sr->numopsbegin = 0;
864: }
865: }
866: BVRestoreColumn(bv,j,&z);
867: return(0);
868: }
872: /*
873: Compute Y^H*A*X: right part column by column (with MatMult) and bottom
874: part row by row (with MatMultHermitianTranspose); result placed in marray[*,ldm]
875: */
876: PETSC_STATIC_INLINE PetscErrorCode BVMatProject_Vec(BV X,Mat A,BV Y,PetscScalar *marray,PetscInt ldm,PetscBool symm)877: {
879: PetscInt i,j,lx,ly,kx,ky,ulim;
880: Vec z,f;
883: lx = X->l; kx = X->k;
884: ly = Y->l; ky = Y->k;
885: BVCreateVec(X,&f);
886: for (j=lx;j<kx;j++) {
887: BVGetColumn(X,j,&z);
888: MatMult(A,z,f);
889: BVRestoreColumn(X,j,&z);
890: ulim = PetscMin(ly+(j-lx)+1,ky);
891: Y->l = 0; Y->k = ulim;
892: (*Y->ops->dotvec)(Y,f,marray+j*ldm);
893: if (symm) {
894: for (i=0;i<j;i++) marray[j+i*ldm] = PetscConj(marray[i+j*ldm]);
895: }
896: }
897: if (!symm) {
898: BV_AllocateCoeffs(Y);
899: for (j=ly;j<ky;j++) {
900: BVGetColumn(Y,j,&z);
901: MatMultHermitianTranspose(A,z,f);
902: BVRestoreColumn(Y,j,&z);
903: ulim = PetscMin(lx+(j-ly),kx);
904: X->l = 0; X->k = ulim;
905: (*X->ops->dotvec)(X,f,Y->h);
906: for (i=0;i<ulim;i++) marray[j+i*ldm] = PetscConj(Y->h[i]);
907: }
908: }
909: VecDestroy(&f);
910: X->l = lx; X->k = kx;
911: Y->l = ly; Y->k = ky;
912: return(0);
913: }
917: /*
918: Compute Y^H*A*X= [ -- | Y0'*W1 ]
919: [ Y1'*W0 | Y1'*W1 ]
920: Allocates auxiliary BV to store the result of A*X, then one BVDot921: call for top-right part and another one for bottom part;
922: result placed in marray[*,ldm]
923: */
924: PETSC_STATIC_INLINE PetscErrorCode BVMatProject_MatMult(BV X,Mat A,BV Y,PetscScalar *marray,PetscInt ldm)925: {
927: PetscInt j,lx,ly,kx,ky;
928: PetscScalar *harray;
929: Mat H;
930: BV W;
933: lx = X->l; kx = X->k;
934: ly = Y->l; ky = Y->k;
935: BVDuplicate(X,&W);
936: X->l = 0; X->k = kx;
937: BVMatMult(X,A,W);
939: /* top-right part, Y0'*AX1 */
940: if (ly>0 && lx<kx) {
941: MatCreateSeqDense(PETSC_COMM_SELF,ly,kx,NULL,&H);
942: W->l = lx; W->k = kx;
943: Y->l = 0; Y->k = ly;
944: BVDot(W,Y,H);
945: MatDenseGetArray(H,&harray);
946: for (j=lx;j<kx;j++) {
947: PetscMemcpy(marray+j*ldm,harray+j*ly,ly*sizeof(PetscScalar));
948: }
949: MatDenseRestoreArray(H,&harray);
950: MatDestroy(&H);
951: }
953: /* bottom part, Y1'*AX */
954: if (kx>0 && ly<ky) {
955: MatCreateSeqDense(PETSC_COMM_SELF,ky,kx,NULL,&H);
956: W->l = 0; W->k = kx;
957: Y->l = ly; Y->k = ky;
958: BVDot(W,Y,H);
959: MatDenseGetArray(H,&harray);
960: for (j=0;j<kx;j++) {
961: PetscMemcpy(marray+j*ldm+ly,harray+j*ky+ly,(ky-ly)*sizeof(PetscScalar));
962: }
963: MatDenseRestoreArray(H,&harray);
964: MatDestroy(&H);
965: }
966: BVDestroy(&W);
967: X->l = lx; X->k = kx;
968: Y->l = ly; Y->k = ky;
969: return(0);
970: }
974: /*
975: Compute Y^H*A*X= [ -- | Y0'*W1 ]
976: [ Y1'*W0 | Y1'*W1 ]
977: First stage: allocate auxiliary BV to store A*X1, one BVDot for right part;
978: Second stage: resize BV to accomodate A'*Y1, then call BVDot for transpose of
979: bottom-left part; result placed in marray[*,ldm]
980: */
981: PETSC_STATIC_INLINE PetscErrorCode BVMatProject_MatMult_2(BV X,Mat A,BV Y,PetscScalar *marray,PetscInt ldm,PetscBool symm)982: {
984: PetscInt i,j,lx,ly,kx,ky;
985: PetscScalar *harray;
986: Mat H;
987: BV W;
990: lx = X->l; kx = X->k;
991: ly = Y->l; ky = Y->k;
993: /* right part, Y'*AX1 */
994: BVDuplicateResize(X,kx-lx,&W);
995: if (ky>0 && lx<kx) {
996: BVMatMult(X,A,W);
997: MatCreateSeqDense(PETSC_COMM_SELF,ky,kx-lx,NULL,&H);
998: Y->l = 0; Y->k = ky;
999: BVDot(W,Y,H);
1000: MatDenseGetArray(H,&harray);
1001: for (j=lx;j<kx;j++) {
1002: PetscMemcpy(marray+j*ldm,harray+(j-lx)*ky,ky*sizeof(PetscScalar));
1003: }
1004: MatDenseRestoreArray(H,&harray);
1005: MatDestroy(&H);
1006: }
1008: /* bottom-left part, Y1'*AX0 */
1009: if (lx>0 && ly<ky) {
1010: if (symm) {
1011: /* do not compute, just copy symmetric elements */
1012: for (i=ly;i<ky;i++) {
1013: for (j=0;j<lx;j++) marray[i+j*ldm] = PetscConj(marray[j+i*ldm]);
1014: }
1015: } else {
1016: BVResize(W,ky-ly,PETSC_FALSE);
1017: Y->l = ly; Y->k = ky;
1018: BVMatMultHermitianTranspose(Y,A,W);
1019: MatCreateSeqDense(PETSC_COMM_SELF,lx,ky-ly,NULL,&H);
1020: X->l = 0; X->k = lx;
1021: BVDot(W,X,H);
1022: MatDenseGetArray(H,&harray);
1023: for (i=0;i<ky-ly;i++) {
1024: for (j=0;j<lx;j++) {
1025: marray[i+j*ldm+ly] = PetscConj(harray[j+i*(ky-ly)]);
1026: }
1027: }
1028: MatDenseRestoreArray(H,&harray);
1029: MatDestroy(&H);
1030: }
1031: }
1032: BVDestroy(&W);
1033: X->l = lx; X->k = kx;
1034: Y->l = ly; Y->k = ky;
1035: return(0);
1036: }
1040: /*
1041: Compute Y^H*X = [ -- | Y0'*X1 ] (X contains A*X):
1042: [ Y1'*X0 | Y1'*X1 ]
1043: one BVDot call for top-right part and another one for bottom part;
1044: result placed in marray[*,ldm]
1045: */
1046: PETSC_STATIC_INLINE PetscErrorCode BVMatProject_Dot(BV X,BV Y,PetscScalar *marray,PetscInt ldm)1047: {
1049: PetscInt j,lx,ly,kx,ky;
1050: PetscScalar *harray;
1051: Mat H;
1054: lx = X->l; kx = X->k;
1055: ly = Y->l; ky = Y->k;
1057: /* top-right part, Y0'*X1 */
1058: if (ly>0 && lx<kx) {
1059: MatCreateSeqDense(PETSC_COMM_SELF,ly,kx,NULL,&H);
1060: X->l = lx; X->k = kx;
1061: Y->l = 0; Y->k = ly;
1062: BVDot(X,Y,H);
1063: MatDenseGetArray(H,&harray);
1064: for (j=lx;j<kx;j++) {
1065: PetscMemcpy(marray+j*ldm,harray+j*ly,ly*sizeof(PetscScalar));
1066: }
1067: MatDenseRestoreArray(H,&harray);
1068: MatDestroy(&H);
1069: }
1071: /* bottom part, Y1'*X */
1072: if (kx>0 && ly<ky) {
1073: MatCreateSeqDense(PETSC_COMM_SELF,ky,kx,NULL,&H);
1074: X->l = 0; X->k = kx;
1075: Y->l = ly; Y->k = ky;
1076: BVDot(X,Y,H);
1077: MatDenseGetArray(H,&harray);
1078: for (j=0;j<kx;j++) {
1079: PetscMemcpy(marray+j*ldm+ly,harray+j*ky+ly,(ky-ly)*sizeof(PetscScalar));
1080: }
1081: MatDenseRestoreArray(H,&harray);
1082: MatDestroy(&H);
1083: }
1084: X->l = lx; X->k = kx;
1085: Y->l = ly; Y->k = ky;
1086: return(0);
1087: }
1091: /*@
1092: BVMatProject - Computes the projection of a matrix onto a subspace.
1094: Collective on BV1096: Input Parameters:
1097: + X - basis vectors
1098: . A - (optional) matrix to be projected
1099: . Y - left basis vectors, can be equal to X
1100: - M - Mat object where the result must be placed
1102: Output Parameter:
1103: . M - the resulting matrix
1105: Notes:
1106: If A=NULL, then it is assumed that X already contains A*X.
1108: This operation is similar to BVDot(), with important differences.
1109: The goal is to compute the matrix resulting from the orthogonal projection
1110: of A onto the subspace spanned by the columns of X, M = X^H*A*X, or the
1111: oblique projection onto X along Y, M = Y^H*A*X.
1113: A difference with respect to BVDot() is that the standard inner product
1114: is always used, regardless of a non-standard inner product being specified
1115: with BVSetMatrix().
1117: On entry, M must be a sequential dense Mat with dimensions ky,kx at least,
1118: where ky (resp. kx) is the number of active columns of Y (resp. X).
1119: Another difference with respect to BVDot() is that all entries of M are
1120: computed except the leading ly,lx part, where ly (resp. lx) is the
1121: number of leading columns of Y (resp. X). Hence, the leading columns of
1122: X and Y participate in the computation, as opposed to BVDot().
1123: The leading part of M is assumed to be already available from previous
1124: computations.
1126: In the orthogonal projection case, Y=X, some computation can be saved if
1127: A is real symmetric (or complex Hermitian). In order to exploit this
1128: property, the symmetry flag of A must be set with MatSetOption().
1130: Level: intermediate
1132: .seealso: BVDot(), BVSetActiveColumns(), BVSetMatrix()
1133: @*/
1134: PetscErrorCode BVMatProject(BV X,Mat A,BV Y,Mat M)1135: {
1137: PetscBool match,set,flg,symm=PETSC_FALSE;
1138: PetscInt m,n;
1139: PetscScalar *marray;
1140: Mat Xmatrix,Ymatrix;
1141: PetscObjectId idx,idy;
1149: BVCheckSizes(X,1);
1150: if (A) {
1153: }
1155: BVCheckSizes(Y,3);
1158: PetscObjectTypeCompare((PetscObject)M,MATSEQDENSE,&match);
1159: if (!match) SETERRQ(PetscObjectComm((PetscObject)X),PETSC_ERR_SUP,"Matrix M must be of type seqdense");
1161: MatGetSize(M,&m,&n);
1162: if (m<Y->k) SETERRQ2(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_SIZ,"Matrix M has %D rows, should have at least %D",m,Y->k);
1163: if (n<X->k) SETERRQ2(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_SIZ,"Matrix M has %D columns, should have at least %D",n,X->k);
1164: if (X->n!=Y->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension X %D, Y %D",X->n,Y->n);
1166: PetscLogEventBegin(BV_MatProject,X,A,Y,0);
1167: /* temporarily set standard inner product */
1168: Xmatrix = X->matrix;
1169: Ymatrix = Y->matrix;
1170: X->matrix = Y->matrix = NULL;
1172: PetscObjectGetId((PetscObject)X,&idx);
1173: PetscObjectGetId((PetscObject)Y,&idy);
1174: if (!A && idx==idy) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot set X=Y if A=NULL");
1176: MatDenseGetArray(M,&marray);
1178: if (A && idx==idy) { /* check symmetry of M=X'AX */
1179: MatIsHermitianKnown(A,&set,&flg);
1180: symm = set? flg: PETSC_FALSE;
1181: }
1183: if (A) {
1184: if (X->vmm==BV_MATMULT_VECS) {
1185: /* perform computation column by column */
1186: BVMatProject_Vec(X,A,Y,marray,m,symm);
1187: } else {
1188: /* use BVMatMult, then BVDot */
1189: MatHasOperation(A,MATOP_MULT_TRANSPOSE,&flg);
1190: if (symm || (flg && X->l>=X->k/2 && Y->l>=Y->k/2)) {
1191: BVMatProject_MatMult_2(X,A,Y,marray,m,symm);
1192: } else {
1193: BVMatProject_MatMult(X,A,Y,marray,m);
1194: }
1195: }
1196: } else {
1197: /* use BVDot on subblocks */
1198: BVMatProject_Dot(X,Y,marray,m);
1199: }
1201: MatDenseRestoreArray(M,&marray);
1202: PetscLogEventEnd(BV_MatProject,X,A,Y,0);
1203: /* restore non-standard inner product */
1204: X->matrix = Xmatrix;
1205: Y->matrix = Ymatrix;
1206: return(0);
1207: }