Recursive Graphs - 2022.2 English

AI Engine Kernel and Graph Programming Guide (UG1079)

Document ID
UG1079
Release Date
2022-10-19
Version
2022.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<window<FRAME_LENGTH*4>>(din, k.in[0]);
    adf::connect<window<FRAME_LENGTH*4>>(k.out[0], RG.din);
    adf::connect<window<FRAME_LENGTH*4>>(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<window<FRAME_LENGTH*4>>(din, k.in[0]);
    adf::connect<window<FRAME_LENGTH*4>>(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 view obtained in Vitis Analyzer is the following:

Figure 1. Recursive Graph View