Search code examples
c++cudaeigen

Float array as a vector of Eigen::Vector3f


I am writing a Eigen on the GPU and would like to use Eigen. I have a float* that represent data triplets (e.g. xyz, rgb, etc) of a known size n. I would like to use them as an Eigen vector, but only with casting (no memory copying, as I am on the device), e.g:

const float* input=...
Eigen::Vector3f* inputAsFloat = ????

Solution

  • You can use Eigen::Map to create a view on a data array:

    float* raw_data = ....;
    Eigen::Map<Vector3f> vector_map(raw_data, raw_data_size);
    

    There are various options you can use:

    • First template argument is the Eigen datatype you are emulating
    • Second template argument is row/column major
    • Third template argument is the stride option, i.e. how far apart elements and rows/columns are in memory (e.g. for extra padding per row)

    Not entirely sure how well this interacts with GPU and/or CUDA.