-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
544 lines (421 loc) · 18.4 KB
/
test.cpp
File metadata and controls
544 lines (421 loc) · 18.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
* Tucker-Octree multiresolution voxel data compression library
* Copyright (C) 2024 CSC - IT Center for Science Ltd
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see
* <https://www.gnu.org/licenses/>.
*/
/*
* Author: Juhani Kataja
* Affiliation: CSC - IT Center for Science Ltd
* Email: juhani.kataja@csc.fi
*/
#include "toctree.cpp"
#include <string>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <argparse.hpp>
namespace toctree_test {
using namespace std;
using namespace tree_compressor;
namespace settings {
string outfile;
};
#define NOTEST
void test_tensorsizes(std::vector<uint16_t> tensorsizes) {
#ifndef NOTEST
using namespace std;
using namespace Eigen;
const uint16_t tensorsize = tensorsizes[0];
auto datatensor = Tensor<double, 3, ColMajor>(tensorsize,tensorsize,tensorsize);
for (int i3 = 0; i3 < tensorsize; i3++) for (int i2 = 0; i2 < tensorsize; i2++) for (int i1 = 0; i1 < tensorsize; i1++)
datatensor(i1,i2,i3) = sin(M_PIf*(i1+i2+i3)/tensorsize);
for (uint16_t tensorsize_m = tensorsizes[1]; tensorsize_m < tensorsizes[2]; tensorsize_m+= tensorsizes[3]){
indexrange<uint16_t,3> K({0,0,0},{tensorsize_m,tensorsize_m,tensorsize_m});
auto view = TensorView<double, uint16_t, 3>(datatensor, K);
cout << tensorsize << ", " << tensorsize_m << ",";
for (size_t mode=0;mode<3;mode++) {
starttime
using namespace Spectra;
NormalFoldProd<double, uint16_t, 3> op(view, double(1));
op.setMode(mode);
SymEigsSolver<NormalFoldProd<double, uint16_t, 3>> eigs(op, 2, 6);
eigs.init();
int nconv = eigs.compute(SortRule::LargestMagn);
Vector<double, Dynamic> evalues;
if (eigs.info() == CompInfo::Successful) evalues = eigs.eigenvalues();
endtime
if (mode < 2) cout << ",";
}
cout << endl;
}
#endif
}
void test_indexing() {
#ifndef NOTEST
using namespace Eigen;
using namespace std;
auto datatensor = Tensor<double, 3, ColMajor>(10,20,30);
indexrange<uint16_t, 3> K({0,0,0},{9,19,29});
auto view = TensorView<double, uint16_t, 3>(datatensor, K);
for (int mode = 0; mode < 3; mode++){
for (int i = 1; i<=10; i++) {
for (int j = 1 ; j<=20;j++) {
cout << view.get_J(mode, i,j,15) << '\t';
}
cout << endl;
}
cout << endl;
}
#endif
}
void test_tucker(int big_N) {
#ifndef NOTEST
using namespace Eigen;
using namespace std;
/* const int big_N = 255; */
typedef size_t UI;
auto datatensor = Tensor<double, 3, ColMajor>(big_N,big_N,big_N);
indexrange<UI, 3> K({0,0,0},{UI(big_N-1),UI(big_N-1),UI(big_N-1)});
auto view = TensorView<double, UI, 3>(datatensor, K);
auto f = [&](int I) {return M_PIf64*(I+1)/big_N;};
for (int i = 0; i<view.size(0); i++) {
for (int j = 0 ;j<view.size(1); j++) {
for (int k = 0 ;k<view.size(2); k++) {
view(i,j,k) = sin(f(i))*sin(f(j))*cos(f(k));
}}}
cout << "sqnorm of view " << view.sqnorm() << endl;
starttime
auto tucker = Tucker<double, UI, 2, 3>(view);
tucker.make_core();
for (int i3 = 0; i3 < 2; i3++) {
for (int i1 = 0; i1 < 2; i1++) {
for (int i2 = 0; i2 < 2; i2++) {
cout << tucker.core(i1,i2,i3) << " ";
}
cout << endl;
}
cout << endl;
}
tucker.fill_residual();
cout << "sqnorm of view " << view.sqnorm() << endl << endl;
cout << "timing: ";
endtime
#endif
}
#if 0
template<typename UI>
void test_img(size_t maxiter, UI Nx, UI Ny) {
using namespace Eigen;
using namespace std;
const size_t core_rank = 2;
auto datatensor = Tensor<double, 2, ColMajor>(Nx,Ny);
indexrange<UI, 2> K({0,0},{UI(Nx-1),UI(Ny-1)});
auto tree = leaf<indexrange<UI,2>,2>();
tree.data = K;
/* divide_leaf(tree); */
UI big_N = MAX(Nx, Ny);
auto f = [&](int I) {return M_PIf64*(I+1)/big_N;};
auto F = [&](int I1, int I2) {return exp(-pow((I1+I2)/(big_N),2))*sin(M_PIf64*(I1+I2+1)/(2*big_N));};
auto view = TensorView<double,UI,2>(datatensor, K);
for (int i1 = 0; i1<view.size(0); i1++) {
for (int i2 = 0; i2<view.size(1); i2++) {
view(i1,i2) = F(i1,i2);
}}
std::stack<unique_ptr<Tucker<double,UI,core_rank,2>>> tuck_stack;
std::vector<unique_ptr<Tucker<double,UI,core_rank,2>>> tuckers;
for(int iter=0; iter<maxiter; iter++) {
cout << "iter: " << iter << " sqnorm of view " << view.sqnorm() << "\t\t| ";
double residual = -1.0;
// find worst leaf
leaf<indexrange<UI,2>,2>* worst_leaf;
for (auto it = tree.begin(); it != tree.end(); ++it) {
auto& leaf = *it;
auto c_view = TensorView<double, UI, 2>(datatensor, leaf.data);
double c_residual = c_view.get_residual();
if (c_residual > residual) {
residual = c_residual;
worst_leaf = &leaf;
}
};
// improve worst leaf
cout << "worst leaf: " << worst_leaf->level << " : "<< worst_leaf->data << " residual: " << residual;
divide_leaf(*worst_leaf);
std::unique_ptr<TensorView<double,UI,2>> c_view(new TensorView<double,UI,2>(datatensor, worst_leaf->data));
std::unique_ptr<Tucker<double, UI, core_rank, 2>> tuck(new Tucker<double,UI,core_rank,2>(std::move(c_view))); /* TODO: save tucker to leaf! */
tuck->fill_residual();
OctreeCoordinates<2> worst_coords = leaf_to_coordinates(*worst_leaf);
uint32_t atomic_coords = worst_coords.template toAtomic<uint32_t>();
cout << "\t| worst_coords: " << worst_coords <<":"<<atomic_coords<< ":"
<< OctreeCoordinates<2>(worst_coords.template toAtomic<uint32_t>(), worst_leaf->level)
<< " inds: " << K.getsubrange(worst_coords) << endl;
tuck->setCoordinates(worst_coords);
/* tuck_stack.push(std::move(tuck)); */
tuckers.push_back(std::move(tuck));
/* delete c_view; */
}
cout << "residual sqnorm: " << view.sqnorm() << std::endl;
view.fill(double(0));
for (auto& it : tuckers) {
auto& tuck = *(it);
tuck.fill_residual(double(1), double(1));
}
auto serialized = SerialTucker<double, UI, core_rank, 2, uint32_t>(tuckers, K);
compressed_toctree_t pod;
pod = serialized.to_pod();
uint8_t* bytes;
uint64_t n_bytes;
compressed_toctree_t_to_bytes(pod, &bytes, &n_bytes);
{
FILE *fp = fopen("bytes_test.bin", "wb");
fwrite(bytes, 1, n_bytes, fp);
fclose(fp);
}
auto repod = bytes_to_compressed_toctree_t(bytes, n_bytes);
if (settings::outfile.length() > 0) {
std::ofstream(settings::outfile, ios::trunc) << serialized;
} else {
cout << serialized;
}
/* auto detuckers = */
/* auto detuckers_old = serialized.Deserialize(); */
auto detuckers = Deserialize<double, UI, core_rank, 2>(repod);
view.fill(double(0));
for (auto&& tuck : detuckers) {
tuck->fill_residual(view, double(1), double(1));
}
cout << "reconstructed sqnorm: " << view.sqnorm() << std::endl;
for (int i1 = 0; i1<view.size(0); i1++) {
for (int i2 = 0; i2<view.size(1); i2++) {
view(i1,i2) = view(i1,i2) - F(i1,i2);
}}
cout << "final reconstruction error sqnorm: " << view.sqnorm() << std::endl;
}
#endif
#if 1
template<typename UI>
void test_tree(size_t maxiter, UI Nx, UI Ny, UI Nz, VDF_REAL_DTYPE tol) {
using namespace Eigen;
using namespace std;
const size_t core_rank = 2;
VDF_REAL_DTYPE* buffer = (VDF_REAL_DTYPE*)malloc(sizeof(VDF_REAL_DTYPE)*Nx*Ny*Nz);
uint8_t* bytes;
uint64_t n_bytes;
TensorMap<Tensor<VDF_REAL_DTYPE, 3, ColMajor>> datatensor(buffer, Nx, Ny,Nz);
indexrange<UI, 3> K({0,0,0},{UI(Nx-1),UI(Ny-1),UI(Nz-1)});
auto tree = leaf<indexrange<UI,3>,3>();
tree.data = K;
/* divide_leaf(tree); */
UI big_N = MAX(Nx, MAX(Ny,Nz));
auto f = [&](int I) {return M_PIf64*(I+1)/big_N;};
auto F = [&](int I1, int I2, int I3) {return exp(-3*pow((I1+I2+I3)/(big_N),2))*cos(M_PIf64*(I1+I2+I3+1)/(2*big_N));};
auto view = TensorView<VDF_REAL_DTYPE,UI,3>(datatensor, K);
for (int i1 = 0; i1<view.size(0); i1++) {
for (int i2 = 0; i2<view.size(1); i2++) {
for (int i3 = 0; i3<view.size(2); i3++) {
view(i1,i2,i3) = F(i1,i2,i3);
}}}
cout << "data sqnorm at start: " << view.sqnorm() << std::endl;
cout << "data supnorm at start: " << view.supnorm() << std::endl;
cout << "buffer[5] = " << buffer[5] << ", " << datatensor(5,0,0) << ", " << view(5,0,0) << endl;
compress_with_toctree_method(buffer, Nx, Ny, Nz, tol, &bytes, &n_bytes, maxiter);
cout << "buffer[5] = " << buffer[5] << ", " << datatensor(5,0,0) << endl;
cout << "residual sqnorm: " << view.sqnorm() << std::endl;
cout << "residual supnorm: " << view.supnorm() << std::endl;
uncompress_with_toctree_method(buffer, Nx, Ny, Nz, bytes, n_bytes);
cout << "reconstructed sqnorm: " << view.sqnorm() << std::endl;
cout << "reconstructed supnorm: " << view.supnorm() << std::endl;
for (int i1 = 0; i1<view.size(0); i1++) {
for (int i2 = 0; i2<view.size(1); i2++) {
for (int i3 = 0; i3<view.size(2); i3++) {
view(i1,i2,i3) = view(i1,i2,i3) - F(i1,i2,i3);
}}}
cout << "final reconstruction error sqnorm: " << view.sqnorm() << std::endl;
cout << "final reconstruction error supnorm: " << view.supnorm() << std::endl;
cout << "buffer[5] = " << buffer[5] << ", " << datatensor(5,0,0) << endl;
free(buffer);
}
#endif
#if 0
template<typename UI>
void test_tree(size_t maxiter, UI Nx, UI Ny, UI Nz, VDF_REAL_DTYPE tol) {
using namespace Eigen;
using namespace std;
const size_t core_rank = 2;
double* buffer = (double*)malloc(sizeof(double)*Nx*Ny*Nz);
TensorMap<Tensor<double, 3, ColMajor>> datatensor(buffer, Nx, Ny,Nz);
/* auto datatensor = Tensor<double, 3, ColMajor>(Nx,Ny,Nz); */
/* Tensor<double, 3, ColMajor> datatensor(datamap); */
indexrange<UI, 3> K({0,0,0},{UI(Nx-1),UI(Ny-1),UI(Nz-1)});
auto tree = leaf<indexrange<UI,3>,3>();
tree.data = K;
/* divide_leaf(tree); */
UI big_N = MAX(Nx, MAX(Ny,Nz));
auto f = [&](int I) {return M_PIf64*(I+1)/big_N;};
auto F = [&](int I1, int I2, int I3) {return exp(-3*pow((I1+I2+I3)/(big_N),2))*cos(M_PIf64*(I1+I2+I3+1)/(2*big_N));};
auto view = TensorView<double,UI,3>(datatensor, K);
for (int i1 = 0; i1<view.size(0); i1++) {
for (int i2 = 0; i2<view.size(1); i2++) {
for (int i3 = 0; i3<view.size(2); i3++) {
view(i1,i2,i3) = F(i1,i2,i3);
}}}
cout << "data sqnorm at start: " << view.sqnorm() << std::endl;
cout << "data supnorm at start: " << view.supnorm() << std::endl;
cout << "buffer[5] = " << buffer[5] << ", " << datatensor(5,0,0) << ", " << view(5,0,0) << endl;
std::stack<unique_ptr<Tucker<double,UI,core_rank,3>>> tuck_stack;
std::vector<unique_ptr<Tucker<double,UI,core_rank,3>>> tuckers;
for(int iter=0; iter<maxiter; iter++) {
cout << "iter: " << iter << " sqnorm of view " << view.sqnorm() << "\t\t| ";
double residual = -1.0;
// find worst leaf
leaf<indexrange<UI,3>,3>* worst_leaf;
for (auto it = tree.begin(); it != tree.end(); ++it) {
auto& leaf = *it;
auto c_view = TensorView<double, UI, 3>(datatensor, leaf.data);
double c_residual = c_view.get_residual();
if (c_residual > residual) {
residual = c_residual;
worst_leaf = &leaf;
}
};
// improve worst leaf
cout << "worst leaf: " << worst_leaf->level << " : "<< worst_leaf->data << " residual: " << residual;
divide_leaf(*worst_leaf);
std::unique_ptr<TensorView<double,UI,3>> c_view(new TensorView<double,UI,3>(datatensor, worst_leaf->data));
std::unique_ptr<Tucker<double, UI, core_rank, 3>> tuck(new Tucker<double,UI,core_rank,3>(std::move(c_view))); /* TODO: save tucker to leaf! */
tuck->fill_residual();
OctreeCoordinates<3> worst_coords = leaf_to_coordinates(*worst_leaf);
uint32_t atomic_coords = worst_coords.template toAtomic<uint32_t>();
cout << "\t| worst_coords: " << worst_coords <<":"<<atomic_coords<< ":"
<< OctreeCoordinates<3>(worst_coords.template toAtomic<uint32_t>(), worst_leaf->level)
<< " inds: " << K.getsubrange(worst_coords) << endl;
tuck->setCoordinates(worst_coords);
/* tuck_stack.push(std::move(tuck)); */
tuckers.push_back(std::move(tuck));
/* delete c_view; */
}
cout << "buffer[5] = " << buffer[5] << ", " << datatensor(5,0,0) << endl;
cout << "residual sqnorm: " << view.sqnorm() << std::endl;
cout << "residual supnorm: " << view.supnorm() << std::endl;
view.fill(double(0));
for (auto& it : tuckers) {
auto& tuck = *(it);
tuck.fill_residual(double(1), double(1));
}
auto serialized = SerialTucker<double, UI, core_rank, 3, uint32_t>(tuckers, K);
compressed_toctree_t pod;
pod = serialized.to_pod();
uint8_t* bytes;
uint64_t n_bytes;
compressed_toctree_t_to_bytes(pod, &bytes, &n_bytes);
{
FILE *fp = fopen("bytes_test.bin", "wb");
fwrite(bytes, 1, n_bytes, fp);
fclose(fp);
}
auto repod = bytes_to_compressed_toctree_t(bytes, n_bytes);
if (settings::outfile.length() > 0) {
std::ofstream(settings::outfile, ios::trunc) << serialized;
} else {
cout << serialized;
}
/* auto detuckers = */
/* auto detuckers_old = serialized.Deserialize(); */
auto detuckers = Deserialize<double, UI, core_rank, 3>(repod);
view.fill(double(0));
for (auto&& tuck : detuckers) {
tuck->fill_residual(view, double(1), double(1));
}
cout << "reconstructed sqnorm: " << view.sqnorm() << std::endl;
cout << "reconstructed supnorm: " << view.supnorm() << std::endl;
for (int i1 = 0; i1<view.size(0); i1++) {
for (int i2 = 0; i2<view.size(1); i2++) {
for (int i3 = 0; i3<view.size(2); i3++) {
view(i1,i2,i3) = view(i1,i2,i3) - F(i1,i2,i3);
}}}
cout << "final reconstruction error sqnorm: " << view.sqnorm() << std::endl;
cout << "final reconstruction error supnorm: " << view.supnorm() << std::endl;
cout << "buffer[5] = " << buffer[5] << ", " << datatensor(5,0,0) << endl;
free(buffer);
}
#endif
void test_normalprods(std::vector<uint16_t> sizes) {
#ifndef NOTEST
using namespace std;
using namespace Eigen;
auto datatensor = Tensor<double, 3, ColMajor>(sizes[0],sizes[1],sizes[2]);
for (int i3 = 0; i3 < sizes[2]; i3++) for (int i2 = 0; i2 < sizes[1]; i2++) for (int i1 = 0; i1 < sizes[0]; i1++)
datatensor(i1,i2,i3) = sin((M_PIf*(i1+i2+i3))/(sizes[1]+sizes[2]+sizes[0])/3.0);
std::array<uint16_t,3> siz = {uint16_t(int(sizes[0])-1), uint16_t(int(sizes[1])-1), uint16_t(int(sizes[2])-1)};
std::array<uint16_t,3> zer = {0,0,0};
indexrange<uint16_t,3> K(zer, siz);
/* std::cout << siz[0] << " " << siz[1] << " " << siz[2] << endl; */
auto view = TensorView<double, uint16_t,3>(datatensor, K);
auto res = view.get_residual();
NormalFoldProd<double,uint16_t, 3> op(view, 1/res);
const int modes[3] = {1,0,2};
for (int n = 0; n < 3; n++) {
const int mode = modes[n];
op.setMode(mode);
double* x = new double[op.rows()];
double* y = new double[op.rows()];
for(int i = 0; i<op.rows(); i++) x[i] = double(i);
for(int i = 0; i<op.rows(); i++) y[i] = double(i);
starttime
op.perform_op(x,y);
endtime
cout << ", ";
delete[] x; delete[] y;
}
#endif
}
}
int main(int argc, const char** argv) {
using namespace std;
using namespace Eigen;
using namespace tree_compressor;
using namespace toctree_test;
struct Myargs : public argparse::Args {
vector<uint16_t> &tensorsizes = kwarg("b,benchmark", "Size of big tensor, size of current view, max size of view, increment").set_default("0,0,0,2");
bool &indextest = flag("i,indextest", "test indexing");
bool &help = flag("h,help", "help");
vector<int> &tucker = kwarg("t,tucker", "Test tucker functionality with given tensor size.").set_default("-1");
vector<uint16_t> &normaltest = kwarg("n,normalsizes", "Size of normal-vector product tensor").set_default("0,0,0");
vector<size_t> &treetest = kwarg("T,tree", "Test toctree. Param: maxiter, Nx,Ny,Nz ").set_default("0,4,4,4");
string& outfile = kwarg("o,outfile", "Output structured data to this file.").set_default("");
vector<size_t> &imgtest = kwarg("I,img", "Test with 2d data. Param: maxiter, Nx, Ny").set_default("0,0,0");
VDF_REAL_DTYPE &tolerance = kwarg("f,tol", "Abs error tolerance. Param: tol").set_default("0.01");
};
/* parser.ignoreFirstArgument(true); */
auto args = argparse::parse<Myargs>(argc, argv);
if (args.help) args.print();
auto tensorsizes = args.tensorsizes;
auto normalsize = args.normaltest;
auto tuckersize = args.tucker;
auto treeparam = args.treetest;
auto imgparam = args.imgtest;
toctree_test::settings::outfile = args.outfile;
if (tensorsizes[0] > 0) test_tensorsizes(tensorsizes);
if (normalsize[0] > 0) test_normalprods(normalsize);
if (args.indextest) test_indexing();
if (tuckersize[0] > 0) test_tucker(tuckersize[0]);
if (treeparam[0] > 0) {
cout << "\ntesting tree next\n";
test_tree(treeparam[0], treeparam[1], treeparam[2], treeparam[3], args.tolerance);
}
#if 0
if (imgparam[0] > 0) test_img<uint32_t>(static_cast<uint32_t>(imgparam[0]), static_cast<uint32_t>(imgparam[1]), static_cast<uint32_t>(imgparam[2]));
#endif
}