Builds a graph which adds N copies of one variable "in". I.e., a + a + a + ... + a The returned graph is parenthesized ramdonly. I.e., a + ((a + a) + a) (a + a) + (a + a) ((a + a) + a) + a are all possibly generated.
| 212 | // ((a + a) + a) + a |
| 213 | // are all possibly generated. |
| 214 | void BuildTree(int N, Graph* g) { |
| 215 | CHECK_GT(N, 1); |
| 216 | // A single input node "in". |
| 217 | auto in = test::graph::Recv(g, "a", "float", ALICE, 1, BOB); |
| 218 | std::vector<Node*> nodes; |
| 219 | int i = 0; |
| 220 | // Duplicate "in" N times. Each copies is named as l0, l1, l2, .... |
| 221 | for (; i < N; ++i) { |
| 222 | nodes.push_back(test::graph::Identity(g, in, 0)); |
| 223 | } |
| 224 | random::PhiloxRandom philox(testing::RandomSeed(), 17); |
| 225 | random::SimplePhilox rnd(&philox); |
| 226 | while (nodes.size() > 1) { |
| 227 | // Randomly pick two from nodes and add them. The resulting node |
| 228 | // is named lik n10, n11, .... and is put back into "nodes". |
| 229 | int x = rnd.Uniform(nodes.size()); |
| 230 | auto in0 = nodes[x]; |
| 231 | nodes[x] = nodes.back(); |
| 232 | nodes.resize(nodes.size() - 1); |
| 233 | x = rnd.Uniform(nodes.size()); |
| 234 | auto in1 = nodes[x]; |
| 235 | // node = in0 + in1. |
| 236 | nodes[x] = test::graph::Add(g, in0, in1); |
| 237 | } |
| 238 | // The final output node "out". |
| 239 | test::graph::Send(g, nodes.back(), "b", BOB, 1, ALICE); |
| 240 | } |
| 241 | |
| 242 | TEST_F(ExecutorTest, RandomTree) { |
| 243 | auto g = absl::make_unique<Graph>(OpRegistry::Global()); |