如何优化HLS仿真脚本运行时间

电子说

1.2w人已加入

描述

菜鸡一枚,记录下自己的学习过程,可能后续有更新,也可能没有更新,谨慎参考。

v1.0 24-02-22  C++ 打印秒级时间戳,向 TXT 文件写入多维数组

2 C++ 打印秒级时间戳

需求:由于自己目前一个 HLS 仿真脚本需要运行 1个多小时,先打算通过打印时间戳的方式找出最耗时的部分,然后想办法优化。

具体代码如下:

 

#include 
#include 
#include 

// Get current timestamp (in seconds)
long long getCurrentTimestampInSeconds() {
    auto now = std::now();
    auto now_seconds = std::time_point_cast(now);
    return now_seconds.time_since_epoch().count();
}

// Convert timestamp to date-time string
std::string timestampToDateTimeString(long long timestamp) {
    auto time = std::from_time_t(timestamp);
    std::time_t c_time = std::to_time_t(time);
    char buffer[80];
    std::strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", std::localtime(&c_time));
    return std::string(buffer);
}

void print_timestamp() {
    // 1 Get current timestamp (seconds)
    long long timestamp_seconds = getCurrentTimestampInSeconds();
    std::cout << "Current timestamp (seconds): " << timestamp_seconds << std::endl;
    // 2 Convert timestamp to date-time string and print it
    std::string datetime_string = timestampToDateTimeString(timestamp_seconds);
    std::cout << "Current date and time: " << datetime_string << std::endl;
}

 

HLS 仿真输出:

 

Current timestamp (seconds): 1708591115
Current date and time: 2024-02-22 1635

 

C++ 代码在线调试 |

使用方式:将上述代码复制到 testbench 对应的cpp中。再需要打印时间戳时,调用 print_timestamp() 函数即可在 vitis hls 的控制台打印当前时间。(也可以放到一个额外的 cpp 文件中,然后testbench 包含该cpp对应的头文件即可)

代码解析:在C++中打印时间戳,可以使用标准库 来获取当前时间,并将其转换为时间戳。调用 getCurrentTimestampInSeconds 函数会返回当前时间的秒级时间戳。然后可以使用 头文件中的函数来将时间戳转换为年月日小时分秒的格式。

在 timestampToDateTimeString 函数中,先将时间戳转换为 std::system_clock 类型的时间点,然后使用将其转换为 std::time_t 类型。最后使用 std::strftime 函数将 std::time_t 类型的时间转换为日期时间字符串。

在 print_timestamp 中先获取秒级时间戳,再将时间戳转换为日期时间字符串并打印出来。

3 向 TXT 文件写入多维数组

我比较喜欢用 python 脚本生成测试激励(存储到 txt 文件中,一行一个数据),然后 HLS testbench 读取激励文件。同时 hls 仿真结果也会写入到指定的 TXT 文件,用于与参考值进行详细对比,从而验证硬件代码是否功能正确。

这里以向 TXT 文件写入 3D 数组为例,具体代码如下:

 

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

// 设置存放 hls 仿真结果的文件夹
const string result_path = "path/to/hls_result/"; 
// 这里的 p_limit, b_limit, i_limit 替换为想设置的常量, 假设 data 为 hls 仿真结果
int data[p_limit][b_limit][i_limit];

int main()
{
 ostringstream oss;
{
 cout << "3 Saving output..."<

 

若指定路径下无该 txt 文件,会自动创建 output_hls.txt 文件。

代码解析:先通过拼接得到文件的绝对路径( 绝对路径已尝试过可行,相对路径未尝试 ),然后创建/打开该文件,再逐个读取数据数据,写入到文件中,最后关闭文件。

也可将该代码写为一个工具函数,方便进行调用, 如下所示

 

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

template
void write_3d_matrix_txt(T src[],
                        string path, string fname,
                        const int p_limit,
                        const int b_limit,
                        const int i_limit
                        )
{   // fname : "output"
    ostringstream oss;
    oss << path << fname << "_hls.txt";
    string filename  = oss.str();
    ofstream outfile(filename);
    if(!outfile.is_open())
    {
        cout<<"open error!"<

 

根据上述模板,可写出 1d, 2d, 4d... 等数组的写入工具函数。

 审核编辑:汤梓红

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分