-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.C
More file actions
2667 lines (2365 loc) · 86 KB
/
code.C
File metadata and controls
2667 lines (2365 loc) · 86 KB
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************/
/* */
/* Copyright (c) 1994 Stanford University */
/* */
/* All rights reserved. */
/* */
/* Permission is given to use, copy, and modify this software for any */
/* non-commercial purpose as long as this copyright notice is not */
/* removed. All other uses, including redistribution in whole or in */
/* part, are forbidden without prior written permission. */
/* */
/* This software is provided with absolutely no warranty and no */
/* support. */
/* */
/*************************************************************************/
/*
Usage: BARNES <options> < inputfile
Command line options:
-h : Print out input file description
Input parameters should be placed in a file and redirected through
standard input. There are a total of twelve parameters, and all of
them have default values.
1) infile (char*) : The name of an input file that contains particle
data.
The format of the file is:
a) An int representing the number of particles in the distribution
b) An int representing the dimensionality of the problem (3-D)
c) A double representing the current time of the simulation
d) Doubles representing the masses of all the particles
e) A vector (length equal to the dimensionality) of doubles
representing the positions of all the particles
f) A vector (length equal to the dimensionality) of doubles
representing the velocities of all the particles
Each of these numbers can be separated by any amount of whitespace.
2) nbody (int) : If no input file is specified (the first line is
blank), this number specifies the number of particles to generate
under a plummer model. Default is 16384.
3) seed (int) : The seed used by the random number generator.
Default is 123.
4) outfile (char*) : The name of the file that snapshots will be
printed to. This feature has been disabled in the SPLASH release.
Default is NULL.
5) dtime (double) : The integration time-step.
Default is 0.025.
6) eps (double) : The usual potential softening
Default is 0.05.
7) tol (double) : The cell subdivision tolerance.
Default is 1.0.
8) fcells (double) : Number of cells created = fcells * number of
leaves.
Default is 2.0.
9) fleaves (double) : Number of leaves created = fleaves * nbody.
Default is 0.5.
10) tstop (double) : The time to stop integration.
Default is 0.075.
11) dtout (double) : The data-output interval.
Default is 0.25.
12) NPROC (int) : The number of processors.
Default is 1.
*/
MAIN_ENV
#include <chrono>
#include <cassert>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <bits/stdc++.h>
#include <unordered_map>
#include <fcntl.h>
#include <sys/mman.h>
#define global /* nada */
#include "stdinc.h"
//#include "cha.h"
#include "topology.h"
#include "load.h"
#include "grav.h"
#include "constants.h"
#include "protocol.h"
#include "hash.h"
std::map<long, std::multiset<double *>> threadid_addresses_map;
pthread_spinlock_t map_spinlock;
thread_local uint64_t sampling_counter = 0;
thread_local uint64_t next_sampling_iteration = 0;
string defv[] = { /* DEFAULT PARAMETER VALUES */
/* file names for input/output */
"in=", /* snapshot of initial conditions */
"out=", /* stream of output snapshots */
/* params, used if no input specified, to make a Plummer Model */
"nbody=16384", /* number of particles to generate */
"seed=123", /* random number generator seed */
/* params to control N-body integration */
"dtime=0.025", /* integration time-step */
"eps=0.05", /* usual potential softening */
"tol=1.0", /* cell subdivision tolerence */
"fcells=2.0", /* cell allocation parameter */
"fleaves=0.5", /* leaf allocation parameter */
"tstop=0.075", /* time to stop integration */
"dtout=0.25", /* data-output interval */
"NPROC=1", /* number of processors */
};
/* The more complicated 3D case */
#define NCORES 28
#define NUM_SOCKETS 2
#define NUM_CHA_BOXES 28
#define NUM_DIRECTIONS 32
#define BRC_FUC 0
#define BRC_FRA 1
#define BRA_FDA 2
#define BRA_FRC 3
#define BLC_FDC 4
#define BLC_FLA 5
#define BLA_FUA 6
#define BLA_FLC 7
#define BUC_FUA 8
#define BUC_FLC 9
#define BUA_FUC 10
#define BUA_FRA 11
#define BDC_FDA 12
#define BDC_FRC 13
#define BDA_FDC 14
#define BDA_FLA 15
#define FRC_BUC 16
#define FRC_BRA 17
#define FRA_BDA 18
#define FRA_BRC 19
#define FLC_BDC 20
#define FLC_BLA 21
#define FLA_BUA 22
#define FLA_BLC 23
#define FUC_BUA 24
#define FUC_BLC 25
#define FUA_BUC 26
#define FUA_BRA 27
#define FDC_BDA 28
#define FDC_BRC 29
#define FDA_BDC 30
#define FDA_BLA 31
#define CACHELINE_SIZE 64
#define ALIGN_TO_CACHE_LINE(addr) ((uint64_t)(addr) & (~(CACHE_LINE_SZ-1)))
#define OFFSET_OF_CACHE_LINE(addr) ((uint64_t)(addr) >> 6)
#define HASHTABLE_SIZE (100000)
#define SAMPLING_PERIOD (100000)
//static const long CHA_MSR_PMON_CTRL_BASE = 0x0E01L;
HashTable *comm_map;//(HASHTABLE_SIZE);
std::vector<std::vector<std::pair<int, std::unordered_map<int, int>>>> comm_matrix(NCORES, std::vector<std::pair<int, std::unordered_map<int, int>>> (NCORES, std::pair<int, std::unordered_map<int, int>> {}));
enum class TrafficType
{
Data,
Address,
Acknowledge,
Invalidate
};
char *data;
uintptr_t allocation_offset = 0;
int shared_mem_fd;
unsigned short rdtsc() {
unsigned short c;
__asm__("rdtsc\n" : "=a" (c));
return c;
}
int getCoreCount() { return static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN)); }
void * cha_aware_malloc(size_t size) {
assert(allocation_offset + size < NUM);
void * allocated_address = reinterpret_cast<void*>(&data[allocation_offset]);
allocation_offset += size;
//std::cerr << "allocation_offset is " << allocation_offset << "\n";
return allocated_address;
}
void * cha_aware_memalign(size_t alignment, size_t size) {
uint64_t allocated_address = (uint64_t) (&data[allocation_offset]);
while((allocated_address % alignment) != 0) {
allocation_offset++;
allocated_address = (uint64_t) (&data[allocation_offset]);
}
assert(allocation_offset + size < NUM);
void * returned_address = reinterpret_cast<void*>(&data[allocation_offset]);
allocation_offset += size;
//std::cerr << "allocation_offset is " << allocation_offset << "\n";
return returned_address;
}
uint8_t cha_of_element(void* offset) {
//std::cerr << (uint64_t) offset - (uint64_t) data << "\n";
assert((uint64_t) offset - (uint64_t) data < NUM);
uint8_t * located_address = reinterpret_cast<uint8_t*>((uint64_t) offset+NUM);
return *located_address;
}
void initialize_shared_memory() {
shared_mem_fd = shm_open(NAME/*"/dev/shm/shared_mem"*/, O_RDWR, 0666);
if (shared_mem_fd < 0) {
perror("shm_open()");
return;
}
data = static_cast<char *const>(mmap(nullptr, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shared_mem_fd, 0));
if (data == MAP_FAILED) {
perror("mmap failed");
std::exit(EXIT_FAILURE);
}
uint64_t holder = 0;
for (int i = 0; i < NUM; ++i) {
holder += (char) data[i];
}
std::cerr << holder << "\n";
}
void inc_comm(int tid,
const int& cha,
const long unsigned int& addr)
{
assert(tid < 28);
//std::cerr << "here 0 2\n";
#if 0
if(sampling_counter++ < next_sampling_iteration) {
//cerr << "inc_comm discarded\n";
return;
}
#endif
//std::cerr << "inc_comm begins\n";
//next_sampling_iteration = SAMPLING_PERIOD + next_sampling_iteration;
//std::cerr << "inc_comm begins 2 " << addr << "\n";
uint64_t line = OFFSET_OF_CACHE_LINE(addr);
//std::cerr << "inc_comm begins 3\n";
//std::cerr << "here 0\n";
//std::cerr << line << "\n";
//std::cerr << "before hashFunction\n";
uint64_t hash_idx = comm_map->hashFunction(line);
//std::cerr << "after hashFunction\n";
//#if 0
//std::cerr << "here 0 1\n";
int sh = 1;
int a;
int b;
//#if 0
comm_map->wait(hash_idx);
//std::cerr << "here 1\n";
//#if 0
if(!comm_map->findItem(hash_idx, line)) {
//std::cerr << "here 2\n";
comm_map->insertItem(hash_idx, line);
//std::cerr << "here 3\n";
}
//#endif
//std::cerr << "here 4\n";
std::pair<long unsigned int, std::pair<int, int>>& comm_map_element = comm_map->getItem(hash_idx, line);
a = comm_map_element.second.first;
b = comm_map_element.second.second;
//#endif
if (a == 0 && b == 0)
sh = 0;
if (a != 0 && b != 0)
sh = 2;
//#endif
//comm_map->print();
//comm_map->signal(hash_idx);
//comm_map->print_long(hash_idx);
//comm_map->print();
//#if 0
switch (sh) {
case 0:
comm_map_element.second.first = tid+1;
//mutex_map[hash_idx].unlock();
comm_map->signal(hash_idx);
//mtx->unlock();
//global_mtx.unlock();
//comm_map[line].first = tid+1;
break;
case 1:
comm_map_element.second.first = tid+1;
//comm_map[line].second = a;
comm_map_element.second.second = a;
//mutex_map[hash_idx].unlock();
comm_map->signal(hash_idx);
//mtx->unlock();
//global_mtx.unlock();
comm_matrix[tid][a-1].first++;
comm_matrix[tid][a-1].second[cha]++;
break;
case 2: // two previous accesses
// if (a != tid+1 && b != tid+1) {
//comm_map[line].first = tid+1;
comm_map_element.second.first = tid+1;
//comm_map[line].second = a;
comm_map_element.second.second = a;
//mutex_map[hash_idx].unlock();
comm_map->signal(hash_idx);
//mtx->unlock();
//global_mtx.unlock();
if (tid!=a-1) {
//if(tid < a-1) {
comm_matrix[tid][a-1].first++;
comm_matrix[tid][a-1].second[cha]++;
//std::cerr << "comm is detected\n";
//} else {
// comm_matrix[a-1][tid].first++;
// comm_matrix[a-1][tid].second[cha]++;
//}
}
if (tid!=b-1) {
//if(tid < b-1) {
comm_matrix[tid][b-1].first++;
comm_matrix[tid][b-1].second[cha]++;
//} else {
// comm_matrix[b-1][tid].first++;
// comm_matrix[b-1][tid].second[cha]++;
//}
}
break;
}
//comm_map->signal(hash_idx);
//#endif
}
std::string enumToStr(TrafficType traffic_type)
{
std::string res;
switch (traffic_type)
{
case TrafficType::Data:
res = "data";
break;
case TrafficType::Address:
res = "address";
break;
case TrafficType::Acknowledge:
res = "acknowledge";
break;
case TrafficType::Invalidate:
res = "invalidate";
break;
default:
res = "unknown";
break;
}
return res;
}
std::map<int, int> getMsrFds()
{
// SPDLOG_TRACE(__PRETTY_FUNCTION__);
std::map<int, int> fd_map;
char filename[100];
auto logical_core_count = getCoreCount();
// SPDLOG_TRACE("logical core count: {}", logical_core_count);
for(auto i = 0; i < logical_core_count; ++i) {
sprintf(filename, "/dev/cpu/%d/msr",i);
int fd = open(filename, O_RDWR);
if(fd >= 0) {
// SPDLOG_TRACE("Opened fd: {}", fd);
fd_map.insert({i, fd});
} else if(fd == -1) {
// SPDLOG_ERROR("error on open(): {}", strerror(errno));
}
}
for(const auto& p : fd_map) {
// SPDLOG_TRACE("MSR fd of core {}: {}", p.first, p.second);
}
return fd_map;
}
void setAllUncoreRegisters(const std::vector<unsigned int>& vals)
{
// SPDLOG_TRACE(__PRETTY_FUNCTION__);
// SPDLOG_TRACE("Setting all uncore registers with values: ");
for(auto val : vals) {
// SPDLOG_TRACE("{:x}", val);
}
int processor_in_socket[NUM_SOCKETS];
int logical_core_count = getCoreCount();
processor_in_socket[0] = 0;
processor_in_socket[1] = logical_core_count - 1;
auto msr_fds = getMsrFds();
for(auto socket = 0; socket < NUM_SOCKETS; ++socket) {
for(auto cha = 0; cha < NUM_CHA_BOXES; ++cha) {
int core = processor_in_socket[socket];
for(auto i = 0u; i < vals.size(); ++i) {
uint64_t val = vals[i];
uint64_t offset = CHA_MSR_PMON_CTRL_BASE + (0x10 * cha) + i;
ssize_t rc64 = pwrite(msr_fds[core], &val, sizeof(val), offset);
if(rc64 == 8) {
// SPDLOG_TRACE("Configuring socket {}, CHA {}, by writing 0x{:x} to core {} (fd: {}), offset 0x{:x}.",
// socket, cha, val, core, msr_fds[core], offset);
} else {
// SPDLOG_ERROR("Error writing all data to MSR device on core {}, written {} bytes.", core, rc64);
}
}
}
}
/// it is important to close this as well, otherwise we will have fd leak.
// SPDLOG_TRACE("closing file descriptors of MSRs.");
for(const auto& p : msr_fds) {
int cpu = p.first;
int to_be_closed = p.second;
// SPDLOG_TRACE("closing fd {} of cpu {}.", to_be_closed, cpu);
::close(to_be_closed);
// SPDLOG_TRACE("closed fd: {}", to_be_closed);
}
}
std::vector<std::vector<uint64_t>> storeTraffic(TrafficType traffic_type)
{
std::vector<std::vector<uint64_t>> res;
auto msr_fds = getMsrFds();
unsigned int left_val = 0;
unsigned int right_val = 0;
unsigned int up_val = 0;
unsigned int down_val = 0;
if (traffic_type == TrafficType::Data)
{
left_val = LEFT_BL_READ;
right_val = RIGHT_BL_READ;
up_val = UP_BL_READ;
down_val = DOWN_BL_READ;
}
else if (traffic_type == TrafficType::Address)
{
left_val = LEFT_AD_READ;
right_val = RIGHT_AD_READ;
up_val = UP_AD_READ;
down_val = DOWN_AD_READ;
}
else if (traffic_type == TrafficType::Acknowledge)
{
left_val = LEFT_AK_READ;
right_val = RIGHT_AK_READ;
up_val = UP_AK_READ;
down_val = DOWN_AK_READ;
}
else if (traffic_type == TrafficType::Invalidate)
{
left_val = LEFT_IV_READ;
right_val = RIGHT_IV_READ;
up_val = UP_IV_READ;
down_val = DOWN_IV_READ;
}
else
{
std::cerr << "unexpected traffic type!";
std::exit(1);
}
std::vector<unsigned int> vals = {left_val, right_val, up_val, down_val, FILTER0_OFF, FILTER1_OFF};
setAllUncoreRegisters(vals);
/// popping up filter values since we will not read from them.
vals.pop_back();
vals.pop_back();
//SPDLOG_DEBUG("---------------- FIRST READINGS ----------------");
for (int socket = 0; socket < /*NUM_SOCKETS*/ 1; ++socket)
{ // we will work on socket-0
long core = 0; // this line will only work for socket-0!
for (int cha = 0; cha < NUM_CHA_BOXES; ++cha)
{
std::vector<uint64_t> direction_vals;
for (int i = 0; i < vals.size(); ++i)
{
uint64_t msr_val;
uint64_t msr_num = CHA_MSR_PMON_CTR_BASE + (CHA_BASE * cha) + i;
//SPDLOG_DEBUG("Executing pread() --> fd: {}, offset: {:x}", msr_fds[core], msr_num);
ssize_t rc64 = pread(msr_fds[core], &msr_val, sizeof(msr_val), msr_num);
if (rc64 != sizeof(msr_val))
{
//SPDLOG_ERROR("EXIT FAILURE. rc64: {}", rc64);
//SPDLOG_ERROR("error: {}", strerror(errno));
exit(EXIT_FAILURE);
}
else
{
direction_vals.push_back(msr_val);
//SPDLOG_DEBUG("Read {} from socket {}, CHA {} on core {}, offset 0x{:x}.", msr_val, socket, cha, core, msr_num);
}
}
res.push_back(direction_vals);
}
}
//SPDLOG_DEBUG("closing file descriptors of MSRs.");
for (const auto &p : msr_fds)
{
int cpu = p.first;
int to_be_closed = p.second;
//SPDLOG_DEBUG("closing fd {} of cpu {}.", to_be_closed, cpu);
::close(to_be_closed);
}
return res;
}
enum class BenchmarkOption
{
None,
OnlyDefault,
OnlyChaAware,
Both
};
static long Child_Sequence[NUM_DIRECTIONS][NSUB] =
{
{ 2, 5, 6, 1, 0, 3, 4, 7}, /* BRC_FUC */
{ 2, 5, 6, 1, 0, 7, 4, 3}, /* BRC_FRA */
{ 1, 6, 5, 2, 3, 0, 7, 4}, /* BRA_FDA */
{ 1, 6, 5, 2, 3, 4, 7, 0}, /* BRA_FRC */
{ 6, 1, 2, 5, 4, 7, 0, 3}, /* BLC_FDC */
{ 6, 1, 2, 5, 4, 3, 0, 7}, /* BLC_FLA */
{ 5, 2, 1, 6, 7, 4, 3, 0}, /* BLA_FUA */
{ 5, 2, 1, 6, 7, 0, 3, 4}, /* BLA_FLC */
{ 1, 2, 5, 6, 7, 4, 3, 0}, /* BUC_FUA */
{ 1, 2, 5, 6, 7, 0, 3, 4}, /* BUC_FLC */
{ 6, 5, 2, 1, 0, 3, 4, 7}, /* BUA_FUC */
{ 6, 5, 2, 1, 0, 7, 4, 3}, /* BUA_FRA */
{ 5, 6, 1, 2, 3, 0, 7, 4}, /* BDC_FDA */
{ 5, 6, 1, 2, 3, 4, 7, 0}, /* BDC_FRC */
{ 2, 1, 6, 5, 4, 7, 0, 3}, /* BDA_FDC */
{ 2, 1, 6, 5, 4, 3, 0, 7}, /* BDA_FLA */
{ 3, 4, 7, 0, 1, 2, 5, 6}, /* FRC_BUC */
{ 3, 4, 7, 0, 1, 6, 5, 2}, /* FRC_BRA */
{ 0, 7, 4, 3, 2, 1, 6, 5}, /* FRA_BDA */
{ 0, 7, 4, 3, 2, 5, 6, 1}, /* FRA_BRC */
{ 7, 0, 3, 4, 5, 6, 1, 2}, /* FLC_BDC */
{ 7, 0, 3, 4, 5, 2, 1, 6}, /* FLC_BLA */
{ 4, 3, 0, 7, 6, 5, 2, 1}, /* FLA_BUA */
{ 4, 3, 0, 7, 6, 1, 2, 5}, /* FLA_BLC */
{ 0, 3, 4, 7, 6, 5, 2, 1}, /* FUC_BUA */
{ 0, 3, 4, 7, 6, 1, 2, 5}, /* FUC_BLC */
{ 7, 4, 3, 0, 1, 2, 5, 6}, /* FUA_BUC */
{ 7, 4, 3, 0, 1, 6, 5, 2}, /* FUA_BRA */
{ 4, 7, 0, 3, 2, 1, 6, 5}, /* FDC_BDA */
{ 4, 7, 0, 3, 2, 5, 6, 1}, /* FDC_BRC */
{ 3, 0, 7, 4, 5, 6, 1, 2}, /* FDA_BDC */
{ 3, 0, 7, 4, 5, 2, 1, 6}, /* FDA_BLA */
};
static long Direction_Sequence[NUM_DIRECTIONS][NSUB] =
{
{ FRC_BUC, BRA_FRC, FDA_BDC, BLA_FUA, BUC_FLC, FUA_BUC, BRA_FRC, FDA_BLA },
/* BRC_FUC */
{ FRC_BUC, BRA_FRC, FDA_BDC, BLA_FUA, BRA_FDA, FRC_BRA, BUC_FUA, FLC_BDC },
/* BRC_FRA */
{ FRA_BDA, BRC_FRA, FUC_BUA, BLC_FDC, BDA_FLA, FDC_BDA, BRC_FRA, FUC_BLC },
/* BRA_FDA */
{ FRA_BDA, BRC_FRA, FUC_BUA, BLC_FDC, BUC_FLC, FUA_BUC, BRA_FRC, FDA_BLA },
/* BRA_FRC */
{ FLC_BDC, BLA_FLC, FUA_BUC, BRA_FDA, BDC_FRC, FDA_BDC, BLA_FLC, FUA_BRA },
/* BLC_FDC */
{ FLC_BDC, BLA_FLC, FUA_BUC, BRA_FDA, BLA_FUA, FLC_BLA, BDC_FDA, FRC_BUC },
/* BLC_FLA */
{ FLA_BUA, BLC_FLA, FDC_BDA, BRC_FUC, BUA_FRA, FUC_BUA, BLC_FLA, FDC_BRC },
/* BLA_FUA */
{ FLA_BUA, BLC_FLA, FDC_BDA, BRC_FUC, BLC_FDC, FLA_BLC, BUA_FUC, FRA_BDA },
/* BLA_FLC */
{ FUC_BLC, BUA_FUC, FRA_BRC, BDA_FLA, BUA_FRA, FUC_BUA, BLC_FLA, FDC_BRC },
/* BUC_FUA */
{ FUC_BLC, BUA_FUC, FRA_BRC, BDA_FLA, BLC_FDC, FLA_BLC, BUA_FUC, FRA_BDA },
/* BUC_FLC */
{ FUA_BRA, BUC_FUA, FLC_BLA, BDC_FRC, BUC_FLC, FUA_BUC, BRA_FRC, FDA_BLA },
/* BUA_FUC */
{ FUA_BRA, BUC_FUA, FLC_BLA, BDC_FRC, BRA_FDA, FRC_BRA, BUC_FUA, FLC_BDC },
/* BUA_FRA */
{ FDC_BRC, BDA_FDC, FLA_BLC, BUA_FRA, BDA_FLA, FDC_BDA, BRC_FRA, FUC_BLC },
/* BDC_FDA */
{ FDC_BRC, BDA_FDC, FLA_BLC, BUA_FRA, BUC_FLC, FUA_BUC, BRA_FRC, FDA_BLA },
/* BDC_FRC */
{ FDA_BLA, BDC_FDA, FRC_BRA, BUC_FLC, BDC_FRC, FDA_BDC, BLA_FLC, FUA_BRA },
/* BDA_FDC */
{ FDA_BLA, BDC_FDA, FRC_BRA, BUC_FLC, BLA_FUA, FLC_BLA, BDC_FDA, FRC_BUC },
/* BDA_FLA */
{ BUC_FLC, FUA_BUC, BRA_FRC, FDA_BLA, FUC_BLC, BUA_FUC, FRA_BRC, BDA_FLA },
/* FRC_BUC */
{ BUC_FLC, FUA_BUC, BRA_FRC, FDA_BLA, FRA_BDA, BRC_FRA, FUC_BUA, BLC_FDC },
/* FRC_BRA */
{ BRA_FDA, FRC_BRA, BUC_FUA, FLC_BDC, FDA_BLA, BDC_FDA, FRC_BRA, BUC_FLC },
/* FRA_BDA */
{ BRA_FDA, FRC_BRA, BUC_FUA, FLC_BDC, FRC_BUC, BRA_FRC, FDA_BDC, BLA_FUA },
/* FRA_BRC */
{ BLC_FDC, FLA_BLC, BUA_FUC, FRA_BDA, FDC_BRC, BDA_FDC, FLA_BLC, BUA_FRA },
/* FLC_BDC */
{ BLC_FDC, FLA_BLC, BUA_FUC, FRA_BDA, FLA_BUA, BLC_FLA, FDC_BDA, BRC_FUC },
/* FLC_BLA */
{ BLA_FUA, FLC_BLA, BDC_FDA, FRC_BUC, FUA_BRA, BUC_FUA, FLC_BLA, BDC_FRC },
/* FLA_BUA */
{ BLA_FUA, FLC_BLA, BDC_FDA, FRC_BUC, FLC_BDC, BLA_FLC, FUA_BUC, BRA_FDA },
/* FLA_BLC */
{ BUC_FLC, FUA_BUC, BRA_FRC, FDA_BLA, FUA_BRA, BUC_FUA, FLC_BLA, BDC_FRC },
/* FUC_BUA */
{ BUC_FLC, FUA_BUC, BRA_FRC, FDA_BLA, FLC_BDC, BLA_FLC, FUA_BUC, BRA_FDA },
/* FUC_BLC */
{ BUA_FRA, FUC_BUA, BLC_FLA, FDC_BRC, FUC_BLC, BUA_FUC, FRA_BRC, BDA_FLA },
/* FUA_BUC */
{ BUA_FRA, FUC_BUA, BLC_FLA, FDC_BRC, FRA_BDA, BRC_FRA, FUC_BUA, BLC_FDC },
/* FUA_BRA */
{ BDC_FRC, FDA_BDC, BLA_FLC, FUA_BRA, FDA_BLA, BDC_FDA, FRC_BRA, BUC_FLC },
/* FDC_BDA */
{ BDC_FRC, FDA_BDC, BLA_FLC, FUA_BRA, FRC_BUC, BRA_FRC, FDA_BDC, BLA_FUA },
/* FDC_BRC */
{ BDA_FLA, FDC_BDA, BRC_FRA, FUC_BLC, FDC_BRC, BDA_FDC, FLA_BLC, BUA_FRA },
/* FDA_BDC */
{ BDA_FLA, FDC_BDA, BRC_FRA, FUC_BLC, FLA_BUA, BLC_FLA, FDC_BDA, BRC_FUC },
/* FDA_BLA */
};
#if 0
int findCha(const double* val)
{
// this part is changed wrt fluidanimate.
return findCHAByHashing(reinterpret_cast<uintptr_t>(val)); // AYDIN: this is not &val, right?
}
#endif
int getMostAccessedCHA(int tid1,
int tid2,
std::multiset<std::tuple<int, int, int, int>, std::greater<std::tuple<int, int, int, int>>> ranked_cha_access_count_per_pair,
Topology topo)
{
int max = 0;
std::vector<int> considered_chas;
std::map<int, bool> considered_chas_flag;
auto it = ranked_cha_access_count_per_pair.begin();
while (it != ranked_cha_access_count_per_pair.end())
{
// std::pair<int, int> tid_pair(std::get<2>(*it), std::get<3>(*it));
if ((std::get<2>(*it) == tid1 && std::get<3>(*it) == tid2) || (std::get<3>(*it) == tid1 && std::get<2>(*it) == tid2))
{
// SPDLOG_INFO("returning {}", std::get<1>(*it));
max = std::get<0>(*it);
considered_chas_flag[std::get<1>(*it)] = true;
considered_chas.push_back(std::get<1>(*it));
break;
}
it++;
}
//if(max == 0)
// return -1;
//SPDLOG_INFO("max: {}, communication between threads {} and {} uses the following chas the most", max, tid1, tid2);
while (it != ranked_cha_access_count_per_pair.end())
{
// std::pair<int, int> tid_pair(std::get<2>(*it), std::get<3>(*it));
if ((std::get<2>(*it) == tid1 && std::get<3>(*it) == tid2) || (std::get<3>(*it) == tid1 && std::get<2>(*it) == tid2))
{
// SPDLOG_INFO("returning {}", std::get<1>(*it));
if (considered_chas_flag[std::get<1>(*it)] == false && std::get<0>(*it) > (0.9 * max))
{
//SPDLOG_INFO("cha {}, access count: {}, max: {}", std::get<1>(*it), std::get<0>(*it), max);
considered_chas_flag[std::get<1>(*it)] = true;
considered_chas.push_back(std::get<1>(*it));
}
}
it++;
}
int x_total = 0;
int y_total = 0;
int cha_count = 0;
//SPDLOG_INFO("communication between threads {} and {} involves the following CHAs:", tid1, tid2);
for (auto it1 : considered_chas)
{
auto tile = topo.getTile(it1);
x_total += tile.x;
y_total += tile.y;
cha_count++;
//SPDLOG_INFO("cha {}, x: {}, y: {}", tile.cha, tile.x, tile.y);
}
int x_coord = x_total / cha_count;
int y_coord = y_total / cha_count;
auto tile = topo.getTile(x_coord, y_coord);
//SPDLOG_INFO("the center of gravity is cha {}, x: {}, y: {}", tile.cha, tile.x, tile.y);
// approximate the algorithm now
return tile.cha;
}
#if 0
int getMostAccessedCHA(int tid1,
int tid2,
std::multiset<std::tuple<int, int, int, int>, std::greater<>> ranked_cha_access_count_per_pair,
Topology topo)
{
int max = 0;
std::vector<int> considered_chas;
std::map<int, bool> considered_chas_flag;
auto it = ranked_cha_access_count_per_pair.begin();
while (it != ranked_cha_access_count_per_pair.end())
{
// std::pair<int, int> tid_pair(std::get<2>(*it), std::get<3>(*it));
if ((std::get<2>(*it) == tid1 && std::get<3>(*it) == tid2) || (std::get<3>(*it) == tid1 && std::get<2>(*it) == tid2))
{
// SPDLOG_INFO("returning {}", std::get<1>(*it));
max = std::get<0>(*it);
considered_chas_flag[std::get<1>(*it)] = true;
considered_chas.push_back(std::get<1>(*it));
break;
}
it++;
}
// SPDLOG_INFO("communication between threads {} and {} uses the following chas the most", std::get<1>(*it), std::get<0>(*it), max);
while (it != ranked_cha_access_count_per_pair.end())
{
// std::pair<int, int> tid_pair(std::get<2>(*it), std::get<3>(*it));
if ((std::get<2>(*it) == tid1 && std::get<3>(*it) == tid2) || (std::get<3>(*it) == tid1 && std::get<2>(*it) == tid2)) {
// SPDLOG_INFO("returning {}", std::get<1>(*it));
if (considered_chas_flag[std::get<1>(*it)] == false && std::get<0>(*it) > (0.9 * max))
{
// SPDLOG_INFO("cha {}, access count: {}, max: {}", std::get<1>(*it), std::get<0>(*it), max);
considered_chas_flag[std::get<1>(*it)] = true;
considered_chas.push_back(std::get<1>(*it));
}
}
it++;
}
int x_total = 0;
int y_total = 0;
int cha_count = 0;
// SPDLOG_INFO("communication between threads {} and {} involves the following CHAs:");
for (auto it1 : considered_chas)
{
auto tile = topo.getTile(it1);
x_total += tile.x;
y_total += tile.y;
cha_count++;
// SPDLOG_INFO("cha {}, x: {}, y: {}", tile.cha, tile.x, tile.y);
}
assert(cha_count != 0);
int x_coord = x_total / cha_count;
int y_coord = y_total / cha_count;
auto tile = topo.getTile(x_coord, y_coord);
// SPDLOG_INFO("the center of gravity is cha {}, x: {}, y: {}", tile.cha, tile.x, tile.y);
// approximate the algorithm now
return tile.cha;
}
#endif
int main (int argc, string argv[])
{
long c;
while ((c = getopt(argc, argv, "h")) != -1) {
switch(c) {
case 'h':
Help();
exit(-1);
break;
default:
fprintf(stderr, "Only valid option is \"-h\".\n");
exit(-1);
break;
}
}
Global = NULL;
//HashTable *comm_map;//(HASHTABLE_SIZE);
//HashTable comm_map(HASHTABLE_SIZE);
//std::vector<std::vector<std::pair<int, std::unordered_map<int, int>>>> comm_matrix(NCORES, std::vector<std::pair<int, std::unordered_map<int, int>>> (NCORES, std::pair<int, std::unordered_map<int, int>> {}));
initialize_shared_memory();
pthread_spin_init(&map_spinlock, PTHREAD_PROCESS_SHARED);
comm_map = new HashTable (HASHTABLE_SIZE);
//comm_map->print();
//HashTable comm_map_new(HASHTABLE_SIZE);
//std::vector<std::vector<std::pair<int, std::unordered_map<int, int>>>> comm_matrix(NCORES, std::vector<std::pair<int, std::unordered_map<int, int>>> (NCORES, std::pair<int, std::unordered_map<int, int>> {}));
initparam(defv); // modify initparam to read input from stdin only once
startrun(); // create another version of this function that reuses loaded data
initoutput(); // no need for modification, can be repeated
tab_init(); // no need to be recalled in the next iteration of barnes computation
#if 0
int *array1 = reinterpret_cast<int*>(cha_aware_malloc(8 * sizeof(int)));
for(int i = 0; i < 8; i++)
assert(cha_of_element((void *) &array1[i]) == findCHAByHashing(uintptr_t(&array1[i]), base_sequence_28_skx));
std::cout << "cha assert for cha_aware_malloc success 1." << std::endl;
int *array2 = reinterpret_cast<int*>(cha_aware_memalign(64, 8 * sizeof(int)));
for(int i = 0; i < 8; i++)
assert(cha_of_element((void *) &array2[i]) == findCHAByHashing(uintptr_t(&array2[i]), base_sequence_28_skx));
std::cout << "cha assert for cha_aware_memalign success 2." << std::endl;
#endif
// the following 5 initializations need to be repeated before each iteration of barnes computation
Global->tracktime = 0;
Global->partitiontime = 0;
Global->treebuildtime = 0;
Global->forcecalctime = 0;
Global->current_id = 0;
using namespace std;
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
//using std::chrono::milliseconds;
using std::chrono::nanoseconds;
#if 0
uint64_t total_traffic_diff = 0;
const int traffic_i = (const int) TrafficType::Invalidate;
const auto traffic_type = static_cast<TrafficType>(traffic_i);
#endif
CLOCK(Global->computestart);
printf("COMPUTESTART = %12lu\n",Global->computestart);
std::cout << "base cores: ";
std::vector<int> base_assigned_cores;
for (int i = 0; i < getCoreCount(); ++i)
{
if (i % 2 == 0)
{
base_assigned_cores.push_back(i);
std::cout << i << ' ';
// this is to bind cores in socket-0. all cores are even numbered in this socket.
}
}
std::cout << std::endl;
assert(base_assigned_cores.size() == 28);
//#if 0
std::cerr << "before SlaveStart\n";
//const auto &before_traffic_vals = storeTraffic(traffic_type);
CREATE(SlaveStart<true>, static_cast<void*>(base_assigned_cores.data()), NPROC);
//#endif
WAIT_FOR_END(NPROC);
//const auto &after_traffic_vals = storeTraffic(traffic_type);
CLOCK(Global->computeend);
printf("COMPUTEEND = %12lu\n",Global->computeend);
printf("COMPUTETIME = %12lu\n",Global->computeend - Global->computestart);
printf("TRACKTIME = %12lu\n",Global->tracktime);
printf("PARTITIONTIME = %12lu\t%5.2f\n",Global->partitiontime,
((float)Global->partitiontime)/Global->tracktime);
printf("TREEBUILDTIME = %12lu\t%5.2f\n",Global->treebuildtime,
((float)Global->treebuildtime)/Global->tracktime);
printf("FORCECALCTIME = %12lu\t%5.2f\n",Global->forcecalctime,
((float)Global->forcecalctime)/Global->tracktime);
printf("RESTTIME = %12lu\t%5.2f\n",
Global->tracktime - Global->partitiontime -
Global->treebuildtime - Global->forcecalctime,
((float)(Global->tracktime-Global->partitiontime-
Global->treebuildtime-Global->forcecalctime))/
Global->tracktime);
// preprocessing begins here
//#if 0
std::cout << "Starting preprocesing algo..." << std::endl;
const auto algo_start = high_resolution_clock::now();
assert(NPROC > 1); // below algo depends on this. we will find thread pairs.
// end
std::multiset<std::tuple<int, int, int, int>, greater<std::tuple<int, int, int, int>>> ranked_cha_access_count_per_pair;
std::multiset<std::tuple<int, int, int>, greater<std::tuple<int, int, int>>> ranked_communication_count_per_pair;
for (int i = 0; i < comm_matrix.size(); ++i)
{
for (int j = i + 1; j < comm_matrix[i].size(); ++j)
{
for (int k = 0; k < base_assigned_cores.size(); k++)
{
// ranked_communication_count_per_pair.insert({it.second, it.first, i, j})
if(comm_matrix[i][j].second[k] > 0 || comm_matrix[j][i].second[k] > 0)
ranked_cha_access_count_per_pair.insert({comm_matrix[i][j].second[k] + comm_matrix[j][i].second[k], k, i, j});
}
if(comm_matrix[i][j].first > 0 || comm_matrix[j][i].first > 0)
ranked_communication_count_per_pair.insert({comm_matrix[i][j].first + comm_matrix[j][i].first, i, j});
}
}
int mapped_thread_count = 0;
auto it = ranked_cha_access_count_per_pair.begin();
auto it1 = ranked_communication_count_per_pair.begin();
//#endif
std::vector<int> thread_to_core(NPROC, -1);
//#if 0
// fprintf(stderr, "before topology creation\n");
auto topo = Topology(cha_core_map, CAPID6);
std::vector<Tile> mapped_tiles;
while (mapped_tiles.size() < NPROC && /*it != ranked_cha_access_count_per_pair.end()*/ it1 != ranked_communication_count_per_pair.end())
{
// std::pair<int, int> tid_pair(std::get<2>(*it), std::get<3>(*it));
std::pair<int, int> tid_pair(std::get<1>(*it1), std::get<2>(*it1));
if (thread_to_core[tid_pair.first] == -1 || thread_to_core[tid_pair.second] == -1)
{
//SPDLOG_INFO("thread {} and thread {} has {} communication count", tid_pair.first, tid_pair.second, std::get<0>(*it1));
int cha_id = getMostAccessedCHA(tid_pair.first, tid_pair.second, ranked_cha_access_count_per_pair, topo);
if (cha_id == -1)
{
//SPDLOG_INFO("error: cha is -1");
it1++;
continue;
}
// auto tile = topo.getTile(std::get<1>(*it));
auto tile = topo.getTile(cha_id);
//SPDLOG_TRACE("cha {}, is colocated with core {}", cha_id, tile.core);
if (thread_to_core[tid_pair.first] == -1)
{
// SPDLOG_INFO("fetching a tile closest to tile with cha {} and core {}, cha supposed to be {}", tile.cha, tile.core, std::get<1>(*it));
auto closest_tile = topo.getClosestTile(tile, mapped_tiles);
// auto closest_tile = topo.getClosestTilewithThreshold(tile, mapped_tiles);
//SPDLOG_TRACE("* closest _available_ core to cha {} is: {}", tile.cha, closest_tile.core);
mapped_tiles.push_back(closest_tile);
thread_to_core[tid_pair.first] = closest_tile.core;
//SPDLOG_TRACE("assigned thread with id {} to core {}", tid_pair.first, closest_tile.core);
}
if (thread_to_core[tid_pair.second] == -1)
{
// SPDLOG_INFO("fetching a tile closest to tile with cha {} and core {}, cha supposed to be {}", tile.cha, tile.core, std::get<1>(*it));
auto closest_tile = topo.getClosestTile(tile, mapped_tiles);
// auto closest_tile = topo.getClosestTilewithThreshold(tile, mapped_tiles);
//SPDLOG_TRACE("# closest _available_ core to cha {} is: {}", tile.cha, closest_tile.core);
mapped_tiles.push_back(closest_tile);
thread_to_core[tid_pair.second] = closest_tile.core;
//SPDLOG_TRACE("assigned thread with id {} to core {}", tid_pair.second, closest_tile.core);
}
}
it1++;
}
//#endif
// end
//#endif
for (auto ptr : base_assigned_cores)
{
//thread_to_core[i] = i;
std::vector<int>::iterator it =
std::find(thread_to_core.begin(), thread_to_core.end(), ptr);
if (it == thread_to_core.end())
{
std::vector<int>::iterator it1 =
std::find(thread_to_core.begin(), thread_to_core.end(), -1);
*it1 = ptr;
}
}
const auto algo_end = high_resolution_clock::now();
//std::cout << "Ended preprocesing algo. elapsed time: " << duration_cast<milliseconds>(algo_end - algo_start).count() << "ms" << std::endl;