-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_bench.cpp
639 lines (485 loc) · 17.1 KB
/
cache_bench.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
* This file is part of mpi-cache-bench.
*
* mpi-cache-bench is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mpi-cache-bench is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mpi-cache-bench. If not, see <http://www.gnu.org/licenses/>.
*/
#include "affinity.h"
#include "papi_wrap.h"
#include "hwloc_wrap.h"
#include <mpi.h>
#include <iostream>
#include <iterator>
#include <string>
#include <cassert>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iomanip>
#include <locale>
volatile size_t g_val = 5;
int rank;
unsigned offset = 0;
#define ENABLE_SYNCH
#define CLEAN \
{ \
MPI_Barrier(MPI_COMM_WORLD); \
for(register size_t idx=0, end=std::max(cache_size,size); idx<end; idx+=cache_line) { \
buff[idx+(idx%(cache_line-1))] += g_val; \
} \
}
//Defines the loop utilized to bring the data into the cache. We do this backwards so that
//we are sure L1 and L2 cache are also filled with the values we are going to access in the
//benchmark
#define _RCOMP \
{\
for(register long idx=size-cache_line; idx>=0; idx-=cache_line) { \
g_val+=msg[idx+(idx%(cache_line-1))]; \
} \
}
// This is the computational loop utilized to load the value of the message buffer
// into the cache
#define RCOMP(x) \
{\
reg.start(x);\
for (register size_t i=0; i<size; i+=cache_line) \
g_val += msg[i]; \
reg.end(x);\
}
// This is the computational loop utilized to load the value of the message buffer
// into the cache
#define _WCOMP \
{\
for(register long idx=size-cache_line; idx>=0; idx-=cache_line) { \
msg[idx+(idx%(cache_line-1))] += g_val; \
} \
}
// This is the computational loop utilized to load the value of the message buffer
// into the cache
#define WCOMP(x) \
{\
reg.start(x);\
for (register size_t i=0; i<size; i+=cache_line) \
msg[i] += g_val; \
reg.end(x);\
}
// This is the communication part of the benchmark which send/recv the array between
// the 2 processes
#define COMM(x) \
{\
if (rank == 0) {\
reg.start(x); \
PMPI_Send((char*)msg, size, MPI_BYTE, 1, 0, MPI_COMM_WORLD);\
reg.end(x); \
} else {\
reg.start(x); \
PMPI_Recv((char*)msg, size, MPI_BYTE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);\
reg.end(x); \
}\
}
// Warm up the instruction cache
#define _COMM \
if (rank == 0) { \
PMPI_Recv(NULL, 0, MPI_BYTE, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); \
PMPI_Send(NULL, 0, MPI_BYTE, 1, 1, MPI_COMM_WORLD); \
} else { \
PMPI_Send(NULL, 0, MPI_BYTE, 0, 0, MPI_COMM_WORLD); \
PMPI_Recv(NULL, 0, MPI_BYTE, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); \
} \
#define MEMCPY(x) \
{ \
reg.start(x); \
memcpy((char*)buff, (char*)msg, size); \
reg.end(x); \
}
typedef void (*TestFunc)(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size);
//=============================================================================
// TEST 1: Load array (read mode) from memory when cache is cold
// [ cached is cleaned up and then msg array is accessed]
//=============================================================================
void test_1(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
CLEAN;
RCOMP(101100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
//=============================================================================
// TEST 2: Load array (read mode) from memory when cache is hot
// [ cached is preloaded and then msg array is accessed]
//=============================================================================
void test_2(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
_RCOMP;
RCOMP(102100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
//=============================================================================
// TEST 3: Load array (write mode) from memory when cache is cold and perform a
// write operation.
// [ cached is cleaned up and then msg array is accessed]
//=============================================================================
void test_3(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
CLEAN;
WCOMP(203100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
//=============================================================================
// TEST 4: Load array (write mode) from memory when cache is hot and perform a
// write operation
// [ cached is preloaded and then msg array is accessed]
//=============================================================================
void test_4(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
_RCOMP;
WCOMP(204100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
//=============================================================================
// TEST 5: Send/recv array from memory when cache is cold
//=============================================================================
void test_5(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
CLEAN;
_COMM;
COMM(305200+offset);
#ifdef ENABLE_SYNCH
_COMM;
#endif
}
//=============================================================================
// TEST 6: Send/recv array from memory when cache is hot (read)
//=============================================================================
void test_6(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
// clean up everything
CLEAN;
// Load array into cache
_RCOMP;
_COMM;
COMM(306200+offset);
#ifdef ENABLE_SYNCH
_COMM;
#endif
}
//=============================================================================
// TEST 8: Send/recv array from memory when cache is hot (write)
//=============================================================================
void test_7(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
CLEAN;
// Load array into cache
_WCOMP;
_COMM;
COMM(307200+offset);
#ifdef ENABLE_SYNCH
_COMM;
#endif
}
//=============================================================================
// TEST 8: Send/recv array from memory when cache is hot (write)
//=============================================================================
void test_8(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
// Load array into cache
CLEAN;
memcpy(NULL, NULL, 0);
MEMCPY(408100 + offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
//=============================================================================
// TEST 8: Send/recv array from memory when cache is hot (write)
//=============================================================================
void test_9(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
CLEAN;
// Load array into cache
memcpy(NULL, NULL, 0);
_RCOMP;
MEMCPY(409100 + offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
//=============================================================================
// TEST 11: Send/recv array from memory when cache is hot (write)
//=============================================================================
void test_10(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
CLEAN;
// Load array into cache
memcpy(NULL, NULL, 0);
_WCOMP;
MEMCPY(410100 + offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
//=============================================================================
// TEST 1: Load array (read mode) from memory when cache is cold
// [ cached is cleaned up and then msg array is accessed]
//=============================================================================
void test_20(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
CLEAN;
MPI_Barrier(MPI_COMM_WORLD);
RCOMP(720100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
void test_21(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
CLEAN;
MPI_Barrier(MPI_COMM_WORLD);
_RCOMP;
MPI_Barrier(MPI_COMM_WORLD);
RCOMP(721100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
void test_22(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
// Load array into cache
CLEAN;
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0) {
MPI_Send((char*)msg, size, MPI_BYTE, 1, 0, MPI_COMM_WORLD);
} else {
MPI_Recv((char*)msg, size, MPI_BYTE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
MPI_Barrier(MPI_COMM_WORLD);
RCOMP(722100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
void test_23(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
// Load array into cache
CLEAN;
MPI_Barrier(MPI_COMM_WORLD);
_RCOMP
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0) {
MPI_Send((char*)msg, size, MPI_BYTE, 1, 0, MPI_COMM_WORLD);
} else {
MPI_Recv((char*)msg, size, MPI_BYTE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
MPI_Barrier(MPI_COMM_WORLD);
RCOMP(723100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
void test_30(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
CLEAN;
MPI_Barrier(MPI_COMM_WORLD);
WCOMP(830100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
void test_31(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
CLEAN;
MPI_Barrier(MPI_COMM_WORLD);
_WCOMP;
MPI_Barrier(MPI_COMM_WORLD);
WCOMP(831100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
void test_32(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
// Load array into cache
CLEAN;
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0) {
MPI_Send((char*)msg, size, MPI_BYTE, 1, 0, MPI_COMM_WORLD);
} else {
MPI_Recv((char*)msg, size, MPI_BYTE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
MPI_Barrier(MPI_COMM_WORLD);
WCOMP(832100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
void test_33(RegionCounter& reg, volatile char* msg, volatile char* buff, size_t cache_size, size_t cache_line, size_t size) {
// Load array into cache
CLEAN;
MPI_Barrier(MPI_COMM_WORLD);
_WCOMP
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0) {
MPI_Send((char*)msg, size, MPI_BYTE, 1, 0, MPI_COMM_WORLD);
} else {
MPI_Recv((char*)msg, size, MPI_BYTE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
MPI_Barrier(MPI_COMM_WORLD);
WCOMP(833100+offset);
#ifdef ENABLE_SYNCH
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
class BenchBinder {
TestFunc func_ptr;
volatile char* msg_ptr;
volatile char* buff_ptr;
size_t cache_size;
size_t curr_size;
unsigned cache_line;
public:
BenchBinder(const TestFunc& func_ptr,
volatile char* msg_ptr,
volatile char* buff_ptr,
size_t cache_size,
size_t curr_size,
unsigned cache_line)
: func_ptr(func_ptr),
msg_ptr(msg_ptr),
buff_ptr(buff_ptr),
cache_size(cache_size),
curr_size(curr_size),
cache_line(cache_line) { }
inline void operator()(RegionCounter& reg) const {
return func_ptr(reg, msg_ptr, buff_ptr, cache_size, cache_line, curr_size);
}
};
void measure(unsigned rep, std::ostream& logFile, const EventNames& evts, size_t cache_size, size_t cache_line_size)
{
TestFunc benchs[] = {
// &test_1, &test_2,
&test_5, &test_6, &test_7,
//&test_8, &test_9 , &test_10,
test_20, test_21, test_22, test_23,
test_30, test_31, test_32, test_33,
};
offset = 0;
MPI_Barrier(MPI_COMM_WORLD);
!rank && std::cout << "~~~> Benchmark STARTS <~~~" << std::endl;
!rank && std::cout << " + Don't move and hold your breath" << std::endl;
for (register size_t size = 64; size <= cache_size*4; size*=2) {
MPI_Barrier(MPI_COMM_WORLD);
!rank && std::cout << "Measuring for size: " << size << std::endl;
++offset;
size_t buff_size = std::max(cache_size, size);
volatile char* msg = new char[ 2 * buff_size ];
// printf("BUFF: %x - %x\n", buff, (buff + buff_size));
volatile char* buff = &msg[ buff_size ];
// printf("MSG: %x - %x\n", msg, (msg + buff_size));
memset((char*)msg, sizeof(char) * 2 * buff_size, 2);
for(size_t idx=0; idx<11; ++idx) {
measure(logFile, evts, BenchBinder(benchs[idx], msg, buff, cache_size, size, cache_line_size), rep);
!rank && std::cout << "%" << std::flush;
}
!rank && std::cout << std::endl;
delete[] msg;
}
}
size_t read_counter_names(const std::string& file_name, std::vector<std::string>& counter_names) {
size_t max_lenght=0;
try {
std::ifstream file(file_name.c_str(), std::ifstream::in);
std::string line;
while(std::getline(file, line)) {
line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
counter_names.push_back(line);
max_lenght = std::max(max_lenght, line.length());
}
}catch(std::exception e) {
std::cerr << "Error while reading file: " << file_name << std::endl;
}
return max_lenght;
}
int main (int argc, char* argv[]) {
MPI_Init(NULL, NULL);
int comm_size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
// Read the PAPI_HOME environment variable
std::string PAPI_HOME = getenv("PAPI_HOME") ? getenv("PAPI_HOME") : "";
if (PAPI_HOME.length() != 0) {
PAPI_HOME = PAPI_HOME+"/bin/";
}
std::cout << "PAPI_HOME=" << PAPI_HOME << std::endl;
std::vector<std::string> evts;
// Read the available events from PAPI
char rankStr[30];
sprintf(rankStr, "%d", rank);
std::string fileName = std::string("/tmp/hw_counters_") + rankStr + ".txt";
system((PAPI_HOME + "papi_avail -a | grep ^PAPI_| cut -d \" \" -f 1 > " + fileName).c_str());
MPI_Barrier(MPI_COMM_WORLD);
// get the hostname of the two processes
char hostname[30];
gethostname(hostname, 30);
std::cout << "[R" << rank << "]: Processes allocated on node " << hostname << std::endl;
char (*hosts)[30];
if (rank == 0) {
hosts = new char[comm_size][30];
}
MPI_Gather(hostname, 30, MPI_CHAR, hosts, 30, MPI_CHAR, 0, MPI_COMM_WORLD);
int sameHost=0;
if (rank==0 && (std::string(hosts[0]) == std::string(hosts[1]))) {
sameHost=1;
}
MPI_Bcast(reinterpret_cast<void*>(&sameHost), 1, MPI_INT, 0, MPI_COMM_WORLD);
std::cout << "[R" << rank << "]: Same host " << sameHost << std::endl;
if (rank == 0) { delete[] hosts; }
size_t length = 0;
length = std::max(length, read_counter_names(fileName, evts));
length = std::max(length, read_counter_names("./counters.txt", evts));
length+=2;
std::cout << "Number of PAPI counters: " << evts.size() << std::endl;
// open log files
std::string logFileName = std::string("cache_bench.r") + rankStr + ".csv";
std::fstream logFile(logFileName.c_str(), std::fstream::out | std::fstream::trunc);
Info info(argc, argv);
if (rank == 0) {
std::cout << "@@ Num cores: " << info.num_cores << std::endl;
std::cout << "@@ Num sockets: " << info.num_sockets << std::endl;
}
size_t cache_size = info.cache_sizes[info.levels-1];
std::cout << "@@ Total last level cache size per CPU is: " << cache_size << std::endl;
if (sameHost!=0 && info.num_sockets==1) {
// shared cache
std::cout << "Running on shared cache" << std::endl;
}
unsigned affinity = 0;
// find best affinity for the benchmark
if (sameHost==1 && info.num_sockets==1) {
affinity = info.num_cores-1;
std::cout << "MPI Processes running on same CPU" << std::endl;
}
if (sameHost==1 && info.num_sockets!=1) {
affinity = info.num_cores/info.num_sockets;
}
std::cout << "[R" << rank <<"] Affinity set to: {0, " << affinity << "}" << std::endl;
set_process_affinity(rank, (size_t[2]){ 0, affinity });
logFile << std::setw(8) << "id" << std::setw(10) << "time";
for(EventNames::const_iterator it=evts.begin(), end=evts.end(); it!=end; ++it) { logFile << std::setw(length) << *it; }
logFile << std::flush << std::endl;
std::cout << "Cache size is: " << cache_size << std::endl;
std::cout << "**** Warmup channels ****" << std::endl;
for(unsigned i=0; i<100; ++i) {
int data=0;
if(rank==0) {
MPI_Send(&data,1,MPI_INT,1,0,MPI_COMM_WORLD);
} else {
MPI_Recv(&data,1,MPI_INT,0,0,MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
}
measure(REPETITIONS, logFile, evts, cache_size, 64);
logFile.close();
std::cout << g_val << std::endl;
MPI_Finalize();
}