CUDA和gcc兼容性问题

8

我收到了这个错误

/usr/local/cuda-5.0/bin/../include/host_config.h:82:2: error: #error -- 不支持的GNU版本!不支持gcc 4.7及以上版本!make:* [src/Throughput.o] Error 1

在host_config.h中,他们保证兼容性最高到4.6版本。

#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)

#error -- unsupported GNU version! gcc 4.7 and up are not supported!

我同时拥有4.6和4.7版本的gcc。
在互联网上找到的建议是在cuda bin目录中添加指向gcc-4.6的链接。
我遵循了这个建议:
elect@elect-desktop:/usr/local/cuda-5.0/bin$ sudo ln -s /usr/bin/gcc-4.6 gcc
但是出现了另一个错误。
**** Build of configuration Debug for project Throughput ****

make all 
Building file: ../src/Throughput.cu
Invoking: NVCC Compiler
nvcc -G -g -O0 -gencode arch=compute_20,code=sm_20 -odir "src" -M -o "src/Throughput.d"     "../src/Throughput.cu"
gcc: error trying to exec 'cc1plus': execvp: No such file or directory
make: *** [src/Throughput.o] Error 1

**** Build Finished ****

重新搜索后,我没有找到明确的情况(如gcc降级等)。
因此,我在这里询问现在的问题,因为CUDA应该与gcc-4.6兼容...
我的系统:
Ubuntu 12.10 64位
cuda_5.0.35_linux_64_ubuntu11.10-1
这是我目前正在尝试编译的教程代码。
/**
 * Copyright 1993-2012 NVIDIA Corporation.  All rights reserved.
 *
 * Please refer to the NVIDIA end user license agreement (EULA) associated
 * with this source code for terms and conditions that govern your use of
 * this software. Any use, reproduction, disclosure, or distribution of
 * this software and related documentation outside the terms of the EULA
 * is strictly prohibited.
 */
#include <stdio.h>
#include <stdlib.h>

static const int WORK_SIZE = 256;

/**
 * This macro checks return value of the CUDA runtime call and exits
 * the application if the call failed.
 */
#define CUDA_CHECK_RETURN(value) {                                          \
    cudaError_t _m_cudaStat = value;                                        \
    if (_m_cudaStat != cudaSuccess) {                                       \
        fprintf(stderr, "Error %s at line %d in file %s\n",                 \
                cudaGetErrorString(_m_cudaStat), __LINE__, __FILE__);       \
        exit(1);                                                            \
    } }

__device__ unsigned int bitreverse(unsigned int number) {
    number = ((0xf0f0f0f0 & number) >> 4) | ((0x0f0f0f0f & number) << 4);
    number = ((0xcccccccc & number) >> 2) | ((0x33333333 & number) << 2);
    number = ((0xaaaaaaaa & number) >> 1) | ((0x55555555 & number) << 1);
    return number;
}

/**
 * CUDA kernel function that reverses the order of bits in each element of the array.
 */
__global__ void bitreverse(void *data) {
    unsigned int *idata = (unsigned int*) data;
    idata[threadIdx.x] = bitreverse(idata[threadIdx.x]);
}

/**
 * Host function that prepares data array and passes it to the CUDA kernel.
 */
int main(void) {
    void *d = NULL;
    int i;
    unsigned int idata[WORK_SIZE], odata[WORK_SIZE];

    for (i = 0; i < WORK_SIZE; i++)
        idata[i] = (unsigned int) i;

    CUDA_CHECK_RETURN(cudaMalloc((void**) &d, sizeof(int) * WORK_SIZE));

    CUDA_CHECK_RETURN(cudaMemcpy(d, idata, sizeof(int) * WORK_SIZE, cudaMemcpyHostToDevice));

    bitreverse<<<1, WORK_SIZE, WORK_SIZE * sizeof(int)>>>(d);

    CUDA_CHECK_RETURN(cudaThreadSynchronize());
    // Wait for the GPU launched work to complete
    CUDA_CHECK_RETURN(cudaGetLastError());
    CUDA_CHECK_RETURN(cudaMemcpy(odata, d, sizeof(int) * WORK_SIZE, cudaMemcpyDeviceToHost));

    for (i = 0; i < WORK_SIZE; i++)
        printf("Input value: %u, device output: %u\n", idata[i], odata[i]);

    CUDA_CHECK_RETURN(cudaFree((void*) d));
    CUDA_CHECK_RETURN(cudaDeviceReset());

    return 0;
}

除了CUDA,你是在处理纯C代码吗?还是可能是C++? - Bart
@bart:nvcc需要一个可用的支持C++编译器。 - talonmies
@Bart 目前是纯C语言(我添加了一些额外的代码) - elect
@talonmies 啊,那不就是问题所在吗?是 g++ 的问题(或缺失)吗? - Bart
那我不应该将链接添加到gcc吗? - elect
2个回答

6
问题源于CUDA工具链无法找到有效的C++编译器。 nvcc 只是一个编译器驱动程序,它需要一个可用的C++编译器来编译任何代码。
做这件事情最正确的方法是[请注意您正在使用不受支持的Linux版本,请自行承担风险],建立一个本地目录,其中包含链接到支持的编译器套件(这意味着匹配、支持的gcc和g++版本),并在编译时通过--compiler-bindir参数传递给nvcc。例如:
$ ls -l $HOME/cuda/bin
total 16
lrwxr-xr-x  1 talonmies  koti  16 Feb  9 12:41 g++ -> /usr/bin/g++-4.2
lrwxr-xr-x  1 talonmies  koti  16 Feb  9 12:41 gcc -> /usr/bin/gcc-4.2

这里我有一组链接指向一个支持的编译器。然后我可以这样进行编译:

$ nvcc --compiler-bindir=$HOME/cuda/bin -c -arch=sm_12 -Xptxas="-v" nanobench.cu 
ptxas info    : 0 bytes gmem
ptxas info    : Compiling entry function '_Z5benchIfLi128000EEvPjPT_i' for 'sm_12'
ptxas info    : Used 5 registers, 28 bytes smem, 12 bytes cmem[1]

这可能是使用替代编译器的最安全和最少侵入性的方式,特别是当系统编译器不受支持时。


好的,但是如果它说gcc-4.6兼容,而我已经有了gcc-4.6,那么我只是缺少g++-4.6,对吗? - elect
没错,已经解决了。我之前缺少g++-4.6的链接,然后我安装了它,并在/cuda-5.0/bin/g++中创建了一个到/usr/bin/g++-4.6的链接。谢谢talonmies。 - elect
顺便问一下,您能否确认一下原始的g++和gcc链接分别指向/usr/bin/g++和/usr/bin/gcc吗? - elect
@elect:我不明白你在问什么原始链接。 - talonmies
对不起,我说原始链接是因为在匆忙让它工作的过程中,我没有使用保存链接的目录,而是覆盖了cuda-5.0/bin/中的原始链接。 - elect

3

作为其他地方发现的内容:

su -c 'update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 10'  
sudo update-alternatives --config gcc

对我有用。不过我正在编译CudaMiner。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接