【问题标题】:Typecasting uint8_t vector to ap_uint<128> openCL将 uint8_t 向量类型转换为 ap_uint<128> openCL
【发布时间】:2021-01-19 12:17:16
【问题描述】:

我正在开发一个关于 Xilinx Vitis 的项目,我使用该项目创建了一个 arrayA

std::vector<uint8_t,aligned_allocator<uint8_t>> arrayA(size);

然后我用一些随机值填充它。

我需要将它传递给接受( ap_uint&lt;128&gt;* arrayA)的内核

如何正确链接这些?

我尝试过创建一个

ap_uint<128>* temp_A = arrayA.data()

但这也给了我一个错误。

当我运行代码时,它会给我一个针对 XRT XILINX Vitis 的未对齐主机分配器警告

我该如何解决这个问题?

编辑:uint8_t 是无符号字符

【问题讨论】:

  • 您为什么认为将uint8_t 内存块转换为ap_uint&lt;128&gt; 块是安全的?
  • @AndrewHenle 我正在使用的项目的旧版本使用了这些数据类型,所以我认为我不需要修改原始代码但似乎无法找出正确的方法
  • 原始代码是错误的 - 有几个原因使得做这样的事情不安全。它可能违反对齐限制,并且始终是严格混叠违规。请参阅Strict aliasing rule 和What is the strict aliasing rule? 编写原始代码的人可能是在 x86 系统上学到的,这些系统对未对齐的访问更加宽容。有 很多 的开发者不知道这些限制
  • @AndrewHenle 感谢您为我提供了那些很有见地的答案,当我阅读这些内容时,有什么可能的方法来解决这个问题?有很多函数使用 uint8_t 数据类型作为输入,所以我需要修改很多代码。当我将所有转换为整数类型时,它给了我一个 Segmentation Fault 的错误
  • 最简单的就是std::vector&lt;ap_uint&lt;128&gt;&gt; arrayA(size / sizeof(ap_uint&lt;128&gt;));

标签: c++ pointers fpga xilinx


【解决方案1】:

您只能使用 char、signed char、unsigned char 和 std::byte 组成的数组 reinterpret_cast,而 uint8_t 可能不是其中之一的别名。

您还需要确保数据正确对齐以将其视为ap_uint&lt;128&gt;。

using raw_vec_t = std::vector<unsigned char, boost::aligned_allocator<unsigned char, alignof(ap_uint<128>)>>;
raw_vec_t arrayA(size);

ap_uint<128>* temp_A = reinterpret_cast<ap_uint<128>>(arrayA.data());

【讨论】:

  • 错误:aligned_allocator 的模板参数数量错误(2,应该为 1):33 |使用 raw_vec_t = vector>;
  • 您使用的是哪个aligned_allocator? boost 的分配器有一个对齐参数。如果您需要它是ap_uint&lt;128&gt;,那么确保对齐匹配某些未指定的其他类型有什么意义?
  • src/host.cpp: In function 'int main(int, char**)': src/host.cpp:275:41: error: 'byte' is not a member of 'std' using raw_vec_t = std::vector&lt;std::byte, aligned_allocator&lt;std::byte, alignof(ap_uint&lt;128&gt;)&gt;&gt;; *********************************** src/host.cpp:275:96: error: wrong number of template arguments (2, should be 1) using raw_vec_t = std::vector&lt;std::byte, aligned_allocator&lt;std::byte, alignof(ap_uint&lt;128&gt;)&gt;&gt;;
  • uint8_t 是无符号字符 btw @Caleth
  • @Rerkfk64 在您当前使用的平台上。不是这样定义的
猜你喜欢
  • 1970-01-01
  • 2019-11-21
  • 2012-06-07
  • 2019-10-26
  • 2014-03-30
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多