Memory access error of structure in .cu file (without kernel function), with lib Eigen 3.3.7

Version Information
cuda : 10.2
g++ : 8
eigen : 3.3.7

We defined a structure in a .h file with a member variable of type from the eigen library (specifically, we used version 3.3.7), and then we used this structure in a .cc file and a .cu file respectively, and found that the sizes of the two did not match. Further, when we passed the structure as a variable in a function in .cc to a function in a .cu file, a segment fault occurred in the assignment of the member variables in it.

Could you tell me why this is happening and how we can solve this problem?

The definition of the structure and the test code snippet are as follows.

struct test_struct
  std::vector<float> vec1;
  Eigen::Affine3d eigen_test;
  std::vector<float> vec2;

void test_cuda_class::test_for_nvcc(test_struct t1)
  std::cout << "=========== in cu ===========" << std::endl;
  std::cout << "sizeof t1: " << sizeof(t1) << std::endl;
  t1.vec2.assign(10,0); //here we got segment fault 
  std::cout << "==========^ in cu ^==========" << std::endl;  

void test_cc_class::test_in_cc()
  std::cout << "=========== in cc ===========" << std::endl;
  test_struct t1;
  std::cout << "sizeof t1: " << sizeof(t1) << std::endl;
  std::cout << "==========^ in cc ^==========" << std::endl;     
  test_cuda_class test1_obj;

The printout is as follows.

=========== in cc ===========
sizeof t1: 192
==========^ in cc ^==========
=========== in cu ===========
sizeof t1: 176
Segmentation fault

The complete test code and CMakeLists are shown in the attachment, please note that if you need to run the complete test, please change the address of the eigen library.
CMakeLists.txt (663 Bytes) (92 Bytes) (321 Bytes)
test.h (160 Bytes) (300 Bytes)
test_cuda.h (378 Bytes)


Here is a similar issue with Eigen:

Could you try the WAR to see if it helps?
Add #define EIGEN_MAX_STATIC_ALIGN_BYTES=16 before any Eigen’s header.


1 Like

Thanks! That solved this issue. Impressive!

And maybe we could go deeper:

  1. since there are hundreds of files in our project that use the types from the eigen library, can we set this macro by setting up the cmakelist file instead of adding this macro definition to every file?
  2. we have tried to use align(16) and attribute ((aligned (16))) methods when defining structs, this did not solve our problem (both still have segment fault), why is that?
  3. why nvcc does not set 16 bytes alignment? This may affect the running efficiency right?
  4. Finally, we are talking about structures here, but is there a similar problem with memory alignment of classes?


  1. Pls try with make/cmake -DEIGEN_MAX_STATIC_ALIGN_BYTES=16
  2. Pls refer to Programming Guide :: CUDA Toolkit Documentation
struct __align__(8) {
    float x;
    float y;

struct __align__(16) {
    float x;
    float y;
    float z;

3,4. pls refer to Programming Guide :: CUDA Toolkit Documentation