/*@ BVMult - Computes Y = beta*Y + alpha*X*Q. Logically Collective on BV Input Parameters: + Y,X - basis vectors . alpha,beta - scalars - Q - a sequential dense matrix Output Parameter: . Y - the modified basis vectors Notes: X and Y must be different objects. The case X=Y can be addressed with BVMultInPlace(). The matrix Q must be a sequential dense Mat, with all entries equal on all processes (otherwise each process will compute a different update). The dimensions of Q must be at least m,n where m is the number of active columns of X and n is the number of active columns of Y. The leading columns of Y are not modified. Also, if X has leading columns specified, then these columns do not participate in the computation. Hence, only rows (resp. columns) of Q starting from lx (resp. ly) are used, where lx (resp. ly) is the number of leading columns of X (resp. Y). Level: intermediate .seealso: BVMultVec(), BVMultColumn(), BVMultInPlace(), BVSetActiveColumns() @*/ PetscErrorCode BVMult(BV Y,PetscScalar alpha,PetscScalar beta,BV X,Mat Q) { PetscErrorCode ierr; PetscBool match; PetscInt m,n; PetscFunctionBegin; PetscValidHeaderSpecific(Y,BV_CLASSID,1); PetscValidLogicalCollectiveScalar(Y,alpha,2); PetscValidLogicalCollectiveScalar(Y,beta,3); PetscValidHeaderSpecific(X,BV_CLASSID,4); PetscValidHeaderSpecific(Q,MAT_CLASSID,5); PetscValidType(Y,1); BVCheckSizes(Y,1); PetscValidType(X,4); BVCheckSizes(X,4); PetscValidType(Q,5); PetscCheckSameTypeAndComm(Y,1,X,4); if (X==Y) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_WRONG,"X and Y arguments must be different"); ierr = PetscObjectTypeCompare((PetscObject)Q,MATSEQDENSE,&match);CHKERRQ(ierr); if (!match) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_SUP,"Mat argument must be of type seqdense"); ierr = MatGetSize(Q,&m,&n);CHKERRQ(ierr); if (m<X->k) SETERRQ2(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_SIZ,"Mat argument has %D rows, should have at least %D",m,X->k); if (n<Y->k) SETERRQ2(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_SIZ,"Mat argument has %D columns, should have at least %D",n,Y->k); if (X->n!=Y->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension X %D, Y %D",X->n,Y->n); if (!X->n) PetscFunctionReturn(0); ierr = PetscLogEventBegin(BV_Mult,X,Y,0,0);CHKERRQ(ierr); ierr = (*Y->ops->mult)(Y,alpha,beta,X,Q);CHKERRQ(ierr); ierr = PetscLogEventEnd(BV_Mult,X,Y,0,0);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ BVMatMult - Computes the matrix-vector product for each column, Y=A*X. Neighbor-wise Collective on Mat and BV Input Parameters: + V - basis vectors context - A - the matrix Output Parameter: . Y - the result Note: Both V and Y must be distributed in the same manner. Only active columns (excluding the leading ones) are processed. In the result Y, columns are overwritten starting from the leading ones. Level: beginner .seealso: BVCopy(), BVSetActiveColumns(), BVMatMultColumn() @*/ PetscErrorCode BVMatMult(BV V,Mat A,BV Y) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(V,BV_CLASSID,1); PetscValidType(V,1); BVCheckSizes(V,1); PetscValidHeaderSpecific(A,MAT_CLASSID,2); PetscValidHeaderSpecific(Y,BV_CLASSID,3); PetscValidType(Y,3); BVCheckSizes(Y,3); PetscCheckSameComm(V,1,A,2); PetscCheckSameTypeAndComm(V,1,Y,3); if (V->n!=Y->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension V %D, Y %D",V->n,Y->n); if (V->k-V->l>Y->m-Y->l) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Y has %D non-leading columns, not enough to store %D columns",Y->m-Y->l,V->k-V->l); ierr = PetscLogEventBegin(BV_MatMult,V,A,Y,0);CHKERRQ(ierr); ierr = (*V->ops->matmult)(V,A,Y);CHKERRQ(ierr); ierr = PetscLogEventEnd(BV_MatMult,V,A,Y,0);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C BVAXPY - Computes Y = Y + alpha*X. Logically Collective on BV Input Parameters: + Y,X - basis vectors - alpha - scalar Output Parameter: . Y - the modified basis vectors Notes: X and Y must be different objects, with compatible dimensions. The effect is the same as doing a VecAXPY for each of the active columns (excluding the leading ones). Level: intermediate .seealso: BVMult(), BVSetActiveColumns() @*/ PetscErrorCode BVAXPY(BV Y,PetscScalar alpha,BV X) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(Y,BV_CLASSID,1); PetscValidLogicalCollectiveScalar(Y,alpha,2); PetscValidHeaderSpecific(X,BV_CLASSID,3); PetscValidType(Y,1); BVCheckSizes(Y,1); PetscValidType(X,3); BVCheckSizes(X,3); PetscCheckSameTypeAndComm(Y,1,X,3); if (X==Y) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_WRONG,"X and Y arguments must be different"); if (X->n!=Y->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Mismatching local dimension X %D, Y %D",X->n,Y->n); if (X->k-X->l!=Y->k-Y->l) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Y has %D non-leading columns, while X has %D",Y->m-Y->l,X->k-X->l); if (!X->n) PetscFunctionReturn(0); ierr = PetscLogEventBegin(BV_AXPY,X,Y,0,0);CHKERRQ(ierr); ierr = (*Y->ops->axpy)(Y,alpha,X);CHKERRQ(ierr); ierr = PetscLogEventEnd(BV_AXPY,X,Y,0,0);CHKERRQ(ierr); ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode TaoLineSearchApply(TaoLineSearch ls, Vec x, PetscReal *f, Vec g, Vec s, PetscReal *steplength, TaoLineSearchConvergedReason *reason) { PetscErrorCode ierr; PetscViewer viewer; PetscInt low1,low2,low3,high1,high2,high3; PetscBool flg; char filename[PETSC_MAX_PATH_LEN]; PetscFunctionBegin; *reason = TAOLINESEARCH_CONTINUE_ITERATING; PetscValidHeaderSpecific(ls,TAOLINESEARCH_CLASSID,1); PetscValidHeaderSpecific(x,VEC_CLASSID,2); PetscValidScalarPointer(f,3); PetscValidHeaderSpecific(g,VEC_CLASSID,4); PetscValidHeaderSpecific(s,VEC_CLASSID,5); PetscValidPointer(reason,7); PetscCheckSameComm(ls,1,x,2); PetscCheckSameTypeAndComm(x,2,g,4); PetscCheckSameTypeAndComm(x,2,s,5); ierr = VecGetOwnershipRange(x, &low1, &high1);CHKERRQ(ierr); ierr = VecGetOwnershipRange(g, &low2, &high2);CHKERRQ(ierr); ierr = VecGetOwnershipRange(s, &low3, &high3);CHKERRQ(ierr); if ( low1!= low2 || low1!= low3 || high1!= high2 || high1!= high3) SETERRQ(PETSC_COMM_SELF,1,"InCompatible vector local lengths"); ierr = PetscObjectReference((PetscObject)s);CHKERRQ(ierr); ierr = VecDestroy(&ls->stepdirection);CHKERRQ(ierr); ls->stepdirection = s; ierr = TaoLineSearchSetUp(ls);CHKERRQ(ierr); if (!ls->ops->apply) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Line Search Object does not have 'apply' routine"); ls->nfeval=0; ls->ngeval=0; ls->nfgeval=0; /* Check parameter values */ if (ls->ftol < 0.0) { ierr = PetscInfo1(ls,"Bad Line Search Parameter: ftol (%g) < 0\n",(double)ls->ftol);CHKERRQ(ierr); *reason=TAOLINESEARCH_FAILED_BADPARAMETER; } if (ls->rtol < 0.0) { ierr = PetscInfo1(ls,"Bad Line Search Parameter: rtol (%g) < 0\n",(double)ls->rtol);CHKERRQ(ierr); *reason=TAOLINESEARCH_FAILED_BADPARAMETER; } if (ls->gtol < 0.0) { ierr = PetscInfo1(ls,"Bad Line Search Parameter: gtol (%g) < 0\n",(double)ls->gtol);CHKERRQ(ierr); *reason=TAOLINESEARCH_FAILED_BADPARAMETER; } if (ls->stepmin < 0.0) { ierr = PetscInfo1(ls,"Bad Line Search Parameter: stepmin (%g) < 0\n",(double)ls->stepmin);CHKERRQ(ierr); *reason=TAOLINESEARCH_FAILED_BADPARAMETER; } if (ls->stepmax < ls->stepmin) { ierr = PetscInfo2(ls,"Bad Line Search Parameter: stepmin (%g) > stepmax (%g)\n",(double)ls->stepmin,(double)ls->stepmax);CHKERRQ(ierr); *reason=TAOLINESEARCH_FAILED_BADPARAMETER; } if (ls->max_funcs < 0) { ierr = PetscInfo1(ls,"Bad Line Search Parameter: max_funcs (%D) < 0\n",ls->max_funcs);CHKERRQ(ierr); *reason=TAOLINESEARCH_FAILED_BADPARAMETER; } if (PetscIsInfOrNanReal(*f)) { ierr = PetscInfo1(ls,"Initial Line Search Function Value is Inf or Nan (%g)\n",(double)*f);CHKERRQ(ierr); *reason=TAOLINESEARCH_FAILED_INFORNAN; } ierr = PetscObjectReference((PetscObject)x); ierr = VecDestroy(&ls->start_x);CHKERRQ(ierr); ls->start_x = x; ierr = PetscLogEventBegin(TaoLineSearch_ApplyEvent,ls,0,0,0);CHKERRQ(ierr); ierr = (*ls->ops->apply)(ls,x,f,g,s);CHKERRQ(ierr); ierr = PetscLogEventEnd(TaoLineSearch_ApplyEvent, ls, 0,0,0);CHKERRQ(ierr); *reason=ls->reason; ls->new_f = *f; if (steplength) { *steplength=ls->step; } ierr = PetscOptionsGetString(((PetscObject)ls)->prefix,"-tao_ls_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (ls->viewls && !PetscPreLoadingOn) { ierr = PetscViewerASCIIOpen(((PetscObject)ls)->comm,filename,&viewer);CHKERRQ(ierr); ierr = TaoLineSearchView(ls,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }