Recursive Graphs - 2024.2 English

AI Engine-ML Kernel and Graph Programming Guide (UG1603)

Document ID
UG1603
Release Date
2024-11-28
Version
2024.2 English
One feature of C++ is recursive templates. Graphs can be created recursively by taking advantage of the recursive templates.
template<int Level>
class RecursiveGraph: public adf::graph {
private:
  adf::kernel k;
  RecursiveGraph<Level-1> RG;

public:
  adf::port<input> din;
  adf::port<output> dout;

  RecursiveGraph() {
    k = adf::kernel::create(passthrough);
    adf::source(k) = "kernels.cpp";
    adf::runtime<ratio>(k) = 0.9;

    adf::connect(din, k.in[0]);
    adf::connect(k.out[0], RG.din);
    adf::connect(RG.dout, dout);
  };
};


template<>
class RecursiveGraph<1>: public adf::graph {
private:
  adf::kernel k;

public:
  adf::port<input> din;
  adf::port<output> dout;

  RecursiveGraph() {
    k = adf::kernel::create(passthrough);
    adf::source(k) = "kernels.cpp";
    adf::runtime<ratio>(k) = 0.9;

    adf::connect(din, k.in[0]);
    adf::connect(k.out[0], dout);
  };
};

The class RecursiveGraph is parametrized with the template parameter Level which represents the depth of the recursive function. Within the RecursiveGraph class, an instantiati on of the same class is performed as RecursiveGraph<Level-1> RG;with the template parameter Level-1. This will recursively instantiate RecursiveGraph Level-1 times. The RecursiveGraph<1> class specialization behaves as the stop condition of the recursion.

class TestRecursiveGraph: public adf::graph {
public:
  adf::input_plio plin;
  adf::output_plio plout;

  RecursiveGraph<5> Recursive;

  TestRecursiveGraph()
  {
    plin = adf::input_plio::create("input",adf::plio_64_bits,"data/Input_64.txt",500);
    adf::connect(plin.out[0],Recursive.din);

    plout = adf::output_plio::create("output",adf::plio_64_bits,"data/Output.txt",500);
    adf::connect(Recursive.dout,plout.in[0]);
  };
};

In this example, five instances of RecursiveGraph will be instantiated in the TestRecursiveGraph graph.

TestRecursiveGraph RecursiveUnitTest;

int main(int argc, char ** argv) {

  RecursiveUnitTest.init();
  RecursiveUnitTest.run(NFRAMES*NITERATIONS);
  RecursiveUnitTest.end();
  return 0;
}

The recursive graph as opposed to a sequential graph view obtained in the Vitis IDE is the following:

Figure 1. Recursive Graph and Sequential Multi-kernel Graph View

The hierarchy obtained using recursivity is clearly visible in the graph view.