mdspan icon indicating copy to clipboard operation
mdspan copied to clipboard

Add pitched allocation example

Open mhoemmen opened this issue 2 years ago • 5 comments

This is the example I wrote for the user question here: https://github.com/kokkos/mdspan/issues/117

Fixes #248.

mhoemmen avatar Mar 21 '23 22:03 mhoemmen

@dalg24 Thanks for reviewing! : - )

Honestly I don't really follow the point you are trying to make with this example.

#117 explains in detail. The point is that users sometimes need to fill a 2-D array where each contiguous segment (e.g., row for layout_right) has extra byte padding at the end. Sometimes, the size of the type being stored does not evenly divide the number of bytes, even though the instances of the type are all stored with their correct alignment.

mhoemmen avatar Apr 07 '23 15:04 mhoemmen

I do not understand how this does what you want with respect to pitched allocations?

crtrott avatar Apr 07 '23 19:04 crtrott

I think the accessor needs to store a pitch or some information that every K elements there are 4 extra bytes or whatever.

crtrott avatar Apr 07 '23 19:04 crtrott

Also your argument with the alignment doesn't make much sense to me. The object can't say that it is 12byte aligned anyway for pitched allocations, since the whole purpose is that for some rows its not.

crtrott avatar Apr 07 '23 19:04 crtrott

Wouldn't this thing do what you want (only posted the relevant parts)?

template<class T>
struct pitched_accessor {
   int n_every;
   int extra_bytes;
   using data_handle_type = char*;
   pitched_accessor(int n_every_, int extra_bytes_) {
     assert(extra_bytes%alignof(T)==0);
   }
   T& access(const data_handle_type& p, size_t i) {
      char* p_offset = p + i*size_of(T) + extra_bytes * (i/n_every);
      return *reinterpret_cast<T&>(p_offset);
    }
 };

Probably could have T* as data_handle_type, just reinterpret_cast to char* during access? Oh and offset can't be implemented with this accessor. It would need to store an extra thing to know how far into the first n_every you are offset.

crtrott avatar Apr 07 '23 19:04 crtrott