【发布时间】:2023-04-01 21:33:01
【问题描述】:
示例代码:https://github.com/Saitama10000/Mixing-cuda-and-cpp-templates-and-lambdas
- 我想在
.cu文件中拥有一个内核,该文件将扩展的__host__ __device__lambda 作为参数并使用它来操作数据。 - 我正在使用
.cuh文件将内核执行包装在包装函数中。 - 我将
.cuh文件包含在main.cpp中,并使用包装函数进行计算。 - 我需要这种
.cuh, .cu类型的代码组织 - 我使用的是 c++20
示例代码无法编译。我应该在.cu 文件中添加一个模板实例化,但我不知道怎么做。我试过这个:
typedef float(*op)(float);
template std::vector<float> f<op>(std::vector<float> const&, op);
但我仍然收到此编译错误:
In file included from Mixing-cuda-and-cpp-templates-and-lambdas/main.cpp:6:
Mixing-cuda-and-cpp-templates-and-lambdas/kernel.cuh:6:20: error: ‘std::vector<float> f(const std::vector<float>&, FUNC) [with FUNC = main()::<lambda(float)>]’, declared using local type ‘main()::<lambda(float)>’, is used but never defined [-fpermissive]
6 | std::vector<float> f(std::vector<float> const& a, FUNC func);
| ^
Mixing-cuda-and-cpp-templates-and-lambdas/kernel.cuh:6:20: warning: ‘std::vector<float> f(const std::vector<float>&, FUNC) [with FUNC = main()::<lambda(float)>]’ used but never defined
make[2]: *** [CMakeFiles/main.dir/build.make:82: CMakeFiles/main.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
【问题讨论】:
-
为什么不把内核和包装函数放到
.cuh文件中呢? -
因为我得到:
In file included from Mixing-cuda-and-cpp-templates-and-lambdas/main.cpp:8: Mixing-cuda-and-cpp-templates-and-lambdas/kernel.cuh: In function ‘std::vector<float> wrapper(const std::vector<float>&, FUNC)’: Mixing-cuda-and-cpp-templates-and-lambdas/kernel.cuh:35:13: error: expected primary-expression before ‘<’ token 35 | kernel<<<256, 256>>>(da, db, a.size(), func); | ^ Mixing-cuda-and-cpp-templates-and-lambdas/kernel.cuh:35:24: error: expected primary-expression before ‘>’ token 35 | kernel<<<256, 256>>>(da, db, a.size(), func); -
你用 CUDA 编译器编译 main.cpp 吗?
-
不,我不是。如果我这样做,它适用于这个示例,但对于其他较大的项目,它会给出奇怪的错误。