简体   繁体   English

CUDA cudaMemcpy“无效的论点”

[英]Cuda cudaMemcpy “invalid argument”

Trying to debug a larger application I'm having an issue where I can't seem to be able to copy values from the host to the device. 尝试调试更大的应用程序时,我似乎无法将值从主机复制到设备上,这是一个问题。 I provide below a minimal example which I think should copy the 6 to the device and then back. 我在下面提供了一个最小的示例, 我认为应该将6复制到设备上,然后再复制回去。

#include <stdio.h>
__device__ float a_d;

main(){
    float a = 6.0;
    float b;
    puts(cudaGetErrorString(cudaMemcpy(&a_d,&a,sizeof(float),cudaMemcpyHostToDevice)));
    puts(cudaGetErrorString(cudaMemcpy(&b,&a_d,sizeof(float),cudaMemcpyDeviceToHost)));
    printf("%e",b);
}

I get the following output with CUDA 5.5 on 64-bit Linux. 我在64位Linux上使用CUDA 5.5获得以下输出。

$ nvcc test.cu -run
invalid argument
invalid argument
0.000000e+00

whereas I'd expect cudaSuccess and 6.000000e+00 . 而我希望cudaSuccess6.000000e+00

You cannot use cudaMemcpy directly with __device__ variables. 您不能将cudaMemcpy直接与__device__变量一起使用。 The correct API to use is cudaMemcpyTo/FromSymbol . 正确使用的APIcudaMemcpyTo/FromSymbol

The following should work: 以下应该工作:

#include <stdio.h>
__device__ float a_d;

main(){
    float a = 6.0;
    float b;
    puts(cudaGetErrorString(cudaMemcpyToSymbol(a_d,&a,sizeof(float)));
    puts(cudaGetErrorString(cudaMemcpyFromSymbol(&b,a_d,sizeof(float)));
    printf("%e",b);
}

It's not clear why you'd expect 0.000000e+00 . 目前尚不清楚为什么会期望0.000000e+00 Based on your code I would expect 6.000000e+00 , or something like that. 根据您的代码,我期望6.000000e+00或类似的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM