-1

I am working on a project on Xilinx Vitis that I created a arrayA with

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

Then I have filled it with some random values.

I need to pass it to a kernel that accepts ( ap_uint<128>* arrayA)

How do I link these properly?

I have tried creating a

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

But it also gives me an error.

When I run the code it gives me an unaligned host allocator warning for XRT XILINX Vitis

How can I solve this issue?

Edit: uint8_t is unsigned char

  • 1
    Why do you think it's safe to just cast a `uint8_t` block of memory to an `ap_uint<128>` block? – Andrew Henle Jan 19 '21 at 12:19
  • @AndrewHenle the old version of the project I am using used these data types so I thought I don't need to modify the original code but can't seem to figure out proper way –  Jan 19 '21 at 12:27
  • The original code is wrong - there are several reasons that make doing things like that unsafe. It can violate alignment restrictions, and it is always a strict-aliasing violation. See [**Strict aliasing rule**](https://stackoverflow.com/questions/31615070/strict-aliasing-rule) and [**What is the strict aliasing rule?**](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule) Whoever wrote the original code likely learned on x86 systems that are a lot more forgiving of misaligned accesses. There are a ***lot*** of developers who are blithely unaware of these restrictions – Andrew Henle Jan 19 '21 at 12:38
  • @AndrewHenle Thank you for providing me answers those are insightful, by the time I am reading on these what could be possible method to solve this issue ? there are lots of functions that use uint8_t data type for inputs so I need to modify lots of codes. When I converted all to integer type it gave me an error of Segmentation Fault –  Jan 19 '21 at 12:54
  • The simplest thing would be `std::vector> arrayA(size / sizeof(ap_uint<128>));` – Caleth Jan 19 '21 at 13:03

1 Answers1

1

You can only reinterpret_cast arrays of char, signed char, unsigned char and std::byte, and uint8_t might not be an alias to one of those.

You also need to ensure that the data is properly aligned to treat it as ap_uint<128>.

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());
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • error: wrong number of template arguments (2, should be 1) for aligned_allocator: 33 | using raw_vec_t = vector>; –  Jan 19 '21 at 12:52
  • which `aligned_allocator` are you using? boost's allocator has an alignment parameter. What's the point of ensuring the alignment matches some unspecified other type if you need it to be `ap_uint<128>`? – Caleth Jan 19 '21 at 12:57
  • `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)>>; *********************************** src/host.cpp:275:96: error: wrong number of template arguments (2, should be 1) using raw_vec_t = std::vector)>>;` –  Jan 19 '21 at 13:07
  • uint8_t is unsigned char btw @Caleth –  Jan 19 '21 at 13:07
  • @Rerkfk64 on the platform you are currently using. It is not defined as such – Caleth Jan 19 '21 at 13:09
  • `std::byte` is C++17 in `` – Caleth Jan 19 '21 at 13:10
  • For the alignment issues (why can I not simply use the standard allocator??) , see e.g. https://sourceware.org/bugzilla/show_bug.cgi?id=206. The underlying issue is that the 128 bit types are unusual in their alignment requirements which cannot easily be communicated to the general library allocation function. At the same time the 16 byte alignment is unsuitably wasteful as a default. – Peter - Reinstate Monica Jan 19 '21 at 13:11
  • @Peter-ReinstateMonica joy. Ignoring "suitable for any type of object" because 128 bit integers aren't on every platform – Caleth Jan 19 '21 at 13:19
  • I am using C++11 and it is not possible to align a type with different allocator –  Jan 19 '21 at 13:51