xref: /petsc/src/ksp/pc/impls/rowscalingviennacl/rowscalingviennacl.cxx (revision f1580f4e3ce5d5b2393648fd039d0d41b440385d)
170baa948SKarl Rupp 
270baa948SKarl Rupp /*
370baa948SKarl Rupp    Include files needed for the ViennaCL row-scaling preconditioner:
470baa948SKarl Rupp      pcimpl.h - private include file intended for use by all preconditioners
570baa948SKarl Rupp */
670baa948SKarl Rupp #define PETSC_SKIP_SPINLOCK
799acd6aaSStefano Zampini #define PETSC_SKIP_IMMINTRIN_H_CUDAWORKAROUND 1
899acd6aaSStefano Zampini 
970baa948SKarl Rupp #include <petsc/private/pcimpl.h> /*I "petscpc.h" I*/
1070baa948SKarl Rupp #include <../src/mat/impls/aij/seq/aij.h>
1170baa948SKarl Rupp #include <../src/vec/vec/impls/dvecimpl.h>
1270baa948SKarl Rupp #include <../src/mat/impls/aij/seq/seqviennacl/viennaclmatimpl.h>
1370baa948SKarl Rupp #include <../src/vec/vec/impls/seq/seqviennacl/viennaclvecimpl.h>
1470baa948SKarl Rupp #include <viennacl/linalg/row_scaling.hpp>
1570baa948SKarl Rupp 
1670baa948SKarl Rupp /*
1770baa948SKarl Rupp    Private context (data structure) for the ROWSCALINGVIENNACL preconditioner.
1870baa948SKarl Rupp */
1970baa948SKarl Rupp typedef struct {
2070baa948SKarl Rupp   viennacl::linalg::row_scaling<viennacl::compressed_matrix<PetscScalar>> *ROWSCALINGVIENNACL;
2170baa948SKarl Rupp } PC_ROWSCALINGVIENNACL;
2270baa948SKarl Rupp 
2370baa948SKarl Rupp /*
2470baa948SKarl Rupp    PCSetUp_ROWSCALINGVIENNACL - Prepares for the use of the ROWSCALINGVIENNACL preconditioner
2570baa948SKarl Rupp                                 by setting data structures and options.
2670baa948SKarl Rupp 
2770baa948SKarl Rupp    Input Parameter:
2870baa948SKarl Rupp .  pc - the preconditioner context
2970baa948SKarl Rupp 
3070baa948SKarl Rupp    Application Interface Routine: PCSetUp()
3170baa948SKarl Rupp 
32*f1580f4eSBarry Smith    Note:
3370baa948SKarl Rupp    The interface routine PCSetUp() is not usually called directly by
3470baa948SKarl Rupp    the user, but instead is called by PCApply() if necessary.
3570baa948SKarl Rupp */
369371c9d4SSatish Balay static PetscErrorCode PCSetUp_ROWSCALINGVIENNACL(PC pc) {
3770baa948SKarl Rupp   PC_ROWSCALINGVIENNACL *rowscaling = (PC_ROWSCALINGVIENNACL *)pc->data;
3870baa948SKarl Rupp   PetscBool              flg        = PETSC_FALSE;
3970baa948SKarl Rupp   Mat_SeqAIJViennaCL    *gpustruct;
4070baa948SKarl Rupp 
4170baa948SKarl Rupp   PetscFunctionBegin;
429566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATSEQAIJVIENNACL, &flg));
4328b400f6SJacob Faibussowitsch   PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Currently only handles ViennaCL matrices");
4470baa948SKarl Rupp   if (pc->setupcalled != 0) {
4570baa948SKarl Rupp     try {
4670baa948SKarl Rupp       delete rowscaling->ROWSCALINGVIENNACL;
479371c9d4SSatish Balay     } catch (char *ex) { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex); }
4870baa948SKarl Rupp   }
4970baa948SKarl Rupp   try {
5070baa948SKarl Rupp #if defined(PETSC_USE_COMPLEX)
5170baa948SKarl Rupp     gpustruct = NULL;
5270baa948SKarl Rupp     SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "No support for complex arithmetic in ROWSCALINGVIENNACL preconditioner");
5370baa948SKarl Rupp #else
549566063dSJacob Faibussowitsch     PetscCall(MatViennaCLCopyToGPU(pc->pmat));
5570baa948SKarl Rupp     gpustruct = (Mat_SeqAIJViennaCL *)(pc->pmat->spptr);
5670baa948SKarl Rupp 
5770baa948SKarl Rupp     viennacl::linalg::row_scaling_tag pc_tag(1);
5870baa948SKarl Rupp     ViennaCLAIJMatrix                *mat = (ViennaCLAIJMatrix *)gpustruct->mat;
5970baa948SKarl Rupp     rowscaling->ROWSCALINGVIENNACL        = new viennacl::linalg::row_scaling<viennacl::compressed_matrix<PetscScalar>>(*mat, pc_tag);
6070baa948SKarl Rupp #endif
619371c9d4SSatish Balay   } catch (char *ex) { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex); }
6270baa948SKarl Rupp   PetscFunctionReturn(0);
6370baa948SKarl Rupp }
6470baa948SKarl Rupp 
6570baa948SKarl Rupp /*
6670baa948SKarl Rupp    PCApply_ROWSCALINGVIENNACL - Applies the ROWSCALINGVIENNACL preconditioner to a vector.
6770baa948SKarl Rupp 
6870baa948SKarl Rupp    Input Parameters:
6970baa948SKarl Rupp .  pc - the preconditioner context
7070baa948SKarl Rupp .  x - input vector
7170baa948SKarl Rupp 
7270baa948SKarl Rupp    Output Parameter:
7370baa948SKarl Rupp .  y - output vector
7470baa948SKarl Rupp 
7570baa948SKarl Rupp    Application Interface Routine: PCApply()
7670baa948SKarl Rupp  */
779371c9d4SSatish Balay static PetscErrorCode PCApply_ROWSCALINGVIENNACL(PC pc, Vec x, Vec y) {
7870baa948SKarl Rupp   PC_ROWSCALINGVIENNACL               *ilu = (PC_ROWSCALINGVIENNACL *)pc->data;
7970baa948SKarl Rupp   PetscBool                            flg1, flg2;
8070baa948SKarl Rupp   viennacl::vector<PetscScalar> const *xarray = NULL;
8170baa948SKarl Rupp   viennacl::vector<PetscScalar>       *yarray = NULL;
8270baa948SKarl Rupp 
8370baa948SKarl Rupp   PetscFunctionBegin;
8470baa948SKarl Rupp   /*how to apply a certain fixed number of iterations?*/
859566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)x, VECSEQVIENNACL, &flg1));
869566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)y, VECSEQVIENNACL, &flg2));
8708401ef6SPierre Jolivet   PetscCheck((flg1 && flg2), PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Currently only handles ViennaCL vectors");
8848a46eb9SPierre Jolivet   if (!ilu->ROWSCALINGVIENNACL) PetscCall(PCSetUp_ROWSCALINGVIENNACL(pc));
899566063dSJacob Faibussowitsch   PetscCall(VecSet(y, 0.0));
909566063dSJacob Faibussowitsch   PetscCall(VecViennaCLGetArrayRead(x, &xarray));
919566063dSJacob Faibussowitsch   PetscCall(VecViennaCLGetArrayWrite(y, &yarray));
9270baa948SKarl Rupp   try {
9370baa948SKarl Rupp #if defined(PETSC_USE_COMPLEX)
9470baa948SKarl Rupp 
9570baa948SKarl Rupp #else
9670baa948SKarl Rupp     *yarray                               = *xarray;
9770baa948SKarl Rupp     ilu->ROWSCALINGVIENNACL->apply(*yarray);
9870baa948SKarl Rupp #endif
999371c9d4SSatish Balay   } catch (char *ex) { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex); }
1009566063dSJacob Faibussowitsch   PetscCall(VecViennaCLRestoreArrayRead(x, &xarray));
1019566063dSJacob Faibussowitsch   PetscCall(VecViennaCLRestoreArrayWrite(y, &yarray));
1029566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)y));
10370baa948SKarl Rupp   PetscFunctionReturn(0);
10470baa948SKarl Rupp }
105*f1580f4eSBarry Smith 
10670baa948SKarl Rupp /*
10770baa948SKarl Rupp    PCDestroy_ROWSCALINGVIENNACL - Destroys the private context for the ROWSCALINGVIENNACL preconditioner
10870baa948SKarl Rupp    that was created with PCCreate_ROWSCALINGVIENNACL().
10970baa948SKarl Rupp 
11070baa948SKarl Rupp    Input Parameter:
11170baa948SKarl Rupp .  pc - the preconditioner context
11270baa948SKarl Rupp 
11370baa948SKarl Rupp    Application Interface Routine: PCDestroy()
11470baa948SKarl Rupp */
1159371c9d4SSatish Balay static PetscErrorCode PCDestroy_ROWSCALINGVIENNACL(PC pc) {
11670baa948SKarl Rupp   PC_ROWSCALINGVIENNACL *rowscaling = (PC_ROWSCALINGVIENNACL *)pc->data;
11770baa948SKarl Rupp 
11870baa948SKarl Rupp   PetscFunctionBegin;
11970baa948SKarl Rupp   if (rowscaling->ROWSCALINGVIENNACL) {
12070baa948SKarl Rupp     try {
12170baa948SKarl Rupp       delete rowscaling->ROWSCALINGVIENNACL;
1229371c9d4SSatish Balay     } catch (char *ex) { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "ViennaCL error: %s", ex); }
12370baa948SKarl Rupp   }
12470baa948SKarl Rupp 
12570baa948SKarl Rupp   /*
12670baa948SKarl Rupp       Free the private data structure that was hanging off the PC
12770baa948SKarl Rupp   */
1289566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc->data));
12970baa948SKarl Rupp   PetscFunctionReturn(0);
13070baa948SKarl Rupp }
13170baa948SKarl Rupp 
1329371c9d4SSatish Balay static PetscErrorCode PCSetFromOptions_ROWSCALINGVIENNACL(PC pc, PetscOptionItems *PetscOptionsObject) {
13370baa948SKarl Rupp   PetscFunctionBegin;
134d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "ROWSCALINGVIENNACL options");
135d0609cedSBarry Smith   PetscOptionsHeadEnd();
13670baa948SKarl Rupp   PetscFunctionReturn(0);
13770baa948SKarl Rupp }
13870baa948SKarl Rupp 
13970baa948SKarl Rupp /*MC
14070baa948SKarl Rupp      PCRowScalingViennaCL  - A diagonal preconditioner (scaling rows of matrices by their norm) that can be used via the CUDA, OpenCL, and OpenMP backends of ViennaCL
14170baa948SKarl Rupp 
14270baa948SKarl Rupp    Level: advanced
14370baa948SKarl Rupp 
144*f1580f4eSBarry Smith    Developer Note:
145*f1580f4eSBarry Smith    This `PCType` does not appear to be registered
14670baa948SKarl Rupp 
147*f1580f4eSBarry Smith .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`
14870baa948SKarl Rupp M*/
14970baa948SKarl Rupp 
1509371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PCCreate_ROWSCALINGVIENNACL(PC pc) {
15170baa948SKarl Rupp   PC_ROWSCALINGVIENNACL *rowscaling;
15270baa948SKarl Rupp 
15370baa948SKarl Rupp   PetscFunctionBegin;
15470baa948SKarl Rupp   /*
15570baa948SKarl Rupp      Creates the private data structure for this preconditioner and
15670baa948SKarl Rupp      attach it to the PC object.
15770baa948SKarl Rupp   */
1589566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(pc, &rowscaling));
15970baa948SKarl Rupp   pc->data = (void *)rowscaling;
16070baa948SKarl Rupp 
16170baa948SKarl Rupp   /*
16270baa948SKarl Rupp      Initialize the pointer to zero
16370baa948SKarl Rupp      Initialize number of v-cycles to default (1)
16470baa948SKarl Rupp   */
16570baa948SKarl Rupp   rowscaling->ROWSCALINGVIENNACL = 0;
16670baa948SKarl Rupp 
16770baa948SKarl Rupp   /*
16870baa948SKarl Rupp       Set the pointers for the functions that are provided above.
16970baa948SKarl Rupp       Now when the user-level routines (such as PCApply(), PCDestroy(), etc.)
17070baa948SKarl Rupp       are called, they will automatically call these functions.  Note we
17170baa948SKarl Rupp       choose not to provide a couple of these functions since they are
17270baa948SKarl Rupp       not needed.
17370baa948SKarl Rupp   */
17470baa948SKarl Rupp   pc->ops->apply               = PCApply_ROWSCALINGVIENNACL;
17570baa948SKarl Rupp   pc->ops->applytranspose      = 0;
17670baa948SKarl Rupp   pc->ops->setup               = PCSetUp_ROWSCALINGVIENNACL;
17770baa948SKarl Rupp   pc->ops->destroy             = PCDestroy_ROWSCALINGVIENNACL;
17870baa948SKarl Rupp   pc->ops->setfromoptions      = PCSetFromOptions_ROWSCALINGVIENNACL;
17970baa948SKarl Rupp   pc->ops->view                = 0;
18070baa948SKarl Rupp   pc->ops->applyrichardson     = 0;
18170baa948SKarl Rupp   pc->ops->applysymmetricleft  = 0;
18270baa948SKarl Rupp   pc->ops->applysymmetricright = 0;
18370baa948SKarl Rupp   PetscFunctionReturn(0);
18470baa948SKarl Rupp }
185