forked from LLNL/mitos
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatmul.cpp
56 lines (46 loc) · 1.2 KB
/
matmul.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
#include <cstdlib>
#include <iostream>
#include <omp.h>
#include "src/virtual_address_writer.h"
#define ROW_MAJOR(x,y,width) y*width+x
void init_matrices(int N, double **a, double **b, double **c)
{
int i,j,k;
*a = new double[N*N];
*b = new double[N*N];
*c = new double[N*N];
for(i=0; i<N; ++i)
{
for(j=0; j<N; ++j)
{
(*a)[ROW_MAJOR(i,j,N)] = (double)rand();
(*b)[ROW_MAJOR(i,j,N)] = (double)rand();
(*c)[ROW_MAJOR(i,j,N)] = 0;
}
}
}
void matmul(int N, double *a, double *b, double *c)
{
for(int i=0; i<N; ++i)
{
for(int j=0; j<N; ++j)
{
for(int k=0; k<N; ++k)
{
c[ROW_MAJOR(i,j,N)] += a[ROW_MAJOR(i,k,N)]*b[ROW_MAJOR(k,j,N)];
}
}
}
int randx = N*((float)rand() / (float)RAND_MAX+1);
int randy = N*((float)rand() / (float)RAND_MAX+1);
std::cout << c[ROW_MAJOR(randx,randy,N)] << std::endl;
}
int main(int argc, char **argv)
{
int N = (argc == 2) ? atoi(argv[1]) : 1024;
Mitos_save_virtual_address_offset("/tmp/mitos_virt_address.txt");
double *a,*b,*c;
init_matrices(N,&a,&b,&c);
matmul(N,a,b,c);
return 0;
}