-
Notifications
You must be signed in to change notification settings - Fork 5
/
udo_regression.cpp
390 lines (346 loc) · 12.4 KB
/
udo_regression.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
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
#include <array>
#include <atomic>
#include <memory>
#include <string_view>
#ifdef UDO_STANDALONE
#include <atomic>
#include <cerrno>
#include <charconv>
#include <chrono>
#include <iostream>
#include <map>
#include <string>
#include <thread>
#include <udo/UDOStandalone.hpp>
#include <fcntl.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
//---------------------------------------------------------------------------
#include <udo/UDOperator.hpp>
//---------------------------------------------------------------------------
using namespace std;
using namespace std::literals::string_view_literals;
//---------------------------------------------------------------------------
/// A tuple this UDO takes as an input
struct Input {
// The value for x
double x;
// The measurement of y that will be fitted
double y;
};
//---------------------------------------------------------------------------
/// An tuple generated by this UDO
struct Output {
// The value for the parameter a
double a;
// The value for the parameter b
double b;
// The value for the parameter c
double c;
};
//---------------------------------------------------------------------------
/// The linear regression operator. It solves the following problem:
/// y_i = a + bx_i + cx_i^2
/// Determine a, b and c for the given values for x and y while minimizing the
/// sum of the squared errors:
/// Sum_i (a + bx_i + cx_i^2 - y_i)^2
///
/// This can be solved as follows:
///
/// Setting the partial derivatives of the squared errors to 0 results in:
///
/// / \ / \ / \
/// | Sum 1 Sum x Sum x^2 | | a | | Sum y |
/// | Sum x Sum x^2 Sum x^3 | * | b | = | Sum xy |
/// | Sum x^2 Sum x^3 Sum x^4 | | c | | Sum x^2y |
/// \ / \ / \ /
///
/// To calculate a, b, and c we calculate the inverse of the first matrix and
/// multiply it from the left:
///
/// / \ / \-1 / \
/// | a | | Sum 1 Sum x Sum x^2 | | Sum y |
/// | b | = | Sum x Sum x^2 Sum x^3 | * | Sum xy |
/// | c | | Sum x^2 Sum x^3 Sum x^4 | | Sum x^2y |
/// \ / \ / \ /
///
/// This results in a closed form solution for a, b, and c:
///
/// a = 1 / det(A) * (
/// Sum y (Sum x^2 Sum x^4 - (Sum x^3)^2) +
/// Sum xy (Sum x^2 Sum x^3 - Sum x Sum x^4) +
/// Sum x^2y (Sum x Sum x^3 - (Sum x^2)^2)
/// )
///
/// b = 1 / det(A) * (
/// Sum y (Sum x^2 Sum x^3 - Sum x Sum x^4) +
/// Sum xy (Sum 1 Sum x^4 - (Sum x^2)^2) +
/// Sum x^2y (Sum x Sum x^2 - Sum 1 Sum x^3)
/// )
///
/// c = 1 / det(A) * (
/// Sum y (Sum x Sum x^3 - (Sum x^2)^2) +
/// Sum xy (Sum x Sum x^2 - Sum 1 Sum x^3) +
/// Sum x^2y (Sum 1 Sum x^2 - (Sum x)^2)
/// )
///
/// with det(A) =
/// Sum 1 Sum x^2 Sum X^4
/// + 2 Sum x Sum x^2 Sum x^3
/// - (Sum x^2)^3
/// - Sum 1 (Sum x^3)^2
/// - (Sum x)^2 Sum x^4
///
/// Since everything we calculate are sums, we can trivially parallelize this by
/// letting each thread calculate the partial sums of the values it receives and
/// then sum up all partial sums once at the end. With the partial sums we then
/// determine det(A) and finally a, b, and c.
class LinearRegression : public udo::UDOperator<Input, Output> {
private:
/// The partial sums of a thread
struct alignas(64) PartialSums {
// The value for Sum 1
double sum1 = 0.0;
// The value for Sum x
double sumx = 0.0;
// The value for Sum x^2
double sumx2 = 0.0;
// The value for Sum x^3
double sumx3 = 0.0;
// The value for Sum x^4
double sumx4 = 0.0;
// The value for Sum y
double sumy = 0.0;
// The value for Sum xy
double sumxy = 0.0;
// The value for Sum x^2y
double sumx2y = 0.0;
};
/// The local state of a thread in the regression
struct RegressionLocalState {
/// The partial sums
PartialSums partialSums;
/// The pointer to the next local state
RegressionLocalState* next = nullptr;
};
/// The list of local states
atomic<RegressionLocalState*> localStateList = nullptr;
/// The mutex flag to return the result
atomic_flag resultMutex = false;
public:
/// Consume an input tuple
void consume(LocalState& rawLocalState, const Input& input) {
auto*& localState = reinterpret_cast<RegressionLocalState*&>(rawLocalState.data);
if (!localState) {
auto newLocalState = make_unique<RegressionLocalState>();
newLocalState->next = localStateList.load();
while (!localStateList.compare_exchange_weak(newLocalState->next, newLocalState.get()))
;
localState = newLocalState.get();
// This will be deallocated in postProduce()
newLocalState.release();
}
double x = input.x;
double y = input.y;
auto x2 = x * x;
auto x3 = x2 * x;
auto x4 = x2 * x2;
auto xy = x * y;
auto x2y = x2 * y;
auto& sums = localState->partialSums;
sums.sum1 += 1;
sums.sumx += x;
sums.sumx2 += x2;
sums.sumx3 += x3;
sums.sumx4 += x4;
sums.sumy += y;
sums.sumxy += xy;
sums.sumx2y += x2y;
}
/// Produce the output
bool postProduce(LocalState& /*localState*/) {
if (resultMutex.test_and_set())
return true;
// Sum up all partial sums from the local states
PartialSums sums;
for (auto* localState = localStateList.load(); localState;) {
unique_ptr<RegressionLocalState> localStatePtr(localState);
auto& lsums = localStatePtr->partialSums;
sums.sum1 += lsums.sum1;
sums.sumx += lsums.sumx;
sums.sumx2 += lsums.sumx2;
sums.sumx3 += lsums.sumx3;
sums.sumx4 += lsums.sumx4;
sums.sumy += lsums.sumy;
sums.sumxy += lsums.sumxy;
sums.sumx2y += lsums.sumx2y;
localState = localStatePtr->next;
}
// clang-format off
double detInv = 1 / (
sums.sum1 * sums.sumx2 * sums.sumx4
+ 2 * sums.sumx * sums.sumx2 * sums.sumx3
- sums.sumx2 * sums.sumx2 * sums.sumx2
- sums.sum1 * sums.sumx3 * sums.sumx3
- sums.sumx * sums.sumx * sums.sumx4
);
double a = detInv * (
sums.sumy * (sums.sumx2 * sums.sumx4 - sums.sumx3 * sums.sumx3)
+ sums.sumxy * (sums.sumx2 * sums.sumx3 - sums.sumx * sums.sumx4)
+ sums.sumx2y * (sums.sumx * sums.sumx3 - sums.sumx2 * sums.sumx2)
);
double b = detInv * (
sums.sumy * (sums.sumx2 * sums.sumx3 - sums.sumx * sums.sumx4)
+ sums.sumxy * (sums.sum1 * sums.sumx4 - sums.sumx2 * sums.sumx2)
+ sums.sumx2y * (sums.sumx * sums.sumx2 - sums.sum1 * sums.sumx3)
);
double c = detInv * (
sums.sumy * (sums.sumx * sums.sumx3 - sums.sumx2 * sums.sumx2)
+ sums.sumxy * (sums.sumx * sums.sumx2 - sums.sum1 * sums.sumx3)
+ sums.sumx2y * (sums.sum1 * sums.sumx2 - sums.sumx * sums.sumx)
);
// clang-format on
produceOutputTuple({a, b, c});
return true;
}
};
//---------------------------------------------------------------------------
#ifdef UDO_STANDALONE
//---------------------------------------------------------------------------
static size_t getNumThreads()
/// Get the number of available threads
{
::cpu_set_t cpuSet = {};
if (::sched_getaffinity(0, sizeof(cpuSet), &cpuSet) != 0)
return ~0ull;
size_t threadCount = CPU_COUNT(&cpuSet);
return threadCount;
}
//---------------------------------------------------------------------------
int main(int argc, const char** argv) {
bool argError = false;
bool benchmark = false;
string_view inputFileName;
const char** argIt = argv;
++argIt;
const char** argEnd = argv + argc;
for (; argIt != argEnd; ++argIt) {
string_view arg(*argIt);
if (arg.empty())
continue;
if (arg == "--benchmark") {
benchmark = true;
} else {
if (inputFileName.empty()) {
inputFileName = arg;
} else {
argError = true;
break;
}
}
}
if (!argError && inputFileName.empty())
argError = true;
if (argError) {
cerr << "Usage: " << argv[0] << " [--benchmark] <input file>" << endl;
return 2;
}
int inputFileFd = ::open(inputFileName.data(), O_RDONLY | O_CLOEXEC);
if (inputFileFd < 0) {
cerr << "Failed opening " << inputFileName << ": " << strerror(errno) << endl;
return 1;
}
struct ::stat fileStat{};
if (::fstat(inputFileFd, &fileStat) < 0) {
cerr << "stat(" << inputFileName << ") failed: " << strerror(errno) << endl;
return 1;
}
void* inputFilePtr = ::mmap(nullptr, fileStat.st_size, PROT_READ, MAP_PRIVATE, inputFileFd, 0);
if (inputFilePtr == MAP_FAILED) {
cerr << "mmap(" << inputFileName << ") failed: " << strerror(errno) << endl;
return 1;
}
::close(inputFileFd);
string_view inputFileData(static_cast<const char*>(inputFilePtr), fileStat.st_size);
// Discard the header line
inputFileData.remove_prefix(inputFileData.find('\n') + 1);
static constexpr size_t sizePerThread = 4096 * 4;
size_t numThreads = getNumThreads();
vector<vector<Input>> threadInputs(numThreads);
vector<thread> threads;
atomic<size_t> currentOffset = 0;
for (size_t threadId = 0; threadId < numThreads; ++threadId) {
threads.emplace_back([&, threadId, inputFileData] {
auto& inputs = threadInputs[threadId];
while (true) {
size_t localOffset = currentOffset.load();
if (localOffset >= inputFileData.size())
break;
size_t offsetEnd = localOffset + sizePerThread;
if (offsetEnd >= inputFileData.size()) {
offsetEnd = inputFileData.size();
} else {
// Go forward until the next newline
size_t newlineOffset = inputFileData.find('\n', offsetEnd);
if (newlineOffset == string_view::npos)
offsetEnd = inputFileData.size();
else
offsetEnd = newlineOffset + 1;
}
if (!currentOffset.compare_exchange_weak(localOffset, offsetEnd))
continue;
string_view inputStr = inputFileData.substr(localOffset, offsetEnd - localOffset);
char strBuffer[64];
while (!inputStr.empty()) {
Input in;
size_t commaPos = inputStr.find(',');
memcpy(strBuffer, inputStr.data(), commaPos - 1);
strBuffer[commaPos] = '\0';
inputStr.remove_prefix(commaPos + 1);
in.x = strtod(strBuffer, nullptr);
size_t nlPos = inputStr.find('\n');
memcpy(strBuffer, inputStr.data(), nlPos - 1);
strBuffer[nlPos] = '\0';
inputStr.remove_prefix(nlPos + 1);
in.y = strtod(strBuffer, nullptr);
inputs.push_back(in);
}
}
});
}
for (auto& thread : threads)
thread.join();
::munmap(inputFilePtr, fileStat.st_size);
vector<Input> inputs;
for (auto& threadInput : threadInputs)
inputs.insert(inputs.end(), threadInput.begin(), threadInput.end());
vector<Output> outputs(3);
if (benchmark) {
for (unsigned i = 0; i < 11; ++i) {
udo::UDOStandalone<LinearRegression> standalone(numThreads, 10000);
LinearRegression regression;
auto start = chrono::steady_clock::now();
standalone.run(regression, inputs, outputs);
auto end = chrono::steady_clock::now();
auto duration_ms = chrono::duration_cast<chrono::nanoseconds>(end - start).count();
// Don't measure the first run
if (i > 0)
cout << duration_ms << '\n';
}
} else {
udo::UDOStandalone<LinearRegression> standalone(numThreads, 10000);
LinearRegression regression;
standalone.run(regression, inputs, outputs);
auto& params = standalone.getOutput()[0];
cout << "a = " << params.a << '\n';
cout << "b = " << params.b << '\n';
cout << "c = " << params.c << '\n';
cout << "-> y = " << params.a << " + " << params.b << "x" << " + " << params.c << "x^2\n";
}
return 0;
}
//---------------------------------------------------------------------------
#endif