| 18 | |
| 19 | template <typename TFUNC> |
| 20 | void RunParallelDependency (FlatTable<int> dag, |
| 21 | TFUNC func) |
| 22 | { |
| 23 | Array<atomic<int>> cnt_dep(dag.Size()); |
| 24 | |
| 25 | for (auto & d : cnt_dep) |
| 26 | d.store (0, memory_order_relaxed); |
| 27 | |
| 28 | static Timer t_cntdep("count dep"); |
| 29 | t_cntdep.Start(); |
| 30 | ParallelFor (Range(dag), |
| 31 | [&] (int i) |
| 32 | { |
| 33 | for (int j : dag[i]) |
| 34 | cnt_dep[j]++; |
| 35 | }); |
| 36 | t_cntdep.Stop(); |
| 37 | |
| 38 | atomic<size_t> num_ready(0), num_final(0); |
| 39 | ParallelForRange (cnt_dep.Size(), [&] (IntRange r) |
| 40 | { |
| 41 | size_t my_ready = 0, my_final = 0; |
| 42 | for (size_t i : r) |
| 43 | { |
| 44 | if (cnt_dep[i] == 0) my_ready++; |
| 45 | if (dag[i].Size() == 0) my_final++; |
| 46 | } |
| 47 | num_ready += my_ready; |
| 48 | num_final += my_final; |
| 49 | }); |
| 50 | |
| 51 | Array<int> ready(num_ready); |
| 52 | ready.SetSize0(); |
| 53 | for (int j : Range(cnt_dep)) |
| 54 | if (cnt_dep[j] == 0) ready.Append(j); |
| 55 | |
| 56 | |
| 57 | auto * task_manager = GetTaskManager(); |
| 58 | if (!task_manager) |
| 59 | // if (true) |
| 60 | { |
| 61 | while (ready.Size()) |
| 62 | { |
| 63 | int size = ready.Size(); |
| 64 | int nr = ready[size-1]; |
| 65 | ready.SetSize(size-1); |
| 66 | |
| 67 | func(nr); |
| 68 | |
| 69 | for (int j : dag[nr]) |
| 70 | { |
| 71 | cnt_dep[j]--; |
| 72 | if (cnt_dep[j] == 0) |
| 73 | ready.Append(j); |
| 74 | } |
| 75 | } |
| 76 | return; |
| 77 | } |
no test coverage detected