Search code examples
c++cudaeigeneigen3gpu

How to pass dynamic Eigen vector to GPU using Cuda?


I am going to learn how to transfer a dynamic Eigen vector to a GPU and get it back. For these purposes, I wrote a test code:

    using Vector = Eigen::Matrix<float, Eigen::Dynamic, 1, Eigen::ColMajor>;
    Vector vector;
    uint64_t size = 6;

    vector.resize(size);
    for (uint64_t i = 0; i < size; ++i)
        vector[i] = i;

    uint64_t sizeInBytes = size * sizeof (float) + sizeof (vector);

    Vector *vectorCuda;
    cudaMalloc((void**)&vectorCuda, sizeInBytes);
    cudaMemcpy(vectorCuda, &vector, sizeInBytes, cudaMemcpyKind::cudaMemcpyHostToDevice);

    Vector result;
    result.resize(size);
    cudaMemcpy(&result, vectorCuda, sizeInBytes, cudaMemcpyKind::cudaMemcpyDeviceToHost);

    cudaFree(vectorCuda);
    std::cout << "result: " << std::endl << result << std::endl;

The output is:

result: 
0
1
2
3
4
5
double free or corruption (fasttop)

So I passed the data to GPU and got it back, but I get the SIGABRT error. The error occurs in std::free(ptr):

1  __GI_raise                                                                      raise.c           50   0x7ffff660b18b 
2  __GI_abort                                                                      abort.c           79   0x7ffff65ea859 
3  __libc_message                                                                  libc_fatal.c      155  0x7ffff66553ee 
4  malloc_printerr                                                                 malloc.c          5347 0x7ffff665d47c 
5  _int_free                                                                       malloc.c          4266 0x7ffff665ede5 
6  Eigen::internal::aligned_free                                                   Memory.h          177  0x555555564e14 
7  Eigen::internal::conditional_aligned_free<true>                                 Memory.h          230  0x55555556601e 
8  Eigen::internal::conditional_aligned_delete_auto<float, true>                   Memory.h          416  0x555555565820 
9  Eigen::DenseStorage<float, -1, -1, 1, 0>::~DenseStorage                         DenseStorage.h    542  0x555555565281 
10 Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1>>::~PlainObjectBase PlainObjectBase.h 98   0x5555555650be 
11 Eigen::Matrix<float, -1, 1, 0, -1, 1>::~Matrix                                  Matrix.h          178  0x5555555650de 
12 main                                                                            main.cpp          26   0x555555564b42 

I thought it was because the destructor is called on an empty object, but when I commented out the line

//    cudaMemcpy(&result, vectorCuda, sizeInBytes, cudaMemcpyKind::cudaMemcpyDeviceToHost);

the error is gone.

So how to fix it?

Also I am writing in Cuda quite recently and there may be some bad lines in my code. Therefore, I would be glad if someone more experienced noticed something that could cause future bugs. I would like to store the start and end dynamic Eigen vectors in the stack.


Solution

  • The easiest way to fix this is to turn this into a float* pointer using the Eigen data() function, which returns a raw pointer to the data in your matrix. You can transfer the data to the GPU, work on it there, then copy that data back and store it in a nice Eigen Matrix again.

    #include <iostream>
    #include <cuda_runtime.h>
    #include <eigen3/Eigen/Core>
    #include <eigen3/Eigen/Dense>
    
    using Vector = Eigen::Matrix<float, Eigen::Dynamic, 1, Eigen::ColMajor>;
    
    int main(){
    Vector vector;
    uint64_t size = 6;
    
    vector.resize(size);
    for (uint64_t i = 0; i < size; ++i)
        vector[i] = i;
    
    uint64_t sizeInBytes = size * sizeof (float);
    
    float *raw_vector = vector.data();
    float *vectorCuda;
    cudaMalloc((void**)&vectorCuda, sizeInBytes);
    cudaMemcpy(vectorCuda, raw_vector, sizeInBytes, cudaMemcpyKind::cudaMemcpyHostToDevice);
    
    Vector result;
    result.resize(size);
    cudaMemcpy(result.data(), vectorCuda, sizeInBytes, cudaMemcpyKind::cudaMemcpyDeviceToHost);
    
    cudaFree(vectorCuda);
    std::cout << "result: " << std::endl << result << std::endl;
    return 0;
    
     }