-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_algorithms.cpp
474 lines (302 loc) · 17.1 KB
/
m_algorithms.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
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
#include "m_algorithms.h"
#include "m_algorithms_utilities.h"
#include "matrix_printer.h"
#include <cilk/cilk.h>
#include <iostream>
#include <math.h>
#include <numeric>
#include <assert.h>
namespace Matrix {
namespace Operations {
namespace Unary {
Matrix::Representation ReLU::operate(
const Matrix::Representation& m) const noexcept{
Matrix::Representation output = Matrix::Representation{
Matrix::Rows(m.num_rows()),
Matrix::Columns(m.num_cols())
};
std::replace_copy_if(m.constScanStart(), m.constScanEnd(), output.scanStart(),
[](float z){ return z < 0;}, 0);
return Matrix::Representation{output};
}
Matrix::Representation Sign::operate(
const Matrix::Representation& m) const noexcept{
Matrix::Representation output = Matrix::Representation(
Matrix::Rows(m.num_rows()),
Matrix::Columns(m.num_cols())
);
std::transform(m.constScanStart(), m.constScanEnd(), output.scanStart(), [](const auto val) { return val >= 0 ? 1 : 0;});
return Matrix::Representation{output};
}
/*
DESCRIPTION:
Is a form of normalising arbitrary values using exponential distribution.
exp(x_i) / SUM(exp(x)) is numerically unstable if dividing large terms,
therefore we divide all intermediate terms by constant C = Max(exp(x)).
https://cs231n.github.io/linear-classify/#softmax
*/
Matrix::Representation SoftMax::operate(
const Matrix::Representation& m) const noexcept{
Matrix::Representation output = Matrix::Representation(
Matrix::Rows(m.num_rows()),
Matrix::Columns(m.num_cols())
);
auto max = std::max(m.constScanStart(), m.constScanEnd());
std::transform(m.constScanStart(), m.constScanEnd(), output.scanStart(), [max](auto val) { return exp(val - *max); });
double sum = std::accumulate(output.constScanStart(), output.constScanEnd(), 0.0);
std::transform(output.constScanStart(), output.constScanEnd(), output.scanStart(),
[sum](auto val) { return val / sum; }
);
return Matrix::Representation{output};
}
Matrix::Representation Transpose::operate(
const Matrix::Representation& m) const noexcept {
Matrix::Representation output = Matrix::Representation{
Matrix::Rows(m.num_cols()),
Matrix::Columns(m.num_rows())
};
transpose_helper(
m.constScanStart(),
output.scanStart(),
0, m.num_rows(),
0, m.num_cols(),
m.num_rows(), m.num_cols());
return Matrix::Representation{output};
}
void transpose_helper(
std::vector<float>::const_iterator in,
std::vector<float>::iterator out,
int rb, int re, int cb, int ce, int rows, int cols) noexcept {
int r = re - rb, c = ce - cb;
if (r <= 16 && c <= 16) {
for (int i = rb; i < re; i++) {
for (int j = cb; j < ce; j++) {
*(out + (j * rows + i)) = *(in + (i * cols + j));
}
}
} else if (r >= c) {
cilk_spawn transpose_helper(in, out, rb, rb + (r / 2), cb, ce, rows, cols);
transpose_helper(in, out, rb + (r / 2), re, cb, ce, rows, cols);
cilk_sync;
} else {
cilk_spawn transpose_helper(in, out, rb, re, cb, cb + (c / 2), rows, cols);
transpose_helper(in, out, rb, re, cb + (c / 2), ce, rows, cols);
cilk_sync;
}
}
} // Unary
/*
DESCRIPTION:
The cross-entropy between a “true” distribution p and an
estimated distribution q is defined as:
H(p, q) = -SUM(p(x), log(q(x)) )
Logit function takes a probability and produces a real number
between negative and positive infinity.
taking the log of the odds ratio brings about a certain
symmetricity in the results, making it easier to
interpret and use in various statistics
*/
namespace Metric {
Matrix::Representation CrossEntropy::operate(
const Matrix::Representation& p,
const Matrix::Representation& q) const noexcept {
Matrix::Representation output = Matrix::Representation(
Matrix::Rows(1),
Matrix::Columns(1)
);
Matrix::Operations::Unary::SoftMax softmax;
Matrix::Representation theta = softmax(q);
double entropy = 0;
for (auto p_i = p.constScanStart(), q_i = theta.constScanStart(); q_i != theta.constScanEnd(); p_i++, q_i++) {
entropy -= *p_i * log(*q_i);
}
output.put(0, 0, entropy);
return Matrix::Representation{output};
}
}
namespace Binary {
namespace Addition {
Matrix::Representation Std::operate(
const Matrix::Representation& l,
const Matrix::Representation& r) const noexcept {
#if DEBUG
if ((l.num_rows() != r.num_rows()) && (l.num_cols() != r.num_cols()))
std::cout << Utility::debug_message_2(l, r) << endl;
#endif
assert((l.num_rows() == r.num_rows()) && (l.num_cols() == r.num_cols()));
auto output = Matrix::Representation(Rows(l.num_rows()), Columns(r.num_cols()));
std::transform(l.constScanStart(), l.constScanEnd(), r.constScanStart(), output.scanStart(), std::plus<float>());
return Matrix::Representation{output};
}
}
namespace Subtraction {
Matrix::Representation Std::operate(
const Matrix::Representation& l,
const Matrix::Representation& r) const noexcept {
#if DEBUG
if ((l.num_rows() != r.num_rows()) && (l.num_cols() != r.num_cols()))
std::cout << Utility::debug_message_2(l, r) << endl;
#endif
assert((l.num_rows() == r.num_rows()) && (l.num_cols() == r.num_cols()));
auto output = Matrix::Representation(Rows(l.num_rows()), Columns(r.num_cols()));
std::transform(l.constScanStart(), l.constScanEnd(), r.constScanStart(), output.scanStart(), std::minus<float>());
return Matrix::Representation{output};
}
}
namespace OuterProduct {
Matrix::Representation Naive::operate(
const Matrix::Representation& l,
const Matrix::Representation& r) const noexcept {
#if DEBUG
if (
l.get_type() =! Matrix::Representation::Type::COLUMN_VECTOR &&
l.get_type() =! Matrix::Representation::Type::ROW_VECTOR ||
r.get_type() =! Matrix::Representation::Type::COLUMN_VECTOR &&
r.get_type() =! Matrix::Representation::Type::ROW_VECTOR
)
std::cout << Utility::debug_message_2(l, r) << endl;
#endif
assert(
l.get_type() == Matrix::Representation::Type::COLUMN_VECTOR ||
l.get_type() == Matrix::Representation::Type::ROW_VECTOR &&
r.get_type() == Matrix::Representation::Type::COLUMN_VECTOR ||
r.get_type() == Matrix::Representation::Type::ROW_VECTOR &&
"Operands are not Vectors.");
u_int64_t x_dimension = l.num_rows() > r.num_rows() ? l.num_rows() : r.num_rows();
u_int64_t y_dimension = r.num_cols() > l.num_cols() ? r.num_cols() : l.num_cols();
auto output = Matrix::Representation(Rows(x_dimension), Columns(y_dimension));
auto li = l.constScanStart();
for (int i = 0; li != l.constScanEnd(); li++, i++) {
auto ri = r.constScanStart();
for (int j = 0; ri != r.constScanEnd(); ri++, j++) {
float val = *li * *ri;
output.put(i, j, val);
}
}
return Matrix::Representation{output};
}
}
namespace HadamardProduct {
Matrix::Representation Std::operate(
const Matrix::Representation& l,
const Matrix::Representation& r) const noexcept {
auto output = Matrix::Representation(
Rows(l.num_rows()),
Columns(r.num_cols()));
std::transform(l.constScanStart(), l.constScanEnd(), r.constScanStart(), output.scanStart(), std::multiplies<float>());
return Matrix::Representation{output};
}
Matrix::Representation Naive::operate(
const Matrix::Representation& l,
const Matrix::Representation& r) const noexcept {
#if DEBUG
if (l.get_type() != r.get_type() ||
l.get_type() =! Matrix::Representation::Type::COLUMN_VECTOR &&
l.get_type() =! Matrix::Representation::Type::ROW_VECTOR)
std::cout << Utility::debug_message_2(l, r) << endl;
#endif
assert(l.get_type() == r.get_type() &&
l.get_type() == Matrix::Representation::Type::COLUMN_VECTOR ||
l.get_type() == Matrix::Representation::Type::ROW_VECTOR &&
"Operands are not Vectors.");
Matrix::Representation output = Matrix::Representation(Rows(l.num_rows()), Columns(r.num_cols()));
for (u_int64_t i = 0; i < l.num_rows(); i++) {
for (u_int64_t j = 0; j < r.num_cols(); j++) {
float val = l.get(i, j) * r.get(i, j);
output.put(i, j, val);
}
}
return Matrix::Representation{output};
}
}
namespace Multiplication {
Matrix::Representation Naive::operate(
const Matrix::Representation& l,
const Matrix::Representation& r) const noexcept {
#if DEBUG
if (l.num_cols() != r.num_rows())
std::cout << Utility::debug_message(l, r) << endl;
#endif
assert(l.num_cols() == r.num_rows());
// if (l.num_cols() != r.num_rows()) {
// throw std::length_error(Utility::debug_message(l, r));
// }
Matrix::Representation output = Matrix::Representation(Rows(l.num_rows()), Columns(r.num_cols()));
for (u_int64_t i = 0; i < l.num_rows(); i++) {
for (u_int64_t j = 0; j < r.num_cols(); j++) {
float val = 0;
for (u_int64_t k = 0; k < l.num_cols(); k++) {
val += l.get(i, k) * r.get(k, j);
}
output.put(i, j, val);
}
}
return Matrix::Representation{output};
}
/*
Adapted from https://ocw.mit.edu/courses/mathematics/18-335j-introduction-to-numerical-methods-spring-2019/week-5/MIT18_335JS19_lec12.pdf
We need to divide the data until it fits into lowest cache.
*/
void add_matmul_rec(std::vector<float>::const_iterator a, std::vector<float>::const_iterator b, std::vector<float>::iterator c,
int m, int n, int p, int fdA, int fdB, int fdC) noexcept {
if (m + n + p <= 48) {
int i, j, k;
for (i = 0; i < m; ++i) {
for (k = 0; k < p; ++k) {
float sum = 0;
for (j = 0; j < n; ++j)
sum += *(a + (i * fdA + j)) * *(b + (j * fdB + k));
*(c + (i * fdC + k)) += sum;
}
}
}
else {
int m2 = m/2, n2 = n/2, p2 = p/2;
cilk_spawn add_matmul_rec(a, b, c, m2, n2, p2, fdA, fdB, fdC);
cilk_spawn add_matmul_rec(a, b + p2, c + p2, m2, n2, p - p2, fdA, fdB, fdC);
cilk_spawn add_matmul_rec(a + m2*fdA + n2, b + n2*fdB, c + m2*fdC, m-m2, n - n2, p2, fdA, fdB, fdC);
add_matmul_rec(a + m2*fdA + n2, b + p2 + n2*fdB, c + m2*fdC + p2, m - m2, n - n2, p - p2, fdA, fdB, fdC);
cilk_sync;
cilk_spawn add_matmul_rec(a + n2, b + n2*fdB, c, m2, n - n2, p2, fdA, fdB, fdC);
cilk_spawn add_matmul_rec(a + m2*fdA, b, c + m2*fdC, m - m2, n2, p2, fdA, fdB, fdC);
cilk_spawn add_matmul_rec(a + n2 , b + p2 + n2*fdB, c + p2, m2, n - n2, p - p2, fdA, fdB, fdC);
add_matmul_rec(a + m2*fdA, b + p2, c + m2*fdC + p2, m - m2, n2, p - p2, fdA, fdB, fdC);
cilk_sync;
}
}
Matrix::Representation ParallelDNC::operate(
const Matrix::Representation& l,
const Matrix::Representation& r) const noexcept {
#if DEBUG
if (l.num_cols() != r.num_rows())
std::cout << Utility::debug_message(l, r) << endl;
#endif
assert(l.num_cols() == r.num_rows());
Matrix::Representation output = Matrix::Representation(Rows(l.num_rows()), Columns(r.num_cols()));
add_matmul_rec(l.constScanStart(), r.constScanStart(), output.scanStart(), l.num_rows(), l.num_cols(), r.num_cols(), l.num_cols(), r.num_cols(), r.num_cols());
return Matrix::Representation{output};
}
Matrix::Representation Square::operate(
const Matrix::Representation& l,
const Matrix::Representation& r) const noexcept {
#if DEBUG
if (l.num_cols() != r.num_rows())
std::cout << Utility::debug_message(l, r) << endl;
#endif
assert(l.num_cols() == r.num_rows());
Matrix::Representation output = Matrix::Representation(Rows(l.num_rows()), Columns(r.num_cols()));
cilk_for (u_int64_t i = 0; i < l.num_rows(); i++) {
for (u_int64_t j = 0; j < r.num_cols(); j++) {
float val = 0;
for (u_int64_t k = 0; k < l.num_cols(); k++) {
val += l.get(i, k) * r.get(k, j);
}
output.put(i, j, val);
}
}
return Matrix::Representation{output};
}
} // namespace Multiplication
} // namespace Binary
} // namespace Operations
} // namespace Matrix