-
Notifications
You must be signed in to change notification settings - Fork 0
/
traccc_fcts.h
2467 lines (1924 loc) · 103 KB
/
traccc_fcts.h
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
#pragma once
#include <iostream>
#include <filesystem>
#include <fstream>
#include <chrono>
// file
#include <sys/stat.h>
#include <unistd.h>
#include <string>
// SyCL specific includes
#include <CL/sycl.hpp>
#include <array>
#include <sys/time.h>
#include <stdlib.h>
#include "utils.h"
#include "constants.h"
// Regroupe des fonctions & structures utiles
namespace traccc {
//const unsigned int microseconds = 4000 * 1000;
const unsigned int microseconds = 0;
const uint TRACCC_LOG_LEVEL = 0; // Seulement afficher les infos du log 0
//bool ignore_allocation_times;// = false;
bool ignore_pointer_graph_benchmark;
bool ignore_flatten_benchmark;
// True pour utiliser la structure implicit_module
// False pour utiliser les structures implicit_input_module et implicit_output_module.
// (et les cellules qui vont avec)
bool implicit_use_unique_module;
using tdtype = unsigned int;
/*
Toutes les données stockées sont de type tdtype.
Pour simplifier les traîtements.
(nb modules, nb cellules, positions)
*/
// A simple cell (input)
struct input_cell {
unsigned int channel0 = 0;
unsigned int channel1 = 0;
//float activation = 0.;
//float time = 0.;
// label
};
// A simple output cell (contains a label)
struct output_cell {
unsigned int label = 0;
};
// TODO : un seul tableau en implicite avec les cellules en entrée et en sortie
// Voir si ça foire pas la cohérence du bordel, mais comme on est en full implicite
// autant vrailent jouer le jeu du graphe de pointeurs.
// Test de la lecture + écriture dans la même structure
struct implicit_cell {
unsigned int channel0 = 0;
unsigned int channel1 = 0;
unsigned int label = 0;
//float activation = 0.;
//float time = 0.;
// label
};
struct implicit_module {
unsigned int cell_count;
unsigned int cluster_count;
implicit_cell* cells;
};
// Contains all the cells for the module (input)
struct implicit_input_module {
unsigned int cell_count;
input_cell* cells;
};
// Contains all the modules (input)
/* inutile, j'utilise un tableau simple
struct implicit_input_module_container {
unsigned int module_count;
implicit_input_module* modules;
};*/
// A simple output module (contains all the output cells)
struct implicit_output_module {
//unsigned int cell_count;
unsigned int cluster_count;
output_cell* cells;
};
// Module container for output data
// inutile, j'utilise un tableau simple
/*struct implicit_output_module_container {
unsigned int module_count;
implicit_output_module* modules;
};*/
// Shared and host will use the output_[...].
// Device ne peut pas utiliser de graphe de pointeur.
// Donc il faut tout aplatir.
struct flat_input_module {
unsigned int cell_count; // nombre de cellules du module
unsigned int cell_start_index; // start index dans le grand tableau des cellules
};
struct flat_output_module {
// unsigned int cell_count; connu
unsigned int cluster_count;
};
// Tout aplati
struct flat_input_data {
input_cell* cells;
flat_input_module* modules;
// En device, il y a les tableaux malloc_host et une allocation explicite device
input_cell* cells_device;
flat_input_module* modules_device;
// Accesseurs
// Buffers on the device for accessors-buffers
// Those are pointers to be created during the allocation phase
cl::sycl::buffer<input_cell, 1> *buffer_cells = nullptr; // wraps cells (cells_device unused)
cl::sycl::buffer<flat_input_module, 1> *buffer_modules = nullptr; // wraps modules (modules_device unused)
};
struct flat_output_data {
output_cell* cells;
flat_output_module* modules;
// Device uniquement :
output_cell* cells_device;
flat_output_module* modules_device;
// Accesseurs
// Buffers on the device for accessors-buffers
// Those are pointers to be created during the allocation phase
cl::sycl::buffer<output_cell, 1> *buffer_cells = nullptr; // wraps cells
cl::sycl::buffer<flat_output_module, 1> *buffer_modules = nullptr; // wraps modules
};
/// Implemementation of SparseCCL, following [DOI: 10.1109/DASIP48288.2019.9049184]
///
/// Requires cells to be sorted in column major.
/// Find root of the tree for entry @param e
///
/// @param L an equivalance table
///
/// @return the root of @param e
unsigned int find_root(const unsigned int* L, unsigned int e) {
unsigned int r = e;
while (L[r] != r) {
r = L[r];
}
return r;
}
unsigned int find_root(const output_cell * L, unsigned int e) {
unsigned int r = e;
while (L[r].label != r) {
r = L[r].label;
}
return r;
}
/// Create a union of two entries @param e1 and @param e2
///
/// @param L an equivalance table
///
/// @return the rleast common ancestor of the entries
unsigned int make_union(unsigned int* L, unsigned int e1, unsigned int e2) {
int e;
if (e1 < e2){
e = e1;
L[e2] = e;
} else {
e = e2;
L[e1] = e;
}
return e;
}
/*unsigned int make_union(output_cell * L, unsigned int e1, unsigned int e2){
int e;
if (e1 < e2){
e = e1;
L[e2].label = e;
} else {
e = e2;
L[e1].label = e;
}
return e;
}
unsigned int make_union(implicit_cell * L, unsigned int e1, unsigned int e2){
int e;
if (e1 < e2){
e = e1;
L[e2].label = e;
} else {
e = e2;
L[e1].label = e;
}
return e;
}*/
/// Helper method to find adjacent cells
///
/// @param a the first cell
/// @param b the second cell
///
/// @return boolan to indicate 8-cell connectivity
bool is_adjacent(input_cell a, input_cell b) {
return (a.channel0 - b.channel0)*(a.channel0 - b.channel0) <= 1
and (a.channel1 - b.channel1)*(a.channel1 - b.channel1) <= 1;
}
bool is_adjacent(implicit_cell a, implicit_cell b) {
return (a.channel0 - b.channel0)*(a.channel0 - b.channel0) <= 1
and (a.channel1 - b.channel1)*(a.channel1 - b.channel1) <= 1;
}
/// Helper method to find define distance,
/// does not need abs, as channels are sorted in
/// column major
///
/// @param a the first cell
/// @param b the second cell
///
/// @return boolan to indicate !8-cell connectivity
bool is_far_enough(input_cell a, input_cell b){
return (a.channel1 - b.channel1) > 1;
}
bool is_far_enough(implicit_cell a, implicit_cell b){
return (a.channel1 - b.channel1) > 1;
}
unsigned int total_module_count;
unsigned int total_cell_count;
unsigned int total_int_written;
bool data_already_loaded_from_disk = false;
unsigned int in_total_size;
unsigned int out_total_size;
// traccc_repeat_load_count dans constants.h
// Valeurs que doivent avoir cluster_count et label_count
// lorsque tout est exécuté (i.e. prendre en compte toutes les sparcités)
unsigned int expected_cluster_count = 380554;
unsigned int expected_label_sum = 23681637;
// pour multiplier en cas de faible sparsité
const unsigned int expected_in_module_count = 38784;
const unsigned int expected_in_cell_count = 2041344;
unsigned int* all_data = nullptr; // allocated with read_cells_lite
unsigned int i_all_data;
unsigned int read_source() {
return all_data[i_all_data++];
}
void inc_source_counter() {
++i_all_data;
}
void reset_source_counter() {
i_all_data = 0;
}
int traccc_last_SPARSITY_MIN = -1;
int traccc_last_SPARSITY_MAX = -1;
void read_cells_lite(std::string fpath) {
i_all_data = 0;
if ( (traccc_last_SPARSITY_MIN == -1) || (traccc_last_SPARSITY_MAX == -1) ) {
traccc_last_SPARSITY_MIN = traccc_SPARSITY_MIN;
traccc_last_SPARSITY_MAX = traccc_SPARSITY_MAX;
data_already_loaded_from_disk = false;
}
if ( (traccc_last_SPARSITY_MIN != traccc_SPARSITY_MIN)
|| (traccc_last_SPARSITY_MAX != traccc_SPARSITY_MAX) ) {
traccc_last_SPARSITY_MIN = traccc_SPARSITY_MIN;
traccc_last_SPARSITY_MAX = traccc_SPARSITY_MAX;
data_already_loaded_from_disk = false;
}
if (data_already_loaded_from_disk) return;
// Suppression des anciennes données
if (all_data != nullptr) {
delete[] all_data;
all_data = nullptr;
}
log("\n\n=========== LOAD DATA ===========");
if (TRACCC_LOG_LEVEL >= 0) {
log("Read from " + fpath + "...");
log(" - traccc_SPARSITY_MIN(" + std::to_string(traccc_SPARSITY_MIN) + ")");
log(" - traccc_SPARSITY_MAX(" + std::to_string(traccc_SPARSITY_MAX) + ")");
}
total_module_count = 0;
total_cell_count = 0;
total_int_written = 0;
data_already_loaded_from_disk = true;
//traccc::host_cell_container cells_per_event;
long fsize = GetFileSize(fpath);
//log("read_cells 0");
std::ifstream rf(fpath, std::ios::out | std::ios::binary);
if(!rf) {
return;
}
rf.read((char *)(&total_module_count), sizeof(unsigned int));
rf.read((char *)(&total_cell_count), sizeof(unsigned int));
rf.read((char *)(&total_int_written), sizeof(unsigned int));
log("total_module_count = " + std::to_string(total_module_count));
log("total_cell_count = " + std::to_string(total_cell_count));
log("total_int_written = " + std::to_string(total_int_written));
unsigned int nb_ints_chk = (fsize / sizeof(unsigned int)) - 3;
if (nb_ints_chk != total_int_written) {
log("ERROR ? nb_ints_chk(" +std::to_string(nb_ints_chk)
+ ") != total_int_written(" + std::to_string(total_int_written) + ")");
}
// fdata = flat data
unsigned int* read_data = new unsigned int[total_int_written];
// read the whole remaining file at once
rf.read((char *)(read_data), total_int_written * sizeof(unsigned int));
//log("read_cells closing...");
rf.close();
all_data = nullptr;
//log("read_cells closed !");
if(!rf.good()) {
delete[] read_data;
in_total_size = 0;
out_total_size = 0;
log("ERROR : unable to open & read file.");
log("\n\n");
return;
}
// Première lecture : calcul du nombre de modules et de cellules
// satisfaisant les valeurs de la sparsité
// Pour avoir la compatibilité avec read_source()
all_data = read_data;
uint nb_ok_modules = 0;
uint nb_ok_cells = 0;
// La taille du tableau sera de :
// nb_ok_cells * 2 (2 channels) + nb_ok_modules (taille du tableau)
// pas d'incrément de ind
for (; i_all_data < total_int_written; ) {
uint cell_count = read_source();
i_all_data += cell_count * 2; // chan0 + chan1
// mauvaise valeur de sparsité
if ( (cell_count > traccc_SPARSITY_MAX)
|| (cell_count < traccc_SPARSITY_MIN) ) {
continue;
}
// Bonne valeur, j'ajoute le compte
nb_ok_modules += 1;
nb_ok_cells += cell_count;
}
i_all_data = 0;
// Allocation
uint * right_sparse_data = new uint[nb_ok_cells * 2 + nb_ok_modules];
uint i_rsd = 0;
for (; i_all_data < total_int_written; ) {
uint cell_count = read_source();
// mauvaise valeur de sparsité
if ( (cell_count > traccc_SPARSITY_MAX)
|| (cell_count < traccc_SPARSITY_MIN) ) {
i_all_data += cell_count * 2; // chan0 + chan1
continue;
}
right_sparse_data[i_rsd++] = cell_count;
for (uint ic = 0; ic < cell_count; ++ic) {
right_sparse_data[i_rsd++] = read_source(); // chan 0
right_sparse_data[i_rsd++] = read_source(); // chan 1
}
}
if ( i_rsd != (nb_ok_cells * 2 + nb_ok_modules) ) {
log(std::string("\n======= ERROR =======\n") +
"ERROR : i_rsd(" + std::to_string(i_rsd) + ") != "
+ " expected size(" + std::to_string(nb_ok_cells * 2 + nb_ok_modules) + ")"
+ "\n======= ERROR =======\n");
}
total_int_written = i_rsd; // = nb_ok_cells * 2 + nb_ok_modules
total_cell_count = nb_ok_cells;
total_module_count = nb_ok_modules;
log("without sparse multiply : total_module_count = " + std::to_string(total_module_count) );
log("without sparse multiply : total_cell_count = " + std::to_string(total_cell_count) );
traccc_repeat_load_count = base_traccc_repeat_load_count; // as defined in class selector_list_devices of utils.h
if (total_cell_count != 0) {
uint multiply_repeat_by = expected_in_cell_count / total_cell_count;
log("-- adjust to sparcity by multiplication = " + std::to_string(multiply_repeat_by));
traccc_repeat_load_count *= multiply_repeat_by;
}
log("alloc traccc_repeat_load_count = " + std::to_string(traccc_repeat_load_count));
all_data = new unsigned int[total_int_written * traccc_repeat_load_count];
for (uint ir = 0; ir < traccc_repeat_load_count; ++ir) {
//log("copy ir = " + std::to_string(ir) + "...");
memcpy(&all_data[ir * total_int_written], right_sparse_data, total_int_written * sizeof(unsigned int));
// et non plus depuis read_data
}
total_module_count = total_module_count * traccc_repeat_load_count;
total_cell_count = total_cell_count * traccc_repeat_load_count;
log("copy ok");
log("total_module_count = " + std::to_string(total_module_count) );
log("total_cell_count = " + std::to_string(total_cell_count) );
expected_cluster_count = expected_cluster_count * traccc_repeat_load_count;
expected_label_sum = expected_label_sum * traccc_repeat_load_count;
in_total_size = total_module_count * sizeof(implicit_input_module) + total_cell_count * sizeof(input_cell);
out_total_size = total_module_count * sizeof(implicit_output_module) + total_cell_count * sizeof(output_cell);
log("IN SIZE = " + std::to_string(in_total_size / 1024) + " KiB");
log("OUT SIZE = " + std::to_string(out_total_size / 1024) + " KiB");
log("\n\n");
delete[] right_sparse_data;
delete[] read_data;
i_all_data = 0;
return;
}
void read_cells_lite() {
std::string wdir_tmp = std::filesystem::current_path();
std::string bin_path = wdir_tmp + "/events_bin/lite_all_events.bin";
read_cells_lite(bin_path);
}
void read_cells_lite_no_sparcity(std::string fpath) {
i_all_data = 0;
if (data_already_loaded_from_disk) return;
if (TRACCC_LOG_LEVEL >= 0) log("Read from " + fpath + "...");
total_module_count = 0;
total_cell_count = 0;
total_int_written = 0;
data_already_loaded_from_disk = true;
//traccc::host_cell_container cells_per_event;
long fsize = GetFileSize(fpath);
//log("read_cells 0");
std::ifstream rf(fpath, std::ios::out | std::ios::binary);
if(!rf) {
return;
}
rf.read((char *)(&total_module_count), sizeof(unsigned int));
rf.read((char *)(&total_cell_count), sizeof(unsigned int));
rf.read((char *)(&total_int_written), sizeof(unsigned int));
log("total_module_count = " + std::to_string(total_module_count));
log("total_cell_count = " + std::to_string(total_cell_count));
log("total_int_written = " + std::to_string(total_int_written));
unsigned int nb_ints_chk = (fsize / sizeof(unsigned int)) - 3;
if (nb_ints_chk != total_int_written) {
log("ERROR ? nb_ints_chk(" +std::to_string(nb_ints_chk)
+ ") != total_int_written(" + std::to_string(total_int_written) + ")");
}
// fdata = flat data
unsigned int* read_data = new unsigned int[total_int_written];
// read the whole remaining file at once
rf.read((char *)(read_data), total_int_written * sizeof(unsigned int));
//log("read_cells closing...");
rf.close();
all_data = nullptr;
//log("read_cells closed !");
if(!rf.good()) {
delete[] read_data;
in_total_size = 0;
out_total_size = 0;
return;
//return nullptr;
}
log("alloc traccc_repeat_load_count = " + std::to_string(traccc_repeat_load_count));
all_data = new unsigned int[total_int_written * traccc_repeat_load_count];
for (uint ir = 0; ir < traccc_repeat_load_count; ++ir) {
log("copy ir = " + std::to_string(ir) + "...");
memcpy(&all_data[ir * total_int_written], read_data, total_int_written * sizeof(unsigned int));
}
log("copy ok");
total_module_count = total_module_count * traccc_repeat_load_count;
total_cell_count = total_cell_count * traccc_repeat_load_count;
expected_cluster_count = expected_cluster_count * traccc_repeat_load_count;
expected_label_sum = expected_label_sum * traccc_repeat_load_count;
in_total_size = total_module_count * sizeof(implicit_input_module) + total_cell_count * sizeof(input_cell);
out_total_size = total_module_count * sizeof(implicit_output_module) + total_cell_count * sizeof(output_cell);
log("IN SIZE = " + std::to_string(in_total_size / 1024) + " KiB");
log("OUT SIZE = " + std::to_string(out_total_size / 1024) + " KiB");
delete[] read_data;
return;
}
// A partir de ce tableau :
// - host/shared : remplir le tableau alloué via SYCL avec ces données
// avec les vraies structures en mode graphe de pointeur (comme décrit ci-dessous)
// - device :
// 1) Allocation mémoire sycl (mem_fill si shared host et mem_dev si device)
// Si host ou shared, plusieurs allocations (graphe de pointeurs)
// 1.5) Pour device : allocation mémoire host (mem_fill)
// 2) Remplissage mémoire SYCL (depuis données brutes)
// 2.5) Pour device : copie explicite vers la mémoire device
// 3) Parallel_for, exécution du kernel
// 4.0) Pour device : copie explicite vers la mémoire host
// 4) Lecture des données de sortie (somme des labels des cases pour faire une lecture)
// et somme du nombre de clusters
// Idées pour le débug :
// print les items et voir s'ils sont dans le même ordre ?
// i.e. vérifier que les cellules sont bien dans le même ordre,
// pour chaque module (comme les cellule doivent être classées en
// "column major, je crois")
// A passer en paramètre à chaque fonction
/*class bench_variables {
public:
// Tableau des modules en implicite
cl::sycl::queue sycl_q;
};*/
enum mem_strategy { pointer_graph, flatten };
std::string mem_strategy_to_str(mem_strategy m) {
switch (m) {
case pointer_graph : return "pointer_graph";
case flatten : return "flatten";
default : return "inconnu";
}
}
unsigned int mem_strategy_to_int(mem_strategy m) {
switch (m) {
case pointer_graph : return 1;
case flatten : return 2;
default : return 0;
}
}
struct traccc_chrono_results {
// alloc et fill sont utiles en flatten uniquement,
// ça n'a pas grand sens en graphe de ponteur
// (vu que la structure change)
//uint t_alloc_fill, t_flatten_alloc, t_flatten_fill, t_copy_kernel, t_read, t_free_mem, t_alloc_only, t_fill_only;
// Nouveau timer
int t_alloc_native, t_alloc_sycl, t_fill, t_copy, t_read, t_dealloc_sycl, t_dealloc_native;
static const uint kernel_count = 2;
int t_kernel[kernel_count];
};
class bench_variables {
public:
implicit_input_module* implicit_modules_in;
implicit_output_module* implicit_modules_out;
implicit_module* implicit_modules;
sycl_mode mode;
cl::sycl::queue sycl_q;
//mem_strategy mstrat = pointer_graph;
mem_strategy mstrat;// = flatten;
flat_input_data flat_input;
flat_output_data flat_output;
traccc_chrono_results chres;
// -1 signifie "n'a pas de sens dans ce contexte"
void reset_timer() {
chres.t_alloc_native = -1;
chres.t_alloc_sycl = -1;
chres.t_fill = -1;
chres.t_copy = -1;
chres.t_read = -1;
chres.t_dealloc_sycl = -1;
chres.t_dealloc_native = -1;
for (uint i = 0; i < chres.kernel_count; ++i) {
chres.t_kernel[i] = -1;
}
}
bench_variables() {
reset_timer();
}
//uint t_alloc_fill, t_copy_kernel, t_read, t_free_mem;
};
void alloc_and_fill(bench_variables & b) {
if (TRACCC_LOG_LEVEL >= 2) log("Alloc & fill...");
stime_utils chrono, chrono_flatten, chrono_ptr_detailed;
reset_source_counter();
//sycl_mode mode = bench.mode;
//traccc::implicit_input_module * implicit_modules_in = bench.implicit_modules_in;
//traccc::implicit_output_module * implicit_modules_out = bench.implicit_modules_out;
// malloc + fill
// (copie) + parallel_for
// (copie) + lecture
// Décomposer le malloc + fill du device en malloc_host initial + fill
// parce que le malloc_host initial n'est fait qu'une seule fois et est voué à resservir.
chrono.reset();
if (b.mstrat == pointer_graph) {
chrono_ptr_detailed.reset();
// b.chres.t_flatten_alloc = 0;
// b.chres.t_flatten_fill = 0;
// b.chres.t_alloc_only = 0; // nouveau
// b.chres.t_fill_only = 0; // nouveau
// Graphe de pointeurs
// Lecture + fill
if ( (b.mode == sycl_mode::host_USM) // aucun support pour device, ni pour accesseurs
|| (b.mode == sycl_mode::shared_USM)
|| (b.mode == sycl_mode::glibc) ) {
if (implicit_use_unique_module) {
// Utilisation d'un unique module pour les in/out
if (b.mode == sycl_mode::host_USM) {
b.implicit_modules = cl::sycl::malloc_host<implicit_module>(total_module_count, b.sycl_q);
b.sycl_q.wait_and_throw();
}
if (b.mode == sycl_mode::glibc) {
b.implicit_modules = new implicit_module[total_module_count];
}
if (b.mode == sycl_mode::shared_USM) {
b.implicit_modules = cl::sycl::malloc_shared<implicit_module>(total_module_count, b.sycl_q);;
b.sycl_q.wait_and_throw();
}
// Allocation des modules, les uns après les autres
for (uint im = 0; im < total_module_count; ++im) {
traccc::implicit_module * module = &b.implicit_modules[im];
// lecture du nombre de cellules du module
unsigned int cell_count = read_source();
module->cell_count = cell_count;
module->cluster_count = 0;
// allocation des cellules
if (b.mode == sycl_mode::host_USM) {
module->cells = cl::sycl::malloc_host<implicit_cell>(cell_count, b.sycl_q);
b.sycl_q.wait_and_throw();
}
if (b.mode == sycl_mode::glibc) {
module->cells = new implicit_cell[cell_count];
}
if (b.mode == sycl_mode::shared_USM) {
module->cells = cl::sycl::malloc_shared<implicit_cell>(cell_count, b.sycl_q);
b.sycl_q.wait_and_throw();
}
uint inc_amount = module->cell_count * 2;
i_all_data += inc_amount;
}
if (b.mode == sycl_mode::glibc) {
b.chres.t_alloc_native = chrono_ptr_detailed.reset();
} else {
b.chres.t_alloc_sycl = chrono_ptr_detailed.reset();
}
// b.chres.t_alloc_only = chrono_ptr_detailed.reset();
reset_source_counter(); // i_all_data = 0;
// Allocation des modules, les uns après les autres
for (uint im = 0; im < total_module_count; ++im) {
traccc::implicit_module * module = &b.implicit_modules[im];
unsigned int cell_count = read_source();
if (cell_count != module->cell_count) {
log("ERREUR @ alloc_and_fille : cell_count("+std::to_string(cell_count)+") != module->cell_count("+std::to_string(module->cell_count)+")");
}
// Remplissage des cellules
for (uint ic = 0; ic < module->cell_count; ++ic) {
implicit_cell * cell = &module->cells[ic];
unsigned int c0 = read_source();
unsigned int c1 = read_source();
cell->channel0 = c0;
cell->channel1 = c1;
//if (im < 10) logs( "(" + std::to_string(c0) + ", " + std::to_string(c1) + ") ");
}
//if (im < 10) log("");
}
b.chres.t_fill = chrono_ptr_detailed.reset();
//b.chres.t_fill_only = chrono_ptr_detailed.reset();
} else {
// Utilisation des modules in/out
if (b.mode == sycl_mode::host_USM) {
b.implicit_modules_in = static_cast<implicit_input_module *> (cl::sycl::malloc_host(total_module_count * sizeof(implicit_input_module), b.sycl_q));
b.implicit_modules_out = static_cast<implicit_output_module *> (cl::sycl::malloc_host(total_module_count * sizeof(implicit_output_module), b.sycl_q));
b.sycl_q.wait_and_throw();
//implicit_modules_in = static_cast<implicit_input_module *> (cl::sycl::malloc_host(total_module_count, sycl_q));
//implicit_modules_out = static_cast<implicit_output_module *> (cl::sycl::malloc_host(total_module_count, sycl_q));
}
if (b.mode == sycl_mode::glibc) {
b.implicit_modules_in = new implicit_input_module[total_module_count];
b.implicit_modules_out = new implicit_output_module[total_module_count];
}
if (b.mode == sycl_mode::shared_USM) {
b.implicit_modules_in = static_cast<implicit_input_module *> (cl::sycl::malloc_shared(total_module_count * sizeof(implicit_input_module), b.sycl_q));
b.implicit_modules_out = static_cast<implicit_output_module *> (cl::sycl::malloc_shared(total_module_count * sizeof(implicit_output_module), b.sycl_q));
b.sycl_q.wait_and_throw();
}
// Allocation des modules, les uns après les autres
for (uint im = 0; im < total_module_count; ++im) {
traccc::implicit_input_module * module_in = &b.implicit_modules_in[im];
traccc::implicit_output_module * module_out = &b.implicit_modules_out[im];
// lecture du nombre de cellules du module
unsigned int cell_count = read_source();
module_in->cell_count = cell_count;
// allocation des cellules
if (b.mode == sycl_mode::host_USM) {
module_in->cells = static_cast<input_cell *> (cl::sycl::malloc_host(cell_count * sizeof(input_cell), b.sycl_q));
module_out->cells = static_cast<output_cell *> (cl::sycl::malloc_host(cell_count * sizeof(output_cell), b.sycl_q));
b.sycl_q.wait_and_throw();
module_out->cluster_count = 0;
}
if (b.mode == sycl_mode::glibc) {
module_in->cells = new input_cell[cell_count];
module_out->cells = new output_cell[cell_count];
module_out->cluster_count = 0;
}
if (b.mode == sycl_mode::shared_USM) {
module_in->cells = static_cast<input_cell *> (cl::sycl::malloc_shared(cell_count * sizeof(input_cell), b.sycl_q));
module_out->cells = static_cast<output_cell *> (cl::sycl::malloc_shared(cell_count * sizeof(output_cell), b.sycl_q));
b.sycl_q.wait_and_throw();
module_out->cluster_count = 0;
}
uint inc_amount = module_in->cell_count * 2;
i_all_data += inc_amount;
}
if (b.mode == sycl_mode::glibc) {
b.chres.t_alloc_native = chrono_ptr_detailed.reset();
} else {
b.chres.t_alloc_sycl = chrono_ptr_detailed.reset();
}
//b.chres.t_alloc_only = chrono_ptr_detailed.reset();
reset_source_counter();
for (uint im = 0; im < total_module_count; ++im) {
traccc::implicit_input_module * module_in = &b.implicit_modules_in[im];
//traccc::implicit_output_module * module_out = &b.implicit_modules_out[im];
/*if (im < 10) {
log("Module " + std::to_string(im)
+ " cell_count(" + std::to_string(cell_count) + ")"
);
logs("cells : ");
}*/
unsigned int cell_count = read_source();
if (cell_count != module_in->cell_count) {
log("ERREUR @ alloc_and_fille : cell_count("+std::to_string(cell_count)+") != module_in->cell_count("+std::to_string(module_in->cell_count)+")");
}
// Remplissage des cellules
for (uint ic = 0; ic < module_in->cell_count; ++ic) {
unsigned int c0 = read_source();
unsigned int c1 = read_source();
module_in->cells[ic].channel0 = c0;
module_in->cells[ic].channel1 = c1;
//if (im < 10) logs( "(" + std::to_string(c0) + ", " + std::to_string(c1) + ") ");
}
//if (im < 10) log("");
}
// b.chres.t_fill_only = chrono_ptr_detailed.reset();
b.chres.t_fill = chrono_ptr_detailed.reset();
}
}
} else { // flatten
chrono_flatten.reset();
log("FLATTEN ===== SIZES :");
log("flat_input.cells = " + std::to_string(total_cell_count * sizeof(input_cell)));
log("flat_output.cells = " + std::to_string(total_cell_count * sizeof(output_cell)));
log("flat_input.modules = " + std::to_string(total_module_count * sizeof(flat_input_module)));
log("flat_output.modules = " + std::to_string(total_module_count * sizeof(flat_output_module)));
log("============================");
// Alloc - b.mode == sycl_mode::device_USM était avec malloc_host avant
// Changement : mémoire USM device allouée via glibc
if ( (b.mode == sycl_mode::glibc) || (b.mode == sycl_mode::device_USM) ) {
b.flat_input.cells = new input_cell[total_cell_count];
b.flat_output.cells = new output_cell[total_cell_count];
b.flat_input.modules = new flat_input_module[total_module_count];
b.flat_output.modules = new flat_output_module[total_module_count];
b.chres.t_alloc_native = chrono_flatten.reset();
}
// Host ou device, le device fera ensuite une allocation explicite
if ( b.mode == sycl_mode::host_USM ) {
b.flat_input.cells = static_cast<input_cell *> (cl::sycl::malloc_host(total_cell_count * sizeof(input_cell), b.sycl_q));
b.flat_output.cells = static_cast<output_cell *> (cl::sycl::malloc_host(total_cell_count * sizeof(output_cell), b.sycl_q));
b.flat_input.modules = static_cast<flat_input_module *> (cl::sycl::malloc_host(total_module_count * sizeof(flat_input_module), b.sycl_q));
b.flat_output.modules = static_cast<flat_output_module *> (cl::sycl::malloc_host(total_module_count * sizeof(flat_output_module), b.sycl_q));
b.chres.t_alloc_sycl = chrono_flatten.reset();
// if (b.mode == sycl_mode::device_USM) { // je fais comme si c'était une allocation native
// b.chres.t_alloc_native = chrono_flatten.reset();
// }
}
// Donc allocation host + allocation device
if ( b.mode == sycl_mode::device_USM ) {
b.flat_input.cells_device = cl::sycl::malloc_device<input_cell>(total_cell_count, b.sycl_q);
b.flat_output.cells_device = cl::sycl::malloc_device<output_cell>(total_cell_count, b.sycl_q);
// TODO : probablement qu'en fait c'est malloc_device ici et non malloc_host
b.flat_input.modules_device = cl::sycl::malloc_device<flat_input_module>(total_module_count, b.sycl_q);
b.flat_output.modules_device = cl::sycl::malloc_device<flat_output_module>(total_module_count, b.sycl_q);
b.chres.t_alloc_sycl = chrono_flatten.reset();
}
if (b.mode == sycl_mode::shared_USM) {
b.flat_input.cells = static_cast<input_cell *> (cl::sycl::malloc_shared(total_cell_count * sizeof(input_cell), b.sycl_q));
b.flat_output.cells = static_cast<output_cell *> (cl::sycl::malloc_shared(total_cell_count * sizeof(output_cell), b.sycl_q));
b.flat_input.modules = static_cast<flat_input_module *> (cl::sycl::malloc_shared(total_module_count * sizeof(flat_input_module), b.sycl_q));
b.flat_output.modules = static_cast<flat_output_module *> (cl::sycl::malloc_shared(total_module_count * sizeof(flat_output_module), b.sycl_q));
b.chres.t_alloc_sycl = chrono_flatten.reset();
}
// if (b.mode == sycl_mode::glibc) {
// b.chres.t_alloc_native = chrono_flatten.reset();
// } else {
// b.chres.t_alloc_sycl = chrono_flatten.reset();
// }
//if (ignore_allocation_times) chrono.reset();
// b.chres.t_flatten_alloc = chrono_flatten.reset();
// b.chres.t_alloc_only = b.chres.t_flatten_alloc;
if (b.mode == sycl_mode::accessors) {
// A l'arrache :
// - allocation des tableaux sur l'hôte d'input et output (4 tableaux donc)
// - remplissage des tableaux input avec les données utiles
// - exécution du kernel
// - récupération des valeurs
// - libération des tableaux de l'hôte
// Allocation des tableaux sur l'hôte d'input et output (4 tableaux donc)
b.flat_input.cells = new input_cell[total_cell_count];
b.flat_output.cells = new output_cell[total_cell_count];
b.flat_input.modules = new flat_input_module[total_module_count];
b.flat_output.modules = new flat_output_module[total_module_count];
b.chres.t_alloc_native = chrono_flatten.reset();
// Création des buffets par-dessus ces tableaux
b.flat_input.buffer_cells = new cl::sycl::buffer<traccc::input_cell, 1> (b.flat_input.cells, cl::sycl::range<1>(total_cell_count));
b.flat_input.buffer_modules = new cl::sycl::buffer<traccc::flat_input_module, 1>(b.flat_input.modules, cl::sycl::range<1>(total_module_count));
b.flat_output.buffer_cells = new cl::sycl::buffer<traccc::output_cell, 1> (b.flat_output.cells, cl::sycl::range<1>(total_cell_count));
b.flat_output.buffer_modules = new cl::sycl::buffer<traccc::flat_output_module, 1>(b.flat_output.modules, cl::sycl::range<1>(total_module_count));
b.chres.t_alloc_sycl = chrono_flatten.reset();
// b.chres.t_flatten_alloc = chrono_flatten.reset();
// b.chres.t_alloc_only = b.chres.t_flatten_alloc;
// Remplissage des tableaux input avec les données utiles : code en commun
}
// Fill
unsigned int global_cell_index = 0;
// Allocation des modules, les uns après les autres
for (uint im = 0; im < total_module_count; ++im) {
// lecture du nombre de cellules du module
unsigned int cell_count = read_source();
flat_input_module * module_in = &b.flat_input.modules[im];
module_in->cell_count = cell_count;
module_in->cell_start_index = global_cell_index;
// Remplissage des cellules
for (uint ic = 0; ic < cell_count; ++ic) {
input_cell * cell = &b.flat_input.cells[global_cell_index++];
unsigned int c0 = read_source();
unsigned int c1 = read_source();
cell->channel0 = c0;
cell->channel1 = c1;
//if (im < 10) logs( "(" + std::to_string(c0) + ", " + std::to_string(c1) + ") ");
}
//if (im < 10) log("");
}
// b.chres.t_flatten_fill = chrono_flatten.reset();
// b.chres.t_fill_only = b.chres.t_flatten_fill;
b.chres.t_fill = chrono_flatten.reset();
}
b.sycl_q.wait_and_throw();
if (TRACCC_LOG_LEVEL >= 2) log("Alloc & fill ok. -----");
//b.chres.t_alloc_fill = chrono.reset();
if (microseconds != 0) usleep(microseconds);
}