-
Notifications
You must be signed in to change notification settings - Fork 4
/
coarse_solver.c
133 lines (105 loc) · 5.17 KB
/
coarse_solver.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
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
#include "coarse_solver.h"
void initializeCoarseSolver(const struct gridContext gc, const int l,
struct CoarseSolverData *solverData,
const struct CSRMatrix M) {
solverData->cholmodCommon = malloc(sizeof(cholmod_common));
cholmod_start(solverData->cholmodCommon);
solverData->cholmodCommon->nmethods = 9;
solverData->sparseMatrix = cholmod_allocate_sparse(
M.nrows, /* # of rows of A */
M.nrows, /* # of columns of A */
M.nnz, /* max # of nonzeros of A */
1, /* TRUE if columns of A sorted, FALSE otherwise */
1, /* TRUE if A will be packed, FALSE otherwise */
1, /* stype of A 0=use both upper and lower, 1=use upper, -1 use lower */
CHOLMOD_REAL, /* CHOLMOD_PATTERN, _REAL, _COMPLEX, or _ZOMPLEX */
solverData->cholmodCommon);
#pragma omp parallel for schedule(static)
for (int i = 0; i < M.nrows + 1; i++)
((int *)solverData->sparseMatrix->p)[i] = M.rowOffsets[i];
#pragma omp parallel for schedule(static)
for (int i = 0; i < M.nnz; i++)
((int *)solverData->sparseMatrix->i)[i] = M.colIndex[i];
solverData->factoredMatrix = cholmod_analyze(
solverData->sparseMatrix, /* matrix to order and analyze */
solverData->cholmodCommon);
solverData->rhs = cholmod_allocate_dense(
M.nrows, /* # of rows of matrix */
1, /* # of cols of matrix */
M.nrows, /* leading dimension */
CHOLMOD_REAL, /* CHOLMOD_REAL, _COMPLEX, or _ZOMPLEX */
solverData->cholmodCommon);
solverData->solution = NULL;
solverData->Y_workspace = NULL;
solverData->E_workspace = NULL;
}
void freeCoarseSolver(const struct gridContext gc, const int l,
struct CoarseSolverData *solverData,
const struct CSRMatrix M) {
// cholmod_print_common("common", solverData->cholmodCommon);
cholmod_free_dense(&solverData->rhs, solverData->cholmodCommon);
cholmod_free_dense(&solverData->solution, solverData->cholmodCommon);
cholmod_free_dense(&solverData->Y_workspace, solverData->cholmodCommon);
cholmod_free_dense(&solverData->E_workspace, solverData->cholmodCommon);
cholmod_free_sparse(&solverData->sparseMatrix, solverData->cholmodCommon);
cholmod_free_factor(&solverData->factoredMatrix, solverData->cholmodCommon);
cholmod_finish(solverData->cholmodCommon);
free(solverData->cholmodCommon);
}
void factorizeSubspaceMatrix(const struct gridContext gc, const int l,
struct CoarseSolverData solverData,
const struct CSRMatrix M) {
#pragma omp parallel for
for (int i = 0; i < M.nnz; i++)
((double *)solverData.sparseMatrix->x)[i] = M.vals[i];
cholmod_factorize(solverData.sparseMatrix, solverData.factoredMatrix,
solverData.cholmodCommon);
}
void solveSubspaceMatrix(const struct gridContext gc, const int l,
struct CoarseSolverData solverData, const CTYPE *in,
CTYPE *out) {
const int ncell = pow(2, l);
const int32_t nelxc = gc.nelx / ncell;
const int32_t nelyc = gc.nely / ncell;
const int32_t nelzc = gc.nelz / ncell;
const int32_t nxc = nelxc + 1;
const int32_t nyc = nelyc + 1;
const int32_t nzc = nelzc + 1;
const int paddingyc =
(STENCIL_SIZE_Y - ((nelyc + 1) % STENCIL_SIZE_Y)) % STENCIL_SIZE_Y;
const int paddingzc =
(STENCIL_SIZE_Z - ((nelzc + 1) % STENCIL_SIZE_Z)) % STENCIL_SIZE_Z;
const int wrapyc = nelyc + paddingyc + 3;
const int wrapzc = nelzc + paddingzc + 3;
// copy grid data to vector
#pragma omp for collapse(3)
for (int i = 1; i < nxc + 1; i++)
for (int k = 1; k < nzc + 1; k++)
for (int j = 1; j < nyc + 1; j++) {
const int nidx = ((i - 1) * nyc * nzc + (k - 1) * nyc + (j - 1));
const int nidx_s = (i * wrapyc * wrapzc + wrapyc * k + j);
((double *)solverData.rhs->x)[3 * nidx + 0] = in[3 * nidx_s + 0];
((double *)solverData.rhs->x)[3 * nidx + 1] = in[3 * nidx_s + 1];
((double *)solverData.rhs->x)[3 * nidx + 2] = in[3 * nidx_s + 2];
}
cholmod_solve2(CHOLMOD_A, /* system to solve */
solverData.factoredMatrix, /* factorization to use */
solverData.rhs, /* right-hand-side */
NULL, /* handle */
&solverData.solution, /* solution, allocated if need be */
NULL, /* handle*/
&solverData.Y_workspace, /* workspace, or NULL */
&solverData.E_workspace, /* workspace, or NULL */
solverData.cholmodCommon);
// copy data back to grid format
#pragma omp for collapse(3)
for (int i = 1; i < nxc + 1; i++)
for (int k = 1; k < nzc + 1; k++)
for (int j = 1; j < nyc + 1; j++) {
const int nidx = ((i - 1) * nyc * nzc + (k - 1) * nyc + (j - 1));
const int nidx_s = (i * wrapyc * wrapzc + wrapyc * k + j);
out[3 * nidx_s + 0] = ((double *)solverData.solution->x)[3 * nidx + 0];
out[3 * nidx_s + 1] = ((double *)solverData.solution->x)[3 * nidx + 1];
out[3 * nidx_s + 2] = ((double *)solverData.solution->x)[3 * nidx + 2];
}
}