-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmatmul-boost.cpp
73 lines (59 loc) · 1.51 KB
/
matmul-boost.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
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <boost/numeric/ublas/matrix.hpp>
using namespace boost::numeric::ublas;
/**********************************
* Pseudo-random number generator *
**********************************/
static uint64_t mat_rng[2] = { 11ULL, 1181783497276652981ULL };
static inline uint64_t xorshift128plus(uint64_t s[2])
{
uint64_t x, y;
x = s[0], y = s[1];
s[0] = y;
x ^= x << 23;
s[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
y += s[1];
return y;
}
double mat_drand(void)
{
return (xorshift128plus(mat_rng)>>11) * (1.0/9007199254740992.0);
}
void mat_gen_random_ublas(matrix<float> &m)
{
size_t i, j;
for (i = 0; i < m.size1(); ++i)
for (j = 0; j < m.size2(); ++j)
m(i, j) = mat_drand();
}
/*****************
* Main function *
*****************/
#include <unistd.h>
#include <time.h>
int main(int argc, char *argv[])
{
int c, n = 1000;
clock_t t;
while ((c = getopt(argc, argv, "n:h")) >= 0) {
if (c == 'n') n = atoi(optarg);
else if (c == 'h') {
fprintf(stderr, "Usage: mat-eval [options]\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -n INT size of the square matrix [%d]\n", n);
fprintf(stderr, " -h this help message\n");
return 1;
}
}
matrix<float> a(n, n), b(n, n), m(n, n);
mat_gen_random_ublas(a);
mat_gen_random_ublas(b);
t = clock();
m = prod(a, b);
fprintf(stderr, "CPU time: %g\n", (double)(clock() - t) / CLOCKS_PER_SEC);
fprintf(stderr, "Central cell: %g\n", m(n/2, n/2));
return 0;
}