-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
133 lines (109 loc) · 2.48 KB
/
main.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
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <time.h>
#include <sys/time.h>
#define QUOTE(name) #name
#define STR(macro) QUOTE(macro)
using namespace std;
#define REPEAT 20
#ifndef LIBBLAS_SO
#define LIBBLAS_SO libblas.so
#endif /* !LIBBLAS_SO */
typedef void (*func_sgemm)(char*, char*, int*, int*, int*, float*, float*, int*, float*, int*, float*, float*, int*);
void* GetLibrarayFunction(string function)
{
void* handle = NULL;
handle = dlopen(STR(LIBBLAS_SO), RTLD_LAZY);
if(!handle)
throw "Could not load library";
void* Func = dlsym(handle, function.c_str());
char* result = dlerror();
if(result)
throw result;
return Func;
}
double CalcTime(timeval start, timeval end)
{
double factor = 1000000;
return (((double)end.tv_sec) * factor + ((double)end.tv_usec) - (((double)start.tv_sec) * factor + ((double)start.tv_usec))) / factor;
}
double min(double* dTimes)
{
double min = dTimes[0];
for(int i = 1; i < REPEAT; i++)
{
if(dTimes[i] < min)
min = dTimes[i];
}
return min;
}
double max(double* dTimes)
{
double max = dTimes[0];
for(int i = 1; i < REPEAT; i++)
{
if(dTimes[i] > max)
max = dTimes[i];
}
return max;
}
double mean(double* dTimes)
{
double sum = 0.0;
for(int i = 0; i < REPEAT; i++)
{
sum += dTimes[0];
}
return sum/REPEAT;
}
int main(int argc, char *argv[])
{
func_sgemm f = (func_sgemm)GetLibrarayFunction("sgemm_");
char no_trans('n');
float zero(0);
float one(1.0);
timeval start, end;
FILE *pF = fopen("cpp.csv", "w");
FILE *pFr = fopen("cppraw.csv", "w");
double dTimes[REPEAT];
for(int i = 0; i < argc - 1; ++i)
{
int dim = atoi(argv[i + 1]);
float A[dim * dim];
float B[dim * dim];
for(int j = 0; j < dim; j++)
{
for(int k = 0; k < dim; k++)
{
A[j * dim + k] = j*dim + k;
if(j == k)
B[j*dim + k] = 1.0;
else
B[j*dim + k] = 0.0;
}
}
float Return[dim * dim];
for(int j = 0; j < REPEAT; j++)
{
gettimeofday(&start, NULL);
f(&no_trans, &no_trans, &dim, &dim, &dim, &one, A, &dim, B, &dim, &zero, Return, &dim);
gettimeofday(&end, NULL);
dTimes[j] = CalcTime(start, end);
fprintf(pFr, "%lf ", dTimes[j]);
}
fprintf(pF, "%d %lf %lf %lf\n", dim, mean(dTimes), min(dTimes), max(dTimes));
fprintf(pFr, "\n");
for(int j = 0; j < dim; j++)
{
for(int k = 0; k < dim; k++)
{
if(A[j*dim + k] != Return[j*dim + k])
printf("%lf != %lf\n", A[j*dim + k], Return[j*dim + k]);
}
}
}
fclose(pF);
return 0;
}