【问题标题】:Error compiling a cuda project编译 cuda 项目时出错
【发布时间】:2014-04-25 12:45:32
【问题描述】:

我在使用 C Cuda 和 lodepng 库编译 cuda 项目时遇到了一些问题。

我的 makefile 看起来像这样。

gpu:    super-resolution.cu
    gcc -g -O -c lodepng.c
    nvcc -c super-resolution.cu
    nvcc -o super-resolution-cuda super-resolution.o 
    rm -rf super-resolution.o
    rm -rf lodepng.o

谁能告诉我我做错了什么,因为它在抱怨

nvcc warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release.
super-resolution.o: In function `main':
parallel-algorithm/super-resolution.cu:238: undefined reference to `lodepng_decode32_file(unsigned char**, unsigned int*, unsigned int*, char const*)'
parallel-algorithm/super-resolution.cu:259: undefined reference to `lodepng_encode32_file(char const*, unsigned char const*, unsigned int, unsigned int)'
parallel-algorithm/super-resolution.cu:269: undefined reference to `lodepng_encode32_file(char const*, unsigned char const*, unsigned int, unsigned int)'
parallel-algorithm/super-resolution.cu:282: undefined reference to `lodepng_encode32_file(char const*, unsigned char const*, unsigned int, unsigned int)'
parallel-algorithm/super-resolution.cu:292: undefined reference to `lodepng_encode32_file(char const*, unsigned char const*, unsigned int, unsigned int)'
parallel-algorithm/super-resolution.cu:301: undefined reference to `lodepng_encode32_file(char const*, unsigned char const*, unsigned int, unsigned int)'
...

我只需要一种方法来编译我的 .cu 文件并在编译过程中使用 nvcc 添加一个 C .o 文件。

编辑:尝试过的建议。没有成功。

gcc -g -O -c lodepng.c
nvcc -c super-resolution.cu
nvcc warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release.
super-resolution.cu:1:2: warning: #import is a deprecated GCC extension [-Wdeprecated]
 #import "cuda.h"
  ^
super-resolution.cu(106): warning: expression has no effect

super-resolution.cu(116): warning: expression has no effect

super-resolution.cu(141): warning: variable "y" was declared but never referenced

super-resolution.cu:1:2: warning: #import is a deprecated GCC extension [-Wdeprecated]
 #import "cuda.h"
  ^
super-resolution.cu(106): warning: expression has no effect

super-resolution.cu(116): warning: expression has no effect

super-resolution.cu(141): warning: variable "y" was declared but never referenced

ptxas /tmp/tmpxft_00000851_00000000-5_super-resolution.ptx, line 197; warning : Double is not supported. Demoting to float
nvcc -o super-resolution-cuda super-resolution.o lodepng.o

nvcc warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release.
super-resolution.o: In function `main':
tmpxft_00000851_00000000-3_super-resolution.cudafe1.cpp:(.text+0x5d): undefined reference to `lodepng_decode32_file(unsigned char**, unsigned int*, unsigned int*, char const*)'

它仍然找不到对目标文件的引用。 编辑:这是我们的 .cu 文件。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cstdio>

extern "C" unsigned lodepng_encode32_file(const char* ,const unsigned char* , unsigned , unsigned h);
extern "C" unsigned lodepng_decode32_file(unsigned char** , unsigned* , unsigned* ,const char* );

【问题讨论】:

  • 您正在与nvcc 链接,但不包括您使用 gcc (lodepng.o) 构建的对象。尝试使用nvcc -o super-resolution-cuda super-resolution.o lodepng.o 代替现有的nvcc -o super-resolution-cuda super-resolution.o 链接步骤。
  • 所以lodepng_encode32_file 参考得到了整理,但lodepng_decode32_file 参考没有?可能需要查看确切的代码,然后才能理解为什么以及是否正确地进行 C/C++ 链接(例如 extern C 等)。您确定 lodepng_encode32_filelodepng_decode32_file 都以相同的方式导出和使用吗?
  • 认真的吗?您要在 cmets 中发布这些更新吗? lodepng中的对应函数怎么样?他们的原型匹配吗?我不确定这个问题可以用小sn-ps来回答。在演示问题的问题中创建一个完整、简短、可编译的案例。是的,您需要大量编辑文件,这需要您付出努力。但是,将 2 个文件编辑到显示问题的关键部分应该不难。

标签: c cuda nvcc


【解决方案1】:
  1. 不要#import。如果你想包含cuda.h(这应该是不必要的)然后使用#include。相反,我会从您的 super-resolution.cu 文件中删除该行。
  2. 您之前没有显示但现在很明显的是,在您的 super-resolution.cu 中,您包含 lodepng.h 稍后为 2 个函数指定 C 链接:lodepng_decode32_filelodepng_encode32_file。当我尝试编译你的super-resolution.cu 时,编译器给了我这样的错误(我不知道你为什么看不到它们):

    super-resolution.cu(8): error: linkage specification is incompatible with previous "lodepng_encode32_file"
    lodepng.h(184): here
    
    super-resolution.cu(9): error: linkage specification is incompatible with previous "lodepng_decode32_file"
    lodepng.h(134): here
    

    所以基本上你正在绊倒 C 和 C++ 链接。

  3. 我相信最简单的解决方案是使用lodepng.cpp(而不是lodepng.c),从您的super-resolution.cu 中删除以下行:

    extern "C" unsigned lodepng_encode32_file(const char* ,const unsigned char* , unsigned , unsigned h);
    extern "C" unsigned lodepng_decode32_file(unsigned char** , unsigned* , unsigned* ,const char* );
    

    只需编译所有内容并链接所有 c++ 样式:

    $ g++ -c lodepng.cpp
    $ nvcc -c super-resolution.cu
    nvcc warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release.
    $ nvcc -o super-resolution super-resolution.o lodepng.o
    nvcc warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release.
    $
    
  4. 1234563在我看来,这会变得一团糟。
  5. 如果您想摆脱有关 sm_10 的警告,请添加 nvcc 开关以针对不同的架构进行编译,例如:

    nvcc -arch=sm_20 ...
    

    但请确保您选择的任何内容都与您的 GPU 兼容。

【讨论】:

  • 你不需要改成lodepng.cpp,如果代码是C那么你应该使用C编译器。只需理顺联动规范即可。
【解决方案2】:

这是一个简单的sn-p代码。

lodepng 库可以从这里获得 (http://lodev.org/lodepng/)。

将其重命名为 C 将使其在 C 上可用。

即使在这个级别,也存在编译问题

"undefined reference to `lodepng_decode32_file'"
"undefined reference to `lodepng_encode32_file'"

