我正在嘗試為我的方法創建一個識別符號。這個識別符號對我的作業很重要,因為它實際上是硬體綜合。
我有以下模板類:
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]);
^
uj5u.com熱心網友回復:
您要尋找的是編譯時的 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 是您通常使用的數字,但位于i < ...for 回圈的一部分
你必須小心這一點,因為這end最多只能作業大約 900(取決于最大模板遞回深度)。否則你會得到一個編譯時錯誤。
使用 std::cout 的現場示例
編輯:
由于@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 function
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/331752.html
標籤:C 模板 vivado-hls
下一篇:C 中帶有C結構的malloc
