【发布时间】:2021-12-31 00:54:49
【问题描述】:
我编写了一个程序,它可以在 C 中运行,但由于某种原因无法在 CUDA 中编译。
问题出现在这里:
int key_size = 1000;
int references = 40;
double **ref_ptr;
ref_ptr = malloc(references * sizeof *ref_ptr);
for(int i = 0; i<references;i++){
ref_ptr[i] = malloc(key_size * sizeof *ref_ptr[i]);
}
在 C 中这个程序编译得很好,它甚至可以用 NVCC 编译为 C 程序 (nvcc my_program.c)。但是当我尝试将它编译为 CUDA 程序时,我得到了这个错误。
main.cu(191): error: a value of type "void *" cannot be assigned to an entity of type "double **"
main.cu(193): error: a value of type "void *" cannot be assigned to an entity of type "double *"
我尝试将它们转换为指针,但这开始给我带来很多内存访问问题,而且我的程序在用 C 编译时可以正常工作。你能帮我想想这里会发生什么吗?
【问题讨论】:
-
那是 C++ 编译器吗?演员阵容是必要的。
-
你是如何投射指针的?你写的是有效的 C,但是在 CUDA(和一般的 C++)中你必须转换指针。为什么不使用“new”而不是“malloc”?