无法将内核的原型放在 header

Cannot place kernel's prototype in header

io.cuh:

#ifndef IO_CUH
#define IO_CUH

#include <cuda.h>

typedef struct{
    unsigned width;
    unsigned height;
    unsigned char *image; //RGBA
}rgb_image;

__global__ void transformToGrayKernel(rgb_image *img);
void decodeTwoSteps(const char* filename, rgb_image *img);
void encodeOneStep(const char* filename, rgb_image *img);
void processImage(const char *filename, rgb_image *img);

#endif // IO_CUH

我正在尝试使用以下 makefile 编译简单程序:

lodepng.o: lodepng.h
        cc -c lodepng.c -o lodepng.o
io.o: io.cuh lodepng.o
        nvcc -c io.cu -o io.o
main: io.o
        nvcc main.c io.o -o main

'main.c' 使用 io.cu 中的一个函数,它依赖于 lodepng.c.

在参考代码的一些小警告之后,我收到以下错误:

nvcc main.c io.o -o main nvcc warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release. In file included from main.c:1:0: io.cuh:12:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘void’ global void transformToGrayKernel(rgb_image *img); ^ makefile:6: recipe for target 'main' failed make: *** [main] Error 1

错误是由关键字 __global__ 引起的,而不是您的内核原型。

那是因为编译器nvcc会解释它的输入文件,并选择相应的规则来编译它。虽然你使用nvcc来编译"main.c",但是它会被当作C源文件,而不是CUDA源代码。所以编译器在编译文件"main.c".

时无法识别CUDA关键字__global__

您可以将文件类型从 "main.c" 更改为 "main.cu",它将被视为 CUDA 源代码。那么编译器就可以识别关键字 __global__.