Repository navigation
Expand file tree
/
Copy pathUsePardiso.cpp
More file actions
284 lines (264 loc) · 10.2 KB
/
Copy pathUsePardiso.cpp
File metadata and controls
284 lines (264 loc) · 10.2 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/******************************************************************************
* HELP (Homogeneous and self-dual Efficient Linear Programming) *
* *
* An experimental implementation of Homogeneous and Self-Dual Algorithm *
* for Linear Programming. *
* *
* Author: Tian Xie (Research Center for Management Science and Information *
* Analytics, Shanghai University of Finance and Economics) *
* *
* Credits: Fundamental implementation idea originated from COPL_LP. *
* (Xiong Zhang and Yinyu Ye) *
* See http://web.stanford.edu/~yyye/Col.html . *
******************************************************************************/
#include "LP.h"
#include "mkl.h"
#include "mkl_types.h"
#include "mkl_spblas.h"
#include "mkl_pardiso.h"
const char CHAR_T = 'T';
const char CHAR_N = 'N';
const double DOUBLE_ONE = 1.0;
const double DOUBLE_NEGONE = -1.0;
void* PARDISO_pt[64];
int PARDISO_iparm[64];
int PARDISO_maxfct, PARDISO_mnum, PARDISO_mtype, PARDISO_phase, PARDISO_n, PARDISO_nrhs, PARDISO_msglvl, PARDISO_error;
int INT_dummy;
double DOUBLE_dummy;
int Perm[MAX_ROWS];
int* LinkerTocsrAt;
int nnzA; double *csrValAt; int csrRowPtrAt[MAX_COLS + 1]; int *csrColIndAt;
int nnzTriADAt; double *csrValTriADAt; int csrRowPtrTriADAt[MAX_ROWS + 1]; int *csrColIndTriADAt;
double tmp_row[MAX_ROWS], tmp_col[MAX_COLS];
double X1[MAX_COLS], X2[MAX_ROWS];
int LinearEquation_Construct()
{
//mkl_set_num_threads(4);
// PARDISO Control Parameters
// https://software.intel.com/zh-cn/node/470284#E44B4021-701A-48DA-BA29-70CFA20766AA
// https://software.intel.com/zh-cn/node/470296#0EADD82B-06F8-4262-8C98-02775C054ABE
#ifdef PRINT_DEBUG
printf("n_Row = %d;\n", n_Row);
printf("n_Col = %d;\n", n_Col);
printf("A = zeros(%d, %d);\n", n_Row, n_Col);
for (int j = 0; j < n_Col; j ++)
for (int p = V_Matrix_Col_Head[j]; p != -1; p = V_Matrix_Col_Next[p])
printf("A(%d, %d) = %lf;\n", V_Matrix_Row[p] + 1, j + 1, V_Matrix_Value[p]);
#endif
// Count A size
nnzA = 0;
for (int j = 0; j < n_Col; j ++)
for (int p = V_Matrix_Col_Head[j]; p != -1; p = V_Matrix_Col_Next[p])
nnzA ++;
csrValAt = (double*) malloc(sizeof(double) * nnzA);
csrColIndAt = (int*) malloc(sizeof(int) * nnzA);
LinkerTocsrAt = (int*) malloc(sizeof(int) * n_Element); // Note here! not nnzA
// Construct matrix At in CSR format.
// After Presolve_Init(), we can assume A is sorted by column.
nnzA = 0;
for (int j = 0; j < n_Col; j ++)
{
csrRowPtrAt[j] = nnzA;
for (int p = V_Matrix_Col_Head[j]; p != -1; p = V_Matrix_Col_Next[p])
{
LinkerTocsrAt[p] = nnzA;
csrValAt[nnzA] = V_Matrix_Value[p];
csrColIndAt[nnzA] = V_Matrix_Row[p];
nnzA ++;
}
}
csrRowPtrAt[n_Col] = nnzA;
#ifdef PRINT_TIME
double Tm = GetTime();
#endif
// Symbolic Analysis: METIS, Nested Dissection Algorithm
// Make a symbolic matrix
#ifdef DEBUG_TRACK
printf("Before ADAt Allocation\n");
#endif
ADAt_Allocate(&nnzTriADAt, &csrValTriADAt, csrRowPtrTriADAt, &csrColIndTriADAt, LinkerTocsrAt, csrRowPtrAt, csrColIndAt);
#ifdef DEBUG_TRACK
printf("After ADAt Allocation\n");
#endif
#ifdef PRINT_TIME
printf("Symbolic Multiplication: %.2lf s\n", GetTime() - Tm);
#endif
// Initialize PARDISO parameters
memset(PARDISO_pt, 0, sizeof(void*) * 64);
memset(PARDISO_iparm, 0, sizeof(int) * 64);
PARDISO_iparm[0] = 1; // Not default
PARDISO_iparm[1] = 3; // OpenMP version of the nested dissection algorithm
PARDISO_iparm[3] = 0; // Direct algorithm
PARDISO_iparm[4] = 2; // Output Perm
PARDISO_iparm[5] = 0; // Write solution into x
PARDISO_iparm[7] = 0; // Two steps of iterative refinement
PARDISO_iparm[9] = 13; // Pivoting pertubation: eps = 1e-13
PARDISO_iparm[10] = 1; // Enable scaling
PARDISO_iparm[11] = 0; // Solve AX = b
PARDISO_iparm[12] = 0; // Disable maximum weighted matching
PARDISO_iparm[17] = 0; // Disable reporting non-zero elements
PARDISO_iparm[18] = 0; // Disable reporting floating point operations (in million)
PARDISO_iparm[23] = 1; // Two-level parallel factorization algorithm
PARDISO_iparm[24] = 0; // Parallel algorithm for the solve step
PARDISO_iparm[26] = 0; // Disable matrix checker
PARDISO_iparm[27] = 0; // Double precision
PARDISO_iparm[30] = 0; // No partial solve / computing
PARDISO_iparm[33] = 0; // CNR mode: Default
PARDISO_iparm[34] = 1; // ZERO-based indexing
PARDISO_iparm[35] = 0; // Do not compute Schur complement
PARDISO_iparm[36] = 0; // CSR format
PARDISO_iparm[55] = 0; // Diagonal and pivoting control: Default
PARDISO_iparm[59] = 0; // PARDISO mode: IC (in-core)
PARDISO_maxfct = 1;
PARDISO_mnum = 1;
PARDISO_mtype = -2;
PARDISO_phase = 11;
PARDISO_n = n_Row;
PARDISO_nrhs = 1;
PARDISO_msglvl = 0;
// Always, DO NOT FORGET Perm!
#ifdef PRINT_TIME
Tm = GetTime();
#endif
#ifdef DEBUG_TRACK
printf("Before Symbolic Nested Dissection\n");
#endif
PARDISO(PARDISO_pt, &PARDISO_maxfct, &PARDISO_mnum, &PARDISO_mtype, &PARDISO_phase, &PARDISO_n, csrValTriADAt, csrRowPtrTriADAt, csrColIndTriADAt,
Perm, &PARDISO_nrhs, PARDISO_iparm, &PARDISO_msglvl, &DOUBLE_dummy, &DOUBLE_dummy, &PARDISO_error);
CheckError(PARDISO_error, "PARDISO Analysis Error!");
#ifdef DEBUG_TRACK
printf("After Symbolic Nested Dissection\n");
#endif
#ifdef PRINT_TIME
printf("Nested Dissection: %.2lf s\n", GetTime() - Tm);
#endif
PARDISO_iparm[4] = 1; // Input Perm
return 0;
}
int LinearEquation_Destruct()
{
if (LinkerTocsrAt) { free(LinkerTocsrAt); LinkerTocsrAt = 0; }
if (csrValAt) { free(csrValAt); csrValAt = 0; }
if (csrColIndAt) { free(csrColIndAt); csrColIndAt = 0; }
if (csrValTriADAt) { free(csrValTriADAt); csrValTriADAt = 0; }
if (csrColIndTriADAt) { free(csrColIndTriADAt); csrColIndTriADAt = 0; }
PARDISO_phase = -1;
PARDISO(PARDISO_pt, &PARDISO_maxfct, &PARDISO_mnum, &PARDISO_mtype, &PARDISO_phase, &PARDISO_n, &DOUBLE_dummy, &INT_dummy, &INT_dummy,
&INT_dummy, &INT_dummy, PARDISO_iparm, &PARDISO_msglvl, &DOUBLE_dummy, &DOUBLE_dummy, &PARDISO_error);
return 0;
}
double dinv[MAX_COLS];
// Renew ADA^T and factorize
void RenewLinearEquation(double* d) // d should be inversed
{
#ifdef PRINT_TIME
double Tm = GetTime();
#endif
for (int j = 0; j < n_Col; j ++)
dinv[j] = 1.0 / d[j];
ADAt_Calc(dinv, csrValTriADAt, csrRowPtrTriADAt, csrColIndTriADAt, LinkerTocsrAt, csrValAt, csrRowPtrAt, csrColIndAt);
//ADAt_Calc_FMA(dinv, csrValTriADAt, csrRowPtrTriADAt, csrColIndTriADAt, LinkerTocsrAt, csrValAt, csrRowPtrAt, csrColIndAt);
#ifdef PRINT_TIME
printf("%%Calc ADAt: %.2lf s\n", GetTime() - Tm);
#endif
#ifdef PRINT_TIME
Tm = GetTime();
#endif
/*
#ifdef DEBUG_TRACK
printf("%%Before Analysis (Given Perm)\n");
#endif
PARDISO_phase = 11;
PARDISO(PARDISO_pt, &PARDISO_maxfct, &PARDISO_mnum, &PARDISO_mtype, &PARDISO_phase, &PARDISO_n, csrValTriADAt, csrRowPtrTriADAt, csrColIndTriADAt,
Perm, &PARDISO_nrhs, PARDISO_iparm, &PARDISO_msglvl, &DOUBLE_dummy, &DOUBLE_dummy, &PARDISO_error);
CheckError(PARDISO_error, "PARDISO Analysis (Given Perm) Error!");
#ifdef DEBUG_TRACK
printf("%%After Analysis (Given Perm)\n");
#endif
#ifdef PRINT_TIME
printf("%%Analysis (Given Perm): %.2lf s\n", GetTime() - Tm);
#endif
*/
#ifdef PRINT_TIME
Tm = GetTime();
#endif
#ifdef DEBUG_TRACK
printf("%%Before Factorize\n");
#endif
PARDISO_phase = 22;
PARDISO(PARDISO_pt, &PARDISO_maxfct, &PARDISO_mnum, &PARDISO_mtype, &PARDISO_phase, &PARDISO_n, csrValTriADAt, csrRowPtrTriADAt, csrColIndTriADAt,
Perm, &PARDISO_nrhs, PARDISO_iparm, &PARDISO_msglvl, &DOUBLE_dummy, &DOUBLE_dummy, &PARDISO_error);
CheckError(PARDISO_error, "PARDISO Factorize Error!");
#ifdef DEBUG_TRACK
printf("%%After Factorize\n");
#endif
#ifdef PRINT_TIME
printf("%%Factorize: %.2lf s\n", GetTime() - Tm);
#endif
}
// Solve [ D A^T ][x_1] == [b_1]
// [ A 0 ][x_2] [b_2]
// A: m * n; D: Diagonal Matrix, n * n; x_1: n * 1; x_2: m * 1; b_1: n * 1; b_2: m * 1
// Solution: (A D^(-1) A^T) x_2 = (A D^(-1) b_1 - b_2)
// x_1 = D^(-1) (b_1 - A^T x_2)
// Need to call RenewLinearEquation(d) if ADA^T is not factorized yet!
int SolveLinearEquation(double* d, double* b_1, double* b_2, double* x_1, double* x_2)
{
#ifdef PRINT_DEBUG
printf("D = zeros(%d, 1);\n", n_Col);
for (int i = 0; i < n_Col; i ++)
printf("D(%d) = %.6lf;\n", i + 1, d[i]);
printf("D = zeros(, 1);\n", n_Col);
printf("b_1 = zeros(%d, 1);\n", n_Col);
for (int i = 0; i < n_Col; i ++)
printf("b_1(%d) = %.6lf;\n", i + 1, b_1[i]);
printf("b_2 = zeros(%d, 1);\n", n_Row);
for (int i = 0; i < n_Row; i ++)
printf("b_2(%d) = %.6lf;\n", i + 1, b_2[i]);
#endif
for (int i = 0; i < n_Row; i ++)
tmp_row[i] = -b_2[i];
// D^(-1) b_1
for (int i = 0; i < n_Col; i ++)
tmp_col[i] = b_1[i] / d[i];
// A * (D^(-1) b_1) - b_2
char MKL_matdescra[6];
MKL_matdescra[0] = 'G';
MKL_matdescra[3] = 'C';
mkl_dcsrmv(&CHAR_T, &n_Col, &n_Row, &DOUBLE_ONE, MKL_matdescra, csrValAt, csrColIndAt, csrRowPtrAt, csrRowPtrAt + 1, tmp_col, &DOUBLE_ONE, tmp_row);
// Solve (A D^(-1) A^T) x_2 = (A D^(-1) b_1 - b_2)
#ifdef PRINT_TIME
double Tm = GetTime();
#endif
#ifdef DEBUG_TRACK
printf("%%Before Solve\n");
#endif
PARDISO_phase = 33;
PARDISO(PARDISO_pt, &PARDISO_maxfct, &PARDISO_mnum, &PARDISO_mtype, &PARDISO_phase, &PARDISO_n, csrValTriADAt, csrRowPtrTriADAt, csrColIndTriADAt,
Perm, &PARDISO_nrhs, PARDISO_iparm, &PARDISO_msglvl, tmp_row, X2, &PARDISO_error);
CheckError(PARDISO_error, "PARDISO Solve Error!");
#ifdef DEBUG_TRACK
printf("%%After Solve\n");
#endif
#ifdef PRINT_TIME
printf("%%Solve: %.2lf s\n", GetTime() - Tm);
#endif
for (int i = 0; i < n_Row; i ++)
x_2[i] = X2[i];
#ifdef PRINT_DEBUG
for (int i = 0; i < n_Row; i ++)
printf("x_2(%d) = %lf\n", i + 1, x_2[i]);
#endif
// b_1 - A^T x_2
for (int i = 0; i < n_Col; i ++)
tmp_col[i] = b_1[i];
mkl_dcsrmv(&CHAR_N, &n_Col, &n_Row, &DOUBLE_NEGONE, MKL_matdescra, csrValAt, csrColIndAt, csrRowPtrAt, csrRowPtrAt + 1, x_2, &DOUBLE_ONE, tmp_col);
// Let x_1 = D^(-1) (b_1 - A^T x_2)
for (int i = 0; i < n_Col; i ++)
x_1[i] = tmp_col[i] / d[i];
#ifdef PRINT_DEBUG
for (int i = 0; i < n_Col; i ++)
printf("x_1(%d) = %lf\n", i + 1, x_1[i]);
#endif
return 0;
}