CUDA 5.0 使用 cmake 单独编译库 [英] CUDA 5.0 separate compilation of library with cmake

查看:39
本文介绍了CUDA 5.0 使用 cmake 单独编译库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 cuda 库的构建时间正在增加,因此我认为 CUDA 5.0 中引入的单独编译可能会对我有所帮助.我不知道如何使用 cmake 实现单独编译.我查看了 NVCC 文档并找到了如何编译设备对象(使用 -dc 选项)以及如何链接它们(使用 -dlink).我尝试使用 cmake 运行它失败了.我正在使用 cmake 2.8.10.2 和 FindCUDA.cmake 的主干头.但是,我不知道如何指定应该编译哪些文件以及如何将它们链接到库中.尤其是 函数的语法(CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS output_file_var cuda_target options object_files source_files) 我不清楚,因为我不知道 output_file_varcuda_target 是什么.这是我尝试的无效结果:

The buildtime of my cuda library is increasing and so I thought that separate compilation introduced in CUDA 5.0 might help me. I couldn't figure out how to achieve separate compilation with cmake. I looked into the NVCC documentation and found how to compile device object (using the -dc option) and how to link them (using the -dlink). My attempts to get it running using cmake failed. I'm using cmake 2.8.10.2 and the head of the trunk of the FindCUDA.cmake. I couldn't however find out how to specify which files should be compiled and how to link them into a library. Especially the syntax of the function(CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS output_file_var cuda_target options object_files source_files) is unclear to me because I don't know what the output_file_var and the cuda_target are. Here the not working results of my attemps:

cuda_compile(DEVICEMANAGER_O devicemanager.cu OPTIONS -dc)
cuda_compile(BLUB_O blub.cu OPTIONS -dc)
CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS(TEST_O gpuacceleration
                                          ""  DEVICEMANGER_O BLUB_O)
set(LIB_TYPE SHARED)
#cuda_add_library(gpuacceleration ${LIB_TYPE} 
  #${gpuacc_SRCS} 
  #devicemanager.cu
  # blub.cu
  #DEVICEMANAGER_O
#  TEST_O
#)

有人知道如何使用 cmake 编译和链接 cuda 库吗?提前致谢.

Does anyone know how to compile and link a cuda library using cmake? Thanks in advance.

在朋友咨询了 FindCUDA.cmake 的开发者后,FindCUDA.cmake 提供的示例中的一个错误得到了修复(https://gforge.sci.utah.edu/gf/project/findcuda/scmsvn/?action=browse&path=%2F结帐em>%2Ftrunk%2FFindCuda.html).我现在可以构建示例.在我的项目中,我可以根据需要使用以下代码构建库(需要 cmake 2.8.10):

After a friend consulted the developer of the FindCUDA.cmake, a bug got fixed in the example provided with FindCUDA.cmake (https://gforge.sci.utah.edu/gf/project/findcuda/scmsvn/?action=browse&path=%2Fcheckout%2Ftrunk%2FFindCuda.html). I'm now able to build the example. In my project I can build the library as needed using the following (cmake 2.8.10 required):

set(LIB_TYPE SHARED)
set(CUDA_SEPARABLE_COMPILATION ON)
cuda_add_library(gpuacceleration ${LIB_TYPE} 
 blub.cu
 blab.cu
)

但是:我无法链接到这个库.当我在没有单独编译的情况下构建 lib 时,我能够链接它.现在收到以下错误:

BUT: I cannot link against this library. When I builded the lib without separate compilation i was able to link against it. Now getting the following error:

 undefined reference to `__cudaRegisterLinkedBinary_53_tmpxft_00005ab4_00000000_6_blub_cpp1_ii_d07d5695'

对于每个具有接口中使用的函数的文件.看起来很奇怪,因为它构建时没有任何警告等.任何想法如何让它工作?

for every file with a function used in the interface. Seems strange since it builds without any warning etc. Any ideas how to get this working?

我终于想出了如何做到这一点.有关详细信息,请参阅@PHD 和我的答案.

I finally figured out how to do this. See @PHD's and my answer for details.

推荐答案

我终于让它运行起来了 ;)

I finally got it running ;)

除了@PHD 的答案和我对它的评论我修改了: set(BUILD_SHARED_LIBS OFF) 在我的 CMakeLists.txt 因为共享库不支持根据nvcc v5.0 page 40手动单独编译.

In Addition to the answer of @PHD and my comment on it I modified: set(BUILD_SHARED_LIBS OFF) in my CMakeLists.txt since shared libs are not supported for separate compilation according to the nvcc manually v5.0 page 40.

除此之外,使用存储库中的最新版本 (1223) 而不是 rev 1221.我联系了开发人员,他修复了一些阻止此问题的问题.这个版本没有正确设置 nvcc -arch=sm_xx 标志,所以我为我的项目手动添加了这个,并通知了 FindCUDA.cmake 的开发人员.所以这可能会在未来得到解决.

In addition to that use the latest rev (1223) from the repository instead of rev 1221. I contacted the developer and he fixed some issue blocking this. This revision doesn't set the nvcc -arch=sm_xx flag correctly, so I added this manually for my project and informed the developer of FindCUDA.cmake. So this might get fixed in the future.

不要忘记获取 cmake > 2.8.10 以使其正常工作.

Don't forget to get cmake > 2.8.10 for this to work.

希望这对我以外的任何人都有帮助;)

Hope this helps anyone but me ;)

这是我的 CMakeLists.txt:

Here is my CMakeLists.txt:

#Required for CUDA-Check
cmake_minimum_required(VERSION 2.8.10)

project(gpulib)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake/cuda" ${CMAKE_MODULE_PATH})
# ============================================
# === Target
# ============================================
file(GLOB_RECURSE gpuacc_SRCS "*.cu")
include_directories(.)

# ---------------------------------------
# Find Cuda
find_package(CUDA REQUIRED)

set(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE ON)

set(BUILD_SHARED_LIBS OFF)

set(CUDA_SEPARABLE_COMPILATION ON)
#list(APPEND CUDA_NVCC_FLAGS -arch=sm_20)

set(LIB_NAME "gpuacceleration")
cuda_add_library(${LIB_NAME}
  ${gpuacc_SRCS} 
  OPTIONS -DSTUFF="blah blah"
  RELEASE -DNDEBUG
  DEBUG -g -DDEBUG
)

set(PUBLIC_HEADERS "myheader1.h;myheader2.h")

INSTALL(FILES ${PUBLIC_HEADERS} DESTINATION include)
INSTALL(FILES "${CMAKE_BINARY_DIR}/src/lib${LIB_NAME}.a" DESTINATION lib)

这不起作用!问题是在主项目中构建可执行文件时链接生成的库时,对所有 cuda 函数(例如 cudaMalloc)都有未定义的引用.

this is not working! The problem is that there are undefined references to all cuda functions (eg. cudaMalloc) when linking the generated library when building a executable in the main project.

还在努力中

这篇关于CUDA 5.0 使用 cmake 单独编译库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