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/>.
24 \*---------------------------------------------------------------------------*/
28 // Preconditioner and solver are hardwired due to
29 // code structure of Cusp library. Below are
30 // external functions with solver/preconditioner combinations
32 // CG with diagonal preconditioning
33 extern "C" void cgDiag
35 cuspEquationSystem* ces,
36 cudaSolverPerformance* solverParam
39 // CG with Ainv preconditioning
40 extern "C" void cgAinv
42 cuspEquationSystem* ces,
43 cudaSolverPerformance* solverPerf,
44 ValueType drop_tolerance,
50 // CG with amg preconditioning
53 cuspEquationSystem* ces,
54 cudaSolverPerformance* solverParam,
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
62 defineTypeNameAndDebug(cudaCG, 0);
64 lduSolver::addsymMatrixConstructorToTable<cudaCG>
65 addcudaCGSymMatrixConstructorToTable_;
69 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
71 // - Construct from matrix and solver data stream
74 const word& fieldName,
75 const lduMatrix& matrix,
76 const FieldField<Field, scalar>& coupleBouCoeffs,
77 const FieldField<Field, scalar>& coupleIntCoeffs,
78 const lduInterfaceFieldPtrsList& interfaces,
79 const dictionary& dict
94 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96 Foam::lduSolverPerformance Foam::cudaCG::solve
103 // Initialize Cusp solver perfomance
104 cudaSolverPerformance solverPerf = cudaSolverPerformanceDefault();
105 solverPerf.minIter = minIter(); // Minimum iterations
106 solverPerf.maxIter = maxIter(); // Maximum iterations
107 solverPerf.relTol = relTolerance(); // Relative tolerance
108 solverPerf.tol = tolerance(); // Tolerance
110 if (lduMatrix::debug >= 2)
112 solverPerf.debugCusp = true;
115 // Initialize and copy matrix data to GPU
116 cuspEquationSystem ces = createSymCuspMatrix(matrix(), x, b);
118 // Call solver externally
119 word preconName(dict().lookup("preconditioner"));
120 if (preconName == "diagonal")
122 cgDiag(&ces, &solverPerf);
124 else if(preconName == "Ainv")
130 dict().lookupOrDefault<scalar>("dropTolerance", 0.1),
131 dict().lookupOrDefault<label>("nonzeroPerRow", -1),
132 dict().lookupOrDefault<bool>("LinDropping", false),
133 dict().lookupOrDefault<label>("LinParameter", 1)
136 else if(preconName == "amg")
142 dict().lookupOrDefault<scalar>("theta", 0)
147 FatalErrorIn("cudaCG::solver()")
148 << "Unknown preconditioner name. "
149 << "Options are:" << nl
155 << abort(FatalError);
158 // Copy the x vector back to Openfoam
159 thrust::copy(ces.X.begin(), ces.X.end(), x.begin());
161 // Return solver output
162 return lduSolverPerformance
168 solverPerf.nIterations,
169 solverPerf.converged,
176 // ************************************************************************* //