-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.h
56 lines (48 loc) · 1.5 KB
/
utils.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
#pragma once
#include "pbbs-include/sequence_ops.h"
#include "pbbs-include/sample_sort.h"
// *******************************************
// Utils
// *******************************************
namespace utils {
// for granularity control
// if the size of a node is smaller than this number then it
// processed sequentially instead of in parallel
constexpr const size_t node_limit = 100;
// for two input sizes of n and m, should we do a parallel fork
// assumes work proportional to m log (n/m + 1)
static bool do_parallel(size_t n, size_t m) {
if (m > n) std::swap(n,m);
return (m > 8 && (m * pbbs::log2_up(n/m + 1)) > 100);
}
// fork-join parallel call, returning a pair of values
template <class RT, class Lf, class Rf>
static std::pair<RT,RT> fork(bool do_parallel, Lf left, Rf right) {
if (do_parallel) {
RT r = cilk_spawn right();
RT l = left();
cilk_sync;
return std::pair<RT,RT>(l,r);
} else {
RT l = left();
RT r = right();
return std::make_pair(l,r);
}
}
// fork-join parallel call, returning nothing
template <class Lf, class Rf>
static void fork_no_result(bool do_parallel, Lf left, Rf right) {
if (do_parallel) {cilk_spawn right(); left(); cilk_sync; }
else {left(); right();}
}
template<class V>
struct get_left {
V operator () (const V& a, const V& b) const
{ return a; }
};
template<class V>
struct get_right {
V operator () (const V& a, const V& b) const
{ return b; }
};
}