将包含指向另一个结构体的指针的结构体传递给CUDA中的内核 [英] Passing an struct including a pointer to another struct, to kernel in CUDA
问题描述
我有两个结构
struct collapsed {
char ** seq;
int num;
};
结构数据{
collapsed * x;
int num;
int numblocks;
int * blocksizes;
float * regmult;
float * learnmult;
};
我将其传递给我的内核;
__ global__ void KERNEL(data * X,...){
...
collapsed x = X-> x [0] // GIVES CUDA_EXPECTION_1:Lane Illegal Address
}
data X;
// init X
data * X_dev;
cudaMalloc((data **)& X_dev,sizeof(data));
cudaMemcpy(X_dev,& X,sizeof(data),cudaMemcpyHostToDevice);
KERNEL<<< ...>>>(X_dev,...);
此代码在内核代码中提供 CUDA_EXPECTION_1:Lane Illegal Address 什么是错误或什么是正确的方式做到吗?任何想法?
您要解除引用设备上的主机指针。
X
是有效的设备指针。
但是当您复制 X
结构到设备,你复制了 x
和它,它包含一个主机指针。当取消引用该指针时:
折叠x = X-> x [0]
^这是解除引用x指针
设备代码引发错误。 >
有关详情,请参阅此处以及如何解决它的说明。
I have two structs as
struct collapsed {
char **seq;
int num;
};
struct data {
collapsed *x;
int num;
int numblocks;
int *blocksizes;
float *regmult;
float *learnmult;
};
I am passing it to my kernel as;
__global__ void KERNEL(data* X,...){
...
collapsed x = X->x[0]; // GIVES CUDA_EXPECTION_1:Lane Illegal Address
}
data X;
//init X
data *X_dev;
cudaMalloc((data **) & X_dev, sizeof(data));
cudaMemcpy(X_dev, &X, sizeof(data), cudaMemcpyHostToDevice);
KERNEL<<<...>>>(X_dev,...);
This code gives CUDA_EXPECTION_1:Lane Illegal Address in the kernel code. What is wrong or what is the right way to do it ? Any idea?
You're dereferencing a host pointer on the device.
X
is a valid device pointer.
But when you copied the X
struct to the device, you copied x
along with it, which contains a host pointer. When you dereference that pointer:
collapsed x = X->x[0];
^ this is dereferencing the x pointer
the device code throws an error.
More detail is given here as well as instructions on how to fix it.
这篇关于将包含指向另一个结构体的指针的结构体传递给CUDA中的内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!