-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogtest.h
More file actions
237 lines (211 loc) · 7.85 KB
/
Copy pathlogtest.h
File metadata and controls
237 lines (211 loc) · 7.85 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
/*
* logtest.h
*
* Created on: Jul 3, 2019
* Author: Matthias Jung
*/
#ifndef SRC_LOGTEST_H_
#define SRC_LOGTEST_H_
#include <chrono>
#include <cmath>
#include <future>
#include <iostream>
#include <vector>
#include <thread>
#include "logapprox.h"
struct MaxErrorFuture {
double dMaxError[7] = { 0 };
};
template <typename T>
static void validateWorkerP5(size_t uNumSamples, size_t uStart, size_t uEnd, std::promise<MaxErrorFuture> *promiseObj) {
T dStepSize = 1.0/static_cast<T>(uNumSamples);
MaxErrorFuture retVal;
double *dMaxError = &retVal.dMaxError[0];
// generate data for mantissa only (since exponent is perfect)
// actual representation is from [1,2], so we sample that range
for(size_t i = uStart; i < uEnd; ++i) {
T x = 1.0 + static_cast<T>(i) * dStepSize;
T dPrecise = log2(x);
T dApprox = fastLog2p5<T>(x);
double dDiff = static_cast<double>(fabs(dPrecise - dApprox));
if(dDiff > dMaxError[0])
{
dMaxError[0] = dDiff;
}
}
promiseObj->set_value(std::move(retVal));
}
template <typename T>
static void validateWorkerP6(size_t uNumSamples, size_t uStart, size_t uEnd, std::promise<MaxErrorFuture> *promiseObj) {
T dStepSize = 1.0/static_cast<T>(uNumSamples);
MaxErrorFuture retVal;
double *dMaxError = &retVal.dMaxError[0];
// generate data for mantissa only (since exponent is perfect)
// actual representation is from [1,2], so we sample that range
for(size_t i = uStart; i < uEnd; ++i) {
T x = 1.0 + static_cast<T>(i) * dStepSize;
T dPrecise = log2(x);
T dApprox = fastLog2p6<T>(x);
double dDiff = static_cast<double>(fabs(dPrecise - dApprox));
if(dDiff > dMaxError[0])
{
dMaxError[0] = dDiff;
}
}
promiseObj->set_value(std::move(retVal));
}
template <typename T>
static void validateWorker(size_t uNumSamples, size_t uStart, size_t uEnd, std::promise<MaxErrorFuture> *promiseObj) {
T dStepSize = 1.0/static_cast<T>(uNumSamples);
MaxErrorFuture retVal;
double *dMaxError = &retVal.dMaxError[0];
// generate data for mantissa only (since exponent is perfect)
// actual representation is from [1,2], so we sample that range
for(size_t i = uStart; i < uEnd; ++i) {
T x = 1.0 + static_cast<T>(i) * dStepSize;
T dPrecise = log2(x);
T dApprox = fastLog2p1<T>(x);
double dDiff = static_cast<double>(fabs(dPrecise - dApprox));
if(dDiff > dMaxError[0])
{
dMaxError[0] = dDiff;
}
dApprox = fastLog2p2<T>(x);
dDiff = fabs(dPrecise - dApprox);
if(dDiff > dMaxError[1])
{
dMaxError[1] = dDiff;
}
dApprox = fastLog2p3<T>(x);
dDiff = fabs(dPrecise - dApprox);
if(dDiff > dMaxError[2])
{
dMaxError[2] = dDiff;
}
dApprox = fastLog2p4<T>(x);
dDiff = fabs(dPrecise - dApprox);
if(dDiff > dMaxError[3])
{
dMaxError[3] = dDiff;
}
dApprox = fastLog2p5<T>(x);
dDiff = fabs(dPrecise - dApprox);
if(dDiff > dMaxError[4])
{
dMaxError[4] = dDiff;
}
dApprox = fastLog2p6<T>(x);
dDiff = fabs(dPrecise - dApprox);
if(dDiff > dMaxError[5])
{
dMaxError[5] = dDiff;
}
// dApprox = log2f(x);
// dDiff = fabs(dPrecise - dApprox);
// if(dDiff > dMaxError[6])
// {
// dMaxError[6] = dDiff;
// }
}
promiseObj->set_value(std::move(retVal));
}
template <typename T>
static inline void validateAccuracy(size_t uNumSamples, const size_t uNumThreads=1)
{
// threads, promises and futures
std::vector<std::unique_ptr<std::thread>> threadList;
threadList.reserve(uNumThreads);
std::vector<std::promise<MaxErrorFuture>> promises(uNumThreads);
std::vector<std::future<MaxErrorFuture>> futures;
futures.reserve(uNumThreads);
// start the workers
size_t uNumSamplesPerThread = uNumSamples/uNumThreads;
size_t uStart = 0;
size_t uEnd = std::min(uStart + uNumSamplesPerThread, uNumSamples);
for(size_t i = 0; i < uNumThreads; ++i) {
futures.emplace_back(promises[i].get_future());
threadList.emplace_back(std::make_unique<std::thread>(validateWorker<T>, uNumSamples, uStart, uEnd, &promises[i]));
uStart = uEnd;
uEnd = (i == (uNumThreads - 2)) ? uNumSamples+1 : (uStart + uNumSamplesPerThread);
}
// get the futures when threads are done
double dMaxError[7] = { 0 };
for(auto &&f : futures) {
auto tf = f.get();
for(int i = 0; i < 7; ++i) {
dMaxError[i] = std::max(dMaxError[i], tf.dMaxError[i]);
}
}
// join all threads
for(auto &&t : threadList) {
t->join();
}
std::cout.precision(24);
std::cout << "Max errors: ";
for(int i = 0; i < 7; ++i) {
std::cout << dMaxError[i] << ",";
}
std::cout << std::endl;
}
template <typename T>
static inline void validatePerformance(size_t uNumSamples) {
std::vector<T> values(uNumSamples);
// generate data
for(size_t i = 1; i < uNumSamples; ++i) {
values[i] = static_cast<T>(i);
}
// compare speed
T dSum = 0;
auto start_time = std::chrono::high_resolution_clock::now();
for(size_t i = 1; i < uNumSamples; ++i) {
dSum += log2(values[i]);
}
auto end_time1 = std::chrono::high_resolution_clock::now();
for(size_t i = 1; i < uNumSamples; ++i) {
dSum += fastLog2p1<T>(values[i]);
}
auto end_time2 = std::chrono::high_resolution_clock::now();
for(size_t i = 1; i < uNumSamples; ++i) {
dSum += fastLog2p2<T>(values[i]);
}
auto end_time3 = std::chrono::high_resolution_clock::now();
for(size_t i = 1; i < uNumSamples; ++i) {
dSum += fastLog2p3<T>(values[i]);
}
auto end_time4 = std::chrono::high_resolution_clock::now();
for(size_t i = 1; i < uNumSamples; ++i) {
dSum += fastLog2p4<T>(values[i]);
}
auto end_time5 = std::chrono::high_resolution_clock::now();
for(size_t i = 1; i < uNumSamples; ++i) {
dSum += fastLog2p5<T>(values[i]);
}
auto end_time6 = std::chrono::high_resolution_clock::now();
for(size_t i = 1; i < uNumSamples; ++i) {
dSum += fastLog2p6<T>(values[i]);
}
auto end_time7 = std::chrono::high_resolution_clock::now();
// compare to logf
// generate new data so the static_cast from T to float is removed
values.clear();
std::vector<float> valuesFloat(uNumSamples);
for(size_t i = 1; i < uNumSamples; ++i) {
valuesFloat[i] = static_cast<T>(i);
}
auto start_time8 = std::chrono::high_resolution_clock::now();
float dSumFloat = 0;
for(size_t i = 1; i < uNumSamples; ++i) {
dSumFloat += log2f(values[i]);
}
auto end_time8 = std::chrono::high_resolution_clock::now();
std::cout << dSum << "," << dSumFloat << std::endl;
std::cout << "speed:" << std::chrono::duration_cast<std::chrono::microseconds>(end_time1-start_time).count()
<< "," << std::chrono::duration_cast<std::chrono::microseconds>(end_time2-end_time1).count()
<< "," << std::chrono::duration_cast<std::chrono::microseconds>(end_time3-end_time2).count()
<< "," << std::chrono::duration_cast<std::chrono::microseconds>(end_time4-end_time3).count()
<< "," << std::chrono::duration_cast<std::chrono::microseconds>(end_time5-end_time4).count()
<< "," << std::chrono::duration_cast<std::chrono::microseconds>(end_time6-end_time5).count()
<< "," << std::chrono::duration_cast<std::chrono::microseconds>(end_time7-end_time6).count()
<< "," << std::chrono::duration_cast<std::chrono::microseconds>(end_time8-start_time8).count() << std::endl;
}
#endif /* SRC_LOGTEST_H_ */