c - 编译 CUDA 时出错

标签 c gcc compiler-construction cuda nvcc

我正在尝试编译一个 C 程序来尝试并行编程,当我尝试使用 nvcc 编译器(Nvidia)编译它时,它给了我这些错误:

inicis.cu(3): error: attribute "global" does not apply here

inicis.cu(3): error: incomplete type is not allowed

inicis.cu(3): error: identifier "a" is undefined

inicis.cu(3): error: expected a ")"

inicis.cu(4): error: expected a ";"

/usr/include/_locale.h(68): error: expected a declaration

inicis.cu(20): error: type name is not allowed

inicis.cu(21): error: type name is not allowed

inicis.cu(22): error: type name is not allowed

inicis.cu(41): error: identifier "dev_a" is undefined

inicis.cu(42): error: identifier "dev_b" is undefined

inicis.cu(43): error: identifier "dev_c" is undefined

nvcc 似乎无法识别 Nvidia 制作的 global 属性...

这是我的 C 程序,非常简单:

__global__ void operate(*memoria1, *memoria2)
{
    memoria2[threadIdx.x] = memoria1[threadIdx.x] + 1;
}


int main(int args, char **argv){

    int a[5], c[5];
    int *memory_1, *memory_2;

    cudaMalloc(void** &memory_1, 5 * sizeof(int));
    cudaMalloc(void** &memory_2, 5 * sizeof(int));

    cudaMemcpy(memory_1, a, 5 * sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(memory_2, c, 5 * sizeof(int), cudaMemcpyHostToDevice);

    operate <<<1, 5>>>(memory_1, memory_2);

    cudaMemcpy(c, memory_2, 5 * sizeof(int), cudaMemcpyDeviceToHost);

    for (int i = 0; i < sizeof(c); ++i)
    {
        printf ("%d" , c[i]);
    }

    cudaFree(memory_1);
    cudaFree(memory_2);

    return 0;
}

我认为它可能是编译器,但你认为它会是什么?

最佳答案

我认为如果你做出这些改变:

__global__ void operate(int* memoria1, int* memoria2)
                         ^              ^

和:

cudaMalloc((void**) &memory_1, 5 * sizeof(int));
cudaMalloc((void**) &memory_2, 5 * sizeof(int));
           ^      ^

您的代码将正确编译并运行。 您的结果会有点奇怪,因为代码实际上并未初始化 CUDA 内核正在操作的 ac 的值。所以你可能想要初始化它们。

关于c - 编译 CUDA 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18238228/

相关文章:

比较两个字符串并查找不匹配计数

将小数点后的值转换为较小的精度

c - fork 到新的 cygwin 终端

c# - 通过 TCP C# 接收结构

c - 全局和函数中的不同初始化和符号指针值

python - 为什么 (python|ruby) 被解释?

c# - 如何检测和纠正无用的 try catch block ?

compiler-construction - 编写本地语言编译器

c - 了解 x86 微基准测试中退役的加载和存储数量

c - 使用 C 中的文件管理陷入循环