1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
5 \\ / A nd | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
25 Preconditioned Conjugate Gradient solver with run-time selectable
29 Dominik Christ, Wikki Ltd.
30 Based on Cufflink library by Daniel P. Combest
32 \*---------------------------------------------------------------------------*/
34 #include "cudaBiCGStab.H"
36 // Preconditioner and solver are hardwired due to
37 // code structure of Cusp library. Below are
38 // external functions with solver/preconditioner combinations
40 // BiCG with diagonal preconditioning
41 extern "C" void bicgDiag
43 cuspEquationSystem* ces,
44 cudaSolverPerformance* solverParam
47 // BiCG with Ainv preconditioning
48 extern "C" void bicgAinv
50 cuspEquationSystem* ces,
51 cudaSolverPerformance* solverPerf,
52 ValueType drop_tolerance,
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 defineTypeNameAndDebug(cudaBiCGStab, 0);
65 lduSolver::addasymMatrixConstructorToTable<cudaBiCGStab>
66 addcudaBiCGStabAsymMatrixConstructorToTable_;
70 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
72 // - Construct from matrix and solver data stream
73 Foam::cudaBiCGStab::cudaBiCGStab
75 const word& fieldName,
76 const lduMatrix& matrix,
77 const FieldField<Field, scalar>& coupleBouCoeffs,
78 const FieldField<Field, scalar>& coupleIntCoeffs,
79 const lduInterfaceFieldPtrsList& interfaces,
80 const dictionary& dict
95 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
97 Foam::lduSolverPerformance Foam::cudaBiCGStab::solve
100 const scalarField& b,
104 // Initialize Cusp solver perfomance
105 cudaSolverPerformance solverPerf = cudaSolverPerformanceDefault();
106 solverPerf.minIter = minIter(); // Minimum iterations
107 solverPerf.maxIter = maxIter(); // Maximum iterations
108 solverPerf.relTol = relTolerance(); // Relative tolerance
109 solverPerf.tol = tolerance(); // Tolerance
111 if (lduMatrix::debug >= 2)
113 solverPerf.debugCusp = true;
116 // Initialize and copy matrix data to GPU
117 cuspEquationSystem ces = createAsymCuspMatrix(matrix(), x, b);
119 // Call solver externally
120 word preconName(dict().lookup("preconditioner"));
121 if (preconName == "diagonal")
123 bicgDiag(&ces, &solverPerf);
125 else if(preconName == "Ainv")
131 dict().lookupOrDefault<scalar>("dropTolerance", 0.1),
132 dict().lookupOrDefault<label>("nonzeroPerRow", -1),
133 dict().lookupOrDefault<bool>("LinDropping", false),
134 dict().lookupOrDefault<label>("LinParameter", 1)
139 FatalErrorIn("cudaBiCGStab::solver()")
140 << "Unknown preconditioner name. "
141 << "Options are:" << nl
146 << abort(FatalError);
149 // copy the x vector back to Openfoam
150 thrust::copy(ces.X.begin(), ces.X.end(), x.begin());
152 // Return solver output
153 return lduSolverPerformance
159 solverPerf.nIterations,
160 solverPerf.converged,
167 // ************************************************************************* //