Vitis HLS 仿真 C++ 工具代码分享

文章介绍了作者如何在C++中使用<chrono>和<ctime>库打印秒级时间戳,以监控一个运行1个多小时的HLS仿真脚本的性能。同时,还展示了如何向TXT文件写入多维数组,以便于测试激励管理和HLS仿真结果的对比。
摘要由CSDN通过智能技术生成

1 自媒体账号

目前运营的自媒体账号如下:


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

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

2 C++ 打印秒级时间戳

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

具体代码如下:

#include <iostream>
#include <chrono>
#include <ctime>

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

// Convert timestamp to date-time string
std::string timestampToDateTimeString(long long timestamp) {
    auto time = std::chrono::system_clock::from_time_t(timestamp);
    std::time_t c_time = std::chrono::system_clock::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 16:38:35
  • C++ 代码在线调试 | 菜鸟工具
  • 使用方式:将上述代码复制到 testbench 对应的cpp中。再需要打印时间戳时,调用 print_timestamp() 函数即可在 vitis hls 的控制台打印当前时间。 (也可以放到一个额外的 cpp 文件中,然后testbench 包含该cpp对应的头文件即可)
  • 代码解析:
    在C++中打印时间戳,可以使用标准库 <chrono><ctime> 来获取当前时间,并将其转换为时间戳。调用 getCurrentTimestampInSeconds 函数会返回当前时间的秒级时间戳。然后可以使用 <ctime> 头文件中的函数来将时间戳转换为年月日小时分秒的格式。

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

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

3 向 TXT 文件写入多维数组

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

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

#include <array>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>

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..."<<endl;
	oss << result_path << "output_hls.txt";
	string filename = oss.str();
	ofstream outfile(filename);
	
	for (int p = 0; p < p_limit; ++p) {
        for (int b = 0; b < b_limit; ++b) {
            for (int i = 0; i < i_limit; ++i) {
                outfile << data[p][b][i] << endl;
                //cout<<" data["<<p<<"]["<<b<<"]["<<i<<"] = "<< data[p][b][i] << endl;
            }
        }
    }
    outfile.close();
}

    return 0;
}
  • 若指定路径下无该 txt 文件,会自动创建 output_hls.txt 文件。
  • 代码解析:先通过拼接得到文件的绝对路径( 绝对路径已尝试过可行,相对路径未尝试 ),然后创建/打开该文件,再逐个读取数据数据,写入到文件中,最后关闭文件。

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

#include <array>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>

using namespace std;

template<typename T>
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!"<<endl;
        return ;
    }
    for (int p = 0; p < p_limit; ++p) {
        for (int b = 0; b < b_limit; ++b) {
            for (int i = 0; i < i_limit; ++i) {
                outfile << src[p][b][i] << endl;
                //cout<<" src["<<p<<"]["<<b<<"]["<<i<<"] = "<<src[p][b][i]<<endl;
            }
        }
    }

    outfile.close();
}


// 调用该函数示例:
write_3d_matrix_txt(data, result_path, "output", p_limit, b_limit, i_limit);

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪天鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值