<commit_before>
#include <../src/qps/impls/tao/qpstaoimpl.h>
#include <petsc/private/taoimpl.h>

#undef __FUNCT__
#define __FUNCT__ "QPSTaoConverged_Tao"
static PetscErrorCode QPSTaoConverged_Tao(Tao tao,void *ctx)
{
  QPS qps = (QPS) ctx;

  PetscFunctionBegin;
  //TODO sqrt?
  qps->rnorm = tao->residual;
  qps->iteration = tao->niter;
  TRY( (*qps->convergencetest)(qps,qps->solQP,qps->iteration,qps->rnorm,&qps->reason,qps->cnvctx) );

  //TODO quick&dirty
  if (qps->reason > 0) {
    tao->reason = TAO_CONVERGED_USER;
  } else if (qps->reason < 0) {
    tao->reason = TAO_DIVERGED_USER;
  } else {
    tao->reason = TAO_CONTINUE_ITERATING;
  }
  PetscFunctionReturn(0);
}

/*  FormFunctionGradient - Evaluates f(x) and gradient g(x). 
 
    Input Parameters:
.   tao     - the TaoSolver context
.   X       - input vector
.   userCtx - optional user-defined context, as set by TaoSetObjectiveAndGradientRoutine()
    
    Output Parameters:
.   fcn     - the function value
.   G       - vector containing the newly evaluated gradient
 */
#undef __FUNCT__
#define __FUNCT__ "FormFunctionGradientQPS"
static PetscErrorCode FormFunctionGradientQPS(Tao tao, Vec X, PetscReal *fcn, Vec G,void *qps_void)
{
  QP          qp;
  QPS         qps = (QPS) qps_void;

  PetscFunctionBegin; 
  TRY( QPSGetSolvedQP(qps,&qp) );
  TRY( QPComputeObjectiveAndGradient(qp,X,G,fcn) );
  PetscFunctionReturn(0);
  
}

/*
   FormHessian - Evaluates Hessian matrix.

   Input Parameters:
.  tao     - the TaoSolver context
.  x       - input vector
.  userCtx - optional user-defined context, as set by TaoSetHessianRoutine()

   Output Parameters:
.  A    - Hessian matrix
.  B    - optionally different preconditioning matrix
.  flag - flag indicating matrix structure
 */
#undef __FUNCT__
#define __FUNCT__ "FormHessianQPS"
static PetscErrorCode FormHessianQPS(Tao tao,Vec X,Mat Hptr, Mat Hpc, void *qps_void)
{
  PetscFunctionBegin; 
  PetscFunctionReturn(0);
}

#undef __FUNCT__
#define __FUNCT__ "QPSTaoGetTao"
PetscErrorCode QPSTaoGetTao(QPS qps,Tao *tao)
{
  PetscBool flg;
  QPS_Tao *qpstao;
  const char* prefix;
  
  PetscFunctionBegin;
  PetscValidHeaderSpecific(qps,QPS_CLASSID,1);
  PetscValidPointer(tao,2);
  TRY( PetscObjectTypeCompare((PetscObject)qps,QPSTAO,&flg) );
  if (!flg) FLLOP_SETERRQ(PetscObjectComm((PetscObject)qps),PETSC_ERR_SUP,"This is a QPSTAO specific routine!");
  qpstao = (QPS_Tao*)qps->data;
  if (!qpstao->tao) {
    TRY( QPSGetOptionsPrefix(qps,&prefix) );
    TRY( TaoCreate(PetscObjectComm((PetscObject)qps),&qpstao->tao) );
    TRY( TaoSetOptionsPrefix(qpstao->tao,prefix) );
    TRY( TaoAppendOptionsPrefix(qpstao->tao,"qps_") );
    TRY( PetscLogObjectParent((PetscObject)qps,(PetscObject)qpstao->tao) );
    TRY( PetscObjectIncrementTabLevel((PetscObject)qpstao->tao,(PetscObject)qps,1) );
  }
  *tao = qpstao->tao;
  PetscFunctionReturn(0);
}

#undef __FUNCT__
#define __FUNCT__ "QPSTaoSetType"
PetscErrorCode QPSTaoSetType(QPS qps,TaoType type)
{
  PetscBool flg;
  QPS_Tao *qpstao;
  
  PetscFunctionBegin;
  PetscValidHeaderSpecific(qps,QPS_CLASSID,1);
  TRY( PetscObjectTypeCompare((PetscObject)qps,QPSTAO,&flg) );
  if (!flg) FLLOP_SETERRQ(((PetscObject)qps)->comm,PETSC_ERR_SUP,"This is a QPSTAO specific routine!");
  qpstao = (QPS_Tao*)qps->data;
  TRY( QPSTaoGetTao(qps,&qpstao->tao) );
  TRY( TaoSetType(qpstao->tao,type) );
  PetscFunctionReturn(0);
}

