-
Notifications
You must be signed in to change notification settings - Fork 2
/
OFM.cpp
2719 lines (2489 loc) · 92 KB
/
OFM.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
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 "Graph.hpp"
#include "helpers.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <vector>
#include <tuple>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <immintrin.h>
#include <atomic>
#include "tbassert.h"
#include <cilk/cilk.h>
#include <cilk/reducer_opadd.h>
#include "Lock.cpp"
// potential TODO: explicitly store the implicit tree (index, len) as a struct
// potential problem: there's a real graph as well as the implicit one.
//TODO order node lock to avoid deadlock
typedef struct _node {
// beginning and end of the associated region in the edge list
uint32_t beginning; // deleted = max int
uint32_t end; // end pointer is exclusive
uint32_t num_neighbors; // number of edgess with this node as source
Lock lock;
} node_t;
// each node has an associated sentinel (max_int, offset) that gets back to its
// offset into the node array
// UINT32_MAX
//
// if value == 0, read it as null.
//typedef struct _edge { // moved to helpers for general use, left here for helpful comment
// uint32_t value;
// uint32_t dest; // destination of this edge in the graph, MAX_INT if this is a
// // sentinel
//} edge_t;
typedef union _edgeu {
edge_t e;
uint64_t i;
} edge_u;
typedef struct csr_ {
uint32_t *nodes;
uint32_t *dests;
uint32_t *values;
} CSR_simple;
#define NULL_VAL (UINT32_MAX)
#define SENT_VAL (UINT32_MAX -1)
//#define REDISTRIBUTE_PAR_SIZE (UINT32_MAX)
#define REDISTRIBUTE_PAR_SIZE (1 << 15)
typedef struct edge_list {
volatile uint64_t N;
volatile uint32_t H;
volatile uint32_t logN;
volatile uint32_t loglogN;
volatile uint32_t mask_for_leaf;
uint32_t volatile * volatile vals;
uint32_t volatile * volatile dests;
// Lock list_lock;
volatile double density_limit;
} edge_list_t;
class OFM : public Graph {
public:
// data members
edge_list_t edges;
std::vector<node_t> nodes;
Lock node_lock;
uint64_t next_task_id;
double upper_density_bound[32];
double lower_density_bound[32];
// graph_t g;
// function headings
OFM(uint32_t init_n);
OFM(OFM &other);
~OFM();
void double_list(uint64_t task_id, std::vector<uint64_t> &sub_counts, uint64_t num_elements);
void half_list(uint64_t task_id, std::vector<uint64_t> &sub_counts, uint64_t num_elements);
//void half_list();
void slide_right(uint64_t index, uint32_t *vals, uint32_t *dests);
void slide_left(uint64_t index, uint32_t *vals, uint32_t *dests);
void redistribute(uint64_t index, uint64_t len);
void redistribute_par(uint64_t index, uint64_t len, std::vector<uint64_t> &sub_counts, uint64_t num_elements, bool for_double = false);
void fix_sentinel(uint32_t node_index, uint64_t in);
void print_array(uint64_t worker_num = 0);
uint32_t find_value(uint32_t src, uint32_t dest);
void print_graph();
void add_node();
void add_node_fast(uint64_t task_id);
void add_edge(uint32_t src, uint32_t dest, uint32_t value);
void remove_edge(uint32_t src, uint32_t dest);
void remove_edge_fast(uint32_t src, uint32_t dest, uint64_t task_id);
void remove_edge_batch(uint32_t *srcs, uint32_t *dest, uint32_t edge_count);
void add_edge_update(uint32_t src, uint32_t dest, uint32_t value);
void add_edge_update_fast(uint32_t src, uint32_t dest, uint32_t value, uint64_t task_id);
void add_edge_batch_update(uint32_t *srcs, uint32_t *dests, uint32_t *values, uint32_t edge_count);
void add_edge_batch_update_no_val(uint32_t *srcs, uint32_t *dests, uint32_t edge_count);
void insert(uint64_t task_id, uint64_t index, uint32_t elem_dest, uint32_t elem_value, uint32_t src, pair_int held_locks);
void remove(uint64_t task_id, uint64_t index, uint32_t elem_dest, uint32_t src, pair_int held_locks);
uint64_t get_size();
uint64_t get_n();
void convert(Graph *g);
void add_file3(string filename);
vector<tuple<uint32_t, uint32_t, uint32_t>> get_edges();
void clear();
bool check_no_locks();
bool check_no_locks_for_me(uint64_t task_id);
bool check_no_node_locks_for_me(uint64_t task_id);
bool check_no_node_locks_held_by_me(uint64_t task_id);
bool grab_all_locks(uint64_t task_id, bool exclusive, REASONS reason = GENERAL);
void release_all_locks(uint64_t task_id, bool exclusive, REASONS reason = GENERAL);
pair_int grab_locks_in_range_with_resets(uint64_t task_id, uint64_t index, uint64_t len, REASONS reason, uint64_t guess);
pair_int grab_locks_for_leaf_with_resets(uint64_t task_id, uint32_t src, REASONS reason = GENERAL);
void release_locks_in_range(uint64_t task_id, pair_int locks, REASONS reason = GENERAL);
uint32_t find_contaning_node(uint64_t index);
pair_int which_locks_in_range(uint64_t index, uint64_t len, uint64_t guess);
pair_int which_locks_for_leaf(uint32_t src);
bool check_every_lock_in_leaf(uint64_t task_id, uint64_t index);
bool check_every_lock_in_node(uint64_t task_id, uint64_t index, uint64_t len);
uint32_t num_neighbors(uint32_t node) {
return nodes[node].num_neighbors;
}
uint64_t num_edges() {
uint64_t num = 0;
for (uint32_t i = 0; i < get_n(); i++) {
num += num_neighbors(i);
}
return num;
}
class iterator {
public:
uint64_t place;
uint64_t end;
uint32_t * vals;
uint32_t * dests;
uint8_t loglogN;
iterator(OFM *G, uint32_t node, bool start) {
if (!start) {
place = G->nodes[node].end;
return;
}
place = G->nodes[node].beginning + 1;
end = G->nodes[node].end;
vals = (uint32_t *)G->edges.vals;
dests = (uint32_t *)G->edges.dests;
loglogN = G->edges.loglogN;
while ((place < end) && (dests[place] == NULL_VAL)) {
place = ((place >> loglogN) + 1) << (loglogN);
}
if (place > end) {
place = end;
}
return;
}
bool operator==(const iterator& other) const {
return (place == other.place);
}
bool operator!=(const iterator& other) const {
return (place != other.place);
}
iterator& operator++() {
place += 1;
while ((place < end) && (dests[place] == NULL_VAL)) {
place = ((place >> loglogN) + 1) << (loglogN);
}
if (place > end) {
place = end;
}
return *this;
}
edge_t operator*() const {
return {vals[place], dests[place]};
}
};
iterator begin(uint32_t node) {
return iterator(this, node, true);
}
iterator end(uint32_t node) {
return iterator(this, node, false);
}
void make_symetric() {
vector<tuple<uint32_t, uint32_t, uint32_t> > edges_to_add = get_edges();
for(uint64_t i = 0; i < edges_to_add.size(); i++) {
uint32_t src = get<0>(edges_to_add[i]);
uint32_t dest = get<1>(edges_to_add[i]);
uint32_t val = get<2>(edges_to_add[i]);
add_edge_update(src,dest,val);
add_edge_update(dest,src,val);
}
return;
}
BFS
PAGERANK
SPMV
TRIANGLE_COUNT_SORTED
pvector<int32_t> parallel_bfs(int32_t source, int32_t total_edges, int alpha, int beta) {
int n_workers = __cilkrts_get_nworkers();
pvector<int32_t> parent(get_n());
cilk_for (int32_t n = 0; n < get_n(); n++) {
parent[n] = num_neighbors(source) != 0 ? - num_neighbors(source) : -1;
}
parent[source] = source;
SlidingQueue<int32_t> queue(get_n());
queue.push_back(source);
queue.slide_window();
Bitmap curr(get_n());
curr.reset();
Bitmap front(get_n());
front.reset();
int64_t edges_to_check = total_edges;
int64_t scout_count = num_neighbors(source);
uint32_t *dests = (uint32_t *) edges.dests;
while (!queue.empty()) {
if (scout_count > edges_to_check / alpha) {
int64_t awake_count, old_awake_count;
cilk_for (int32_t i = queue.shared_out_start; i < queue.shared_out_end; i++) {
int32_t u = queue.shared[i];
front.set_bit_atomic(u);
}
awake_count = queue.size();
queue.slide_window();
do {
old_awake_count = awake_count;
awake_count = 0;
curr.reset();
vector<int64_t> awake_count_vector(n_workers, 0);
parallel_for (int32_t u=0; u < get_n(); u++) {
uint32_t worker_num = __cilkrts_get_worker_number();
if (parent[u] < 0) {
int64_t awake_count_local = awake_count_vector[worker_num];
uint64_t start = nodes[u].beginning + 1;
uint64_t end = nodes[u].end;
for (uint32_t i = start; i < end; i++) {
int32_t v = dests[i];
if (v < NULL_VAL) {
if (front.get_bit(v)) {
parent[u] = v;
awake_count_local+=1;
curr.set_bit_atomic(u);
break;
}
}
}
/*
for (iterator it = begin(u); it != end(u); ++it) {
edge_t edge = *it;
int32_t v = edge.dest;
if (front.get_bit(v)) {
parent[u] = v;
awake_count_local+=1;
curr.set_bit(u);
break;
}
}
*/
awake_count_vector[worker_num] = awake_count_local;
}
}
for (auto &item : awake_count_vector) {
awake_count+=item;
}
front.swap(curr);
} while ((awake_count >= old_awake_count) || (awake_count > get_n() / beta));
QueueBuffer<int32_t> *queue_array = (QueueBuffer<int32_t> *)malloc(4*sizeof(QueueBuffer<int32_t>) * n_workers);
if (queue_array == NULL) {
printf("bad malloc in bfs\n");
exit(-1);
}
for (int i = 0; i < n_workers; i++) {
new(&queue_array[i*4]) QueueBuffer(queue);
}
cilk_for (int32_t n=0; n < get_n(); n++) {
if (front.get_bit(n)) {
queue_array[__cilkrts_get_worker_number()*4].push_back(n);
}
}
cilk_for (int i = 0; i < n_workers; i++) {
queue_array[i*4].flush();
delete &queue_array[i*4];
}
free(queue_array);
queue.slide_window();
scout_count = 1;
} else {
edges_to_check -= scout_count;
scout_count = 0;
vector<int64_t> scout_count_vector(n_workers, 0);
QueueBuffer<int32_t> *queue_array = (QueueBuffer<int32_t> *)malloc(4*sizeof(QueueBuffer<int32_t>) * n_workers);
if (queue_array == NULL) {
printf("bad malloc in bfs\n");
exit(-1);
}
for (int i = 0; i < n_workers; i++) {
new(&queue_array[i*4]) QueueBuffer(queue);
}
cilk_for (int32_t i = queue.shared_out_start; i < queue.shared_out_end; i++) {
int32_t u = queue.shared[i];
uint32_t worker_num = __cilkrts_get_worker_number();
for (iterator it = begin(u); it != end(u); ++it) {
edge_t edge = *it;
int32_t v = edge.dest;
int32_t curr_val = parent[v];
if (curr_val < 0) {
if (compare_and_swap(parent[v], curr_val, u)) {
queue_array[4*worker_num].push_back(v);
scout_count_vector[worker_num] += -curr_val;
}
}
}
}
cilk_for (int i = 0; i < n_workers; i++) {
queue_array[i*4].flush();
delete &queue_array[i*4];
}
free(queue_array);
for (auto &item : scout_count_vector) {
scout_count+=item;
}
queue.slide_window();
}
}
for (int32_t n = 0; n < get_n(); n++) {
if (parent[n] < -1) {
parent[n] = -1;
}
}
return parent;
}
template <class F, typename VS, bool output>
void map_sparse(F &f, VS &vs, uint64_t self_index) {
uint32_t idx = nodes[self_index].beginning + 1;
uint32_t idx_end = nodes[self_index].end;
while ( idx < idx_end) {
uint32_t v = edges.dests[idx];
if ( v != NULL_VAL) {
if (f.cond(v) == 1 && f.updateAtomic(self_index, v) == 1) {
if constexpr (output) {
vs.insert_sparse(v);
}
}
idx++;
} else {
idx = ((idx >> edges.loglogN) +1 ) << (edges.loglogN);
}
}
}
template <class F, typename VS, bool output, bool vs_all>
void map_dense(F &f, VS &vs, uint64_t self_index) {
if constexpr (!vs_all) {
uint32_t idx = nodes[self_index].beginning + 1;
uint32_t idx_end = nodes[self_index].end;
while ( idx < idx_end) {
uint32_t v = edges.dests[idx];
if ( v != NULL_VAL) {
if (vs.has_dense_no_all(v) && f.update(v, self_index) == 1) {
if constexpr(output) {
vs.insert_dense(self_index);
}
}
if (f.cond(self_index) == 0) {
return;
}
idx++;
} else {
idx = ((idx >> edges.loglogN) +1 ) << (edges.loglogN);
}
}
} else {
uint32_t idx = nodes[self_index].beginning + 1;
uint32_t idx_end = nodes[self_index].end;
while ( idx < idx_end) {
uint32_t v = edges.dests[idx];
if ( v != NULL_VAL) {
if (f.update(v, self_index) == 1) {
if constexpr(output) {
vs.insert_dense(self_index);
}
}
if (f.cond(self_index) == 0) {
return;
}
idx++;
} else {
idx = ((idx >> edges.loglogN) +1 ) << (edges.loglogN);
}
}
}
}
CSR_simple __attribute__ ((noinline)) convert_to_csr() {
uint32_t N = nodes.size();
uint32_t M = num_edges();
CSR_simple csr = {NULL, NULL};
csr.nodes = (uint32_t *) malloc(N * sizeof(uint32_t));
uint32_t start = 0;
// parallel_prefix_sum
for (uint32_t i = 0; i < N; i++) {
csr.nodes[i] = start;
start += num_neighbors(i);
}
csr.dests = (uint32_t *) malloc(M * sizeof(uint32_t));
csr.values = (uint32_t *) malloc(M * sizeof(uint32_t));
cilk_for(uint32_t i = 0; i < N; i++) {
uint32_t * dest_array = &csr.dests[csr.nodes[i]];
uint32_t * value_array = &csr.values[csr.nodes[i]];
iterator it_end = end(i);
uint32_t count = 0;
for (iterator it = begin(i); it != it_end; ++it) {
edge_t edge = *it;
dest_array[count] = edge.dest;
value_array[count] = edge.value;
count++;
}
}
return csr;
}
};
// given index, return the starting index of the leaf it is in
//TODO this could be aster if we store a mask and just do a single and
uint64_t find_leaf(edge_list_t *list, uint64_t index) {
return index & list->mask_for_leaf;
}
uint64_t find_prev_valid(uint32_t volatile * volatile dests, uint64_t start) {
while (dests[start] == NULL_VAL) {
start--;
}
return start;
}
bool OFM::check_no_locks() {
bool ret = true;
ret = ret && node_lock.check_unlocked();
assert(node_lock.check_unlocked());
// ret = ret && edges.list_lock.check_unlocked();
// assert(edges.list_lock.check_unlocked());
for (uint32_t i = 0; i < nodes.size(); i++) {
ret = ret && nodes[i].lock.check_unlocked();
if (!ret) {
printf("found lock on iter %d\n", i);
}
ret = ret && (nodes[i].lock.reason == GENERAL);
if (!ret) {
printf("found lock on iter %d with %d reason\n", i, nodes[i].lock.reason);
}
assert(nodes[i].lock.check_unlocked());
assert(nodes[i].lock.reason == GENERAL);
}
return ret;
}
bool OFM::check_no_locks_for_me(uint64_t task_id) {
bool ret = true;
ret = ret && !node_lock.i_own_lock(task_id);
assert(!node_lock.i_own_lock(task_id));
// ret = ret && edges.list_lock.check_unlocked();
// assert(edges.list_lock.check_unlocked());
for (uint32_t i = 0; i < nodes.size(); i++) {
ret = ret && !nodes[i].lock.i_own_lock(task_id);
if (!ret) {
printf("found lock on iter %d\n", i);
}
ret = ret && (nodes[i].lock.reason_set_by != task_id);
if (!ret) {
printf("found lock on iter %d that worker %lu set the reason with %d\n", i,task_id, nodes[i].lock.reason);
}
assert(!nodes[i].lock.i_own_lock(task_id));
assert(nodes[i].lock.reason_set_by != task_id);
}
return ret;
}
bool OFM::check_no_node_locks_for_me(uint64_t task_id) {
bool ret = true;
for (uint32_t i = 0; i < nodes.size(); i++) {
ret = ret && !nodes[i].lock.i_own_lock(task_id);
if (!ret) {
printf("found lock on iter %d\n", i);
}
ret = ret && (nodes[i].lock.reason_set_by != task_id);
if (!ret) {
printf("found lock on iter %d that worker %lu set the reason with %d\n", i,task_id, nodes[i].lock.reason);
}
assert(!nodes[i].lock.i_own_lock(task_id));
assert(nodes[i].lock.reason_set_by != task_id);
}
return ret;
}
bool OFM::check_no_node_locks_held_by_me(uint64_t task_id) {
bool ret = true;
for (uint32_t i = 0; i < nodes.size(); i++) {
ret = ret && !nodes[i].lock.i_own_lock(task_id);
if (!ret) {
printf("found lock on iter %d\n", i);
}
assert(!nodes[i].lock.i_own_lock(task_id));
}
return ret;
}
uint64_t next_leaf(uint64_t index, uint32_t loglogN) {
return ((index >> loglogN) + 1) << (loglogN);
}
bool OFM::grab_all_locks(uint64_t task_id, bool exclusive, REASONS reason) {
for (uint32_t i = 0; i < nodes.size(); i++) {
if (exclusive) {
if (!nodes[i].lock.lock(task_id, reason)) {
return false;
}
} else {
if (!nodes[i].lock.lock_shared(task_id)) {
return false;
}
}
}
return true;
}
void OFM::release_all_locks(uint64_t task_id, bool exclusive, REASONS reason) {
cilk_for (uint32_t i = 0; i < nodes.size(); i++) {
if (exclusive) {
nodes[i].lock.unlock(task_id, reason);
} else {
nodes[i].lock.unlock_shared(task_id);
}
}
}
void OFM::clear() {
printf("clear called\n");
uint64_t task_id = __sync_fetch_and_add(&next_task_id, 2);
grab_all_locks(task_id, true, GENERAL);
free((void*)edges.vals);
free((void*)edges.dests);
edges.N = 2 << bsr_word(0);
// printf("%d\n", bsf_word(list->N));
edges.loglogN = bsr_word(bsr_word(edges.N) + 1);
edges.logN = (1 << edges.loglogN);
edges.H = bsr_word(edges.N / edges.logN);
}
// for soc-
// starting at 1
void OFM::add_file3(string filename) {
ifstream myfile(filename.c_str());
string line;
if (myfile.is_open()) {
while (getline(myfile, line)) {
vector<string> elems = split(line, '\t');
int src = atoi(elems[0].c_str()) - 1;
while (src >= get_n()) {
add_node();
}
int dest = atoi(elems[1].c_str()) - 1;
while (dest >= get_n()) {
add_node();
}
add_edge(src, dest, 1);
// if (line_num++ > 400000000) {
// break;
// }
}
myfile.close();
// return 0;
} else {
printf("file was not opened\n");
}
}
// TODO jump to next leaf
vector<tuple<uint32_t, uint32_t, uint32_t>> OFM::get_edges() {
// TODO grab locks in the lock list
// for now, grabs the global lock
node_lock.lock_shared(); // lock node array
// edges.list_lock.lock();
uint64_t n = get_n();
vector<tuple<uint32_t, uint32_t, uint32_t>> output;
for (uint64_t i = 0; i < n; i++) {
uint64_t start = nodes[i].beginning;
uint64_t end = nodes[i].end;
nodes[i].lock.lock_shared();
for (uint64_t j = start + 1; j < end; j++) {
if (edges.dests[j]!=NULL_VAL) {
output.push_back(
make_tuple(i, edges.dests[j], edges.vals[j]));
}
}
nodes[i].lock.unlock_shared();
}
// edges.list_lock.unlock();
node_lock.unlock_shared(); // lock node array
return output;
}
//TODO this might only work if you start empty
void OFM::convert(Graph *g) {
uint64_t n = g->get_n();
free((void*)edges.vals);
free((void*)edges.dests);
edges.N = 2UL << bsr_word(n);
// printf("%d\n", bsf_word(list->N));
edges.loglogN = bsr_word(bsr_word(edges.N) + 1);
edges.logN = (1 << edges.loglogN);
assert(edges.logN > 0);
edges.H = bsr_word(edges.N / edges.logN);
// printf("N = %d, logN = %d, loglogN = %d, H = %d\n", list->N, list->logN,
// list->loglogN, list->H);
edges.dests = (uint32_t *)malloc(edges.N * sizeof(*(edges.dests)));
if (edges.dests == NULL) {
printf("bad malloc in convert\n");
exit(-1);
}
edges.vals = (uint32_t *)malloc(edges.N * sizeof(*(edges.vals)));
if (edges.vals == NULL) {
printf("bad malloc in convert\n");
exit(-1);
}
for (uint64_t i = 0; i < edges.N; i++) {
edges.dests[i] = NULL_VAL;
edges.vals[i] = 0;
}
for (uint64_t i = 0; i < n; i++) {
add_node();
}
//TODO why does making these cilk_for crash the compilier
for (uint64_t i = 0; i < n; i++) {
for (uint64_t j = 0; j < n; j++) {
// find_value returns 0 if not found.
uint32_t value = g->find_value(i, j);
if (value != 0) {
add_edge(i, j, value);
}
}
}
}
uint64_t OFM::get_n() {
node_lock.lock_shared();
uint64_t size = nodes.size();
node_lock.unlock_shared();
return size;
}
uint64_t OFM::get_size() {
node_lock.lock_shared();
uint64_t size = nodes.capacity() * sizeof(node_t);
size += sizeof(OFM);
size += edges.N * sizeof(edge_t);
node_lock.unlock_shared();
return size;
}
void print_array(edge_list_t *edges) {
printf("N = %lu, logN = %d\n", edges->N, edges->logN);
for (uint64_t i = 0; i < edges->N; i++) {
if (edges->dests[i]==NULL_VAL) {
printf("%lu-x ", i);
} else if ((edges->dests[i]==SENT_VAL) || i == 0) {
uint32_t value = edges->vals[i];
if (value == NULL_VAL) {
value = 0;
}
printf("\n%lu-s(%u):(?, ?) ", i, value);
} else {
printf("%lu-(%d, %u) ", i, edges->dests[i], edges->vals[i]);
}
}
printf("\n\n");
}
void OFM::print_array(uint64_t worker_num) {
printf("worker num: %lu, N = %lu, logN = %d, density_limit = %f\n", worker_num, edges.N, edges.logN, edges.density_limit);
for (uint64_t i = 0; i < edges.N; i++) {
if (edges.dests[i]==NULL_VAL) {
printf("%lu-x ", i);
} else if ((edges.dests[i] == SENT_VAL) || i == 0) {
uint32_t value = edges.vals[i];
if (value == NULL_VAL) {
value = 0;
}
printf("\n worker num: %lu, %lu-s(%u):(%d, %d)(%d) (%s, %d ", worker_num, i, value, nodes[value].beginning,
nodes[value].end, nodes[value].num_neighbors, nodes[value].lock.check_unlocked() ? "Unlocked": "Locked", nodes[value].lock.reason);
#ifndef NDEBUG
printf(" by %u)", nodes[value].lock.owner);
#else
printf(")");
#endif
} else {
printf("%lu-(%d, %u) ", i, edges.dests[i], edges.vals[i]);
}
}
printf("\n\n");
}
// get density of a node
// should already be locked if you are calling get density
double get_density(edge_list_t *list, uint64_t index, uint64_t len) {
uint64_t full = 0;
uint32_t volatile * volatile dests = list->dests;
for (uint64_t i = index; i < index+len; i+=4) {
uint64_t add = (dests[i]!=NULL_VAL) + (dests[i+1]!=NULL_VAL) + (dests[i+2]!=NULL_VAL) + (dests[i+3]!=NULL_VAL);
full += add;
}
double full_d = (double)full;
return full_d / len;
}
uint64_t get_density_count(edge_list_t *list, uint64_t index, uint64_t len) {
/*
uint32_t full = 0;
uint32_t i = index;
while (i < index + len) {
if (!is_null(list->items[i].e)) {
full++;
i++;
} else {
i = next_leaf(i, list->logN);
}
}
return full;
*/
/*
cilk::reducer< cilk::op_add<uint32_t> > full;
cilk_for (uint32_t i = index; i < index+len; i++) {
if (!is_null(list->items[i].e)) {
(*full)++;
}
}
return full.get_value();
*/
// fater without paralleliszation since it gets properly vectorized
uint32_t volatile * volatile dests = list->dests;
uint64_t full = 0;
for (uint64_t i = index; i < index+len; i+=4) {
uint64_t add = (dests[i]!=NULL_VAL) + (dests[i+1]!=NULL_VAL) + (dests[i+2]!=NULL_VAL) + (dests[i+3]!=NULL_VAL);
//__sync_fetch_and_add(&full, add);
full+=add;
}
return full;
/*
cilk::reducer< cilk::op_add<uint32_t> > full;
cilk_for (uint32_t i = index; i < index+len; i+=4) {
uint32_t add = !is_null(list->items[i].e) + !is_null(list->items[i+1].e) + !is_null(list->items[i+2].e) + !is_null(list->items[i+3].e);
(*full)+=add;
}
return full.get_value();
*/
}
uint64_t get_density_count_par(edge_list_t *list, uint64_t index, uint64_t len, std::vector<uint64_t> &sub_counts) {
cilk::reducer< cilk::op_add<uint64_t> > total;
uint32_t volatile * volatile dests = list->dests;
cilk_for(uint64_t j = index; j < index+len; j+= REDISTRIBUTE_PAR_SIZE) {
uint64_t full = 0;
for (uint64_t i = j; i < j+REDISTRIBUTE_PAR_SIZE; i+=4) {
uint64_t add = (dests[i]!=NULL_VAL) + (dests[i+1]!=NULL_VAL) + (dests[i+2]!=NULL_VAL) + (dests[i+3]!=NULL_VAL);
full+=add;
}
(*total)+=full;
sub_counts[(j-index)/REDISTRIBUTE_PAR_SIZE] = full;
}
/*
for (int i = 0; i < sub_counts.size(); i++) {
printf("subcount[%d] = %u\n", i, sub_counts[i]);
}
print_array(list);
*/
return total.get_value();
}
bool check_no_full_leaves(edge_list_t *list, uint64_t index, uint64_t len) {
for (uint64_t i = index; i < index + len; i+= list->logN) {
bool full = true;
for (uint64_t j = i; j < i + list->logN; j++) {
if (list->dests[j]==NULL_VAL) {
full = false;
}
}
if (full) {
return false;
}
}
return true;
}
// height of this node in the tree
uint32_t get_depth(edge_list_t *list, uint64_t len) { return bsr_word(list->N / len); }
// when adjusting the list size, make sure you're still in the
// density bound
pair_double density_bound(edge_list_t *list, uint64_t depth) {
pair_double pair;
// between 1/4 and 1/2
// pair.x = 1.0/2.0 - (( .25*depth)/list->H);
// between 1/8 and 1/4
pair.x = 1.0 / 4.0 - ((.125 * depth) / list->H);
pair.y = 3.0 / 4.0 + ((.25 * depth) / list->H);
if (pair.y > list->density_limit) {
pair.y = list->density_limit+.001;
}
return pair;
}
//TODO make it so the first element is known to always be the first element and don't special case it so much
//assumes the node_lock is held
void OFM::fix_sentinel(uint32_t node_index, uint64_t in) {
// we know the first sentinal will never move so we just ignore it
assert(node_index > 0);
//node_lock.lock_shared();
if (in >= 1UL<<32) {
printf("fix_sentinel called with something too big: %lu, the graph exceeds the capabilities\n",in);
exit(-1);
}
if (node_index == 0) {
printf("got 0 for node index, should never happen\n");
while(1){}
}
nodes[node_index - 1].end = in;
nodes[node_index].beginning = in;
if (node_index == nodes.size() - 1) {
nodes[node_index].end = edges.N - 1;
}
//node_lock.unlock_shared();
}
// Evenly redistribute elements in the ofm, given a range to look into
// index: starting position in ofm structure
// len: area to redistribute
// should already be locked
void OFM::redistribute(uint64_t index, uint64_t len) {
//printf("len = %u\n", len);
assert(find_leaf(&edges, index) == index);
//printf("REDISTRIBUTE START: index:%u, len %u, worker %lu\n", index, len, get_worker_num());
//print_array(get_worker_num());
// std::vector<edge_t> space(len); //
//TODO if its small use the stack
// for small cases put on the stack
uint32_t *space_vals;
uint32_t *space_dests;
uint32_t volatile * volatile vals = edges.vals;
uint32_t volatile * volatile dests = edges.dests;
uint64_t j = 0;
if (len == edges.logN) {
return;
j = index;
//printf("index = %u\n",index);
//print_array(0);
for (uint64_t i = index; i < index + len; i++) {
vals[j] = vals[i];
dests[j] = dests[i];
if (dests[j]==SENT_VAL) {
// fixing pointer of node that goes to this sentinel
uint64_t node_index = vals[j];
fix_sentinel(node_index, j);
}
j += (dests[j]!=NULL_VAL);
}
for (uint64_t i = j; i < index+len; i++) {
vals[i] = 0;
dests[i] = NULL_VAL;
}
//print_array(0);
return;
} else {
space_vals = (uint32_t *)malloc(len * sizeof(*(edges.vals)));
if (space_vals == NULL) {
printf("bad malloc in redistribute 1\n");
exit(-1);
}
space_dests = (uint32_t *)malloc(len * sizeof(*(edges.dests)));
if (space_dests == NULL) {
printf("bad malloc in redistribute 2\n");
exit(-1);
}
}
// move all items in ofm in the range into
// a temp array
/*
int i = index;
while (i < index + len) {
if (!is_null(edges.items[i])) {
space[j] = edges.items[i];
edges.items[i].value = 0;
edges.items[i].dest = 0;
i++;
j++;
} else {
i = next_leaf(i, edges.logN);
}
}
*/
//TODO could parralize if get_density_count gave us more data, but doesn't seem to be a bottle neck
// could get better cache behavior if I go back and forth with reading and writing
for (uint64_t i = index; i < index + len; i++) {
space_vals[j] = vals[i];
space_dests[j] = dests[i];
// counting non-null edges
j += (space_dests[j]!=NULL_VAL);
// setting section to null
vals[i] = 0;
dests[i] = NULL_VAL;
}
/*
if (((double)j)/len > ((double)(edges.logN-1)/edges.logN)) {
printf("too dense in redistribute, j = %u, len = %u, index = %u for worker %lu\n",j, len, index, get_worker_num() );
print_array(get_worker_num());
}*/
assert( ((double)j)/len <= ((double)(edges.logN-1)/edges.logN));
uint64_t num_leaves = len >> edges.loglogN;
uint64_t count_per_leaf = j / num_leaves;
uint64_t extra = j % num_leaves;
// parallizing does not make it faster
for (uint64_t i = 0; i < num_leaves; i++) {
uint64_t count_for_leaf = count_per_leaf + (i < extra);
uint64_t in = index + (edges.logN * (i));
uint64_t j2 = count_per_leaf*i + min(i,extra);
//TODO could be parallized, but normally only up to size 32
uint64_t j3 = j2;
for (uint64_t k = in; k < count_for_leaf+in; k++) {
vals[k] = space_vals[j2];
j2++;
}
for (uint64_t k = in; k < count_for_leaf+in; k++) {
dests[k] = space_dests[j3];
if (dests[k]==SENT_VAL) {
// fixing pointer of node that goes to this sentinel
uint32_t node_index = vals[k];
fix_sentinel(node_index, k);
}
j3++;
}
}
free(space_dests);
free(space_vals);
/*
if (!check_no_full_leaves(&edges, index, len)) {
printf("some leaves are full, current density is %f, index = %u, len = %u\n", get_density(&edges, index, len), index, len);
print_array(get_worker_num());
}*/
assert(check_no_full_leaves(&edges, index, len));
//printf("REDISTRIBUTE END: index:%u, len %u, worker %lu\n", index, len, get_worker_num());
}
void OFM::redistribute_par(uint64_t index, uint64_t len, std::vector<uint64_t> &sub_counts, uint64_t num_elements, bool for_double) {
assert(find_leaf(&edges, index) == index);
//printf("par len = %u\n", len);
uint32_t *space_vals = (uint32_t *)aligned_alloc(64, len * sizeof(*(edges.vals)));