-
Notifications
You must be signed in to change notification settings - Fork 0
/
mt-dgemm.c
214 lines (163 loc) · 5.64 KB
/
mt-dgemm.c
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#ifdef USE_CBLAS
#include "cblas.h"
#elif USE_BLIS
#include <blis/blis.h>
#elif USE_MKL
#include "mkl.h"
#endif
#define DGEMM_RESTRICT __restrict__
// ------------------------------------------------------- //
// Function: get_seconds
//
// Vendor may modify this call to provide higher resolution
// timing if required
// ------------------------------------------------------- //
double get_seconds() {
struct timeval now;
gettimeofday(&now, NULL);
const double seconds = (double) now.tv_sec;
const double usec = (double) now.tv_usec;
return seconds + (usec * 1.0e-6);
}
// ------------------------------------------------------- //
// Function: main
//
// Modify only in permitted regions (see comments in the
// function)
// ------------------------------------------------------- //
int main(int argc, char* argv[]) {
// ------------------------------------------------------- //
// DO NOT CHANGE CODE BELOW
// ------------------------------------------------------- //
int N = 256;
int repeats = 30;
double alpha = 1.0;
double beta = 1.0;
if(argc > 1) {
N = atoi(argv[1]);
printf("Matrix size input by command line: %d\n", N);
if(argc > 2) {
repeats = atoi(argv[2]);
if(repeats < 30) {
fprintf(stderr, "Error: repeats must be at least 30, setting is: %d\n", repeats);
exit(-1);
}
printf("Repeat multiply %d times.\n", repeats);
if(argc > 3) {
alpha = (double) atof(argv[3]);
if(argc > 4) {
beta = (double) atof(argv[4]);
}
}
} else {
printf("Repeat multiply defaulted to %d\n", repeats);
}
} else {
printf("Matrix size defaulted to %d\n", N);
}
printf("Alpha = %f\n", alpha);
printf("Beta = %f\n", beta);
if(N < 128) {
printf("Error: N (%d) is less than 128, the matrix is too small.\n", N);
exit(-1);
}
printf("Allocating Matrices...\n");
double* DGEMM_RESTRICT matrixA = (double*) malloc(sizeof(double) * N * N);
double* DGEMM_RESTRICT matrixB = (double*) malloc(sizeof(double) * N * N);
double* DGEMM_RESTRICT matrixC = (double*) malloc(sizeof(double) * N * N);
printf("Allocation complete, populating with values...\n");
int i, j, k, r;
#pragma omp parallel for private(i,j)
for(i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
matrixA[i*N + j] = 2.0;
matrixB[i*N + j] = 0.5;
matrixC[i*N + j] = 1.0;
}
}
printf("Performing multiplication...\n");
const double start = get_seconds();
// ------------------------------------------------------- //
// VENDOR NOTIFICATION: START MODIFIABLE REGION
//
// Vendor is able to change the lines below to call optimized
// DGEMM or other matrix multiplication routines. Do *NOT*
// change any lines above this statement.
// ------------------------------------------------------- //
double sum = 0;
// Repeat multiple times
for(r = 0; r < repeats; r++) {
#if defined( USE_MKL ) || defined (USE_CBLAS)
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
N, N, N, alpha, matrixA, N, matrixB, N, beta, matrixC, N);
#elif defined( USE_BLIS )
char transA = 'N';
char transB = 'N';
dgemm_(&transA, &transB, &N, &N, &N, &alpha, matrixA, &N,
matrixB, &N, &beta, matrixC, &N);
#else
//#pragma omp parallel for private(sum,j,k)
#pragma omp target data map(to:matrixA[0:N*N]), map(to:matrixB[0:N*N]), map(tofrom:matrixC[0:N*N])
#pragma omp target
#pragma omp teams distribute parallel for collapse(2) private(i,j,k,sum)
for(i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
sum = 0;
for(k = 0; k < N; k++) {
sum += matrixA[i*N + k] * matrixB[k*N + j];
}
matrixC[i*N + j] = (alpha * sum) + (beta * matrixC[i*N + j]);
}
}
#endif
}
// ------------------------------------------------------- //
// VENDOR NOTIFICATION: END MODIFIABLE REGION
// ------------------------------------------------------- //
// ------------------------------------------------------- //
// DO NOT CHANGE CODE BELOW
// ------------------------------------------------------- //
const double end = get_seconds();
printf("Calculating matrix check...\n");
double final_sum = 0;
long long int count = 0;
#pragma omp parallel for reduction(+:final_sum, count) private(i,j)
for(i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
final_sum += matrixC[i*N + j];
count++;
}
}
double N_dbl = (double) N;
double matrix_memory = (3 * N_dbl * N_dbl) * ((double) sizeof(double));
printf("\n");
printf("===============================================================\n");
const double count_dbl = (double) count;
const double scaled_result = (final_sum / (count_dbl * repeats));
printf("Final Sum is: %f\n", scaled_result);
const double check_sum = N_dbl + (1.0 / (double) (repeats));
const double allowed_margin = 1.0e-8;
if( (check_sum >= (scaled_result - allowed_margin)) &&
(check_sum <= (scaled_result + allowed_margin)) ) {
printf(" -> Solution check PASSED successfully.\n");
} else {
printf(" -> Solution check FAILED.\n");
}
printf("Memory for Matrices: %f MB\n",
(matrix_memory / (1024 * 1024)));
const double time_taken = (end - start);
printf("Multiply time: %f seconds\n", time_taken);
const double flops_computed = (N_dbl * N_dbl * N_dbl * 2.0 * (double)(repeats)) +
(N_dbl * N_dbl * 2 * (double)(repeats));
printf("FLOPs computed: %f\n", flops_computed);
printf("GFLOP/s rate: %f GF/s\n", (flops_computed / time_taken) / 1000000000.0);
printf("===============================================================\n");
printf("\n");
free(matrixA);
free(matrixB);
free(matrixC);
return 0;
}