-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.cpp
154 lines (134 loc) · 4.86 KB
/
types.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
//
// types.hh
// waveSZ
//
// Created by JianNan Tian on 6/8/19.
// Copyright © 2019 JianNan Tian. All rights reserved.
//
#include <algorithm>
#include <cmath> // for FP32 bit representation
#include <cstddef> // size_t
#include <cstdlib>
#include <iostream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#include "constants.hh"
#include "format.hh"
#include "io.hh"
#include "timer.hh"
#include "types.hh"
using namespace std;
template <typename T>
double GetDatumValueRange(string fname, size_t l)
{
auto d = io::ReadBinaryFile<T>(fname, l);
T max_ = *std::max_element(d, d + l);
T min_ = *std::min_element(d, d + l);
delete[] d;
return max_ - min_;
}
template double GetDatumValueRange<float>(string fname, size_t l);
template double GetDatumValueRange<double>(string fname, size_t l);
size_t* InitializeDims(size_t cap, size_t n_dims, size_t dim0, size_t dim1, size_t dim2, size_t dim3)
{
auto dims_L16 = new size_t[16]();
size_t dims[] = {dim0, dim1, dim2, dim3};
std::copy(dims, dims + 4, dims_L16);
dims_L16[nDIM] = n_dims;
int BLK;
if (dims_L16[nDIM] == 1) BLK = B_1d;
else if (dims_L16[nDIM] == 2)
BLK = B_2d;
else if (dims_L16[nDIM] == 3)
BLK = B_3d;
dims_L16[nBLK0] = (dims_L16[DIM0] - 1) / (size_t)BLK + 1;
dims_L16[nBLK1] = (dims_L16[DIM1] - 1) / (size_t)BLK + 1;
dims_L16[nBLK2] = (dims_L16[DIM2] - 1) / (size_t)BLK + 1;
dims_L16[nBLK3] = (dims_L16[DIM3] - 1) / (size_t)BLK + 1;
dims_L16[LEN] = dims_L16[DIM0] * dims_L16[DIM1] * dims_L16[DIM2] * dims_L16[DIM3];
dims_L16[CAP] = cap;
dims_L16[RADIUS] = cap / 2;
return dims_L16;
}
// for example, binning needs to set new dimensions
void SetDims(size_t* dims_L16, size_t new_dims[4])
{
std::copy(new_dims, new_dims + 4, dims_L16);
int BLK;
if (dims_L16[nDIM] == 1) BLK = B_1d;
else if (dims_L16[nDIM] == 2)
BLK = B_2d;
else if (dims_L16[nDIM] == 3)
BLK = B_3d;
dims_L16[nBLK0] = (dims_L16[DIM0] - 1) / (size_t)BLK + 1;
dims_L16[nBLK1] = (dims_L16[DIM1] - 1) / (size_t)BLK + 1;
dims_L16[nBLK2] = (dims_L16[DIM2] - 1) / (size_t)BLK + 1;
dims_L16[nBLK3] = (dims_L16[DIM3] - 1) / (size_t)BLK + 1;
dims_L16[LEN] = dims_L16[DIM0] * dims_L16[DIM1] * dims_L16[DIM2] * dims_L16[DIM3];
}
// typedef struct ErrorBoundConfigurator {
// int capacity, radius;
// double base, exp_base2, exp_base10;
// double eb_base2, eb_base10, eb_final;
// std::string mode;
ErrorBoundConfigurator::ErrorBoundConfigurator(int _capacity, double _precision, double _exponent, int _base)
{
capacity = _capacity;
radius = capacity / 2;
mode = std::string("ABS");
if (_precision != 1 and _base == 2) { cerr << "tmp.ly we only support 1 x pow(2, \?\?)" << endl; }
eb_final = _precision * pow(_base, _exponent);
base = _base;
exp_base10 = _base == 10 ? _exponent : log10(eb_final);
exp_base2 = _base == 2 ? _exponent : log2(eb_final);
cout << log_info << "bin.cap:\t\t" << _capacity << endl;
if (_base == 10) {
cout << log_info << "user-set eb:\t" << _precision;
cout << " x 10^(" << _exponent << ") = " << eb_final << endl;
}
else if (_base == 2) {
cout << "eb.set.to:\t"
<< "2^(" << _exponent << ") = " << eb_final << endl;
}
}
void ErrorBoundConfigurator::ChangeToRelativeMode(double value_range)
{
if (value_range == 0) {
cerr << log_err << "INVALID VALUE RANGE!" << endl;
exit(1);
}
cout << log_info << "change to r2r mode \e[2m(relative-to-value-range)\e[0m" << endl;
cout << log_null << "eb --> " << eb_final << " x " << value_range << " = ";
this->eb_final *= value_range;
cout << eb_final << endl;
mode = std::string("VRREL");
}
void ErrorBoundConfigurator::ChangeToTightBase2()
{
base = 2;
cout << log_info << "switch.to.tight.base2.mode, eb changed from " << eb_final << " = 2^(" << exp_base2 << ") to ";
cout << "the exp base2 before changing:\t" << exp_base2 << endl;
exp_base2 = floor(exp_base2);
cout << "the exp base2 after changing:\t" << exp_base2 << endl;
eb_final = pow(2, exp_base2);
cout << eb_final << " = 2^(" << exp_base2 << ")" << endl;
}
void ErrorBoundConfigurator::debug() const
{
cout << log_dbg << "exp.base10:\t" << exp_base10 << endl;
cout << log_dbg << "exp.base2:\t" << exp_base2 << endl;
cout << log_dbg << "final.eb:\t" << eb_final << endl;
}
//} config_t;
typedef struct ErrorBoundConfigurator config_t;
double* InitializeErrorBoundFamily(config_t* eb_config)
{
auto ebs_L4 = new double[4]();
ebs_L4[0] = eb_config->eb_final; // eb
ebs_L4[1] = 1 / eb_config->eb_final; // 1/eb
ebs_L4[2] = 2 * eb_config->eb_final; // 2* eb
ebs_L4[3] = 1 / (2 * eb_config->eb_final); // 1/(2*eb)
return ebs_L4;
}