LCOV - code coverage report
Current view: top level - sys/classes/bv/interface - bvops.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 340 436 78.0 %
Date: 2024-11-21 00:40:22 Functions: 17 21 81.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : /*
      11             :    BV operations, except those involving global communication
      12             : */
      13             : 
      14             : #include <slepc/private/bvimpl.h>      /*I "slepcbv.h" I*/
      15             : #include <slepcds.h>
      16             : 
      17             : /*@
      18             :    BVMult - Computes Y = beta*Y + alpha*X*Q.
      19             : 
      20             :    Logically Collective
      21             : 
      22             :    Input Parameters:
      23             : +  Y     - first basis vectors context (modified on output)
      24             : .  alpha - first scalar
      25             : .  beta  - second scalar
      26             : .  X     - second basis vectors context
      27             : -  Q     - (optional) sequential dense matrix
      28             : 
      29             :    Notes:
      30             :    X and Y must be different objects. The case X=Y can be addressed with
      31             :    BVMultInPlace().
      32             : 
      33             :    If matrix Q is NULL, then an AXPY operation Y = beta*Y + alpha*X is done
      34             :    (i.e. results as if Q = identity). If provided,
      35             :    the matrix Q must be a sequential dense Mat, with all entries equal on
      36             :    all processes (otherwise each process will compute a different update).
      37             :    The dimensions of Q must be at least m,n where m is the number of active
      38             :    columns of X and n is the number of active columns of Y.
      39             : 
      40             :    The leading columns of Y are not modified. Also, if X has leading
      41             :    columns specified, then these columns do not participate in the computation.
      42             :    Hence, only rows (resp. columns) of Q starting from lx (resp. ly) are used,
      43             :    where lx (resp. ly) is the number of leading columns of X (resp. Y).
      44             : 
      45             :    Level: intermediate
      46             : 
      47             : .seealso: BVMultVec(), BVMultColumn(), BVMultInPlace(), BVSetActiveColumns()
      48             : @*/
      49        6695 : PetscErrorCode BVMult(BV Y,PetscScalar alpha,PetscScalar beta,BV X,Mat Q)
      50             : {
      51        6695 :   PetscInt       m,n;
      52             : 
      53        6695 :   PetscFunctionBegin;
      54        6695 :   PetscValidHeaderSpecific(Y,BV_CLASSID,1);
      55       20085 :   PetscValidLogicalCollectiveScalar(Y,alpha,2);
      56       20085 :   PetscValidLogicalCollectiveScalar(Y,beta,3);
      57        6695 :   PetscValidHeaderSpecific(X,BV_CLASSID,4);
      58        6695 :   if (Q) PetscValidHeaderSpecific(Q,MAT_CLASSID,5);
      59        6695 :   PetscValidType(Y,1);
      60        6695 :   BVCheckSizes(Y,1);
      61        6695 :   BVCheckOp(Y,1,mult);
      62        6695 :   PetscValidType(X,4);
      63        6695 :   BVCheckSizes(X,4);
      64        6695 :   if (Q) PetscValidType(Q,5);
      65        6695 :   PetscCheckSameTypeAndComm(Y,1,X,4);
      66        6695 :   PetscCheck(X!=Y,PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_WRONG,"X and Y arguments must be different");
      67        6695 :   if (Q) {
      68        5183 :     SlepcMatCheckSeq(Q);
      69        5183 :     PetscCall(MatGetSize(Q,&m,&n));
      70        5183 :     PetscCheck(m>=X->k,PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_SIZ,"Mat argument has %" PetscInt_FMT " rows, should have at least %" PetscInt_FMT,m,X->k);
      71        5183 :     PetscCheck(n>=Y->k,PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_SIZ,"Mat argument has %" PetscInt_FMT " columns, should have at least %" PetscInt_FMT,n,Y->k);
      72             :   }
      73        6695 :   PetscCheck(X->n==Y->n,PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_INCOMP,"Mismatching local dimension X %" PetscInt_FMT ", Y %" PetscInt_FMT,X->n,Y->n);
      74             : 
      75        6695 :   PetscCall(PetscLogEventBegin(BV_Mult,X,Y,0,0));
      76        6695 :   PetscUseTypeMethod(Y,mult,alpha,beta,X,Q);
      77        6695 :   PetscCall(PetscLogEventEnd(BV_Mult,X,Y,0,0));
      78        6695 :   PetscCall(PetscObjectStateIncrease((PetscObject)Y));
      79        6695 :   PetscFunctionReturn(PETSC_SUCCESS);
      80             : }
      81             : 
      82             : /*@
      83             :    BVMultVec - Computes y = beta*y + alpha*X*q.
      84             : 
      85             :    Logically Collective
      86             : 
      87             :    Input Parameters:
      88             : +  X     - a basis vectors object
      89             : .  alpha - first scalar
      90             : .  beta  - second scalar
      91             : .  y     - a vector (modified on output)
      92             : -  q     - an array of scalars
      93             : 
      94             :    Notes:
      95             :    This operation is the analogue of BVMult() but with a BV and a Vec,
      96             :    instead of two BV. Note that arguments are listed in different order
      97             :    with respect to BVMult().
      98             : 
      99             :    If X has leading columns specified, then these columns do not participate
     100             :    in the computation.
     101             : 
     102             :    The length of array q must be equal to the number of active columns of X
     103             :    minus the number of leading columns, i.e. the first entry of q multiplies
     104             :    the first non-leading column.
     105             : 
     106             :    Level: intermediate
     107             : 
     108             : .seealso: BVMult(), BVMultColumn(), BVMultInPlace(), BVSetActiveColumns()
     109             : @*/
     110      466885 : PetscErrorCode BVMultVec(BV X,PetscScalar alpha,PetscScalar beta,Vec y,PetscScalar q[])
     111             : {
     112      466885 :   PetscInt       n,N;
     113             : 
     114      466885 :   PetscFunctionBegin;
     115      466885 :   PetscValidHeaderSpecific(X,BV_CLASSID,1);
     116     1400655 :   PetscValidLogicalCollectiveScalar(X,alpha,2);
     117     1400655 :   PetscValidLogicalCollectiveScalar(X,beta,3);
     118      466885 :   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
     119      466885 :   PetscAssertPointer(q,5);
     120      466885 :   PetscValidType(X,1);
     121      466885 :   BVCheckSizes(X,1);
     122      466885 :   BVCheckOp(X,1,multvec);
     123      466885 :   PetscValidType(y,4);
     124      466885 :   PetscCheckSameComm(X,1,y,4);
     125             : 
     126      466885 :   PetscCall(VecGetSize(y,&N));
     127      466885 :   PetscCall(VecGetLocalSize(y,&n));
     128      466885 :   PetscCheck(N==X->N && n==X->n,PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_INCOMP,"Vec sizes (global %" PetscInt_FMT ", local %" PetscInt_FMT ") do not match BV sizes (global %" PetscInt_FMT ", local %" PetscInt_FMT ")",N,n,X->N,X->n);
     129             : 
     130      466885 :   PetscCall(PetscLogEventBegin(BV_MultVec,X,y,0,0));
     131      466885 :   PetscUseTypeMethod(X,multvec,alpha,beta,y,q);
     132      466885 :   PetscCall(PetscLogEventEnd(BV_MultVec,X,y,0,0));
     133      466885 :   PetscFunctionReturn(PETSC_SUCCESS);
     134             : }
     135             : 
     136             : /*@
     137             :    BVMultColumn - Computes y = beta*y + alpha*X*q, where y is the j-th column
     138             :    of X.
     139             : 
     140             :    Logically Collective
     141             : 
     142             :    Input Parameters:
     143             : +  X     - a basis vectors object
     144             : .  alpha - first scalar
     145             : .  beta  - second scalar
     146             : .  j     - the column index
     147             : -  q     - an array of scalars
     148             : 
     149             :    Notes:
     150             :    This operation is equivalent to BVMultVec() but it uses column j of X
     151             :    rather than taking a Vec as an argument. The number of active columns of
     152             :    X is set to j before the computation, and restored afterwards.
     153             :    If X has leading columns specified, then these columns do not participate
     154             :    in the computation. Therefore, the length of array q must be equal to j
     155             :    minus the number of leading columns.
     156             : 
     157             :    Developer Notes:
     158             :    If q is NULL, then the coefficients are taken from position nc+l of the
     159             :    internal buffer vector, see BVGetBufferVec().
     160             : 
     161             :    Level: advanced
     162             : 
     163             : .seealso: BVMult(), BVMultVec(), BVMultInPlace(), BVSetActiveColumns()
     164             : @*/
     165      253970 : PetscErrorCode BVMultColumn(BV X,PetscScalar alpha,PetscScalar beta,PetscInt j,PetscScalar *q)
     166             : {
     167      253970 :   PetscInt       ksave;
     168      253970 :   Vec            y;
     169             : 
     170      253970 :   PetscFunctionBegin;
     171      253970 :   PetscValidHeaderSpecific(X,BV_CLASSID,1);
     172      761910 :   PetscValidLogicalCollectiveScalar(X,alpha,2);
     173      761910 :   PetscValidLogicalCollectiveScalar(X,beta,3);
     174      761910 :   PetscValidLogicalCollectiveInt(X,j,4);
     175      253970 :   PetscValidType(X,1);
     176      253970 :   BVCheckSizes(X,1);
     177             : 
     178      253970 :   PetscCheck(j>=0,PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_OUTOFRANGE,"Index j must be non-negative");
     179      253970 :   PetscCheck(j<X->m,PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_OUTOFRANGE,"Index j=%" PetscInt_FMT " but BV only has %" PetscInt_FMT " columns",j,X->m);
     180             : 
     181      253970 :   PetscCall(PetscLogEventBegin(BV_MultVec,X,0,0,0));
     182      253970 :   ksave = X->k;
     183      253970 :   X->k = j;
     184      253970 :   if (!q && !X->buffer) PetscCall(BVGetBufferVec(X,&X->buffer));
     185      253970 :   PetscCall(BVGetColumn(X,j,&y));
     186      253970 :   PetscUseTypeMethod(X,multvec,alpha,beta,y,q);
     187      253970 :   PetscCall(BVRestoreColumn(X,j,&y));
     188      253970 :   X->k = ksave;
     189      253970 :   PetscCall(PetscLogEventEnd(BV_MultVec,X,0,0,0));
     190      253970 :   PetscCall(PetscObjectStateIncrease((PetscObject)X));
     191      253970 :   PetscFunctionReturn(PETSC_SUCCESS);
     192             : }
     193             : 
     194             : /*@
     195             :    BVMultInPlace - Update a set of vectors as V(:,s:e-1) = V*Q(:,s:e-1).
     196             : 
     197             :    Logically Collective
     198             : 
     199             :    Input Parameters:
     200             : +  Q - a sequential dense matrix
     201             : .  s - first column of V to be overwritten
     202             : -  e - first column of V not to be overwritten
     203             : 
     204             :    Input/Output Parameter:
     205             : .  V - basis vectors
     206             : 
     207             :    Notes:
     208             :    The matrix Q must be a sequential dense Mat, with all entries equal on
     209             :    all processes (otherwise each process will compute a different update).
     210             : 
     211             :    This function computes V(:,s:e-1) = V*Q(:,s:e-1), that is, given a set of
     212             :    vectors V, columns from s to e-1 are overwritten with columns from s to
     213             :    e-1 of the matrix-matrix product V*Q. Only columns s to e-1 of Q are
     214             :    referenced.
     215             : 
     216             :    Level: intermediate
     217             : 
     218             : .seealso: BVMult(), BVMultVec(), BVMultInPlaceHermitianTranspose(), BVSetActiveColumns()
     219             : @*/
     220       25695 : PetscErrorCode BVMultInPlace(BV V,Mat Q,PetscInt s,PetscInt e)
     221             : {
     222       25695 :   PetscInt       m,n;
     223             : 
     224       25695 :   PetscFunctionBegin;
     225       25695 :   PetscValidHeaderSpecific(V,BV_CLASSID,1);
     226       25695 :   PetscValidHeaderSpecific(Q,MAT_CLASSID,2);
     227       77085 :   PetscValidLogicalCollectiveInt(V,s,3);
     228       77085 :   PetscValidLogicalCollectiveInt(V,e,4);
     229       25695 :   PetscValidType(V,1);
     230       25695 :   BVCheckSizes(V,1);
     231       25695 :   PetscValidType(Q,2);
     232       25695 :   SlepcMatCheckSeq(Q);
     233             : 
     234       25695 :   PetscCheck(s>=V->l && s<=V->m,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Argument s has wrong value %" PetscInt_FMT ", should be between %" PetscInt_FMT " and %" PetscInt_FMT,s,V->l,V->m);
     235       25695 :   PetscCheck(e>=V->l && e<=V->m,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Argument e has wrong value %" PetscInt_FMT ", should be between %" PetscInt_FMT " and %" PetscInt_FMT,e,V->l,V->m);
     236       25695 :   PetscCall(MatGetSize(Q,&m,&n));
     237       25695 :   PetscCheck(m>=V->k,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Mat argument has %" PetscInt_FMT " rows, should have at least %" PetscInt_FMT,m,V->k);
     238       25695 :   PetscCheck(e<=n,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Mat argument only has %" PetscInt_FMT " columns, the requested value of e is larger: %" PetscInt_FMT,n,e);
     239             : 
     240       25695 :   PetscCall(PetscLogEventBegin(BV_MultInPlace,V,Q,0,0));
     241       25695 :   PetscUseTypeMethod(V,multinplace,Q,s,e);
     242       25695 :   PetscCall(PetscLogEventEnd(BV_MultInPlace,V,Q,0,0));
     243       25695 :   PetscCall(PetscObjectStateIncrease((PetscObject)V));
     244       25695 :   PetscFunctionReturn(PETSC_SUCCESS);
     245             : }
     246             : 
     247             : /*@
     248             :    BVMultInPlaceHermitianTranspose - Update a set of vectors as V(:,s:e-1) = V*Q'(:,s:e-1).
     249             : 
     250             :    Logically Collective
     251             : 
     252             :    Input Parameters:
     253             : +  Q - a sequential dense matrix
     254             : .  s - first column of V to be overwritten
     255             : -  e - first column of V not to be overwritten
     256             : 
     257             :    Input/Output Parameter:
     258             : .  V - basis vectors
     259             : 
     260             :    Notes:
     261             :    This is a variant of BVMultInPlace() where the conjugate transpose
     262             :    of Q is used.
     263             : 
     264             :    Level: intermediate
     265             : 
     266             : .seealso: BVMultInPlace()
     267             : @*/
     268           4 : PetscErrorCode BVMultInPlaceHermitianTranspose(BV V,Mat Q,PetscInt s,PetscInt e)
     269             : {
     270           4 :   PetscInt       m,n;
     271             : 
     272           4 :   PetscFunctionBegin;
     273           4 :   PetscValidHeaderSpecific(V,BV_CLASSID,1);
     274           4 :   PetscValidHeaderSpecific(Q,MAT_CLASSID,2);
     275          12 :   PetscValidLogicalCollectiveInt(V,s,3);
     276          12 :   PetscValidLogicalCollectiveInt(V,e,4);
     277           4 :   PetscValidType(V,1);
     278           4 :   BVCheckSizes(V,1);
     279           4 :   PetscValidType(Q,2);
     280           4 :   SlepcMatCheckSeq(Q);
     281             : 
     282           4 :   PetscCheck(s>=V->l && s<=V->m,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Argument s has wrong value %" PetscInt_FMT ", should be between %" PetscInt_FMT " and %" PetscInt_FMT,s,V->l,V->m);
     283           4 :   PetscCheck(e>=V->l && e<=V->m,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Argument e has wrong value %" PetscInt_FMT ", should be between %" PetscInt_FMT " and %" PetscInt_FMT,e,V->l,V->m);
     284           4 :   PetscCall(MatGetSize(Q,&m,&n));
     285           4 :   PetscCheck(n>=V->k,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Mat argument has %" PetscInt_FMT " columns, should have at least %" PetscInt_FMT,n,V->k);
     286           4 :   PetscCheck(e<=m,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Mat argument only has %" PetscInt_FMT " rows, the requested value of e is larger: %" PetscInt_FMT,m,e);
     287             : 
     288           4 :   PetscCall(PetscLogEventBegin(BV_MultInPlace,V,Q,0,0));
     289           4 :   PetscUseTypeMethod(V,multinplacetrans,Q,s,e);
     290           4 :   PetscCall(PetscLogEventEnd(BV_MultInPlace,V,Q,0,0));
     291           4 :   PetscCall(PetscObjectStateIncrease((PetscObject)V));
     292           4 :   PetscFunctionReturn(PETSC_SUCCESS);
     293             : }
     294             : 
     295             : /*@
     296             :    BVScale - Multiply the BV entries by a scalar value.
     297             : 
     298             :    Logically Collective
     299             : 
     300             :    Input Parameters:
     301             : +  bv    - basis vectors
     302             : -  alpha - scaling factor
     303             : 
     304             :    Note:
     305             :    All active columns (except the leading ones) are scaled.
     306             : 
     307             :    Level: intermediate
     308             : 
     309             : .seealso: BVScaleColumn(), BVSetActiveColumns()
     310             : @*/
     311        1680 : PetscErrorCode BVScale(BV bv,PetscScalar alpha)
     312             : {
     313        1680 :   PetscFunctionBegin;
     314        1680 :   PetscValidHeaderSpecific(bv,BV_CLASSID,1);
     315        5040 :   PetscValidLogicalCollectiveScalar(bv,alpha,2);
     316        1680 :   PetscValidType(bv,1);
     317        1680 :   BVCheckSizes(bv,1);
     318        1680 :   if (alpha == (PetscScalar)1.0) PetscFunctionReturn(PETSC_SUCCESS);
     319             : 
     320         411 :   PetscCall(PetscLogEventBegin(BV_Scale,bv,0,0,0));
     321         411 :   PetscUseTypeMethod(bv,scale,-1,alpha);
     322         411 :   PetscCall(PetscLogEventEnd(BV_Scale,bv,0,0,0));
     323         411 :   PetscCall(PetscObjectStateIncrease((PetscObject)bv));
     324         411 :   PetscFunctionReturn(PETSC_SUCCESS);
     325             : }
     326             : 
     327             : /*@
     328             :    BVScaleColumn - Scale one column of a BV.
     329             : 
     330             :    Logically Collective
     331             : 
     332             :    Input Parameters:
     333             : +  bv    - basis vectors
     334             : .  j     - column number to be scaled
     335             : -  alpha - scaling factor
     336             : 
     337             :    Level: intermediate
     338             : 
     339             : .seealso: BVScale(), BVSetActiveColumns()
     340             : @*/
     341       96315 : PetscErrorCode BVScaleColumn(BV bv,PetscInt j,PetscScalar alpha)
     342             : {
     343       96315 :   PetscFunctionBegin;
     344       96315 :   PetscValidHeaderSpecific(bv,BV_CLASSID,1);
     345      288945 :   PetscValidLogicalCollectiveInt(bv,j,2);
     346      288945 :   PetscValidLogicalCollectiveScalar(bv,alpha,3);
     347       96315 :   PetscValidType(bv,1);
     348       96315 :   BVCheckSizes(bv,1);
     349             : 
     350       96315 :   PetscCheck(j>=0 && j<bv->m,PetscObjectComm((PetscObject)bv),PETSC_ERR_ARG_OUTOFRANGE,"Argument j has wrong value %" PetscInt_FMT ", the number of columns is %" PetscInt_FMT,j,bv->m);
     351       96315 :   if (alpha == (PetscScalar)1.0) PetscFunctionReturn(PETSC_SUCCESS);
     352             : 
     353       95041 :   PetscCall(PetscLogEventBegin(BV_Scale,bv,0,0,0));
     354       95041 :   PetscUseTypeMethod(bv,scale,j,alpha);
     355       95041 :   PetscCall(PetscLogEventEnd(BV_Scale,bv,0,0,0));
     356       95041 :   PetscCall(PetscObjectStateIncrease((PetscObject)bv));
     357       95041 :   PetscFunctionReturn(PETSC_SUCCESS);
     358             : }
     359             : 
     360        2825 : static inline PetscErrorCode BVSetRandomColumn_Private(BV bv,PetscInt k)
     361             : {
     362        2825 :   PetscInt       i,low,high;
     363        2825 :   PetscScalar    *px,t;
     364        2825 :   Vec            x;
     365             : 
     366        2825 :   PetscFunctionBegin;
     367        2825 :   PetscCall(BVGetColumn(bv,k,&x));
     368        2825 :   if (bv->rrandom) {  /* generate the same vector irrespective of number of processes */
     369           0 :     PetscCall(VecGetOwnershipRange(x,&low,&high));
     370           0 :     PetscCall(VecGetArray(x,&px));
     371           0 :     for (i=0;i<bv->N;i++) {
     372           0 :       PetscCall(PetscRandomGetValue(bv->rand,&t));
     373           0 :       if (i>=low && i<high) px[i-low] = t;
     374             :     }
     375           0 :     PetscCall(VecRestoreArray(x,&px));
     376        2825 :   } else PetscCall(VecSetRandom(x,bv->rand));
     377        2825 :   PetscCall(BVRestoreColumn(bv,k,&x));
     378        2825 :   PetscFunctionReturn(PETSC_SUCCESS);
     379             : }
     380             : 
     381         106 : static inline PetscErrorCode BVSetRandomNormalColumn_Private(BV bv,PetscInt k,Vec w1,Vec w2)
     382             : {
     383         106 :   PetscInt       i,low,high;
     384         106 :   PetscScalar    *px,s,t;
     385         106 :   Vec            x;
     386             : 
     387         106 :   PetscFunctionBegin;
     388         106 :   PetscCall(BVGetColumn(bv,k,&x));
     389         106 :   if (bv->rrandom) {  /* generate the same vector irrespective of number of processes */
     390           0 :     PetscCall(VecGetOwnershipRange(x,&low,&high));
     391           0 :     PetscCall(VecGetArray(x,&px));
     392           0 :     for (i=0;i<bv->N;i++) {
     393           0 :       PetscCall(PetscRandomGetValue(bv->rand,&s));
     394           0 :       PetscCall(PetscRandomGetValue(bv->rand,&t));
     395           0 :       if (i>=low && i<high) {
     396             : #if defined(PETSC_USE_COMPLEX)
     397           0 :         px[i-low] = PetscCMPLX(PetscSqrtReal(-2.0*PetscLogReal(PetscRealPart(s)))*PetscCosReal(2.0*PETSC_PI*PetscRealPart(t)),PetscSqrtReal(-2.0*PetscLogReal(PetscImaginaryPart(s)))*PetscCosReal(2.0*PETSC_PI*PetscImaginaryPart(t)));
     398             : #else
     399             :         px[i-low] = PetscSqrtReal(-2.0*PetscLogReal(s))*PetscCosReal(2.0*PETSC_PI*t);
     400             : #endif
     401             :       }
     402             :     }
     403           0 :     PetscCall(VecRestoreArray(x,&px));
     404         106 :   } else PetscCall(VecSetRandomNormal(x,bv->rand,w1,w2));
     405         106 :   PetscCall(BVRestoreColumn(bv,k,&x));
     406         106 :   PetscFunctionReturn(PETSC_SUCCESS);
     407             : }
     408             : 
     409        1094 : static inline PetscErrorCode BVSetRandomSignColumn_Private(BV bv,PetscInt k)
     410             : {
     411        1094 :   PetscInt       i,low,high;
     412        1094 :   PetscScalar    *px,t;
     413        1094 :   Vec            x;
     414             : 
     415        1094 :   PetscFunctionBegin;
     416        1094 :   PetscCall(BVGetColumn(bv,k,&x));
     417        1094 :   PetscCall(VecGetOwnershipRange(x,&low,&high));
     418        1094 :   if (bv->rrandom) {  /* generate the same vector irrespective of number of processes */
     419           0 :     PetscCall(VecGetArray(x,&px));
     420           0 :     for (i=0;i<bv->N;i++) {
     421           0 :       PetscCall(PetscRandomGetValue(bv->rand,&t));
     422           0 :       if (i>=low && i<high) px[i-low] = (PetscRealPart(t)<0.5)? -1.0: 1.0;
     423             :     }
     424           0 :     PetscCall(VecRestoreArray(x,&px));
     425             :   } else {
     426        1094 :     PetscCall(VecSetRandom(x,bv->rand));
     427        1094 :     PetscCall(VecGetArray(x,&px));
     428      231482 :     for (i=low;i<high;i++) {
     429      343402 :       px[i-low] = (PetscRealPart(px[i-low])<0.5)? -1.0: 1.0;
     430             :     }
     431        1094 :     PetscCall(VecRestoreArray(x,&px));
     432             :   }
     433        1094 :   PetscCall(BVRestoreColumn(bv,k,&x));
     434        1094 :   PetscFunctionReturn(PETSC_SUCCESS);
     435             : }
     436             : 
     437             : /*@
     438             :    BVSetRandom - Set the columns of a BV to random numbers.
     439             : 
     440             :    Logically Collective
     441             : 
     442             :    Input Parameters:
     443             : .  bv - basis vectors
     444             : 
     445             :    Note:
     446             :    All active columns (except the leading ones) are modified.
     447             : 
     448             :    Level: advanced
     449             : 
     450             : .seealso: BVSetRandomContext(), BVSetRandomColumn(), BVSetRandomNormal(), BVSetRandomSign(), BVSetRandomCond(), BVSetActiveColumns()
     451             : @*/
     452           0 : PetscErrorCode BVSetRandom(BV bv)
     453             : {
     454           0 :   PetscInt       k;
     455             : 
     456           0 :   PetscFunctionBegin;
     457           0 :   PetscValidHeaderSpecific(bv,BV_CLASSID,1);
     458           0 :   PetscValidType(bv,1);
     459           0 :   BVCheckSizes(bv,1);
     460             : 
     461           0 :   PetscCall(BVGetRandomContext(bv,&bv->rand));
     462           0 :   PetscCall(PetscLogEventBegin(BV_SetRandom,bv,0,0,0));
     463           0 :   for (k=bv->l;k<bv->k;k++) PetscCall(BVSetRandomColumn_Private(bv,k));
     464           0 :   PetscCall(PetscLogEventEnd(BV_SetRandom,bv,0,0,0));
     465           0 :   PetscCall(PetscObjectStateIncrease((PetscObject)bv));
     466           0 :   PetscFunctionReturn(PETSC_SUCCESS);
     467             : }
     468             : 
     469             : /*@
     470             :    BVSetRandomColumn - Set one column of a BV to random numbers.
     471             : 
     472             :    Logically Collective
     473             : 
     474             :    Input Parameters:
     475             : +  bv - basis vectors
     476             : -  j  - column number to be set
     477             : 
     478             :    Level: advanced
     479             : 
     480             : .seealso: BVSetRandomContext(), BVSetRandom(), BVSetRandomNormal(), BVSetRandomCond()
     481             : @*/
     482        2793 : PetscErrorCode BVSetRandomColumn(BV bv,PetscInt j)
     483             : {
     484        2793 :   PetscFunctionBegin;
     485        2793 :   PetscValidHeaderSpecific(bv,BV_CLASSID,1);
     486        8379 :   PetscValidLogicalCollectiveInt(bv,j,2);
     487        2793 :   PetscValidType(bv,1);
     488        2793 :   BVCheckSizes(bv,1);
     489        2793 :   PetscCheck(j>=0 && j<bv->m,PetscObjectComm((PetscObject)bv),PETSC_ERR_ARG_OUTOFRANGE,"Argument j has wrong value %" PetscInt_FMT ", the number of columns is %" PetscInt_FMT,j,bv->m);
     490             : 
     491        2793 :   PetscCall(BVGetRandomContext(bv,&bv->rand));
     492        2793 :   PetscCall(PetscLogEventBegin(BV_SetRandom,bv,0,0,0));
     493        2793 :   PetscCall(BVSetRandomColumn_Private(bv,j));
     494        2793 :   PetscCall(PetscLogEventEnd(BV_SetRandom,bv,0,0,0));
     495        2793 :   PetscCall(PetscObjectStateIncrease((PetscObject)bv));
     496        2793 :   PetscFunctionReturn(PETSC_SUCCESS);
     497             : }
     498             : 
     499             : /*@
     500             :    BVSetRandomNormal - Set the columns of a BV to random numbers with a normal
     501             :    distribution.
     502             : 
     503             :    Logically Collective
     504             : 
     505             :    Input Parameter:
     506             : .  bv - basis vectors
     507             : 
     508             :    Notes:
     509             :    All active columns (except the leading ones) are modified.
     510             : 
     511             :    Other functions such as BVSetRandom(), BVSetRandomColumn(), and BVSetRandomCond()
     512             :    produce random numbers with a uniform distribution. This function returns values
     513             :    that fit a normal distribution (Gaussian).
     514             : 
     515             :    Developer Notes:
     516             :    The current implementation obtains each of the columns by applying the Box-Muller
     517             :    transform on two random vectors with uniformly distributed entries.
     518             : 
     519             :    Level: advanced
     520             : 
     521             : .seealso: BVSetRandomContext(), BVSetRandom(), BVSetRandomColumn(), BVSetRandomCond(), BVSetActiveColumns()
     522             : @*/
     523          10 : PetscErrorCode BVSetRandomNormal(BV bv)
     524             : {
     525          10 :   PetscInt       k;
     526          10 :   Vec            w1=NULL,w2=NULL;
     527             : 
     528          10 :   PetscFunctionBegin;
     529          10 :   PetscValidHeaderSpecific(bv,BV_CLASSID,1);
     530          10 :   PetscValidType(bv,1);
     531          10 :   BVCheckSizes(bv,1);
     532             : 
     533          10 :   PetscCall(BVGetRandomContext(bv,&bv->rand));
     534          10 :   if (!bv->rrandom) {
     535          10 :     PetscCall(BVCreateVec(bv,&w1));
     536          10 :     PetscCall(BVCreateVec(bv,&w2));
     537             :   }
     538          10 :   PetscCall(PetscLogEventBegin(BV_SetRandom,bv,0,0,0));
     539         116 :   for (k=bv->l;k<bv->k;k++) PetscCall(BVSetRandomNormalColumn_Private(bv,k,w1,w2));
     540          10 :   PetscCall(PetscLogEventEnd(BV_SetRandom,bv,0,0,0));
     541          10 :   if (!bv->rrandom) {
     542          10 :     PetscCall(VecDestroy(&w1));
     543          10 :     PetscCall(VecDestroy(&w2));
     544             :   }
     545          10 :   PetscCall(PetscObjectStateIncrease((PetscObject)bv));
     546          10 :   PetscFunctionReturn(PETSC_SUCCESS);
     547             : }
     548             : 
     549             : /*@
     550             :    BVSetRandomSign - Set the entries of a BV to values 1 or -1 with equal
     551             :    probability.
     552             : 
     553             :    Logically Collective
     554             : 
     555             :    Input Parameter:
     556             : .  bv - basis vectors
     557             : 
     558             :    Notes:
     559             :    All active columns (except the leading ones) are modified.
     560             : 
     561             :    This function is used, e.g., in contour integral methods when estimating
     562             :    the number of eigenvalues enclosed by the contour via an unbiased
     563             :    estimator of tr(f(A)) [Bai et al., JCAM 1996].
     564             : 
     565             :    Developer Notes:
     566             :    The current implementation obtains random numbers and then replaces them
     567             :    with -1 or 1 depending on the value being less than 0.5 or not.
     568             : 
     569             :    Level: advanced
     570             : 
     571             : .seealso: BVSetRandomContext(), BVSetRandom(), BVSetRandomColumn(), BVSetActiveColumns()
     572             : @*/
     573          93 : PetscErrorCode BVSetRandomSign(BV bv)
     574             : {
     575          93 :   PetscScalar    low,high;
     576          93 :   PetscInt       k;
     577             : 
     578          93 :   PetscFunctionBegin;
     579          93 :   PetscValidHeaderSpecific(bv,BV_CLASSID,1);
     580          93 :   PetscValidType(bv,1);
     581          93 :   BVCheckSizes(bv,1);
     582             : 
     583          93 :   PetscCall(BVGetRandomContext(bv,&bv->rand));
     584          93 :   PetscCall(PetscRandomGetInterval(bv->rand,&low,&high));
     585          93 :   PetscCheck(PetscRealPart(low)==0.0 && PetscRealPart(high)==1.0,PetscObjectComm((PetscObject)bv),PETSC_ERR_ARG_WRONGSTATE,"The PetscRandom object in the BV must have interval [0,1]");
     586          93 :   PetscCall(PetscLogEventBegin(BV_SetRandom,bv,0,0,0));
     587        1187 :   for (k=bv->l;k<bv->k;k++) PetscCall(BVSetRandomSignColumn_Private(bv,k));
     588          93 :   PetscCall(PetscLogEventEnd(BV_SetRandom,bv,0,0,0));
     589          93 :   PetscCall(PetscObjectStateIncrease((PetscObject)bv));
     590          93 :   PetscFunctionReturn(PETSC_SUCCESS);
     591             : }
     592             : 
     593             : /*@
     594             :    BVSetRandomCond - Set the columns of a BV to random numbers, in a way that
     595             :    the generated matrix has a given condition number.
     596             : 
     597             :    Logically Collective
     598             : 
     599             :    Input Parameters:
     600             : +  bv    - basis vectors
     601             : -  condn - condition number
     602             : 
     603             :    Note:
     604             :    All active columns (except the leading ones) are modified.
     605             : 
     606             :    Level: advanced
     607             : 
     608             : .seealso: BVSetRandomContext(), BVSetRandom(), BVSetRandomColumn(), BVSetRandomNormal(), BVSetActiveColumns()
     609             : @*/
     610           4 : PetscErrorCode BVSetRandomCond(BV bv,PetscReal condn)
     611             : {
     612           4 :   PetscInt       k,i;
     613           4 :   PetscScalar    *eig,*d;
     614           4 :   DS             ds;
     615           4 :   Mat            A,X,Xt,M,G;
     616             : 
     617           4 :   PetscFunctionBegin;
     618           4 :   PetscValidHeaderSpecific(bv,BV_CLASSID,1);
     619           4 :   PetscValidType(bv,1);
     620           4 :   BVCheckSizes(bv,1);
     621             : 
     622           4 :   PetscCall(BVGetRandomContext(bv,&bv->rand));
     623           4 :   PetscCall(PetscLogEventBegin(BV_SetRandom,bv,0,0,0));
     624             :   /* B = rand(n,k) */
     625          36 :   for (k=bv->l;k<bv->k;k++) PetscCall(BVSetRandomColumn_Private(bv,k));
     626           4 :   PetscCall(DSCreate(PetscObjectComm((PetscObject)bv),&ds));
     627           4 :   PetscCall(DSSetType(ds,DSHEP));
     628           4 :   PetscCall(DSAllocate(ds,bv->m));
     629           4 :   PetscCall(DSSetDimensions(ds,bv->k,bv->l,bv->k));
     630             :   /* [V,S] = eig(B'*B) */
     631           4 :   PetscCall(DSGetMat(ds,DS_MAT_A,&A));
     632           4 :   PetscCall(BVDot(bv,bv,A));
     633           4 :   PetscCall(DSRestoreMat(ds,DS_MAT_A,&A));
     634           4 :   PetscCall(PetscMalloc1(bv->m,&eig));
     635           4 :   PetscCall(DSSolve(ds,eig,NULL));
     636           4 :   PetscCall(DSSynchronize(ds,eig,NULL));
     637           4 :   PetscCall(DSVectors(ds,DS_MAT_X,NULL,NULL));
     638             :   /* M = diag(linspace(1/condn,1,n)./sqrt(diag(S)))' */
     639           4 :   PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,bv->k,bv->k,NULL,&M));
     640           4 :   PetscCall(MatZeroEntries(M));
     641           4 :   PetscCall(MatDenseGetArray(M,&d));
     642          36 :   for (i=0;i<bv->k;i++) d[i+i*bv->m] = (1.0/condn+(1.0-1.0/condn)/(bv->k-1)*i)/PetscSqrtScalar(eig[i]);
     643           4 :   PetscCall(MatDenseRestoreArray(M,&d));
     644             :   /* G = X*M*X' */
     645           4 :   PetscCall(DSGetMat(ds,DS_MAT_X,&X));
     646           4 :   PetscCall(MatTranspose(X,MAT_INITIAL_MATRIX,&Xt));
     647           4 :   PetscCall(MatProductCreate(Xt,M,NULL,&G));
     648           4 :   PetscCall(MatProductSetType(G,MATPRODUCT_PtAP));
     649           4 :   PetscCall(MatProductSetFromOptions(G));
     650           4 :   PetscCall(MatProductSymbolic(G));
     651           4 :   PetscCall(MatProductNumeric(G));
     652           4 :   PetscCall(MatProductClear(G));
     653           4 :   PetscCall(DSRestoreMat(ds,DS_MAT_X,&X));
     654           4 :   PetscCall(MatDestroy(&Xt));
     655           4 :   PetscCall(MatDestroy(&M));
     656             :   /* B = B*G */
     657           4 :   PetscCall(BVMultInPlace(bv,G,bv->l,bv->k));
     658           4 :   PetscCall(MatDestroy(&G));
     659           4 :   PetscCall(PetscFree(eig));
     660           4 :   PetscCall(DSDestroy(&ds));
     661           4 :   PetscCall(PetscLogEventEnd(BV_SetRandom,bv,0,0,0));
     662           4 :   PetscCall(PetscObjectStateIncrease((PetscObject)bv));
     663           4 :   PetscFunctionReturn(PETSC_SUCCESS);
     664             : }
     665             : 
     666             : /*@
     667             :    BVMatMult - Computes the matrix-vector product for each column, Y=A*V.
     668             : 
     669             :    Neighbor-wise Collective
     670             : 
     671             :    Input Parameters:
     672             : +  V - basis vectors context
     673             : -  A - the matrix
     674             : 
     675             :    Output Parameter:
     676             : .  Y - the result
     677             : 
     678             :    Notes:
     679             :    Both V and Y must be distributed in the same manner. Only active columns
     680             :    (excluding the leading ones) are processed.
     681             :    In the result Y, columns are overwritten starting from the leading ones.
     682             :    The number of active columns in V and Y should match, although they need
     683             :    not be the same columns.
     684             : 
     685             :    It is possible to choose whether the computation is done column by column
     686             :    or as a Mat-Mat product, see BVSetMatMultMethod().
     687             : 
     688             :    Level: beginner
     689             : 
     690             : .seealso: BVCopy(), BVSetActiveColumns(), BVMatMultColumn(), BVMatMultTranspose(), BVMatMultHermitianTranspose(), BVSetMatMultMethod()
     691             : @*/
     692       11075 : PetscErrorCode BVMatMult(BV V,Mat A,BV Y)
     693             : {
     694       11075 :   PetscInt       M,N,m,n;
     695             : 
     696       11075 :   PetscFunctionBegin;
     697       11075 :   PetscValidHeaderSpecific(V,BV_CLASSID,1);
     698       11075 :   PetscValidType(V,1);
     699       11075 :   BVCheckSizes(V,1);
     700       11075 :   BVCheckOp(V,1,matmult);
     701       11075 :   PetscValidHeaderSpecific(A,MAT_CLASSID,2);
     702       11075 :   PetscValidType(A,2);
     703       11075 :   PetscValidHeaderSpecific(Y,BV_CLASSID,3);
     704       11075 :   PetscValidType(Y,3);
     705       11075 :   BVCheckSizes(Y,3);
     706       11075 :   PetscCheckSameComm(V,1,A,2);
     707       11075 :   PetscCheckSameTypeAndComm(V,1,Y,3);
     708             : 
     709       11075 :   PetscCall(MatGetSize(A,&M,&N));
     710       11075 :   PetscCall(MatGetLocalSize(A,&m,&n));
     711       11075 :   PetscCheck(M==Y->N,PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_INCOMP,"Mismatching row dimension A %" PetscInt_FMT ", Y %" PetscInt_FMT,M,Y->N);
     712       11075 :   PetscCheck(m==Y->n,PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_INCOMP,"Mismatching local row dimension A %" PetscInt_FMT ", Y %" PetscInt_FMT,m,Y->n);
     713       11075 :   PetscCheck(N==V->N,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_INCOMP,"Mismatching column dimension A %" PetscInt_FMT ", V %" PetscInt_FMT,N,V->N);
     714       11075 :   PetscCheck(n==V->n,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_INCOMP,"Mismatching local column dimension A %" PetscInt_FMT ", V %" PetscInt_FMT,n,V->n);
     715       11075 :   PetscCheck(V->k-V->l==Y->k-Y->l,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Y has %" PetscInt_FMT " active columns, should match %" PetscInt_FMT " active columns in V",Y->k-Y->l,V->k-V->l);
     716             : 
     717       11075 :   PetscCall(PetscLogEventBegin(BV_MatMult,V,A,Y,0));
     718       11075 :   PetscUseTypeMethod(V,matmult,A,Y);
     719       11075 :   PetscCall(PetscLogEventEnd(BV_MatMult,V,A,Y,0));
     720       11075 :   PetscCall(PetscObjectStateIncrease((PetscObject)Y));
     721       11075 :   PetscFunctionReturn(PETSC_SUCCESS);
     722             : }
     723             : 
     724             : /*@
     725             :    BVMatMultTranspose - Computes the matrix-vector product with the transpose
     726             :    of a matrix for each column, Y=A^T*V.
     727             : 
     728             :    Neighbor-wise Collective
     729             : 
     730             :    Input Parameters:
     731             : +  V - basis vectors context
     732             : -  A - the matrix
     733             : 
     734             :    Output Parameter:
     735             : .  Y - the result
     736             : 
     737             :    Notes:
     738             :    Both V and Y must be distributed in the same manner. Only active columns
     739             :    (excluding the leading ones) are processed.
     740             :    In the result Y, columns are overwritten starting from the leading ones.
     741             :    The number of active columns in V and Y should match, although they need
     742             :    not be the same columns.
     743             : 
     744             :    Currently implemented via MatCreateTranspose().
     745             : 
     746             :    Level: beginner
     747             : 
     748             : .seealso: BVMatMult(), BVMatMultHermitianTranspose()
     749             : @*/
     750           0 : PetscErrorCode BVMatMultTranspose(BV V,Mat A,BV Y)
     751             : {
     752           0 :   PetscInt       M,N,m,n;
     753           0 :   Mat            AT;
     754             : 
     755           0 :   PetscFunctionBegin;
     756           0 :   PetscValidHeaderSpecific(V,BV_CLASSID,1);
     757           0 :   PetscValidType(V,1);
     758           0 :   BVCheckSizes(V,1);
     759           0 :   PetscValidHeaderSpecific(A,MAT_CLASSID,2);
     760           0 :   PetscValidType(A,2);
     761           0 :   PetscValidHeaderSpecific(Y,BV_CLASSID,3);
     762           0 :   PetscValidType(Y,3);
     763           0 :   BVCheckSizes(Y,3);
     764           0 :   PetscCheckSameComm(V,1,A,2);
     765           0 :   PetscCheckSameTypeAndComm(V,1,Y,3);
     766             : 
     767           0 :   PetscCall(MatGetSize(A,&M,&N));
     768           0 :   PetscCall(MatGetLocalSize(A,&m,&n));
     769           0 :   PetscCheck(M==V->N,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_INCOMP,"Mismatching row dimension A %" PetscInt_FMT ", V %" PetscInt_FMT,M,V->N);
     770           0 :   PetscCheck(m==V->n,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_INCOMP,"Mismatching local row dimension A %" PetscInt_FMT ", V %" PetscInt_FMT,m,V->n);
     771           0 :   PetscCheck(N==Y->N,PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_INCOMP,"Mismatching column dimension A %" PetscInt_FMT ", Y %" PetscInt_FMT,N,Y->N);
     772           0 :   PetscCheck(n==Y->n,PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_INCOMP,"Mismatching local column dimension A %" PetscInt_FMT ", Y %" PetscInt_FMT,n,Y->n);
     773           0 :   PetscCheck(V->k-V->l==Y->k-Y->l,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Y has %" PetscInt_FMT " active columns, should match %" PetscInt_FMT " active columns in V",Y->k-Y->l,V->k-V->l);
     774             : 
     775           0 :   PetscCall(MatCreateTranspose(A,&AT));
     776           0 :   PetscCall(BVMatMult(V,AT,Y));
     777           0 :   PetscCall(MatDestroy(&AT));
     778           0 :   PetscFunctionReturn(PETSC_SUCCESS);
     779             : }
     780             : 
     781             : /*@
     782             :    BVMatMultHermitianTranspose - Computes the matrix-vector product with the
     783             :    conjugate transpose of a matrix for each column, Y=A^H*V.
     784             : 
     785             :    Neighbor-wise Collective
     786             : 
     787             :    Input Parameters:
     788             : +  V - basis vectors context
     789             : -  A - the matrix
     790             : 
     791             :    Output Parameter:
     792             : .  Y - the result
     793             : 
     794             :    Note:
     795             :    Both V and Y must be distributed in the same manner. Only active columns
     796             :    (excluding the leading ones) are processed.
     797             :    In the result Y, columns are overwritten starting from the leading ones.
     798             :    The number of active columns in V and Y should match, although they need
     799             :    not be the same columns.
     800             : 
     801             :    Currently implemented via MatCreateHermitianTranspose().
     802             : 
     803             :    Level: beginner
     804             : 
     805             : .seealso: BVMatMult(), BVMatMultTranspose()
     806             : @*/
     807          91 : PetscErrorCode BVMatMultHermitianTranspose(BV V,Mat A,BV Y)
     808             : {
     809          91 :   PetscInt       j,M,N,m,n;
     810          91 :   Vec            v,y;
     811             : 
     812          91 :   PetscFunctionBegin;
     813          91 :   PetscValidHeaderSpecific(V,BV_CLASSID,1);
     814          91 :   PetscValidType(V,1);
     815          91 :   BVCheckSizes(V,1);
     816          91 :   PetscValidHeaderSpecific(A,MAT_CLASSID,2);
     817          91 :   PetscValidType(A,2);
     818          91 :   PetscValidHeaderSpecific(Y,BV_CLASSID,3);
     819          91 :   PetscValidType(Y,3);
     820          91 :   BVCheckSizes(Y,3);
     821          91 :   PetscCheckSameComm(V,1,A,2);
     822          91 :   PetscCheckSameTypeAndComm(V,1,Y,3);
     823             : 
     824          91 :   PetscCall(MatGetSize(A,&M,&N));
     825          91 :   PetscCall(MatGetLocalSize(A,&m,&n));
     826          91 :   PetscCheck(M==V->N,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_INCOMP,"Mismatching row dimension A %" PetscInt_FMT ", V %" PetscInt_FMT,M,V->N);
     827          91 :   PetscCheck(m==V->n,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_INCOMP,"Mismatching local row dimension A %" PetscInt_FMT ", V %" PetscInt_FMT,m,V->n);
     828          91 :   PetscCheck(N==Y->N,PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_INCOMP,"Mismatching column dimension A %" PetscInt_FMT ", Y %" PetscInt_FMT,N,Y->N);
     829          91 :   PetscCheck(n==Y->n,PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_INCOMP,"Mismatching local column dimension A %" PetscInt_FMT ", Y %" PetscInt_FMT,n,Y->n);
     830          91 :   PetscCheck(V->k-V->l==Y->k-Y->l,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_SIZ,"Y has %" PetscInt_FMT " active columns, should match %" PetscInt_FMT " active columns in V",Y->k-Y->l,V->k-V->l);
     831             : 
     832             :   /* TODO: recover this code if PETSc ever gets MATPRODUCT_AhB done
     833             :   PetscCall(MatCreateHermitianTranspose(A,&AH));
     834             :   PetscCall(BVMatMult(V,AH,Y));
     835             :   PetscCall(MatDestroy(&AH));
     836             :   */
     837         402 :   for (j=0;j<V->k-V->l;j++) {
     838         311 :     PetscCall(BVGetColumn(V,V->l+j,&v));
     839         311 :     PetscCall(BVGetColumn(Y,Y->l+j,&y));
     840         311 :     PetscCall(MatMultHermitianTranspose(A,v,y));
     841         311 :     PetscCall(BVRestoreColumn(V,V->l+j,&v));
     842         311 :     PetscCall(BVRestoreColumn(Y,Y->l+j,&y));
     843             :   }
     844          91 :   PetscFunctionReturn(PETSC_SUCCESS);
     845             : }
     846             : 
     847             : /*@
     848             :    BVMatMultColumn - Computes the matrix-vector product for a specified
     849             :    column, storing the result in the next column v_{j+1}=A*v_j.
     850             : 
     851             :    Neighbor-wise Collective
     852             : 
     853             :    Input Parameters:
     854             : +  V - basis vectors context
     855             : .  A - the matrix
     856             : -  j - the column
     857             : 
     858             :    Level: beginner
     859             : 
     860             : .seealso: BVMatMult(), BVMatMultTransposeColumn(), BVMatMultHermitianTransposeColumn()
     861             : @*/
     862       60391 : PetscErrorCode BVMatMultColumn(BV V,Mat A,PetscInt j)
     863             : {
     864       60391 :   Vec            vj,vj1;
     865             : 
     866       60391 :   PetscFunctionBegin;
     867       60391 :   PetscValidHeaderSpecific(V,BV_CLASSID,1);
     868       60391 :   PetscValidType(V,1);
     869       60391 :   BVCheckSizes(V,1);
     870       60391 :   PetscValidHeaderSpecific(A,MAT_CLASSID,2);
     871       60391 :   PetscCheckSameComm(V,1,A,2);
     872      181173 :   PetscValidLogicalCollectiveInt(V,j,3);
     873       60391 :   PetscCheck(j>=0,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Index j must be non-negative");
     874       60391 :   PetscCheck(j+1<V->m,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Result should go in index j+1=%" PetscInt_FMT " but BV only has %" PetscInt_FMT " columns",j+1,V->m);
     875             : 
     876       60391 :   PetscCall(PetscLogEventBegin(BV_MatMultVec,V,A,0,0));
     877       60391 :   PetscCall(BVGetColumn(V,j,&vj));
     878       60391 :   PetscCall(BVGetColumn(V,j+1,&vj1));
     879       60391 :   PetscCall(MatMult(A,vj,vj1));
     880       60391 :   PetscCall(BVRestoreColumn(V,j,&vj));
     881       60391 :   PetscCall(BVRestoreColumn(V,j+1,&vj1));
     882       60391 :   PetscCall(PetscLogEventEnd(BV_MatMultVec,V,A,0,0));
     883       60391 :   PetscCall(PetscObjectStateIncrease((PetscObject)V));
     884       60391 :   PetscFunctionReturn(PETSC_SUCCESS);
     885             : }
     886             : 
     887             : /*@
     888             :    BVMatMultTransposeColumn - Computes the transpose matrix-vector product for a
     889             :    specified column, storing the result in the next column v_{j+1}=A^T*v_j.
     890             : 
     891             :    Neighbor-wise Collective
     892             : 
     893             :    Input Parameters:
     894             : +  V - basis vectors context
     895             : .  A - the matrix
     896             : -  j - the column
     897             : 
     898             :    Level: beginner
     899             : 
     900             : .seealso: BVMatMult(), BVMatMultColumn(), BVMatMultHermitianTransposeColumn()
     901             : @*/
     902           0 : PetscErrorCode BVMatMultTransposeColumn(BV V,Mat A,PetscInt j)
     903             : {
     904           0 :   Vec            vj,vj1;
     905             : 
     906           0 :   PetscFunctionBegin;
     907           0 :   PetscValidHeaderSpecific(V,BV_CLASSID,1);
     908           0 :   PetscValidType(V,1);
     909           0 :   BVCheckSizes(V,1);
     910           0 :   PetscValidHeaderSpecific(A,MAT_CLASSID,2);
     911           0 :   PetscCheckSameComm(V,1,A,2);
     912           0 :   PetscValidLogicalCollectiveInt(V,j,3);
     913           0 :   PetscCheck(j>=0,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Index j must be non-negative");
     914           0 :   PetscCheck(j+1<V->m,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Result should go in index j+1=%" PetscInt_FMT " but BV only has %" PetscInt_FMT " columns",j+1,V->m);
     915             : 
     916           0 :   PetscCall(PetscLogEventBegin(BV_MatMultVec,V,A,0,0));
     917           0 :   PetscCall(BVGetColumn(V,j,&vj));
     918           0 :   PetscCall(BVGetColumn(V,j+1,&vj1));
     919           0 :   PetscCall(MatMultTranspose(A,vj,vj1));
     920           0 :   PetscCall(BVRestoreColumn(V,j,&vj));
     921           0 :   PetscCall(BVRestoreColumn(V,j+1,&vj1));
     922           0 :   PetscCall(PetscLogEventEnd(BV_MatMultVec,V,A,0,0));
     923           0 :   PetscCall(PetscObjectStateIncrease((PetscObject)V));
     924           0 :   PetscFunctionReturn(PETSC_SUCCESS);
     925             : }
     926             : 
     927             : /*@
     928             :    BVMatMultHermitianTransposeColumn - Computes the conjugate-transpose matrix-vector
     929             :    product for a specified column, storing the result in the next column v_{j+1}=A^H*v_j.
     930             : 
     931             :    Neighbor-wise Collective
     932             : 
     933             :    Input Parameters:
     934             : +  V - basis vectors context
     935             : .  A - the matrix
     936             : -  j - the column
     937             : 
     938             :    Level: beginner
     939             : 
     940             : .seealso: BVMatMult(), BVMatMultColumn(), BVMatMultTransposeColumn()
     941             : @*/
     942           0 : PetscErrorCode BVMatMultHermitianTransposeColumn(BV V,Mat A,PetscInt j)
     943             : {
     944           0 :   Vec            vj,vj1;
     945             : 
     946           0 :   PetscFunctionBegin;
     947           0 :   PetscValidHeaderSpecific(V,BV_CLASSID,1);
     948           0 :   PetscValidType(V,1);
     949           0 :   BVCheckSizes(V,1);
     950           0 :   PetscValidHeaderSpecific(A,MAT_CLASSID,2);
     951           0 :   PetscCheckSameComm(V,1,A,2);
     952           0 :   PetscValidLogicalCollectiveInt(V,j,3);
     953           0 :   PetscCheck(j>=0,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Index j must be non-negative");
     954           0 :   PetscCheck(j+1<V->m,PetscObjectComm((PetscObject)V),PETSC_ERR_ARG_OUTOFRANGE,"Result should go in index j+1=%" PetscInt_FMT " but BV only has %" PetscInt_FMT " columns",j+1,V->m);
     955             : 
     956           0 :   PetscCall(PetscLogEventBegin(BV_MatMultVec,V,A,0,0));
     957           0 :   PetscCall(BVGetColumn(V,j,&vj));
     958           0 :   PetscCall(BVGetColumn(V,j+1,&vj1));
     959           0 :   PetscCall(MatMultHermitianTranspose(A,vj,vj1));
     960           0 :   PetscCall(BVRestoreColumn(V,j,&vj));
     961           0 :   PetscCall(BVRestoreColumn(V,j+1,&vj1));
     962           0 :   PetscCall(PetscLogEventEnd(BV_MatMultVec,V,A,0,0));
     963           0 :   PetscCall(PetscObjectStateIncrease((PetscObject)V));
     964           0 :   PetscFunctionReturn(PETSC_SUCCESS);
     965             : }

Generated by: LCOV version 1.14