-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcuFoamJacobi.C
79 lines (60 loc) · 2.33 KB
/
cuFoamJacobi.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "cuFoamJacobi.H"
#include "gis/gis.h"
#include "gis/ldu2csr.h"
namespace Foam {
defineTypeNameAndDebug(cuFoamJacobi, 0);
// 对称的(symmetric)
lduMatrix::solver::addsymMatrixConstructorToTable<cuFoamJacobi>
addJacobiSolverSymMatrixConstructorToTable_;
// 不对称的(asymmetric)
lduMatrix::solver::addasymMatrixConstructorToTable<cuFoamJacobi>
addJacobiSolverAsymMatrixConstructorToTable_;
} // ~namespace Foam
Foam::cuFoamJacobi::cuFoamJacobi(
const word &fieldName,
const lduMatrix &matrix,
const FieldField<Field, scalar> &coupleBouCoeffs,
const FieldField<Field, scalar> &coupleIntCoeffs,
const lduInterfaceFieldPtrsList &interfaces,
const dictionary &solverControls
) :
lduMatrix::solver(
fieldName,
matrix,
coupleIntCoeffs,
coupleIntCoeffs,
interfaces,
solverControls
)
{}
Foam::solverPerformance Foam::cuFoamJacobi::solve(
scalarField &psi,
const scalarField &source,
const direction cmpt
) const {
IndexType N = matrix().diag().size();
// 将ldu格式的矩阵转换成csr格式
CPU::SparseCSR<ValueType, IndexType> A = getSparseCSRMat<ValueType, IndexType>(matrix());
// 拷贝传入的向量
CPU::Vector<ValueType, IndexType> b(N);
std::copy(source.begin(), source.end(), b.begin());
CPU::Vector<ValueType, IndexType> x(N);
std::copy(psi.begin(), psi.end(), x.begin());
// 将数据传输到GPU上
SingleGPU::SparseCSR<ValueType, IndexType> d_A{A};
SingleGPU::Vector<ValueType, IndexType> d_b{b};
SingleGPU::Vector<ValueType, IndexType> d_x{x};
ValueType norm_factor = NormFactor(d_A, d_b, d_x);
IndexType max_steps = maxIter_; // 最大迭代步数
ValueType error = tolerance_; // 绝对误差,relTol_是相对误差
std::vector<ValueType> res(max_steps);
Jacobi(&d_x, d_A, d_b, error * norm_factor, max_steps, &res);
// 将结果传回OpenFOAM
d_x.CopyToHost(x);
std::copy(x.begin(), x.end(), psi.begin());
solverPerformance solverPerf("cuFoamJacobi", fieldName());
solverPerf.initialResidual() = res.front();
solverPerf.finalResidual() = res.back();
solverPerf.nIterations() = res.size() - 1;
return solverPerf;
}