main.cpp 1.97 KB
Newer Older
1
// Headers are available because we added the pls target
2 3
#include <string>
#include <cstdio>
4
#include <tuple>
5
#include <array>
6

7
#include <pls/pls.h>
8 9
#include <pls/dataflow/internal/inputs.h>
#include <pls/dataflow/internal/outputs.h>
10
#include <pls/dataflow/internal/function_node.h>
11
#include <pls/dataflow/internal/graph.h>
12
#include <pls/dataflow/internal/out_port.h>
13

14
int main() {
15
  using namespace pls::dataflow;
16 17
  using namespace pls::dataflow::internal;

18 19
  // Define
  graph<inputs<int, int>, outputs<int>> graph;
20

21 22
  auto triple = [](const int &i1, int &o1) {
    o1 = i1 * 3;
23
  };
24
  function_node<inputs<int>, outputs<int>, decltype(triple)> triple_node{triple};
25

26 27
  auto minus_one = [](const int &i1, int &o1) {
    o1 = i1 - 1;
28
  };
29 30 31 32 33 34 35 36 37 38 39 40 41
  function_node<inputs<int>, outputs<int>, decltype(minus_one)> minus_one_node{minus_one};

  auto recursion = [&](const int &i1, const int &i2, int &o1) {
    if (i1 > 0) {
      std::tuple<int> out;
      graph.run({i1, i2}, out);
      pls::scheduler::wait_for_all();
      o1 = std::get<0>(out);
    } else {
      o1 = i2;
    }
  };
  function_node<inputs<int, int>, outputs<int>, decltype(recursion)> recursion_node{recursion};
42

43
  // Connect
44 45
  graph.input<0>() >> minus_one_node.in_port<0>();
  minus_one_node.out_port<0>() >> recursion_node.in_port<0>();
46

47 48
  graph.input<1>() >> triple_node.in_port<0>();
  triple_node.out_port<0>() >> recursion_node.in_port<1>();
49

50
  recursion_node.out_port<0>() >> graph.output<0>();
51 52 53 54

  // Build
  graph.build();

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  pls::malloc_scheduler_memory my_scheduler_memory{8, 2u << 18u};
  pls::scheduler scheduler{&my_scheduler_memory, 8};
  scheduler.perform_work([&] {
    // Schedule Execution
    std::tuple<int> out1, out2, out3;
    graph.run({1, 2}, out1);
    graph.run({1, 1}, out2);
    graph.run({5, 6}, out3);

    // Wait for results and print
    pls::scheduler::wait_for_all();
    std::cout << std::get<0>(out1) << std::endl;
    std::cout << std::get<0>(out2) << std::endl;
    std::cout << std::get<0>(out3) << std::endl;
  });
70
}