带有C ++ 11的CUDA 6.0的CMake脚本 [英] CMake script for CUDA 6.0 with C++11

查看:126
本文介绍了带有C ++ 11的CUDA 6.0的CMake脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为使用makefile编译的CUDA 6.0 + Boost 1.55.0 + OpenCV 2.4.9项目开发64位Mac OSX 10.9,但是由于最终我将在64位Windows 8.1上对其进行测试,因此以为我会结识CMake.由于我使用的是C ++ 11,因此我将分别使用clang ++(版本5.1(clang-503.0.40)和gda现在链接到该版本)和CUDA代码(使用nvcc)为C ++代码生成目标文件,然后链接该对象文件一起生成我的makefile中的最终可执行文件.

I am working on a 64-bit Mac OSX 10.9 for my CUDA 6.0 + Boost 1.55.0 + OpenCV 2.4.9 project, compiled using a makefile, but since I will eventually test it on 64-bit Windows 8.1, I thought I'd get acquainted with CMake. Since I am using C++11, I am generating object files for the C++ code with clang++ (version 5.1 (clang-503.0.40), which g++ now links to in mavericks) and CUDA code with nvcc separately, then linking the object files together for the final executable in my makefile.

我不知道如何在CMake中做到这一点.我尝试将CUDA代码编译为静态库(同时使用CMake内置程序和FindCUDA.cmake实用程序),然后将其链接,但这没有用.我无法在线找到将C ++ 11考虑在内的解决方案.

I have no idea how to do this in CMake though. I tried compiling the CUDA code as a static library (using both CMake built-ins and FindCUDA.cmake utilities) and then linking it, but that did not work. I have not been able to find a solution online which takes C++11 into account.

到目前为止,这是我的CMake脚本:

Here is my CMake script so far:

cmake_minimum_required(VERSION 2.8)

project(pupil_tracker)

include_directories(include)
include_directories(include/cuda)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_20, code=sm_20)
#set(CUDA_HOST_COMPILER clang++) # Fails with 'invalid argument '-std=c++11' not allowed with 'C/ObjC''

find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIR})

find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}) # So CMake finds FindOpenCV.cmake
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

CUDA_ADD_LIBRARY(cuda_obj STATIC src/cuda/Tools.cu) # Doesn't seem to work

add_executable(main src/main.cpp src/CenteredHaarFeature.cpp src/PupilTracker.cpp src/Tools.cpp)

target_link_libraries(main ${Boost_LIBRARIES})
target_link_libraries(main ${CUDA_LIBRARIES})
target_link_libraries(main ${OpenCV_LIBS})
target_link_libraries(main ${cuda_obj})

install(TARGETS main DESTINATION ../bin)

如果全部失败,我将不得不尝试在Windows计算机上设置一个MSVC项目,以执行相同的操作.

If all fails, I'll have to try and set up a MSVC project on my Windows machine to do the same.

推荐答案

CMAKE_CXX_FLAGS可能会干扰NVCC编译,因此您必须谨慎放置它们.

The CMAKE_CXX_FLAGS can interfere with the NVCC compilation so you have to be careful about where you put them.

我仅使用CUDA库,通过一个非常简单的示例重新创建了您的设置.重新排列和编辑了一些CMake命令后,我得以编译并运行该程序.

I recreated your setup with a really simple example using only the CUDA library. After rearranging and editing a few of the CMake commands I was able to compile and run the program.

CMakeLists.txt:

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(pupil_tracker)

include_directories(include)
include_directories(include/cuda)

# removed a space and added C++11 functionality (note the double '--' for NVCC)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_20,code=sm_20;--std=c++11)
set(CUDA_HOST_COMPILER clang++) # shouldn't fail anymore

# didn't test with Boost
# find_package(Boost COMPONENTS system filesystem REQUIRED)
# include_directories(${Boost_INCLUDE_DIR})

find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})

# didn't test with OpenCV
# set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}) # So CMake finds FindOpenCV.cmake
# find_package(OpenCV REQUIRED)
# include_directories(${OpenCV_INCLUDE_DIRS})

CUDA_ADD_LIBRARY(cuda_obj STATIC src/cuda/Tools.cu) # works for me now

# moved the CXX flags to after the cuda_add_library call
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
add_executable(main src/main.cpp src/Tools.cpp) # only used one c++ class for this test

# target_link_libraries(main ${Boost_LIBRARIES})
target_link_libraries(main ${CUDA_LIBRARIES})
# target_link_libraries(main ${OpenCV_LIBS})
target_link_libraries(main cuda_obj) # changed ${cuda_obj} to cuda_obj

install(TARGETS main DESTINATION ../bin)

可以从CMake文件中推断项目目录的设置,但为澄清起见,它看起来像:

The project directory setup can be inferred from the CMake file but for clarification it looks like:

ProjectDir:

ProjectDir:

  • CMakeLists.txt
  • 包括
    • Tools.hpp
    • cuda
      • Tools.cuh
      • CMakeLists.txt
      • include
        • Tools.hpp
        • cuda
          • Tools.cuh
          • main.cpp
          • Tools.cpp
          • cuda
            • Tools.cu
            • main.cpp
            • Tools.cpp
            • cuda
              • Tools.cu

              Tools.cuh:

              Tools.cuh:

              #ifndef TOOLS_CUH
              #define TOOLS_CUH
              
              extern "C"
              {
                  void do_cuda_stuff();
              }
              
              #endif
              

              Tools.cu:

              #include <cuda_runtime.h>
              #include <stdio.h>
              
              extern "C"
              {
                  __global__
                  void do_cuda_stuff_kernel()
                  {
                      int idx = blockIdx.x * blockDim.x + threadIdx.x;
                      printf("Hello from thread %d!\n", idx);
                  }
              
                  void do_cuda_stuff()
                  {
                      // 2 blocks, 3 threads each
                      do_cuda_stuff_kernel<<<2, 3>>>();
                      cudaDeviceSynchronize(); // to print results
                  }
              }
              

              Tools.hpp:

              Tools.hpp:

              #ifndef TOOLS_HPP
              #define TOOLS_HPP
              
              class Tools
              {
              public:
                  int execute();
              };
              
              #endif
              

              Tools.cpp:

              Tools.cpp:

              #include "Tools.hpp"
              #include "Tools.cuh"
              
              int Tools::execute()
              {
                  do_cuda_stuff();
                  return 0;
              }
              

              main.cpp:

              #include "Tools.hpp"
              
              int main(void)
              {
                  Tools tools;
                  return tools.execute();
              }
              

              输出:

              Hello from thread 3!
              Hello from thread 4!
              Hello from thread 5!
              Hello from thread 0!
              Hello from thread 1!
              Hello from thread 2!
              

              我目前正在使用CUDA 7.5和OSX 10.10,但我认为最大的问题是CMAKE_CXX_FLAGS变量的位置.希望这可以帮助!干杯!

              I'm currently using CUDA 7.5 and OSX 10.10 but I think the biggest issue was the placement of the CMAKE_CXX_FLAGS variable. Hope this helps! Cheers!

              这篇关于带有C ++ 11的CUDA 6.0的CMake脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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