-
Notifications
You must be signed in to change notification settings - Fork 2
/
interleave-write.cpp
406 lines (350 loc) · 13 KB
/
interleave-write.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#ifdef _WIN32
#include <intrin.h>
#else
#include <x86intrin.h>
#endif
#include <stdlib.h>
#include "bf16.hpp"
#include <omp.h>
// #include "kernels_avx512.hpp"
#include "kernels_amx.hpp"
#include "tensor2D.hpp"
#include "timeit.hpp"
timeit timer({
{PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES, "HW_CYCLES"},
//{PERF_TYPE_RAW, 0x3c, "CPU_CLK_UNHALTED.THREAD"},
//{PERF_TYPE_RAW, 0x81d0, "MEM_LOAD_RETIRED.ALL_LOADS"},
//{PERF_TYPE_HW_CACHE, 0x10002, "LLC_load_misses"},
//{PERF_TYPE_RAW, 0x10d1, "L2_MISS"},
//{PERF_TYPE_RAW, 0x02d1, "L2_HIT"}, // https://github.com/intel/perfmon/blob/2dfe7d466d46e89899645c094f8a5a2b8ced74f4/SPR/events/sapphirerapids_core.json#L7397
//{PERF_TYPE_RAW, 0x04d1, "L3_HIT"},
//{PERF_TYPE_RAW, 0x01d1, "L1_HIT"}, {PERF_TYPE_RAW, 0x08d1, "L1_MISS"},
//{PERF_TYPE_RAW, 0x01d1, "L1_HIT"}, {PERF_TYPE_RAW, 0x02d1, "L2_HIT"}, {PERF_TYPE_RAW, 0x40d1, "FB_HIT"},
// https://en.wikipedia.org/wiki/MESI_protocol#Read_For_Ownership
// A Read For Ownership (RFO) is an operation in cache coherency protocols that combines a read and an invalidate broadcast.
// The operation is issued by a processor trying to write into a cache line that is in the shared (S) or invalid (I) states
// of the MESI protocol. The operation causes all other caches to set the state of such a line to I. A read for ownership transaction
// is a read operation with intent to write to that memory address. Therefore, this operation is exclusive. It brings data to the cache
// and invalidates all other processor caches that hold this memory line. This is termed "BusRdX" in tables above.
//
//{PERF_TYPE_RAW, 0xC224, "RFO_HIT"},
//{PERF_TYPE_RAW, 0x2224, "RFO_MISS"},
{PERF_TYPE_RAW, 0x04d2, "XSNP_FWD"},
//{PERF_TYPE_RAW, 0xe224, "ALL_RFO"},
//{PERF_TYPE_RAW, 0x412e, "LONGEST_LAT_CACHE.MISS"},
//{PERF_TYPE_RAW, 0x0426, "USELESS_HWPF"},
//{PERF_TYPE_RAW, 0x012A, "HWPF_L2"},
{PERF_TYPE_RAW, 0x2051, "L1D.HWPF_MISS"},
{PERF_TYPE_RAW, 0x3024, "L2.HWPF_MISS"},
//{PERF_TYPE_RAW, 0x10d1, "L2_MISS"},
//{PERF_TYPE_RAW, 0x04d1, "L3_HIT"},
//{PERF_TYPE_RAW, 0x20d1, "L3_MISS"},
//{PERF_TYPE_RAW, 0x81d0, "ALL_LOADS"}, // MEM_INST_RETIRED.ALL_LOADS
//{PERF_TYPE_RAW, 0x08d1, "L1_MISS"},
//{PERF_TYPE_RAW, 0x10d1, "L2_MISS"},
//{PERF_TYPE_RAW, 0x20d1, "L3_MISS"},
//{PERF_TYPE_HW_CACHE, 0x2, "LLC_loads"},
//{PERF_TYPE_RAW, 0x02b1, "UOPS_EXECUTED.CORE"},
});
constexpr double nbytes = 512 * 4096 * sizeof(ov::bfloat16);
inline void copy_out_32x32(tensor2D<float>& C, int x0, int x1) {
auto strideC = C.stride;
#define M512_STORE _mm512_storeu_ps
// #define M512_STORE _mm512_stream_ps
for (int y = 0; y < C.dims[0]; y += 32) {
uint8_t* dst0 = reinterpret_cast<uint8_t*>(&C(y, x0));
for (int x = x0; x < x1; x += 32, dst0 += 32 * sizeof(float)) {
auto ra0 = _mm512_set1_ps(0.1f);
auto ra1 = _mm512_set1_ps(0.2f);
auto rb0 = _mm512_set1_ps(0.3f);
auto rb1 = _mm512_set1_ps(0.4f);
auto* dst = dst0;
for (int i = 0; i < 16; i += 2) {
M512_STORE(dst, ra0);
M512_STORE(dst + 64, rb0);
dst += strideC;
M512_STORE(dst, ra1);
M512_STORE(dst + 64, rb1);
dst += strideC;
}
for (int i = 0; i < 16; i += 2) {
M512_STORE(dst, ra0);
M512_STORE(dst + 64, rb0);
dst += strideC;
M512_STORE(dst, ra1);
M512_STORE(dst + 64, rb1);
dst += strideC;
}
}
}
}
inline void copy_out(tensor2D<float>& C, int x0, int x1) {
auto strideC = C.stride;
auto ra0 = _mm512_set1_ps(0.1f);
#define M512_STORE _mm512_storeu_ps
uint8_t* dst0 = reinterpret_cast<uint8_t*>(&C(0, 0));
for (int y = 0; y < C.dims[0]; y += 16, dst0 += 16 * strideC) {
for (int x = x0; x < x1; x += 16) {
auto* dst = dst0 + x * sizeof(float);
for (int i = 0; i < 16; i++) {
M512_STORE(dst, ra0);
dst += strideC;
}
}
}
}
/*
inline void copy_out(tensor2D<float>& C, int x0, int x1) {
auto strideC = C.stride;
auto ra0 = _mm256_set1_ps (0.1f);
#define M512_STORE _mm256_storeu_ps
uint8_t* dst0 = reinterpret_cast<uint8_t*>(&C(0, x0));
for (int y = 0; y < C.dims[0]; y ++, dst0 += strideC) {
auto* dst = dst0;
for (int x = x0; x < x1; x += 8, dst += 8*sizeof(float)) {
M512_STORE( reinterpret_cast<float*>(dst), ra0);
}
}
}
*/
int test_copy_out(int subN = 8) {
auto nthr = get_nthr();
tensor2D<float> fullC(256, nthr * subN);
std::vector<tensor2D<float>> partC(nthr);
#pragma omp parallel
{
int ithr = omp_get_thread_num();
partC[ithr] = tensor2D<float>(256, subN);
}
for (int r = 0; r < 4; r++) {
auto latency = timer.tag("part", partC[0].capacity / 1000, "KBytes")(1, [&]() {
#pragma omp parallel
{
int ithr = omp_get_thread_num();
copy_out(partC[ithr], 0, subN);
}
});
std::cout << "\t copy_out part BW : " << partC[0].capacity * 1e-9 / latency << " x " << nthr << "=" << fullC.capacity * 1e-9 / latency << " GB/s" << std::endl;
latency = timer.tag("full", partC[0].capacity / 1000, "KBytes")(1, [&]() {
#pragma omp parallel
{
int ithr = omp_get_thread_num();
copy_out(fullC, ithr * subN, ithr * subN + subN);
}
});
std::cout << "\t copy_out full BW : " << partC[0].capacity * 1e-9 / latency << " x " << nthr << "=" << fullC.capacity * 1e-9 / latency << " GB/s" << std::endl;
}
return 0;
}
void my_memset(void* dst_mem, int stride, int H, int W, float f = 0.1f) {
auto* dst0 = reinterpret_cast<uint8_t*>(dst_mem);
auto vf = _mm256_set1_ps(f);
for (int w = 0; w < W; w += 32) _mm_prefetch(dst0 + w, _MM_HINT_NTA);
for (int h = 0; h < H; h++, dst0 += stride) {
// 256bits/32bytes/8floats
for (int w = 0; w < W; w += 32) _mm_prefetch(dst0 + stride + w, _MM_HINT_NTA);
for (int w = 0; w < W; w += 32) {
_mm256_storeu_ps(reinterpret_cast<float*>(dst0 + w), vf);
}
}
}
int write_to_Cache(int per_thread_size, bool is_full) {
auto nthr = get_nthr();
const int M = 256;
std::vector<std::shared_ptr<uint8_t>> buff(nthr, nullptr);
std::vector<uint8_t*> ptrs(nthr, nullptr);
int stride;
if (!is_full) {
#pragma omp parallel
{
int ithr = omp_get_thread_num();
buff[ithr] = std::shared_ptr<uint8_t>(reinterpret_cast<uint8_t*>(aligned_alloc(64, M * per_thread_size)), [](void* p) { ::free(p); });
ptrs[ithr] = buff[ithr].get();
}
stride = (per_thread_size);
} else {
// use single big buffer
buff[0] = std::shared_ptr<uint8_t>(reinterpret_cast<uint8_t*>(aligned_alloc(64, M * nthr * per_thread_size)), [](void* p) { ::free(p); });
for (int ithr = 0; ithr < nthr; ithr++) {
ptrs[ithr] = buff[0].get() + ithr * per_thread_size;
}
stride = (nthr * per_thread_size);
}
timer.tag(is_full ? "full" : "part", M, "x", per_thread_size, "Bytes", nthr, "Threads")(
100,
[&]() {
#pragma omp parallel
{
int ithr = omp_get_thread_num();
my_memset(ptrs[ithr], stride, M, per_thread_size);
#if 0
auto v = _mm256_set1_ps(ithr);
auto* dst0 = reinterpret_cast<float*>(ptrs[ithr]);
for (int i = 0; i < 256; i++, dst0 += stride) {
auto* dst = dst0;
for (int r = 0; r < per_thread_size; r += 32, dst += 8) {
_mm256_storeu_ps(dst, v);
}
}
for (int r = 0; r < per_thread_size; r += 32) {
auto* dst = reinterpret_cast<float*>(&perThreadArgs[ithr * per_thread_size + r]);
for (int i = 0; i < 256; i++, dst += stride) {
_mm256_storeu_ps(dst, v);
}
}
#endif
}
},
1.0 * per_thread_size * M * nthr);
return 1;
}
int read_from_Cache(size_t nbytes, bool USE_SAME) {
int nthr;
uint8_t* thr_data[128];
#pragma omp parallel
{
int ithr = omp_get_thread_num();
if (0 == ithr) {
nthr = omp_get_num_threads();
thr_data[ithr] = reinterpret_cast<uint8_t*>(aligned_alloc(4096, nbytes));
memset(thr_data[ithr], 1, nbytes);
} else if (!USE_SAME) {
thr_data[ithr] = reinterpret_cast<uint8_t*>(aligned_alloc(4096, nbytes));
memset(thr_data[ithr], 1, nbytes);
}
}
if (USE_SAME) {
for (int ithr = 1; ithr < nthr; ithr++) {
thr_data[ithr] = thr_data[0];
}
}
timer.tag(USE_SAME ? "SAME" : "MULTI", static_cast<uint32_t>(nbytes / 1e3), "KBytes", nbytes / 64, "CacheLines", nthr, "threads");
for (int xx = 0; xx < 2; xx++) {
std::cout << "========== clflush " << xx << " ===========" << std::endl;
#pragma omp parallel
{
int ithr = omp_get_thread_num();
clflush(thr_data[ithr], nbytes);
}
for (int t = 0; t < 5; t++) {
timer(
1,
[&]() {
#pragma omp parallel
{
int ithr = omp_get_thread_num();
load_prefetch_L2(thr_data[ithr], nbytes);
}
},
nbytes);
}
}
return 0;
}
void cross_core_L2_read(size_t nbytes = 256 * 256 * 4) {
int nthr = get_nthr();
uint8_t* thr_data[128];
#pragma omp parallel
{
int ithr = omp_get_thread_num();
thr_data[ithr] = reinterpret_cast<uint8_t*>(aligned_alloc(64, nbytes));
memset(thr_data[ithr], 1, nbytes);
}
timer.tag(nthr, "threads");
std::cout << ":::::::::::::::::::::::::::::::\n";
for (int r = 0; r < 5; r++) {
for (int k = 0; k < 5; k++) {
timer(1, [&]() {
#pragma omp parallel
{
int ithr = omp_get_thread_num();
load_prefetch_L2(thr_data[ithr], nbytes);
}
});
}
std::cout << "======= cross-core-cache-read r=" << r << std::endl;
timer(1, [&]() {
#pragma omp parallel
{
int ithr = omp_get_thread_num() + 1;
if (ithr >= nthr)
ithr -= nthr;
load_prefetch_L2(thr_data[ithr], nbytes);
}
});
}
}
void cross_core_L2_write(size_t nbytes = 256 * 256 * 4) {
int nthr = get_nthr();
uint8_t* thr_data[128];
#pragma omp parallel
{
int ithr = omp_get_thread_num();
thr_data[ithr] = reinterpret_cast<uint8_t*>(aligned_alloc(64, nbytes));
memset(thr_data[ithr], 1, nbytes);
}
timer.tag(nthr, "threads");
std::cout << ":::::::::::::::::::::::::::::::\n";
for (int r = 0; r < 5; r++) {
for (int k = 0; k < 5; k++) {
timer(1, [&]() {
#pragma omp parallel
{
int ithr = omp_get_thread_num();
my_memset(thr_data[ithr], nbytes, 1, nbytes);
}
});
}
std::cout << "======= cross-core-cache-write r=" << r << std::endl;
timer(1, [&]() {
#pragma omp parallel
{
int ithr = omp_get_thread_num() + 1;
if (ithr >= nthr)
ithr -= nthr;
my_memset(thr_data[ithr], nbytes, 1, nbytes);
}
});
}
}
int main() {
// cross_core_L2_write(); cross_core_L2_read(); return 0;
MSRConfig _msr1;
for(int r = 0; r < 10; r++) {
write_to_Cache(1024, true);
write_to_Cache(1024, false);
//write_to_Cache(2048, true);
//write_to_Cache(2048, false);
//write_to_Cache(4096, true);
//write_to_Cache(4096, false);
}
return 0;
read_from_Cache(1024 * 1024 * 1, false);
read_from_Cache(1024 * 1024 * 1.5, false);
read_from_Cache(1024 * 1024 * 2, false);
read_from_Cache(1024 * 1024 * 2, true);
read_from_Cache(1024 * 1024 * 3.8, false);
read_from_Cache(1024 * 1024 * 3.8, true);
return test_copy_out(256);
#if 0
std::cout << "========== clflush ===========" << std::endl;
#pragma omp parallel
{
int ithr = omp_get_thread_num();
clflush(thr_data[ithr], nbytes);
}
for (int ithr = 0; ithr < nthr; ithr++) {
std::cout << " load on ithr " << ithr << std::endl;
for (int t = 0; t < 3; t++) {
auto latency = timer(1, [&]() {
#pragma omp parallel
{
if (ithr == omp_get_thread_num())
load_prefetch_L2(thr_data[ithr], nbytes);
}
});
}
}
#endif
return 0;
}