CUDA - 将设备数据复制到主机? [英] CUDA - Copy device data to host?

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

问题描述

我有设备变量,在这个变量我分配和填充数组在设备,但我有问题,获取数据主机。 cudaMemcpy()返回cudaErrorInvalidValue错误。我怎么能这样做?

I have device variable and in this variable I alloc and fill array in device, but I have problem to get data to host. cudaMemcpy() return cudaErrorInvalidValue error. how can i do it?

PS:代码只是例子,我知道,在这个特殊情况下,我可以使用cudaMalloc,因为我知道数组的大小,在我的REAL代码中,它计算设备中的数组大小,它需要立即分配内存。

PS: The Code is just example, I know, that In this particular case I can use cudaMalloc, because I know size of array, but In my REAL code, It computes size of array in device and it needs immediately alloc memory.

PS2:我发现类似的问题,但我还是不知道,我该如何解决呢? - 复制在设备中从设备分配的数据寄存

PS2: I found similar problem, but I still dont know, how can i solve it? - copy data which is allocated in device from device to host

PS3:我已更新程式码,但仍无法运作:{

PS3: I have updated code, but still doesnt works :{

PS4 :我只是试图在笔记本电脑上运行这个代码与Nvidia GT 520MX(最新的游戏驱动程序)和不工作太:(

PS4: I am just trying to run this code on notebook with Nvidia GT 520MX(lastest game driver) and doesnt work too :(

thx

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

#define N 400
__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: %lld\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.0。

i have tested your code and there is no error here. I am running CUDA 4.0.

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

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