1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #include <vector>
18 #include <algorithm>
19 
20 #include "oneapi/tbb/parallel_for_each.h"
21 
22 #include "Graph.hpp"
23 
24 class Body {
25 public:
Body()26     Body(){};
27 
28     //------------------------------------------------------------------------
29     // Following signatures are required by parallel_for_each
30     //------------------------------------------------------------------------
31     typedef Cell* argument_type;
32 
operator ()(Cell * c,oneapi::tbb::feeder<Cell * > & feeder) const33     void operator()(Cell* c, oneapi::tbb::feeder<Cell*>& feeder) const {
34         c->update();
35         // Restore ref_count in preparation for subsequent traversal.
36         c->ref_count = ArityOfOp[c->op];
37         for (std::size_t k = 0; k < c->successor.size(); ++k) {
38             Cell* successor = c->successor[k];
39             // ref_count is used for inter-task synchronization.
40             // Correctness checking tools might not take this into account, and report
41             // data races between different tasks, that are actually synchronized.
42             if (0 == --(successor->ref_count)) {
43                 feeder.add(successor);
44             }
45         }
46     }
47 };
48 
ParallelPreorderTraversal(const std::vector<Cell * > & root_set)49 void ParallelPreorderTraversal(const std::vector<Cell*>& root_set) {
50     oneapi::tbb::parallel_for_each(root_set.begin(), root_set.end(), Body());
51 }
52