-
Notifications
You must be signed in to change notification settings - Fork 0
/
cblas.cpp
161 lines (132 loc) · 4.44 KB
/
cblas.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
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <chrono>
#include<vector>
#include<numeric>
#include<unistd.h>
using namespace std;
using namespace std::chrono;
using std::chrono::high_resolution_clock;
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
/*
Timing Intel's MKL cblas function
*/
extern "C"
{
#include <mkl.h>
#include <mkl_cblas.h>
}
int main(int argc, char *argv[]) {
if (argc != 2) {
cout << "Missing argument n" << endl;
return -1;
}
char *pEnd;
unsigned long long int n = strtoull(argv[1], &pEnd, 10);
unsigned long long int rows = n;
unsigned long long int cols = n;
double *A;
double *B;
double *C;
int align = 64;
unsigned int microsecond = 1000000; //For sleep
//Throwaway
cout << "Throw away test of " << n << "x" << n << endl;
A = (double *) mkl_malloc(n * n * sizeof(double), align);
if (A == NULL) {
cout << "Memory allocation failed." << endl;
return -1;
}
B = (double *) mkl_malloc(n * n * sizeof(double), align);
if (B == NULL) {
cout << "Memory allocation failed." << endl;
mkl_free(A);
return -1;
}
C = (double *) mkl_calloc(n * n, sizeof(double), align);
if (C == NULL) {
cout << "Memory allocation failed." << endl;
mkl_free(A);
mkl_free(B);
return -1;
}
cout << "Initializing matrices A, B, C (throwaway)" << endl;
for (unsigned long long int i = 0; i < rows; i++) {
for (unsigned long long int j = 0; j < cols; j++) {
A[i * cols + j] = (double) rand() / RAND_MAX * 2.0 - 1.0;
}
}
for(unsigned long long int i = 0; i < rows; i++) {
for(unsigned long long int j = 0; j < cols; j++) {
B[i * cols + j] = (double) rand() / RAND_MAX * 2.0 - 1.0;
}
}
usleep(3 * microsecond);//sleeps for 3 second
auto t1 = high_resolution_clock::now();
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1., A, n, B, n, 0., C, n);
auto t2 = high_resolution_clock::now();
usleep(3 * microsecond);//sleeps for 3 second
/* Getting number of milliseconds as a double. */
duration<double, std::milli> duration = t2 - t1;
cout << "Time for throwaway: " << duration.count() / 1000.0 << endl;
mkl_free(A);
mkl_free(B);
mkl_free(C);
cout << "Performing cblas_dgemm() 10 times" << endl;
vector <double> times = {};
for (int i = 0; i < 10; i++) {
double *A;
double *B;
double *C;
A = (double *) mkl_malloc(n * n * sizeof(double), align);
if (A == NULL) {
cout << "Memory allocation failed." << endl;
return -1;
}
B = (double *) mkl_malloc(n * n * sizeof(double), align);
if (B == NULL) {
cout << "Memory allocation failed." << endl;
mkl_free(A);
return -1;
}
C = (double *) mkl_calloc(n * n, sizeof(double), align);
if (C == NULL) {
cout << "Memory allocation failed." << endl;
mkl_free(A);
mkl_free(B);
return -1;
}
cout << ".";
for (unsigned long long int i = 0; i < rows; i++) {
for (unsigned long long int j = 0; j < cols; j++) {
A[i * cols + j] = (double) rand() / RAND_MAX * 2.0 - 1.0;
}
}
for(unsigned long long int i = 0; i < rows; i++) {
for(unsigned long long int j = 0; j < cols; j++) {
B[i * cols + j] = (double) rand() / RAND_MAX * 2.0 - 1.0;
}
}
cout << "." << endl;
usleep(3 * microsecond);//sleeps for 3 second
auto t1 = high_resolution_clock::now();
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1., A, n, B, n, 0., C, n);
auto t2 = high_resolution_clock::now();
usleep(3 * microsecond);//sleeps for 3 second
/* Getting number of milliseconds as a double. */
duration = t2 - t1;
cout << "Time: " << duration.count() / 1000.0 << endl;
times.push_back(duration.count() / 1000.0);
mkl_free(A);
mkl_free(B);
mkl_free(C);
}
cout << "Average time: " << accumulate(times.begin(), times.end(), 0.0) / 10.0 << " seconds" << endl;
return 0;
}