#undef __FUNCT__
#define __FUNCT__ "QPSTaoGetType"
PetscErrorCode QPSTaoGetType(QPS qps, const TaoType *type)
{
  PetscBool flg;
  QPS_Tao *qpstao;
  
  PetscFunctionBegin;
  PetscValidHeaderSpecific(qps,QPS_CLASSID,1);
  TRY( PetscObjectTypeCompare((PetscObject)qps,QPSTAO,&flg) );
  if (!flg) FLLOP_SETERRQ(((PetscObject)qps)->comm,PETSC_ERR_SUP,"This is a QPSTAO specific routine!");
  qpstao = (QPS_Tao*)qps->data;
  TRY( QPSTaoGetTao(qps,&qpstao->tao) );
  TRY( TaoGetType(qpstao->tao,type) );
  PetscFunctionReturn(0);
}

#undef __FUNCT__  
#define __FUNCT__ "QPSSetUp_Tao"
PetscErrorCode QPSSetUp_Tao(QPS qps)
{
  QPS_Tao          *qpstao = (QPS_Tao*)qps->data;
  Tao              tao;
  QP               qp;
  Vec              b,x,lb,ub;
  KSP              ksp;
  PC               pc;
  
  PetscFunctionBegin;
  TRY( QPSGetSolvedQP(qps,&qp) );
  TRY( QPGetRhs(qp,&b) );
  TRY( QPGetSolutionVector(qp,&x) );
  TRY( QPGetBox(qp,&lb,&ub) );
  
  TRY( QPSTaoGetTao(qps,&tao) );
  TRY( TaoSetInitialVector(tao,x) );

  /* Set routines for function, gradient and hessian evaluation */
  TRY( TaoSetObjectiveAndGradientRoutine(tao,FormFunctionGradientQPS,qps) );
  TRY( TaoSetHessianRoutine(tao,qp->A,qp->A,FormHessianQPS,qps) );

  /* Set Variable bounds */
  if (!lb) {
    TRY( VecDuplicate(x,&lb) );
    TRY( VecSet(lb,PETSC_NINFINITY) );
  } else {
    TRY( PetscObjectReference((PetscObject)lb) );
  }
  if (!ub) {
    TRY( VecDuplicate(x,&ub) );
    TRY( VecSet(ub,PETSC_INFINITY) );
  } else {
    TRY( PetscObjectReference((PetscObject)ub) );
  }
  TRY( TaoSetVariableBounds(tao,lb,ub) );
  TRY( VecDestroy(&lb) );
  TRY( VecDestroy(&ub) );

  /* set specific defaults for TAO inside QPSTAO */
  TRY( TaoSetType(qpstao->tao,TAOGPCG) );
  TRY( TaoSetConvergenceTest(tao,QPSTaoConverged_Tao,qps) );
  TRY( TaoSetTolerances( tao, qps->atol, qps->rtol, PETSC_DEFAULT ) );

  /* Check for any tao command line options */
  if (qpstao->setfromoptionscalled) {
    TRY( TaoSetFromOptions(tao) );
  }

  TRY( TaoGetKSP(tao,&ksp) );
  if (ksp) {
    /* set KSP defaults after TaoSetFromOptions as it can create new KSP instance */
    const char *prefix;
    TRY( QPSGetOptionsPrefix(qps,&prefix) );
    TRY( KSPSetOptionsPrefix(ksp,prefix) );
    TRY( KSPAppendOptionsPrefix(ksp,"qps_tao_") );
    TRY( KSPGetPC(ksp,&pc) );
    TRY( PCSetType(pc,PCNONE) );
    TRY( KSPSetFromOptions(ksp) );
    TRY( KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD) );
  }

  TRY( TaoSetUp(tao) );
  PetscFunctionReturn(0);  
}

