将包含指向另一个结构体的指针的结构体传递给CUDA中的内核 [英] Passing an struct including a pointer to another struct, to kernel in CUDA

查看:407
本文介绍了将包含指向另一个结构体的指针的结构体传递给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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