Array of Graph Objects - 2023.2 English

AI Engine Kernel and Graph Programming Guide (UG1079)

Document ID
UG1079
Release Date
2023-12-04
Version
2023.2 English

A graph may contain multiple ports of the same type depending on some template parameter. This case is handled efficiently by the standard C vector notation, for example, input_port in[5];.

If you want this set of ports to be conditionally instantiated, the compiler will reject this notation. In that case, you must use C++ std::array notation:
std::array<T,N> Object;
Where,
  • T is the type of the object input_port/output_port, input_plio/output_plio, input_gmio/output_gmio, sub-graph, kernel, and so on.
  • N is the number of elements of the array.
  • Object is the name of the array to instantiate.
The following example shows how to declare various objects in arrays:
#include "adf.h"
#include <stdlib.h>

using namespace adf;

void f0(input_stream<int32> *, output_stream<int32> *);

constexpr int getNum(int id) {return id == 1? 2: 3;}

template<int ID>
struct Sub0: public graph
{
  std::array<input_port,getNum(ID)> _ins;
  std::array<output_port,getNum(ID)> _outs;
  std::array<kernel,getNum(ID)> _ks;
  Sub0()
  {
    for (auto &k: _ks)
    {
      k = kernel::create(f0);
      runtime<ratio>(k) = 0.9;
      source(k) = "k0.cpp";
    }
    for (int ind = 0; ind < _ins.size(); ++ind)
    {
      connect(_ins[ind], _ks[ind].in[0]);
      connect(_ks[ind].out[0], _outs[ind]);
    }
  }
};


template<int ID>
struct MyGraph: public graph
{
  std::array<input_plio , getNum(ID)> _plioIs;
  std::array<output_plio, getNum(ID)> _plioOs;
  Sub0<ID> _sub;

  MyGraph()
  {
    for (int ind = 0; ind < getNum(ID); ++ind)
    {
      char iName[40];
      char oName[40];
      sprintf(iName, "data/i_%d_%d.txt", ID, ind);
      sprintf(oName, "data/o_%d_%d.txt", ID, ind);

      _plioIs[ind] = input_plio::create(adf::plio_32_bits, iName);
      _plioOs[ind] = output_plio::create(adf::plio_32_bits, oName);

      connect(_plioIs[ind].out[0], _sub._ins[ind]);
      connect(_sub._outs[ind], _plioOs[ind].in[0]);
    }
  }
};