-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatmul.c
225 lines (165 loc) · 6.43 KB
/
matmul.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
215
216
217
218
219
220
221
222
223
224
225
#include <stdio.h>
#include "stuff.h"
#include "matmul_kernels.h"
#include "matmul_kernels_avx.h"
const int N = 1000; // Default elements per row and col: NxN matrix
const int BLK = 16;
int main(int argc, char *argv[])
{
resnfo start, end;
timenfo time;
// Parameters
// 0: Elements per row and per column (default: N)
unsigned int n = (argc > 1)?atoi (argv[1]):N;
// 1: Tiling: elements per row and per column in block (default: BLK)
unsigned int blk = (argc > 2)?atoi (argv[2]):BLK;
adjust_params(&n);
// Matrices size
unsigned int numBytes = n * n * sizeof(basetype);
// Allocate and init matrices (32-byte aligned addresses for AVX2)
// About aligned memory allocation:
// https://stackoverflow.com/questions/32612881/why-use-mm-malloc-as-opposed-to-aligned-malloc-alligned-alloc-or-posix-mem
// C11 alt.: void * aligned_alloc (size_t alignment, size_t size)
// POSIX alt.: int posix_memalign (void **memptr, size_t alignment, size_t size)
// Intel's used here: void* _mm_malloc (int size, int align)
// void _mm_free (void *p)
timestamp(&start);
// basetype *vectorA = (basetype *) malloc(numBytes);
// basetype *vectorB = (basetype *) malloc(numBytes);
// basetype *vectorR = (basetype *) malloc(numBytes);
basetype *vectorA = (basetype *) _mm_malloc (numBytes, 32);
basetype *vectorB = (basetype *) _mm_malloc (numBytes, 32);
basetype *vectorR = (basetype *) _mm_malloc (numBytes, 32);
populating_arrays(vectorA, vectorB, vectorR, n*n);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrices allocation and initialiation (%ux%u %s)\n\n", n, n, labelelem);
// Naive ijk version
timestamp(&start);
matmul(vectorA, vectorB, vectorR, n, n, n);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: naive ijk implementation\n");
// reset_matrix(vectorR, n*n);
basetype check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// ijk opt version
timestamp(&start);
matmul_opt(vectorA, vectorB, vectorR, n, n, n);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: ijk opt implementation\n\n");
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// ikj version
timestamp(&start);
matmul_ikj(vectorA, vectorB, vectorR, n, n, n);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: ikj implementation\n\n");
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// ikj opt version
timestamp(&start);
matmul_ikj_opt(vectorA, vectorB, vectorR, n, n, n);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: ikj opt implementation\n\n");
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// Tiling version
timestamp(&start);
matmul_tiling(vectorA, vectorB, vectorR, n, n, n, blk, blk);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: tiling approach (blk: %dx%d)\n\n", blk, blk);
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// Tiling version with additional opt
timestamp(&start);
matmul_tiling_opt(vectorA, vectorB, vectorR, n, n, n, blk, blk);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: tiling approach with opt\n\n");
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// Tiling version with additional opts
timestamp(&start);
matmul_tiling_opt2(vectorA, vectorB, vectorR, n, n, n, blk, blk);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: tiling approach with additional opts\n\n");
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// Tiling+ikj version
timestamp(&start);
matmul_tiling_ikj(vectorA, vectorB, vectorR, n, n, n, blk, blk);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: tiling+ikj approach\n\n");
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// Tiling+ikj with a minimal optimization effort
timestamp(&start);
matmul_tiling_ikj_opt(vectorA, vectorB, vectorR, n, n, n, blk, blk);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: tiling+ikj approach with min opts\n\n");
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// Tiling+ikj with a medium optimization effort
timestamp(&start);
matmul_tiling_ikj_opt2(vectorA, vectorB, vectorR, n, n, n, blk, blk);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: tiling+ikj approach with med opts\n\n");
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// cls version
timestamp(&start);
matmul_tiling_cls_opt(vectorA, vectorB, vectorR, n, n, n, 16);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: cls implementation\n\n");
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// ikj AVX version
timestamp(&start);
matmul_ikj_avx(vectorA, vectorB, vectorR, n, n, n);
timestamp(&end);
get_walltime(start, end, &time);
printtime(time);
printf(" -> Matrix product: ikj AVX implementation\n\n");
// reset_matrix(vectorR, n*n);
check = check_result_and_reset(vectorR, n*n);
printf("%29s", "Check: "); printdata(check); printf("\n\n");
// free(vectorA);
// free(vectorB);
// free(vectorR);
_mm_free(vectorA);
_mm_free(vectorB);
_mm_free(vectorR);
return(0);
}