-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathMKLDSSolver.cpp
More file actions
199 lines (168 loc) · 5.95 KB
/
Copy pathMKLDSSolver.cpp
File metadata and controls
199 lines (168 loc) · 5.95 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*This file is part of the FEBio source code and is licensed under the MIT license
listed below.
See Copyright-FEBio.txt for details.
Copyright (c) 2021 University of Utah, The Trustees of Columbia University in
the City of New York, and others.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "MKLDSSolver.h"
#include <FECore/CompactUnSymmMatrix.h>
#include <FECore/CompactSymmMatrix.h>
#ifdef PARDISO
#undef PARDISO
#include "mkl_dss.h"
#include "mkl_types.h"
#define PARDISO
#endif
#ifdef PARDISO
class MKLDSSolver::Imp
{
public:
CompactMatrix* A = nullptr;
_MKL_DSS_HANDLE_t handle = nullptr;
MKL_INT opt = MKL_DSS_DEFAULTS;
MKL_INT sym = MKL_DSS_SYMMETRIC;
int msglvl = 0;
};
BEGIN_FECORE_CLASS(MKLDSSolver, LinearSolver)
ADD_PARAMETER(m->msglvl, "msglvl");
END_FECORE_CLASS();
//-----------------------------------------------------------------------------
MKLDSSolver::MKLDSSolver(FEModel* fem) : LinearSolver(fem), m(new MKLDSSolver::Imp)
{
}
//-----------------------------------------------------------------------------
MKLDSSolver::~MKLDSSolver()
{
Destroy();
delete m;
}
//-----------------------------------------------------------------------------
SparseMatrix* MKLDSSolver::CreateSparseMatrix(Matrix_Type ntype)
{
// allocate the correct matrix format depending on matrix symmetry type
switch (ntype)
{
case REAL_SYMMETRIC : m->A = new CompactSymmMatrix(1); m->sym = MKL_DSS_SYMMETRIC; break;
case REAL_UNSYMMETRIC : m->A = new CRSSparseMatrix(1); m->sym = MKL_DSS_NON_SYMMETRIC; break;
case REAL_SYMM_STRUCTURE: m->A = new CRSSparseMatrix(1); m->sym = MKL_DSS_SYMMETRIC_STRUCTURE; break;
default:
assert(false);
m->A = nullptr;
}
return m->A;
}
//-----------------------------------------------------------------------------
bool MKLDSSolver::SetSparseMatrix(SparseMatrix* pA)
{
return (m->A != nullptr);
}
//-----------------------------------------------------------------------------
bool MKLDSSolver::PreProcess()
{
SparseMatrix& A = *m->A;
int nRows = A.Rows();
int nCols = A.Columns();
int nnz = A.NonZeroes();
// create handle
if (m->handle == nullptr)
{
m->opt = MKL_DSS_DEFAULTS;
MKL_INT error = dss_create(m->handle, m->opt);
if (error != MKL_DSS_SUCCESS) return false;
}
// define the structure
MKL_INT error = dss_define_structure(m->handle, m->sym, A.Pointers(), nRows, nCols, A.Indices(), nnz);
if (error != MKL_DSS_SUCCESS) return false;
// reorder
// m->opt = MKL_DSS_AUTO_ORDER;
// m->opt = MKL_DSS_METIS_ORDER;
m->opt = MKL_DSS_METIS_OPENMP_ORDER;
error = dss_reorder(m->handle, m->opt, 0);
if (error != MKL_DSS_SUCCESS) return false;
if (m->msglvl != 0)
{
m->opt = MKL_DSS_DEFAULTS;
const char* szstat = "PeakMem,FactorMem";
double ret[2];
dss_statistics(m->handle, m->opt, szstat, ret);
fprintf(stdout, "\nMKLDSS Memory info:\n");
fprintf(stdout, "===================\n");
fprintf(stdout, "Peak memory of symbolic factorization phase ........... : %lg KB\n", ret[0]);
fprintf(stdout, "Permanent memory for the factorization and solve phases : %lg KB\n", ret[1]);
fprintf(stdout, "\n");
}
return LinearSolver::PreProcess();
}
//-----------------------------------------------------------------------------
bool MKLDSSolver::Factor()
{
// make sure we have work to do
if ((m->A == nullptr) || (m->A->Rows() == 0)) return true;
SparseMatrix& A = *m->A;
// MKL_INT type = MKL_DSS_POSITIVE_DEFINITE;
MKL_INT type = MKL_DSS_INDEFINITE;
MKL_INT error = dss_factor_real(m->handle, type, A.Values());
if (error != MKL_DSS_SUCCESS) return false;
return true;
}
//-----------------------------------------------------------------------------
bool MKLDSSolver::BackSolve(double* x, double* b)
{
// make sure we have work to do
if ((m->A == nullptr) || (m->A->Rows() == 0)) return true;
int nRhs = 1;
m->opt = MKL_DSS_DEFAULTS;
MKL_INT error = dss_solve_real(m->handle, m->opt, b, nRhs, x);
if (error != MKL_DSS_SUCCESS) return false;
// update stats
UpdateStats(1);
return true;
}
//-----------------------------------------------------------------------------
void MKLDSSolver::Destroy()
{
if (m->handle)
{
m->opt = MKL_DSS_DEFAULTS;
MKL_INT error = dss_delete(m->handle, m->opt);
assert(error == MKL_DSS_SUCCESS);
m->handle = nullptr;
}
}
#else
//-----------------------------------------------------------------------------
class MKLDSSolver::Imp
{
public:
CompactMatrix* A = nullptr;
int msglvl = 0;
};
BEGIN_FECORE_CLASS(MKLDSSolver, LinearSolver)
ADD_PARAMETER(m->msglvl, "msglvl");
END_FECORE_CLASS();
MKLDSSolver::MKLDSSolver(FEModel* fem) : LinearSolver(fem), m(new MKLDSSolver::Imp) {}
MKLDSSolver::~MKLDSSolver() {}
SparseMatrix* MKLDSSolver::CreateSparseMatrix(Matrix_Type ntype) { return nullptr; }
bool MKLDSSolver::SetSparseMatrix(SparseMatrix* pA) { return false; }
bool MKLDSSolver::PreProcess() { return false; }
bool MKLDSSolver::Factor() { return false; }
bool MKLDSSolver::BackSolve(double* x, double* b) { return false; }
void MKLDSSolver::Destroy() {}
#endif