CUDA:指针到指针的内存访问



我不知道是什么导致了这个问题。我在最后一行得到"访问冲突写入位置"错误。我没有正确分配内存吗?

    typedef struct {
    doubleXYZW cen_sum; //struct with 4 doubles
    double STS[6];
    XYZW *Points;// //struct with 4 floats
}BUNDLE;
BUNDLE *cpu_data = NULL;
size_t bundle_size = NUM_POINTS * sizeof(XYZW) + sizeof(doubleXYZW) + 6*sizeof(double);
HANDLE_ERROR(cudaMallocHost((BUNDLE**)&cpu_data, bundle_size));
//error in the next line
cpu_data->Points[0].x = 0; //x is the first element in the XYZW struct

您有两个必须完成的分配,而您只执行其中一个。

您正在为cpu_data指针分配一些存储空间,但是您没有为Points指针分配任何存储空间。因此当你解参点时:

cpu_data->Points[0].x = 0;
         ^      ^
         |      this dereferences the Points pointer (NOT allocated!)
         |
        this dereferences the cpu_data pointer (allocated)

是对一个没有分配的指针解引用,所以它是无效的。尝试以这种方式访问某些内容将生成无效访问。

你有(至少)两个选项来修复它:

  1. 在为cpu_points分配空间之后,您可以在cpu_points->Points上执行另一个cudaMallocHost分配
  2. 如果你知道Points数组的大小(似乎你做- NUM_POINTS),那么你可以静态地为它分配:

    typedef struct {
    doubleXYZW cen_sum; //struct with 4 doubles
    double STS[6];
    XYZW Points[NUM_POINTS];// //struct with 4 floats
    }BUNDLE;
    

注意,您的bundle_size计算是按照建议使用第二种方法的方式进行的。如果您使用第一种方法,您的bundle_size计算不正确。在任何情况下,无论使用哪一种方法,都更容易将bundle_size计算为sizeof(BUNDLE)

要清楚,这里没有特定于cuda的东西(例如,如果您使用malloc而不是cudaMallocHost,则会出现错误)。

最新更新