| 28 | |
| 29 | template <typename TFUNC> |
| 30 | void RunParallelDependency (const Table<int> & dag, |
| 31 | const Table<int> & trans_dag, // transposed dag |
| 32 | TFUNC func) |
| 33 | { |
| 34 | Array<atomic<int>> cnt_dep(dag.Size()); |
| 35 | for (auto i : Range(cnt_dep)) |
| 36 | cnt_dep[i].store (trans_dag[i].Size(), memory_order_relaxed); |
| 37 | |
| 38 | Array<int> ready(dag.Size()); |
| 39 | ready.SetSize0(); |
| 40 | int num_final = 0; |
| 41 | |
| 42 | for (int j : Range(cnt_dep)) |
| 43 | { |
| 44 | if (cnt_dep[j] == 0) ready.Append(j); |
| 45 | if (dag[j].Size() == 0) num_final++; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | if (!GetTaskManager()) |
| 50 | { |
| 51 | while (ready.Size()) |
| 52 | { |
| 53 | int size = ready.Size(); |
| 54 | int nr = ready[size-1]; |
| 55 | ready.SetSize(size-1); |
| 56 | |
| 57 | func(nr); |
| 58 | |
| 59 | for (int j : dag[nr]) |
| 60 | { |
| 61 | cnt_dep[j]--; |
| 62 | if (cnt_dep[j] == 0) |
| 63 | ready.Append(j); |
| 64 | } |
| 65 | } |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | atomic<int> cnt_final(0); |
| 70 | SharedLoop sl(Range(ready)); |
| 71 | |
| 72 | TaskManager :: CreateJob |
| 73 | ([&] (const TaskInfo & ti) |
| 74 | { |
| 75 | TPToken ptoken(queue); |
| 76 | TCToken ctoken(queue); |
| 77 | |
| 78 | for (int i : sl) |
| 79 | queue.enqueue (ptoken, ready[i]); |
| 80 | |
| 81 | auto *tm = GetTaskManager(); |
| 82 | while (1) |
| 83 | { |
| 84 | if (cnt_final >= num_final) break; |
| 85 | |
| 86 | while (tm->ProcessTask()); // do the nested tasks |
| 87 |
no test coverage detected