将设备中动态分配的数据复制到主机时出错? [英] error when copying dynamically allocated data in device to host?

查看:40
本文介绍了将设备中动态分配的数据复制到主机时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在将设备中动态分配的数据复制到主机内存时遇到问题.数据是使用malloc分配的,我在主机功能中将这些数据从设备复制到主机.这是代码:

I recently meet a problem when copying dynamically allocated data in device to host memory. The data is allocated with malloc, and I copy those data from device to host in host function. Here is the code:

#include <cuda.h> 
#include <stdio.h> 

#define N 100 
__device__ int* d_array; 
__global__ void allocDeviceMemory() 
{ 
d_array = new int[N]; 
for(int i=0; i < N; i++) 
d_array[i] = 123; 
} 
int main() 
{ 
allocDeviceMemory<<<1, 1>>>(); 
cudaDeviceSynchronize(); 
int* d_a = NULL; 
cudaMemcpyFromSymbol((void**)&d_a, "d_array", sizeof(d_a), 0, cudaMemcpyDeviceToHost); 
printf("gpu adress: %p\n", d_a); 

int* h_array = (int*)malloc(N*sizeof(int)); 
cudaError_t errr = cudaMemcpy(h_array, d_a, N*sizeof(int), cudaMemcpyDeviceToHost); 
printf("h_array: %d, %d\n", h_array[0], errr); 

getchar(); 
return 0; 
} 

已经有一个关于CUDA 4.1的发布者存在相同的问题,一些专家建议将CUDA驱动程序和运行时升级为较新的版本可以解决此问题. CUDA-将设备数据复制到主机吗?

There is already a poster had the same issue for CUDA 4.1, and some experts suggest upgreading the CUDA driver and runtime to newer version can solve this issue. CUDA - Copy device data to host?

我有CUDA工具包4.2和最新的开发人员驱动程序以及C2075,但是仍然遇到上述问题.请让我知道如何解决此问题.

I have CUDA toolkit 4.2 and lastest developer drivers and C2075, but it still come up with the above problem. Please let me know how to solve this problem.

推荐答案

不幸的是,您无法做任何尝试做的事情CUDA4.主机API无法从设备运行时堆上动态分配的地址复制,只能从设备复制代码可以访问它们.如果要使用主机API复制,则需要将数据写入到输出"目录中.首先使用主机API分配的缓冲区,然后您可以随意使用 cudaMemcpy 从主机中检索它.

Unfortunately there is no way to do what you are trying to do it CUDA 4. The host API cannot copy from dynamically allocated addresses on device runtime heap, only device code can access them. If you want to copy with the host API, you will need to write the data into an "output" buffer allocated with the host API first, then you are free to use cudaMemcpy to retrieve it from the host.

您可以在Nvidia的Mark Harris的此处中看到对该限制的确认.

You can see confirmation of this limitation from Mark Harris of Nvidia here.

自2012年发布此答案以来,对主机API互操作性的限制似乎已经定下来,并且是

Since this answer was posted in 2012, the restriction on host API interoperability appears to have been set in stone, and is explicitly documented in the CUDA programming guide.

这篇关于将设备中动态分配的数据复制到主机时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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