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 <cstdlib>
18 
19 #include <iostream>
20 
21 #include "Graph.hpp"
22 
Cell(const Cell & other)23 Cell::Cell(const Cell& other) : op(other.op), value(other.value), successor(other.successor) {
24     ref_count = other.ref_count.load();
25 
26     input[0] = other.input[0];
27     input[1] = other.input[1];
28 }
29 
create_random_dag(std::size_t number_of_nodes)30 void Graph::create_random_dag(std::size_t number_of_nodes) {
31     my_vertex_set.resize(number_of_nodes);
32     for (std::size_t k = 0; k < number_of_nodes; ++k) {
33         Cell& c = my_vertex_set[k];
34         int op = int((rand() >> 8) % 5u);
35         if (op > int(k))
36             op = int(k);
37         switch (op) {
38             default:
39                 c.op = OP_VALUE;
40                 c.value = Cell::value_type((float)k);
41                 break;
42             case 1: c.op = OP_NEGATE; break;
43             case 2: c.op = OP_SUB; break;
44             case 3: c.op = OP_ADD; break;
45             case 4: c.op = OP_MUL; break;
46         }
47         for (int j = 0; j < ArityOfOp[c.op]; ++j) {
48             Cell& input = my_vertex_set[rand() % k];
49             c.input[j] = &input;
50         }
51     }
52 }
53 
print()54 void Graph::print() {
55     for (std::size_t k = 0; k < my_vertex_set.size(); ++k) {
56         std::cout << "Cell " << k << ":";
57         for (std::size_t j = 0; j < my_vertex_set[k].successor.size(); ++j)
58             std::cout << " " << int(my_vertex_set[k].successor[j] - &my_vertex_set[0]);
59         std::cout << "\n";
60     }
61 }
62 
get_root_set(std::vector<Cell * > & root_set)63 void Graph::get_root_set(std::vector<Cell*>& root_set) {
64     for (std::size_t k = 0; k < my_vertex_set.size(); ++k) {
65         my_vertex_set[k].successor.clear();
66     }
67     root_set.clear();
68     for (std::size_t k = 0; k < my_vertex_set.size(); ++k) {
69         Cell& c = my_vertex_set[k];
70         c.ref_count = ArityOfOp[c.op];
71         for (int j = 0; j < ArityOfOp[c.op]; ++j) {
72             c.input[j]->successor.push_back(&c);
73         }
74         if (ArityOfOp[c.op] == 0)
75             root_set.push_back(&my_vertex_set[k]);
76     }
77 }
78 
update()79 void Cell::update() {
80     switch (op) {
81         case OP_VALUE: break;
82         case OP_NEGATE: value = -(input[0]->value); break;
83         case OP_ADD: value = input[0]->value + input[1]->value; break;
84         case OP_SUB: value = input[0]->value - input[1]->value; break;
85         case OP_MUL: value = input[0]->value * input[1]->value; break;
86     }
87 }
88