【发布时间】:2016-05-19 04:34:03
【问题描述】:
我在正确编译包含动态并行性的 CUDA 代码时遇到问题。 问题是编译和链接没有错误,但是生成的文件是无效的可执行文件。
配置:
特斯拉 K40、Ubuntu 14.04 LTS、CUDA 7.5
编译命令:
nvcc -o cdp -rdc=true -dc -dlink -arch=sm_35 cdp.cu -lcudadevrt
代码:
#include <iostream>
#include <cuda_runtime.h>
using namespace std;
__global__ void kernel_find(int* data, int count, int value, int* index)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx<count)
{
bool exists = (data[idx] == value);
if(exists)
atomicMin(index, idx);
}
}
__host__ __device__ int find_device(int* data, int count, int value)
{
int* idx = new int;
(*idx) = count;
dim3 block(8);
dim3 grid((count + block.x - 1)/block.x);
kernel_find<<<grid, block>>>(data, count, value, idx);
cudaDeviceSynchronize();
int retval = *idx;
delete idx;
return retval;
}
__global__ void kernel_find_bulk(int* data, int count, const int* toFind, int* foundIndices, int toFindCount)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if(idx<toFindCount)
{
int val = toFind[idx];
int foundIndex = find_device(data, count, val);
foundIndices[idx] = foundIndex;
}
}
int main()
{
const int count = 100, toFindCount = 10;
int *data, *toFind, *foundIndices;
cudaMallocManaged(&data, count * sizeof(int));
cudaMallocManaged(&toFind, toFindCount * sizeof(int));
cudaMallocManaged(&foundIndices, toFindCount * sizeof(int));
for(int i=0; i<count; i++)
{
data[i] = rand() % 30;
}
for(int i=0; i<toFindCount; i++)
{
toFind[i] = i;
}
dim3 block(8);
dim3 grid((toFindCount + block.x - 1)/block.x);
kernel_find_bulk<<<grid, block>>>(data, count, toFind, foundIndices, toFindCount);
cudaDeviceSynchronize();
for(int i=0; i<toFindCount; i++)
{
if(foundIndices[i] < count)
{
cout<<toFind[i]<<" found at index "<<foundIndices[i]<<endl;
}
else
{
cout<<toFind[i]<<" not found"<<endl;
}
}
return 0;
}
如果我尝试运行可执行文件,我会收到 Permission denied 错误。如果使用chmod强制更改权限,则错误变为cannot execute binary file: Exec format error。
我无法找出解决方案,因为 CUDA 动态并行示例运行良好,而没有动态并行的 CUDA 程序也运行良好。任何帮助将不胜感激。
file 命令的输出:
cdp:ELF 64 位 LSB 可重定位,x86-64,版本 1 (SYSV),不是 剥离
objdump -f 命令的输出:
cdp:文件格式elf64-x86-64 架构:i386:x86-64,标志 0x00000011: HAS_RELOC,HAS_SYMS 起始地址 0x0000000000000000
【问题讨论】:
-
我认为你不能在一个命令中完成设备编译、设备链接和主机链接。
file或objdump告诉您有关发出的编译器输出的信息。我猜它是一个设备精灵目标文件。 -
@talonmies... 添加了两个命令的输出。我必须单独编译和链接吗?
-
设备编译、设备链接和最终链接可以在一个命令中执行。正如@talonmies 的回答所表明的那样,正确的命令行开关是
-rdc=true(仅限)。-dc在很多方面类似于-c开关。如果您指定,您将获得一个设备编译only,而不管指定的其他开关。这会创建一个不可执行的对象,这是您在问题中概述的一些困难的根源(-o cdp进一步使事情变得混乱。)请注意,可以使用这种方法指定多个源文件和/或对象(单个命令)。 -
@RobertCrovella... 谢谢您的意见。最终,我最终进行了单独的编译。