forked from rioyokotalab/nbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
umv.cxx
209 lines (171 loc) · 8.28 KB
/
umv.cxx
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
#include "nbd.hxx"
#include "stdio.h"
#include "string.h"
#include "math.h"
#include <cstdlib>
#include <algorithm>
void allocNodes(struct Node A[], double** Workspace, int64_t* Lwork, const struct Base basis[], const struct CSC rels_near[], const struct CSC rels_far[], const struct CellComm comm[], int64_t levels) {
int64_t work_size = 0;
for (int64_t i = levels; i >= 0; i--) {
int64_t n_i = 0, ulen = 0, nloc = 0;
content_length(&n_i, &ulen, &nloc, &comm[i]);
int64_t nnz = rels_near[i].ColIndex[n_i];
int64_t nnz_f = rels_far[i].ColIndex[n_i];
struct Matrix* arr_m = (struct Matrix*)malloc(sizeof(struct Matrix) * (nnz + nnz_f));
A[i].A = arr_m;
A[i].S = &arr_m[nnz];
A[i].lenA = nnz;
A[i].lenS = nnz_f;
int64_t dimn = basis[i].dimR + basis[i].dimS;
int64_t dimn_up = i > 0 ? basis[i - 1].dimN : 0;
int64_t stride = dimn * dimn;
A[i].A_ptr = (double*)calloc(stride * nnz, sizeof(double));
A[i].X_ptr = (double*)calloc(dimn * ulen, sizeof(double));
int64_t k1, k2;
countMaxIJ(&k1, &k2, &rels_near[i]);
int64_t acc_required = std::max(k1 * ulen, k2 * n_i);
int64_t work_required = std::max(n_i * stride, (acc_required + n_i) * dimn);
work_size = std::max(work_size, work_required);
for (int64_t x = 0; x < n_i; x++) {
for (int64_t yx = rels_near[i].ColIndex[x]; yx < rels_near[i].ColIndex[x + 1]; yx++)
arr_m[yx] = (struct Matrix) { &A[i].A_ptr[yx * stride], dimn, dimn, dimn }; // A
for (int64_t yx = rels_far[i].ColIndex[x]; yx < rels_far[i].ColIndex[x + 1]; yx++)
arr_m[yx + nnz] = (struct Matrix) { NULL, basis[i].dimS, basis[i].dimS, dimn_up }; // S
}
if (i < levels) {
int64_t ploc = 0;
content_length(NULL, NULL, &ploc, &comm[i + 1]);
int64_t seg = basis[i + 1].dimS;
for (int64_t j = 0; j < rels_near[i].N; j++) {
int64_t x0 = std::get<0>(comm[i].LocalChild[j + nloc]) - ploc;
int64_t lenx = std::get<1>(comm[i].LocalChild[j + nloc]);
for (int64_t ij = rels_near[i].ColIndex[j]; ij < rels_near[i].ColIndex[j + 1]; ij++) {
int64_t li = rels_near[i].RowIndex[ij];
int64_t y0 = std::get<0>(comm[i].LocalChild[li]);
int64_t leny = std::get<1>(comm[i].LocalChild[li]);
for (int64_t x = 0; x < lenx; x++)
if ((x + x0) >= 0 && (x + x0) < rels_far[i + 1].N)
for (int64_t yx = rels_far[i + 1].ColIndex[x + x0]; yx < rels_far[i + 1].ColIndex[x + x0 + 1]; yx++)
for (int64_t y = 0; y < leny; y++)
if (rels_far[i + 1].RowIndex[yx] == (y + y0))
A[i + 1].S[yx].A = &A[i].A[ij].A[(y * dimn + x) * seg];
}
}
}
}
set_work_size(work_size, Workspace, Lwork);
for (int64_t i = levels; i > 0; i--) {
int64_t ibegin = 0, N_rows = 0, N_cols = 0;
content_length(&N_cols, &N_rows, &ibegin, &comm[i]);
int64_t nnz = A[i].lenA;
int64_t dimc = basis[i].dimR;
int64_t dimr = basis[i].dimS;
int64_t n_next = basis[i - 1].dimR + basis[i - 1].dimS;
int64_t ibegin_next = 0;
content_length(NULL, NULL, &ibegin_next, &comm[i - 1]);
std::vector<double*> A_next(nnz);
for (int64_t x = 0; x < N_cols; x++)
for (int64_t yx = rels_near[i].ColIndex[x]; yx < rels_near[i].ColIndex[x + 1]; yx++) {
int64_t y = rels_near[i].RowIndex[yx];
std::pair<int64_t, int64_t> px = comm[i].LocalParent[x + ibegin];
std::pair<int64_t, int64_t> py = comm[i].LocalParent[y];
int64_t ij = -1;
lookupIJ(&ij, &rels_near[i - 1], std::get<0>(py), std::get<0>(px) - ibegin_next);
A_next[yx] = &A[i - 1].A_ptr[(std::get<1>(py) * n_next + std::get<1>(px)) * basis[i].dimS + ij * n_next * n_next];
}
std::vector<double*> X_next(N_rows);
for (int64_t x = 0; x < N_rows; x++) {
std::pair<int64_t, int64_t> p = comm[i].LocalParent[x];
X_next[x] = &A[i - 1].X_ptr[std::get<1>(p) * basis[i].dimS + std::get<0>(p) * n_next];
}
batchParamsCreate(&A[i].params, dimc, dimr, basis[i].U_cpu, A[i].A_ptr, A[i].X_ptr, n_next, &A_next[0], &X_next[0],
*Workspace, work_size, N_rows, N_cols, ibegin, rels_near[i].RowIndex, rels_near[i].ColIndex);
}
int64_t child = std::get<0>(comm[0].LocalChild[0]);
int64_t clen = std::get<1>(comm[0].LocalChild[0]);
std::vector<int64_t> cdims(clen);
if (child >= 0)
for (int64_t i = 0; i < clen; i++)
cdims[i] = basis[1].DimsLr[child + i];
else
cdims.emplace_back(basis[0].Dims[0]);
int64_t low_s = clen > 0 ? basis[1].dimS : 0;
lastParamsCreate(&A[0].params, A[0].A_ptr, A[0].X_ptr, basis[0].dimN, low_s, cdims.size(), &cdims[0]);
}
void node_free(struct Node* node) {
free(node->A_ptr);
free(node->X_ptr);
free(node->A);
batchParamsDestory(&node->params);
}
struct RightHandSides { struct Matrix *X, *Xc, *Xo, *B; };
void allocRightHandSidesMV(struct RightHandSides rhs[], const struct Base base[], const struct CellComm comm[], int64_t levels) {
for (int64_t l = 0; l <= levels; l++) {
int64_t len;
content_length(NULL, &len, NULL, &comm[l]);
int64_t len_arr = len * 4;
struct Matrix* arr_m = (struct Matrix*)malloc(sizeof(struct Matrix) * len_arr);
rhs[l].X = arr_m;
rhs[l].B = &arr_m[len];
rhs[l].Xo = &arr_m[len * 2];
rhs[l].Xc = &arr_m[len * 3];
int64_t len_data = len * base[l].dimN * 2;
double* data = (double*)calloc(len_data, sizeof(double));
for (int64_t i = 0; i < len; i++) {
std::pair<int64_t, int64_t> p = comm[l].LocalParent[i];
arr_m[i] = (struct Matrix) { &data[i * base[l].dimN], base[l].dimN, 1, base[l].dimN }; // X
arr_m[i + len] = (struct Matrix) { &data[len * base[l].dimN + i * base[l].dimN], base[l].dimN, 1, base[l].dimN }; // B
double* x_next = (l == 0) ? NULL : &rhs[l - 1].X[0].A[std::get<1>(p) * base[l].dimS + std::get<0>(p) * base[l - 1].dimN];
arr_m[i + len * 2] = (struct Matrix) { x_next, base[l].dimS, 1, base[l].dimS }; // Xo
double* b_next = (l == 0) ? NULL : &rhs[l - 1].B[0].A[std::get<1>(p) * base[l].dimS + std::get<0>(p) * base[l - 1].dimN];
arr_m[i + len * 3] = (struct Matrix) { b_next, base[l].dimS, 1, base[l].dimS }; // Xc
}
}
}
void rightHandSides_free(struct RightHandSides* rhs) {
double* data = rhs->X[0].A;
if (data)
free(data);
free(rhs->X);
}
void matVecA(const struct Node A[], const struct Base basis[], const struct CSC rels_near[], double* X, const struct CellComm comm[], int64_t levels) {
int64_t lbegin = 0, llen = 0;
content_length(&llen, NULL, &lbegin, &comm[levels]);
std::vector<RightHandSides> rhs(levels + 1);
allocRightHandSidesMV(&rhs[0], basis, comm, levels);
memcpy(rhs[levels].X[lbegin].A, X, llen * basis[levels].dimN * sizeof(double));
for (int64_t i = levels; i > 0; i--) {
int64_t ibegin = 0, iboxes = 0, xlen = 0;
content_length(&iboxes, &xlen, &ibegin, &comm[i]);
level_merge_cpu(rhs[i].X[0].A, xlen * basis[i].dimN, &comm[i]);
neighbor_bcast_cpu(rhs[i].X[0].A, basis[i].dimN, &comm[i]);
dup_bcast_cpu(rhs[i].X[0].A, xlen * basis[i].dimN, &comm[i]);
for (int64_t j = 0; j < iboxes; j++)
mmult('T', 'N', &basis[i].Uo[j + ibegin], &rhs[i].X[j + ibegin], &rhs[i].Xo[j + ibegin], 1., 0.);
}
mmult('T', 'N', &A[0].A[0], &rhs[0].X[0], &rhs[0].B[0], 1., 0.);
for (int64_t i = 1; i <= levels; i++) {
int64_t ibegin = 0, iboxes = 0;
content_length(&iboxes, NULL, &ibegin, &comm[i]);
for (int64_t j = 0; j < iboxes; j++)
mmult('N', 'N', &basis[i].Uo[j + ibegin], &rhs[i].Xc[j + ibegin], &rhs[i].B[j + ibegin], 1., 0.);
for (int64_t y = 0; y < iboxes; y++)
for (int64_t xy = rels_near[i].ColIndex[y]; xy < rels_near[i].ColIndex[y + 1]; xy++) {
int64_t x = rels_near[i].RowIndex[xy];
mmult('N', 'N', &A[i].A[xy], &rhs[i].X[x], &rhs[i].B[y + ibegin], 1., 1.);
}
}
memcpy(X, rhs[levels].B[lbegin].A, llen * basis[levels].dimN * sizeof(double));
for (int64_t i = 0; i <= levels; i++)
rightHandSides_free(&rhs[i]);
}
void solveRelErr(double* err_out, const double* X, const double* ref, int64_t lenX) {
double err[2] = { 0., 0. };
for (int64_t i = 0; i < lenX; i++) {
double diff = X[i] - ref[i];
err[0] = err[0] + diff * diff;
err[1] = err[1] + ref[i] * ref[i];
}
MPI_Allreduce(MPI_IN_PLACE, err, 2, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
*err_out = sqrt(err[0] / err[1]);
}