文件:生成文件

all:    gpu
    gcc -g -O -c lodepng.c
    nvcc -c super-resolution.cu
    nvcc -o super-resolution-cuda super-resolution.o lodepng.o
    rm -rf super-resolution.o
    rm -rf lodepng.o

文件:super-resolution.cu

#import "cuda.h"
#include "lodepng.h"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cstdio>

extern "C" unsigned lodepng_encode32_file(const char* ,const unsigned char* , unsigned , unsigned h);
extern "C" unsigned lodepng_decode32_file(unsigned char** , unsigned* , unsigned* ,const char* );

//GPU 3x3 Blur.
__global__ void gpuBlur(unsigned char* image, unsigned char* buffer, int width, int height)
{
    int i = threadIdx.x%width;
    int j = threadIdx.x/width;
    if (i == 0 || j == 0 || i == width - 1 || j == height - 1)
        return;

    int k;
    for (k = 0; k <= 4; k++)
    {
        buffer[4*width*j + 4*i + k] =           (image[4*width*(j-1) + 4*(i-1) + k] +
                                image[4*width*(j-1) + 4*i + k] +
                                image[4*width*(j-1) + 4*(i+1) + k] +
                                image[4*width*j + 4*(i-1) + k] +
                                image[4*width*j + 4*i + k] +
                                image[4*width*j + 4*(i+1) + k] +
                                image[4*width*(j+1) + 4*(i-1) + k] +
                                image[4*width*(j+1) + 4*i + k] +
                                image[4*width*(j+1) + 4*(i+1) + k])/9;
    }
}

int main(int argc, char *argv[])
{
    //Items for image processing;
    //int threshold = 100;
    unsigned int error;
    unsigned char* image;
    unsigned int width, height;

    //Load the image;
    if (argc > 1)
    {
        error = lodepng_decode32_file(&image, &width, &height, argv[1]);
        printf("Loaded file: %s[%d]\n", argv[1], error);
    }
    else
    {

        return 0;
    }

    unsigned char* buffer =(unsigned char*)malloc(sizeof(char) * 4*width*height);

    //GPU Blur Section.
    unsigned char* image_gpu;
    unsigned char* blur_gpu;
    cudaMalloc( (void**) &image_gpu, sizeof(char) * 4*width*height);
    cudaMalloc( (void**) &blur_gpu, sizeof(char) * 4*width*height);
    cudaMemcpy(image_gpu,image, sizeof(char) * 4*width*height, cudaMemcpyHostToDevice);
    cudaMemcpy(blur_gpu,image, sizeof(char) * 4*width*height, cudaMemcpyHostToDevice);
    gpuBlur<<< 1, height*width >>> (image_gpu, blur_gpu, width, height);
    cudaMemcpy(buffer, blur_gpu, sizeof(char) * 4*width*height, cudaMemcpyDeviceToHost);
    //Spit out buffer as an image.
    error = lodepng_encode32_file("GPU_OUTPUT1_Blur.png", buffer, width, height);
    cudaFree(image_gpu);
    cudaFree(blur_gpu);

    free(buffer);
    free(image);

}

【讨论】:

  • 最好将此类材料编辑到您的问题中。不要在 SO 上发布答案,除非您真的回答该问题(即使这是您自己的问题)。
猜你喜欢
  • 2013-03-05
  • 2013-08-16
  • 2017-01-07
  • 2013-06-10
  • 1970-01-01
  • 2016-03-20
  • 2018-12-30
  • 1970-01-01
相关资源
最近更新 更多