将对象复制到设备? [英] Copy an object to device?

查看:110
本文介绍了将对象复制到设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将C ++物件复制到装置吗?



表示我有:

  class CudaClass 
{
public:
int * data;
CudaClass(int x){
data = new int [1]; data [0] = x;
}
};

__global__ void useClass(CudaClass cudaClass)
{
printf(%dcudaClass.data [0]);
};


int main()
{
CudaClass c(1);
}



现在如何将c复制到设备内存并启动内核useClass

解决方案

是的,您可以将对象复制到设备上以便在设备上使用。当对象嵌入了指向动态分配区域的指针时,该过程需要一些额外的步骤。



请参阅我的答案在这里的讨论涉及。



另外,在你的类定义中,如果你想让某些函数在设备上可用,你应该装饰这些函数适当地(即可能具有 __ device__ __host __ );



EDIT:到现在的问题(现在删除)这里是我可以想出基于提供的代码最简单的示例代码:

  #include < stdio.h> 

class CudaClass
{
public:
int * data;
CudaClass(int x){
data = new int [1]; data [0] = x;
}
};

__global__ void useClass(CudaClass * cudaClass)
{
printf(%d\\\
,cudaClass-> data [0]);
};




int main()
{
CudaClass c(1);
//在设备上创建类存储和复制顶级类
CudaClass * d_c;
cudaMalloc((void **)& d_c,sizeof(CudaClass)); $ b $ c cudaMemcpy(d_c,& c,sizeof(CudaClass),cudaMemcpyHostToDevice);
//在设备上创建一个分配的区域,以供类
中的指针使用int * hostdata;
cudaMalloc((void **)& hostdata,sizeof(int));
cudaMemcpy(hostdata,c.data,sizeof(int),cudaMemcpyHostToDevice);
//将分配的设备存储器的指针复制到设备类
cudaMemcpy(&(d_c-> data),& hostdata,sizeof(int *),cudaMemcpyHostToDevice);
useClass<<< 1,1>>>(d_c);
cudaDeviceSynchronize();
return 0;
}



为了简洁起见,我省略了通常的cuda错误检查。



响应此问题,您不能使用基于设备的类中的指针从主机直接分配存储。这是因为cudaMalloc需要一个普通的基于主机的指针存储,例如你得到的:

  int * hostdata; 

cudaMalloc无法使用存储在设备上的指针。这不会工作:

  cudaMalloc(&(d_c-> data),sizeof(int)); 

,因为它需要在主机代码中取消引用设备指针(d_c) p>

Can I copy a C++ object to the device?

say I have:

class CudaClass
{
public:
int* data;
CudaClass(int x) {
    data = new int[1]; data[0] = x;
}
};

__global__ void useClass(CudaClass cudaClass)
{
    printf("%d" cudaClass.data[0]);
};


int main()
{
    CudaClass c(1);
}

Now how do I copy "c" to device memory and launch kernel "useClass"?

解决方案

Yes, you can copy an object to the device for use on the device. When the object has embedded pointers to dynamically allocated regions, the process requires some extra steps.

See my answer here for a discussion of what is involved. That answer also has a few samples code answers linked to it.

Also, in your class definition, if you want certain functions to be usable on the device, you should decorate those functions appropriately (i.e. probably with __device__ __host__);

EDIT: In response to a question (now deleted) here is the simplest sample code I could come up with based on the supplied code:

#include <stdio.h>

class CudaClass
{
public:
int* data;
CudaClass(int x) {
    data = new int[1]; data[0] = x;
}
};

__global__ void useClass(CudaClass *cudaClass)
{
    printf("%d\n", cudaClass->data[0]);
};




int main()
{
    CudaClass c(1);
    // create class storage on device and copy top level class
    CudaClass *d_c;
    cudaMalloc((void **)&d_c, sizeof(CudaClass));
    cudaMemcpy(d_c, &c, sizeof(CudaClass), cudaMemcpyHostToDevice);
    // make an allocated region on device for use by pointer in class
    int *hostdata;
    cudaMalloc((void **)&hostdata, sizeof(int));
    cudaMemcpy(hostdata, c.data, sizeof(int), cudaMemcpyHostToDevice);
    // copy pointer to allocated device storage to device class
    cudaMemcpy(&(d_c->data), &hostdata, sizeof(int *), cudaMemcpyHostToDevice);
    useClass<<<1,1>>>(d_c);
    cudaDeviceSynchronize();
    return 0;
}

In the interest of brevity/clarity I have dispensed with the usual cuda error checking.

Responding to the question, you cannot allocate storage directly from the host using the pointer in the device-based class. This is because cudaMalloc expects an ordinary host based pointer storage, such as what you get with:

int *hostdata;

cudaMalloc cannot work with a pointer whose storage is already on the device. This will not work:

cudaMalloc(&(d_c->data), sizeof(int));

because it requires dereferencing a device pointer (d_c) in host code, which is not allowed.

这篇关于将对象复制到设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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