-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathptree.cpp
More file actions
executable file
·428 lines (327 loc) · 11.4 KB
/
ptree.cpp
File metadata and controls
executable file
·428 lines (327 loc) · 11.4 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <math.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>
#include <cmath>
#include "ptree.h"
extern long long num_insert;
// Constructs cell
Cell::Cell(unsigned int inp_dimension) {
dimension = inp_dimension;
corner = (int*) malloc(dimension * sizeof(int));
width = (int*) malloc(dimension * sizeof(int));
}
Cell::Cell(unsigned int inp_dimension, int* inp_corner, int* inp_width) {
dimension = inp_dimension;
corner = (int*) malloc(dimension * sizeof(int));
width = (int*) malloc(dimension * sizeof(int));
for(int d = 0; d < dimension; d++) setCorner(d, inp_corner[d]);
for(int d = 0; d < dimension; d++) setWidth(d, inp_width[d]);
}
// Destructs cell
Cell::~Cell() {
free(corner);
free(width);
}
int Cell::getCorner(unsigned int d) {
return corner[d];
}
int Cell::getWidth(unsigned int d) {
return width[d];
}
void Cell::setCorner(unsigned int d, int val) {
corner[d] = val;
}
void Cell::setWidth(unsigned int d, int val) {
width[d] = val;
}
// Checks whether a point lies in a cell
bool Cell::containsPoint(double point[])
{
for(int d = 0; d < dimension; d++) {
if(point[d] <= corner[d] - width[d]) return false;
if(corner[d] + width[d] < point[d]) return false;
}
return true;
}
PTree::PTree(unsigned int D, double* inp_data, unsigned int N, unsigned int bins, int lv, int iter_cnt)
{
num_insert = 0;
// #ifdef USE_BITWISE_OP
// printf("ptree.cpp USE_BITWISE_OP\n");
// #else
// printf("ptree.cpp not USE_BITWISE_OP\n");
// #endif
// Compute mean, width, and height of current map (boundaries of SPTree)
int nD = 0;
int* mean_Y = (int*) calloc(D, sizeof(int));
#ifdef USE_BITWISE_OP
for (int d = 0; d < D; d++) mean_Y[d] = (bins >> 1) - 1;/*Op*/
#else
for(int d = 0; d < D; d++) mean_Y[d] = (bins/2) - 1;/*Op*/
#endif
// Construct PTree
int* width = (int*) malloc(D * sizeof(int));
#ifdef USE_BITWISE_OP
for (int d = 0; d < D; d++) width[d] = (bins >> 1);/*Op*/
#else
for (int d = 0; d < D; d++) width[d] = (bins /2);/*Op*/
#endif
init(NULL, D, inp_data, mean_Y, width, bins, lv, iter_cnt);
fill(N, iter_cnt); // fill every data.
// Clean up memory
free(mean_Y);
free(width);
}
// Constructor for PTree with particular size and parent -- build the tree, too!
PTree::PTree(unsigned int D, double* inp_data, unsigned int N, int* inp_corner, int* inp_width, unsigned int pixel_width, int lv, int iter_cnt)
{
init(NULL, D, inp_data, inp_corner, inp_width, pixel_width, lv, iter_cnt);
fill(N, iter_cnt);
}
// Constructor for PTree with particular size (do not fill the tree)
PTree::PTree(unsigned int D, double* inp_data, int* inp_corner, int* inp_width, unsigned int pixel_width, int lv, int iter_cnt)
{
init(NULL, D, inp_data, inp_corner, inp_width, pixel_width, lv, iter_cnt);
}
// Constructor for PTree with particular size and parent (do not fill tree)
PTree::PTree(PTree* inp_parent, unsigned int D, double* inp_data, int* inp_corner, int* inp_width, unsigned int pixel_width, int lv, int iter_cnt) {
init(inp_parent, D, inp_data, inp_corner, inp_width, pixel_width, lv, iter_cnt);
}
// Constructor for PTree with particular size and parent -- build the tree, too!
PTree::PTree(PTree* inp_parent, unsigned int D, double* inp_data, unsigned int N, int* inp_corner, int* inp_width, unsigned int pixel_width, int lv, int iter_cnt)
{
init(inp_parent, D, inp_data, inp_corner, inp_width, pixel_width, lv, iter_cnt);
fill(N, iter_cnt);
}
// Main initialization function
void PTree::init(PTree* inp_parent, unsigned int D, double* inp_data, int* inp_corner, int* inp_width, unsigned int pix_width, int lv, int iter_cnt)
{
iter_count = iter_cnt;
level = lv;
parent = inp_parent;
dimension = D;
no_children=(1<<D);
data = inp_data;
size = 0;
cum_size = 0;
pixel_width = pix_width;
//printf("tree level: %d, pixel_width: %d\n", level, pixel_width);
if (pixel_width == 1) {
//printf("pixel_width = 1, level: %d\n", level);
is_leaf = true;
} else {
is_leaf = false;
}
boundary = new Cell(dimension);
for(unsigned int d = 0; d < D; d++) boundary->setCorner(d, inp_corner[d]);
for(unsigned int d = 0; d < D; d++) boundary->setWidth(d, inp_width[d]);
children = (PTree**) malloc(no_children * sizeof(PTree*));
for(unsigned int i = 0; i < no_children; i++) children[i] = NULL;
buff = (double*) malloc(D * sizeof(double));
for(unsigned int i = 0; i < D; i++) buff[i] = .0;
}
void PTree::clean(int iter_cnt) {
size = 0;
cum_size = 0;
iter_count = iter_cnt;
}
// Destructor for PTree
PTree::~PTree()
{
for(unsigned int i = 0; i < no_children; i++) {
if(children[i] != NULL) delete children[i];
}
free(children);
free(buff);
delete boundary;
}
// Update the data underlying this tree
void PTree::setData(double* inp_data)
{
data = inp_data;
}
// Get the parent of the current tree
PTree* PTree::getParent()
{
return parent;
}
// Insert a point into the PTree
bool PTree::insert(unsigned int new_index, int iter_cnt)
{
if (is_leaf) {
size++;
num_insert++;
return true;
}
// Ignore objects which do not belong in this quad tree
double* point = data + new_index * dimension;
if(!boundary->containsPoint(point)) {
return false;
}
num_insert++;
cum_size++;
bool hasPoint = false;
for (int i = 0 ; i < no_children ; i++) {
if (children[i] != NULL){
hasPoint = true;
break;
}
}
if (hasPoint == false){
subdivide();
}
bool isOldTree = false;
for (int i = 0 ; i < no_children ; i++) {
if (children[i]->iter_count < iter_count) {
isOldTree = true;
continue;
}
}
if (isOldTree == true) {
for(unsigned int i = 0; i < no_children; i++) {
children[i]->clean(iter_cnt);
}
}
// Find out where the point can be inserted
for(unsigned int i = 0; i < no_children; i++) {
if(children[i]->insert(new_index, iter_cnt)) return true;
}
// Otherwise, the point cannot be inserted (this should never happen)
return false;
}
// Create four children which fully divide this cell into four quads of equal area
void PTree::subdivide() {
// Create new children
int* new_corner = (int*) malloc(dimension * sizeof(int));
int* new_width = (int*) malloc(dimension * sizeof(int));
#ifdef USE_BITWISE_OP
unsigned int new_pixel_width = (pixel_width >> 1); /*Op*/
#else
unsigned int new_pixel_width = (pixel_width / 2); /*Op*/
#endif
// printf("subdivide, level: %d\n", level);
for(unsigned int i = 0; i < no_children; i++) {
unsigned int div = 1;
for(unsigned int d = 0; d < dimension; d++) {
new_width[d] = .5 * boundary->getWidth(d);
#ifdef USE_BITWISE_OP
if (((i / div) & 1) == 1) new_corner[d] = boundary->getCorner(d) - .5 * boundary->getWidth(d); /*Op*/
#else
if (((i / div) % 2) == 1) new_corner[d] = boundary->getCorner(d) - .5 * boundary->getWidth(d); /*Op*/
#endif
else
new_corner[d] = boundary->getCorner(d) + .5 * boundary->getWidth(d);
#ifdef USE_BITWISE_OP
div = (div << 1);/*Op*/
#else
div = (div * 2);/*Op*/
#endif
}
children[i] = new PTree(this, dimension, data, new_corner, new_width, new_pixel_width, level+1, iter_count);
}
free(new_corner);
free(new_width);
// Empty parent node
size = 0;
is_leaf = false;
}
// Build PTree on dataset
void PTree::fill(unsigned int N, int iter_cnt)
{
for(unsigned int i = 0; i < N; i++) insert(i, iter_cnt);
}
unsigned int PTree::getDepth() {
if(is_leaf) return 1;
int depth = 0;
for(unsigned int i = 0; i < no_children; i++) depth = fmax(depth, children[i]->getDepth());
return 1 + depth;
}
// Compute non-edge forces using Barnes-Hut algorithm
void PTree::computeNonEdgeForces(unsigned int point_index, double theta, double neg_f[], double* sum_Q, double beta, int iter_cnt)
{
// Make sure that we spend no time on empty nodes or self-interactions
if(cum_size == 0 || iter_count < iter_cnt)
return;
if (is_leaf == true) {
double* point = data + point_index * dimension;
if (boundary->containsPoint(point))
return;
}
// Compute distance between point and center-of-mass
double D = .0;
unsigned int ind = point_index * dimension;
for(unsigned int d = 0; d < dimension; d++) {buff[d] = data[ind + d] - boundary->getCorner(d); }
for(unsigned int d = 0; d < dimension; d++) D += buff[d] * buff[d];
int max_width = 0;
int cur_width;
for(unsigned int d = 0; d < dimension; d++) {
cur_width = boundary->getWidth(d);
max_width = (max_width > cur_width) ? max_width : cur_width;
}
#ifdef USE_BITWISE_OP
if (is_leaf || (D!=0 && (max_width*max_width / D < theta*theta))) {/*Op*/
#else
if (is_leaf || (D!=0 && (max_width / sqrt(D) < theta))) {/*Op*/
#endif
// Compute and add t-SNE force between point and current node
D = beta / (beta + D);
double mult = cum_size * D;
*sum_Q += mult;
mult *= D;
for(unsigned int d = 0; d < dimension; d++) neg_f[d] += mult * buff[d];
}
else {
// Recursively apply Barnes-Hut to children
for(unsigned int i = 0; i < no_children; i++) {
children[i]->computeNonEdgeForces(point_index, theta, neg_f, sum_Q, beta, iter_cnt);
}
}
}
// Computes edge forces
void PTree::computeEdgeForces(unsigned long long* row_P, unsigned long long* col_P, double* val_P, int N, double* pos_f, double beta)
{
// Loop over all edges in the graph
unsigned long long ind1 = 0;
unsigned long long ind2 = 0;
double D;
for(unsigned int n = 0; n < N; n++) {
for(unsigned long long i = row_P[n]; i < row_P[n + 1]; i++) {
// Compute pairwise distance and Q-value
D = beta;
ind2 = col_P[i] * dimension;
for(unsigned int d = 0; d < dimension; d++) buff[d] = data[ind1 + d] - data[ind2 + d];
for(unsigned int d = 0; d < dimension; d++) D += buff[d] * buff[d];
D = val_P[i] * beta / D;
// Sum positive force
for(unsigned int d = 0; d < dimension; d++) pos_f[ind1 + d] += D * buff[d];
}
ind1 += dimension;
}
}
// Print out tree
void PTree::print()
{
// if(cum_size == 0) {
// printf("[%d]Empty node\n", level);
// return;
// }
//printf("level: %d, is_leaf: %d\n", level, is_leaf);
if (level == 0)
printf("total data size: %d\n", cum_size);
if(is_leaf) {
printf("Leaf node: %d, size: %d\n", level, size);
// printf("[%d]Leaf node; data = [", level);
// for(int i = 0; i < size; i++) {
// if(i < size - 1) printf("%.3f, ", data[i]);
// else printf("]\n");
// }
}
else {
// for(int d = 0; d < dimension; d++) {
// }
for(int i = 0; i < no_children; i++)
if (children[i] != NULL)
children[i]->print();
}
}