"exited with code 255" 尝试在 __global__ 函数中调用 __device__ 函数时
"exited with code 255" when trying to call __device__ function within __global__ function
我有以下 test.hpp 声明 test()
:
#pragma once
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
__host__ __device__ void test();
和 test.cpp 定义 test()
:
#include "test.hpp"
__host__ __device__ void test() { }
以下 kernel.cu 编译失败(退出代码 255,没有其他信息):
#include "test.hpp"
__global__
void gpu(int x)
{
test(); // compiles just fine if I comment out this line
}
int main()
{
// can be called multiple times from host with no problems
test();
test();
test();
return 0;
}
如评论所述,如果我从 gpu
函数中删除 test()
调用,则代码会编译并运行而不会出错。
这是为什么?我该如何解决?
编辑:
值得一提的是,我的环境和编译命令都是正确的,我成功地编译了许多示例项目。
@Robert Crovella 的评论让我走上了解决这个问题的正确轨道。
我将 test.cpp
移动到 test.cu
,并将 test.hpp
移动到 test.cuh
。
然后,我能够按照以下答案启用可分离的编译和设备代码链接:
我有以下 test.hpp 声明 test()
:
#pragma once
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
__host__ __device__ void test();
和 test.cpp 定义 test()
:
#include "test.hpp"
__host__ __device__ void test() { }
以下 kernel.cu 编译失败(退出代码 255,没有其他信息):
#include "test.hpp"
__global__
void gpu(int x)
{
test(); // compiles just fine if I comment out this line
}
int main()
{
// can be called multiple times from host with no problems
test();
test();
test();
return 0;
}
如评论所述,如果我从 gpu
函数中删除 test()
调用,则代码会编译并运行而不会出错。
这是为什么?我该如何解决?
编辑: 值得一提的是,我的环境和编译命令都是正确的,我成功地编译了许多示例项目。
@Robert Crovella 的评论让我走上了解决这个问题的正确轨道。
我将 test.cpp
移动到 test.cu
,并将 test.hpp
移动到 test.cuh
。
然后,我能够按照以下答案启用可分离的编译和设备代码链接: