这是我第一次尝试用 CUDA 实现递归。目标是使用 CUDA 的强大功能从一组字符“12345”中提取所有组合以动态并行化任务。这是我的内核:
__device__ char route[31] = { "_________________________"};
__device__ char init[6] = { "12345" };
__global__ void Recursive(int depth) {
// up to depth 6
if (depth == 5) return;
// newroute = route - idx
int x = depth * 6;
printf("%s\n", route);
int o = 0;
int newlen = 0;
for (int i = 0; i<6; ++i)
{
if (i != threadIdx.x)
{
route[i+x-o] = init[i];
newlen++;
}
else
{
o = 1;
}
} …Run Code Online (Sandbox Code Playgroud)