#undef __FUNCT__  
#define __FUNCT__ "QPSSolve_Tao"
PetscErrorCode QPSSolve_Tao(QPS qps)
{
  QPS_Tao          *qpstao = (QPS_Tao*)qps->data;
  Tao              tao;
  PetscInt         its;
  
  PetscFunctionBegin;
  TRY( QPSTaoGetTao(qps,&tao) );
  TRY( TaoSolve(tao) );<commit_msg>QPSSetUp_Tao: removed KSPView call<commit_after>
#include <../src/qps/impls/tao/qpstaoimpl.h>
#include <petsc/private/taoimpl.h>

#undef __FUNCT__
#define __FUNCT__ "QPSTaoConverged_Tao"
static PetscErrorCode QPSTaoConverged_Tao(Tao tao,void *ctx)
{
  QPS qps = (QPS) ctx;

  PetscFunctionBegin;
  //TODO sqrt?
  qps->rnorm = tao->residual;
  qps->iteration = tao->niter;
  TRY( (*qps->convergencetest)(qps,qps->solQP,qps->iteration,qps->rnorm,&qps->reason,qps->cnvctx) );

  //TODO quick&dirty
  if (qps->reason > 0) {
    tao->reason = TAO_CONVERGED_USER;
  } else if (qps->reason < 0) {
    tao->reason = TAO_DIVERGED_USER;
  } else {
    tao->reason = TAO_CONTINUE_ITERATING;
  }
  PetscFunctionReturn(0);
}

/*  FormFunctionGradient - Evaluates f(x) and gradient g(x). 
 
    Input Parameters:
.   tao     - the TaoSolver context
.   X       - input vector
.   userCtx - optional user-defined context, as set by TaoSetObjectiveAndGradientRoutine()
    
    Output Parameters:
.   fcn     - the function value
.   G       - vector containing the newly evaluated gradient
 */
#undef __FUNCT__
#define __FUNCT__ "FormFunctionGradientQPS"
static PetscErrorCode FormFunctionGradientQPS(Tao tao, Vec X, PetscReal *fcn, Vec G,void *qps_void)
{
  QP          qp;
  QPS         qps = (QPS) qps_void;

  PetscFunctionBegin; 
  TRY( QPSGetSolvedQP(qps,&qp) );
  TRY( QPComputeObjectiveAndGradient(qp,X,G,fcn) );
  PetscFunctionReturn(0);
  
}

/*
   FormHessian - Evaluates Hessian matrix.

   Input Parameters:
.  tao     - the TaoSolver context
.  x       - input vector
.  userCtx - optional user-defined context, as set by TaoSetHessianRoutine()

   Output Parameters:
.  A    - Hessian matrix
.  B    - optionally different preconditioning matrix
.  flag - flag indicating matrix structure
 */
#undef __FUNCT__
#define __FUNCT__ "FormHessianQPS"
static PetscErrorCode FormHessianQPS(Tao tao,Vec X,Mat Hptr, Mat Hpc, void *qps_void)
{
  PetscFunctionBegin; 
  PetscFunctionReturn(0);
}

#undef __FUNCT__
#define __FUNCT__ "QPSTaoGetTao"
PetscErrorCode QPSTaoGetTao(QPS qps,Tao *tao)
{
  PetscBool flg;
  QPS_Tao *qpstao;
  const char* prefix;
  
  PetscFunctionBegin;
  PetscValidHeaderSpecific(qps,QPS_CLASSID,1);
  PetscValidPointer(tao,2);
  TRY( PetscObjectTypeCompare((PetscObject)qps,QPSTAO,&flg) );
  if (!flg) FLLOP_SETERRQ(PetscObjectComm((PetscObject)qps),PETSC_ERR_SUP,"This is a QPSTAO specific routine!");
  qpstao = (QPS_Tao*)qps->data;
  if (!qpstao->tao) {
    TRY( QPSGetOptionsPrefix(qps,&prefix) );
    TRY( TaoCreate(PetscObjectComm((PetscObject)qps),&qpstao->tao) );
    TRY( TaoSetOptionsPrefix(qpstao->tao,prefix) );
    TRY( TaoAppendOptionsPrefix(qpstao->tao,"qps_") );
    TRY( PetscLogObjectParent((PetscObject)qps,(PetscObject)qpstao->tao) );
    TRY( PetscObjectIncrementTabLevel((PetscObject)qpstao->tao,(PetscObject)qps,1) );
  }
  *tao = qpstao->tao;
  PetscFunctionReturn(0);
}

#undef __FUNCT__
#define __FUNCT__ "QPSTaoSetType"
PetscErrorCode QPSTaoSetType(QPS qps,TaoType type)
{
  PetscBool flg;
  QPS_Tao *qpstao;
  
  PetscFunctionBegin;
  PetscValidHeaderSpecific(qps,QPS_CLASSID,1);
  TRY( PetscObjectTypeCompare((PetscObject)qps,QPSTAO,&flg) );
  if (!flg) FLLOP_SETERRQ(((PetscObject)qps)->comm,PETSC_ERR_SUP,"This is a QPSTAO specific routine!");
  qpstao = (QPS_Tao*)qps->data;
  TRY( QPSTaoGetTao(qps,&qpstao->tao) );
  TRY( TaoSetType(qpstao->tao,type) );
  PetscFunctionReturn(0);
}

#undef __FUNCT__
#define __FUNCT__ "QPSTaoGetType"
PetscErrorCode QPSTaoGetType(QPS qps, const TaoType *type)
{
  PetscBool flg;
  QPS_Tao *qpstao;
  
  PetscFunctionBegin;
  PetscValidHeaderSpecific(qps,QPS_CLASSID,1);
  TRY( PetscObjectTypeCompare((PetscObject)qps,QPSTAO,&flg) );
  if (!flg) FLLOP_SETERRQ(((PetscObject)qps)->comm,PETSC_ERR_SUP,"This is a QPSTAO specific routine!");
  qpstao = (QPS_Tao*)qps->data;
  TRY( QPSTaoGetTao(qps,&qpstao->tao) );
  TRY( TaoGetType(qpstao->tao,type) );
  PetscFunctionReturn(0);
}

#undef __FUNCT__  
#define __FUNCT__ "QPSSetUp_Tao"
PetscErrorCode QPSSetUp_Tao(QPS qps)
{
  QPS_Tao          *qpstao = (QPS_Tao*)qps->data;
  Tao              tao;
  QP               qp;
  Vec              b,x,lb,ub;
  KSP              ksp;
  PC               pc;
  
  PetscFunctionBegin;
  TRY( QPSGetSolvedQP(qps,&qp) );
  TRY( QPGetRhs(qp,&b) );
  TRY( QPGetSolutionVector(qp,&x) );
  TRY( QPGetBox(qp,&lb,&ub) );
  
  TRY( QPSTaoGetTao(qps,&tao) );
  TRY( TaoSetInitialVector(tao,x) );

  /* Set routines for function, gradient and hessian evaluation */
  TRY( TaoSetObjectiveAndGradientRoutine(tao,FormFunctionGradientQPS,qps) );
  TRY( TaoSetHessianRoutine(tao,qp->A,qp->A,FormHessianQPS,qps) );

  /* Set Variable bounds */
  if (!lb) {
    TRY( VecDuplicate(x,&lb) );
    TRY( VecSet(lb,PETSC_NINFINITY) );
  } else {
    TRY( PetscObjectReference((PetscObject)lb) );
  }
  if (!ub) {
    TRY( VecDuplicate(x,&ub) );
    TRY( VecSet(ub,PETSC_INFINITY) );
  } else {
    TRY( PetscObjectReference((PetscObject)ub) );
  }
  TRY( TaoSetVariableBounds(tao,lb,ub) );
  TRY( VecDestroy(&lb) );
  TRY( VecDestroy(&ub) );

  /* set specific defaults for TAO inside QPSTAO */
  TRY( TaoSetType(qpstao->tao,TAOGPCG) );
  TRY( TaoSetConvergenceTest(tao,QPSTaoConverged_Tao,qps) );
  TRY( TaoSetTolerances( tao, qps->atol, qps->rtol, PETSC_DEFAULT ) );

  /* Check for any tao command line options */
  if (qpstao->setfromoptionscalled) {
    TRY( TaoSetFromOptions(tao) );
  }

  TRY( TaoGetKSP(tao,&ksp) );
  if (ksp) {
    /* set KSP defaults after TaoSetFromOptions as it can create new KSP instance */
    const char *prefix;
    TRY( QPSGetOptionsPrefix(qps,&prefix) );
    TRY( KSPSetOptionsPrefix(ksp,prefix) );
    TRY( KSPAppendOptionsPrefix(ksp,"qps_tao_") );
    TRY( KSPGetPC(ksp,&pc) );
    TRY( PCSetType(pc,PCNONE) );
    TRY( KSPSetFromOptions(ksp) );
  }

  TRY( TaoSetUp(tao) );
  PetscFunctionReturn(0);  
}

#undef __FUNCT__  
#define __FUNCT__ "QPSSolve_Tao"
PetscErrorCode QPSSolve_Tao(QPS qps)
{
  QPS_Tao          *qpstao = (QPS_Tao*)qps->data;
  Tao              tao;
  PetscInt         its;
  
  PetscFunctionBegin;
  TRY( QPSTaoGetTao(qps,&tao) );
  TRY( TaoSolve(tao) );
{'fraction_non_alphanumeric': 0.13105851839844382, 'fraction_numerical': 0.0022694115739990274, 'mean_word_length': 4.105088953247828, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '785085', 'n_tokens_mistral': 5271, 'n_tokens_neox': 4994, 'n_words': 867}