我正试图为我的方法创建一个标识符。这个标识符对我的工作很重要,因为它实际上是硬件综合。
我有以下模板类:
template <int FF_COUNT, int FB_COUNT, int NEXT_COUNT>
class Core{
public:
...
template<int id> void consume_fb_events (hls::stream<event> feedback_stream [FB_COUNT] [FB_COUNT], weight w_mem [128*128]);
}
template <int FF_COUNT, int FB_COUNT, int NEXT_COUNT>
template <int id>
void Core<FF_COUNT, FB_COUNT, NEXT_COUNT>::consume_fb_events (hls::stream<event> feedback_stream [FB_COUNT] [FB_COUNT], weight w_mem [128*128]){
#pragma HLS INLINE off
event e;
for(int i = 0 ; i < FB_COUNT ; i++) {
while (!feedback_stream[id][i].empty()) {
feedback_stream[id][i].read(e);
ap_uint<16> mem_offset = e << size_exp;
consume_event (e, mem_offset, w_mem);
}
}
}这是我的函数调用
#define sth 8
for int i = 0 ; i < sth; i++
core[i].consume_fb_events<i>(....);我得到了编译错误:
ERROR: [HLS 200-70] Compilation errors found: In file included from c1/srnn.cpp:1:
c1/srnn.cpp:197:14: error: no matching member function for call to 'consume_fb_events'
core_1[i].consume_fb_events<i>(buffer_layer1_1, w1[i]);
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
c1/core.h:52:24: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'id'
template<int id> void consume_fb_events (hls::stream<event> feedback_stream [FB_COUNT] [FB_COUNT], weight w_mem [128*128]);
^发布于 2021-10-22 09:32:51
您所寻找的是编译时的for循环。因为模板参数必须是一个constexpr。我通常是这样做的,因为在constexpr函数中不能有for循环:
template<int i>
struct MyFunc
{
MyFunc()
{
// do something with i
core[i].consume_fb_events<i>(....);
}
};
template<int end, template <int I> class func, int i = 0>
struct ForLoop
{
ForLoop()
{
func<i>{};
ForLoop<end, func, i+1>{};
}
};
template<int end, template <int I> class func>
struct ForLoop<end, func, end>
{
ForLoop()
{
}
};您可以在MyFunc的构造函数中运行任何代码。
然后,您可以这样执行:
ForLoop<8, MyFunc>{};其中8是您通常需要的数字,但在for循环的i < ...部分
您必须小心这一点,因为这只适用于大约900的end (取决于最大模板递归深度)。否则,您将得到编译时错误。
编辑:
由于@SherifBadawy在一条注释中要求,您不需要声明一个结构/类MyFunc就可以做到这一点,但是我采用了这种方法,因为它使ForLoop更动态,并且可以多次重用它。
但如果你愿意的话,这也能奏效:
template<int i>
void foo()
{
// code here
}
template<int end, int i = 0>
struct ForLoop
{
ForLoop()
{
core[i].consume_fb_events<i>(....);
// more code
// or ...
foo<i>();
ForLoop<end, func, i+1>{};
}
};
template<int end>
struct ForLoop<end, end>
{
ForLoop()
{
}
};要运行ForLoop,您可以再次执行以下操作:
ForLoop<8>{}; // without passing a class or functionhttps://stackoverflow.com/questions/69673767
复制相似问题