-
Notifications
You must be signed in to change notification settings - Fork 1
/
bbfmm.cpp
287 lines (245 loc) · 10.2 KB
/
bbfmm.cpp
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
284
285
286
/*
* BlackBox Fast Multipole Method without cache in 2D.
*
* copyright@ Yimin Zhong. yzhong@math.utexas.edu. All Rights Reserved.
*
* Algorithm refers Darve and Fong's paper on bbfmm.
*
*/
#include "bbfmm.h"
namespace bbfmm {
void
tree::populate(vector<point> &_source, vector<point> &_target, index_t _nSource, index_t _nTarget, index_t _rank,
index_t _maxLevel) {
this->sourceTree = _source;
this->targetTree = _target;
this->nSource = _nSource;
this->nTarget = _nTarget;
this->maxLevel = 0;
this->rank = _rank;
getCenterRadius(_source);
this->root = 0;
this->dict.push_back(node(0, 0));
this->maxId = root;
dict[root].nSource = nSource;
dict[root].nTarget = nTarget;
dict[root].center = center;
dict[root].radius = radius;
dict[root].sourceIndex.resize((unsigned long) nSource);
dict[root].targetIndex.resize((unsigned long) nTarget);
for (index_t i = 0; i < nSource; ++i) {
dict[root].sourceIndex[i] = i;
}
for (index_t i = 0; i < nTarget; ++i) {
dict[root].targetIndex[i] = i;
}
RUN("initialization", assignChildren(root, _maxLevel));
RUN("assign lists", buildTree());
}
void tree::getCenterRadius(vector<point> &_source) {
assert(_source.size() > 0);
scalar_t x_max = _source[0].x;
scalar_t x_min = _source[0].x;
scalar_t y_max = _source[0].y;
scalar_t y_min = _source[0].y;
for (size_t i = 0; i < _source.size(); ++i) {
x_max = std::max(x_max, _source[i].x);
y_max = std::max(y_max, _source[i].y);
x_min = std::min(x_min, _source[i].x);
y_min = std::min(y_min, _source[i].y);
}
this->center.x = (x_max + x_min) / 2.0;
this->center.y = (y_max + y_min) / 2.0;
this->radius.x = (x_max - x_min) / 2.0;
this->radius.y = (y_max - y_min) / 2.0;
}
void tree::assignChildren(index_t _id, index_t _maxLevel) {
/*
* when assigning children nodes, the points are not assigned due to storage.
*
* Now the limitation of nodes is around 2^24.
*/
assert(root != -1); // check tree is non-empty
// check source
if (dict[_id].nSource == 0) {
dict[_id].isLeaf = true;
dict[_id].isEmpty = true;
} else {
// divide
if ((dict[_id].nSource <= rank) || (dict[_id].nLevel == _maxLevel)) {
dict[_id].isLeaf = true;
if (maxLevel < dict[_id].nLevel) {
maxLevel = dict[_id].nLevel;
}
} else {
// not a leaf
for (index_t i = 0; i < 4; ++i) {
maxId += 1;
dict[_id].child[i] = maxId;
dict.push_back(node(dict[_id].nLevel + 1, i));
dict[maxId].parent = _id;
dict[maxId].center.x = dict[_id].center.x + ((i & 1) - 0.5) * dict[_id].radius.x;
dict[maxId].center.y = dict[_id].center.y + (((i >> 1) & 1) - 0.5) * dict[_id].radius.y;
dict[maxId].radius.x = dict[_id].radius.x * 0.5;
dict[maxId].radius.y = dict[_id].radius.y * 0.5;
dict[maxId].nSource = 0;
dict[maxId].nTarget = 0;
}
/*
* can be accelerated by **reduce**
*/
for (index_t i = 0; i < dict[_id].nSource; ++i) {
index_t index = dict[_id].sourceIndex[i];
index_t y_bit = sourceTree[index].y < dict[_id].center.y ? 0 : 1;
index_t x_bit = sourceTree[index].x < dict[_id].center.x ? 0 : 1;
index_t childIndex = 2 * y_bit + x_bit;
index_t childId = dict[_id].child[childIndex];
dict[childId].sourceIndex.push_back(index);
dict[childId].nSource += 1;
}
/*
* can be accelerated by **reduce**
*/
for (index_t i = 0; i < dict[_id].nTarget; ++i) {
index_t index = dict[_id].targetIndex[i];
index_t y_bit = targetTree[index].y < dict[_id].center.y ? 0 : 1;
index_t x_bit = targetTree[index].x < dict[_id].center.x ? 0 : 1;
index_t childIndex = 2 * y_bit + x_bit;
index_t childId = dict[_id].child[childIndex];
dict[childId].targetIndex.push_back(index);
dict[childId].nTarget += 1;
}
for (index_t i = 0; i < 4; ++i) {
assignChildren(dict[_id].child[i], _maxLevel);
}
}
}
}
void tree::buildTree() {
point min_p(dict[root].center.x - dict[root].radius.x,
dict[root].center.y - dict[root].radius.y);
point max_p(dict[root].center.x + dict[root].radius.x,
dict[root].center.y + dict[root].radius.y);
index_t i;
#ifdef RUN_OMP
#pragma omp parallel for private(i) shared(min_p, max_p) schedule(dynamic)
#endif
for (i = 0; i < dict.size(); ++i) {
buildNode(i, min_p, max_p);
}
}
void tree::buildNode(index_t _id, point &min_p, point &max_p) {
node &n = dict[_id];
n.uList.clear();
n.vList.clear();
n.wList.clear();
n.xList.clear();
// not root
if (n.parent != -1) {
node &pn = dict[n.parent];
scalar_t dx = n.radius.x;
scalar_t dy = n.radius.y;
scalar_t xs = pn.center.x - dx;
scalar_t ys = pn.center.y - dy;
point cur;
for (index_t x_id = -2; x_id < 4; x_id++) {
for (index_t y_id = -2; y_id < 4; y_id++) {
cur.x = xs + 2 * x_id * dx;
cur.y = ys + 2 * y_id * dy;
// check box and not itself.
if (cur <= max_p && cur >= min_p && !(cur == n.center)) {
//find node.
index_t curId = findNode(0, cur);
bool adj = isAdjacent(_id, curId);
node &curNode = dict[curId];
if (curNode.nLevel < n.nLevel) {
if (adj) {
if (curNode.isLeaf) {
n.uList.insert(curId);
}
} else {
n.xList.insert(curId);
}
}
if (curNode.nLevel == n.nLevel) {
if (!adj) {
n.vList.insert(curId);
} else {
if (n.isLeaf) {
std::queue<index_t> rest;
rest.push(curId);
while (!rest.empty()) {
index_t frontId = rest.front();
rest.pop();
node &frontNode = dict[frontId];
if (!isAdjacent(frontId, _id)) {
n.wList.insert(frontId);
} else {
if (frontNode.isLeaf) {
n.uList.insert(frontId);
} else {
for (index_t i = 0; i < 4; ++i) {
rest.push(frontNode.child[i]);
}
}
}
}
}
}
}
}
}
}
}
if (n.isLeaf) {
n.uList.insert(_id);
}
n.nUList = (index_t) n.uList.size();
n.nWList = (index_t) n.wList.size();
n.nVList = (index_t) n.vList.size();
n.nXList = (index_t) n.xList.size();
}
index_t tree::findNode(index_t _id, point &p) {
node &n = dict[_id];
if (n.center == p) return _id;
else {
if (n.isLeaf) {
return _id;
} else {
index_t x_bit = n.center.x > p.x ? 0 : 1;
index_t y_bit = n.center.y > p.y ? 0 : 1;
index_t id = 2 * y_bit + x_bit;
return findNode(n.child[id], p);
}
}
}
bool tree::isAdjacent(index_t _aId, index_t _bId) {
node &nA = dict[_aId];
node &nB = dict[_bId];
scalar_t diff_x = fabs(nA.center.x - nB.center.x);
scalar_t diff_y = fabs(nA.center.y - nB.center.y);
scalar_t r_x = fabs(nA.radius.x + nB.radius.x);
scalar_t r_y = fabs(nA.radius.y + nB.radius.y);
bool rdx = r_x >= diff_x - EPS;
bool rdy = r_y >= diff_y - EPS;
bool x_adj = (fabs(diff_x - r_x) < EPS) && (rdy);
bool y_adj = (fabs(diff_y - r_y) < EPS) && (rdx);
return x_adj || y_adj;
}
void tree::output(std::string file) {
std::ofstream file_stream(file);
if (file_stream.is_open()) {
for (size_t i = 0; i < dict.size(); ++i) {
file_stream << dict[i].center.x << " "
<< dict[i].center.y << " "
<< dict[i].radius.x << " "
<< dict[i].radius.y << " "
<< dict[i].nVList << " " << dict[i].nXList << " " << dict[i].nUList << " "
<< dict[i].nWList << " " << dict[i].isLeaf << " " << dict[i].nSource << "\n";
}
file_stream.close();
} else {
std::cout << "cannot open file: " << file << std::endl;
}
}
}