使用Cmake进行简单的CUDA程序

时间:2017-08-23 12:48:19

标签: c++ cmake cuda

我最近在处理C ++ / CUDA。 我正在学习这个简单的例子:

## this is the kernel build file - a CUDA lib emerges from this

option(GPU "Build gpu-lisica" OFF)
# use -DGPU=ON to compile this

if(GPU)

  find_package(CUDA QUIET)

  if(CUDA_FOUND)

    include_directories(${CUDA_INCLUDE_DIRS})
    message(STATUS "CUDA detected -- LiSiCa library")
    set(CUDA_PROPAGATE_HOST_FLAGS ON)
    set(CUDA_SEPARABLE_COMPILATION OFF)
    list(APPEND CUDA_NVCC_FLAGS "-arch=sm_20;-O2;-DVERBOSE")       

    # build static library
    # CUDA_ADD_LIBRARY(lisica_kernel_lib ${KERNELS}  STATIC)

    CUDA_ADD_EXECUTABLE(demo test.cu)

    cuda_add_executable(
    hellocuda
    test.cu)

    message(STATUS "File setup seems ok.")

  else()

    message(STATUS "NO CUDA DETECTED! GPU-based compilation canceled.")

  endif()

endif()

和.cu(这是直接来自nvidia示例网站)。我故意使用适用于裸nvcc的东西(例如nvcc test.cu -o test works)。我的问题是,我用cuda代码做错了什么来接收这些错误?

#include "stdafx.h" 
#include <stdio.h>
#include <cuda.h>

// Kernel that executes on the CUDA device
__global__ void square_array(float *a, int N)
{
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx<N) a[idx] = a[idx] * a[idx];
}

// main routine that executes on the host
int main(void)
{
  float *a_h, *a_d;  // Pointer to host & device arrays
  const int N = 10;  // Number of elements in arrays
  size_t size = N * sizeof(float);
  a_h = (float *)malloc(size);        // Allocate array on host
  cudaMalloc((void **) &a_d, size);   // Allocate array on device
  // Initialize host array and copy it to CUDA device
  for (int i=0; i<N; i++) a_h[i] = (float)i;
  cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice);
  // Do calculation on device:
  int block_size = 4;
  int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
  square_array <<< n_blocks, block_size >>> (a_d, N);
  // Retrieve result from device and store it in host array
  cudaMemcpy(a_h, a_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
  // Print results


or (int i=0; i<N; i++) printf("%d %f\n", i, a_h[i]);
  // Cleanup
  free(a_h); cudaFree(a_d);
}

运行makefile时,我得到:

[ 33%] Building NVCC (Device) object lib/gpu_kernels/CMakeFiles/demo.dir//./demo_generated_test.cu.o
/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: identifier "nullptr" is undefined

/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: expected a ";"

/usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h(190): error: expected a ";"

/usr/include/c++/4.8/exception(63): error: expected a ";"

.....

错误的样式,我不明白,好像我只是尝试nvcc test.cu -o test,它有效。

编辑:make VERBOSE = ON run:

Re-run cmake file: Makefile older than: lib/gpu_kernels/CMakeFiles/demo.dir/demo_generated_test.cu.o.depend
-- Boost version: 1.54.0
-- Found the following Boost libraries:
--   date_time
--   filesystem
--   system
--   regex
--   iostreams
-- GSL using gsl-config /usr/bin/gsl-config
-- Using GSL from /usr
-- :::OPENMM_INCLUDE_DIR:         /home/user/project/foreign/openmm/include
-- :::OPENMM_LIBRARY:         /home/user/project/foreign/openmm/lib/libOpenMM_static.a
-- :::PROBIS_INCLUDE_DIR:         /home/user/project/foreign/probis/include
-- :::PROBIS_LIBRARY:         /home/user/project/foreign/probis/lib/libProBiS_static.a
-- CUDA detected 
-- File setup seems ok.
-- Boost version: 1.54.0
-- Found the following Boost libraries:
--   regex
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/project/debug
cd /home/user/project/debug && /usr/bin/cmake -E cmake_progress_start /home/user/project/debug/CMakeFiles /home/user/project/debug/lib/gpu_kernels/CMakeFiles/progress.marks
cd /home/user/project/debug && make -f CMakeFiles/Makefile2 lib/gpu_kernels/all
make[1]: Entering directory `/home/user/project/debug'
make -f lib/gpu_kernels/CMakeFiles/demo.dir/build.make lib/gpu_kernels/CMakeFiles/demo.dir/depend
make[2]: Entering directory `/home/user/project/debug'
/usr/bin/cmake -E cmake_progress_report /home/user/project/debug/CMakeFiles 7
[ 20%] Building NVCC (Device) object lib/gpu_kernels/CMakeFiles/demo.dir//./demo_generated_test.cu.o
cd /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir && /usr/bin/cmake -E make_directory /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//.
cd /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir && /usr/bin/cmake -D verbose:BOOL=ON -D build_configuration:STRING=Debug -D generated_file:STRING=/home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//./demo_generated_test.cu.o -D generated_cubin_file:STRING=/home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//./demo_generated_test.cu.o.cubin.txt -P /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.cmake
-- Removing /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//./demo_generated_test.cu.o
/usr/bin/cmake -E remove /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//./demo_generated_test.cu.o
-- Generating dependency file: /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.NVCC-depend
/usr/bin/nvcc -M -D__CUDACC__ /home/user/project/lib/gpu_kernels/test.cu -o /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.NVCC-depend -ccbin /usr/bin/cc -m64 -Xcompiler ,\"-std=c++11\",\"-DNDEBUG\" -arch=sm_20 -O2 -DVERBOSE -DNVCC -I/usr/include -I/usr/include -I/home/user/project/foreign/openmm/include -I/home/user/project/foreign/probis/include -I/home/user/project/lib -I/home/user/project/data -I/home/user/project/debug
-- Generating temporary cmake readable file: /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.depend.tmp
/usr/bin/cmake -D input_file:FILEPATH=/home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.NVCC-depend -D output_file:FILEPATH=/home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.depend.tmp -P /usr/share/cmake-2.8/Modules/FindCUDA/make2cmake.cmake
-- Copy if different /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.depend.tmp to /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.depend
/usr/bin/cmake -E copy_if_different /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.depend.tmp /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.depend
-- Removing /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.depend.tmp and /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.NVCC-depend
/usr/bin/cmake -E remove /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.depend.tmp /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//demo_generated_test.cu.o.NVCC-depend
-- Generating /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//./demo_generated_test.cu.o
/usr/bin/nvcc /home/user/project/lib/gpu_kernels/test.cu -dc -o /home/user/project/debug/lib/gpu_kernels/CMakeFiles/demo.dir//./demo_generated_test.cu.o -ccbin /usr/bin/cc -m64 -Xcompiler ,\"-std=c++11\",\"-DNDEBUG\" -arch=sm_20 -O2 -DVERBOSE -DNVCC -I/usr/include -I/usr/include -I/home/user/project/foreign/openmm/include -I/home/user/project/foreign/probis/include -I/home/user/project/lib -I/home/user/project/data -I/home/user/project/debug
/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: identifier "nullptr" is undefined

/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: expected a ";"

/usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h(190): error: expected a ";"

/usr/include/c++/4.8/exception(63): error: expected a ";"

/usr/include/c++/4.8/exception(68): error: expected a ";"

/usr/include/c++/4.8/exception(76): error: expected a ";"

/usr/include/c++/4.8/exception(83): error: expected a ";"

/usr/include/c++/4.8/exception(93): error: expected a "{"

/usr/include/c++/4.8/bits/exception_ptr.h(64): error: function "std::current_exception" returns incomplete type "std::__exception_ptr::exception_ptr"

/usr/include/c++/4.8/bits/exception_ptr.h(64): error: expected a "{"

/usr/include/c++/4.8/bits/exception_ptr.h(79): error: expected a ";"

/usr/include/c++/4.8/bits/exception_ptr.h(81): error: expected a ";"

/usr/include/c++/4.8/bits/exception_ptr.h(82): error: expected a ";"

...and so on ...

它似乎试图包含一些其他项目并使用nvcc编译它们,因此错误?

附加信息:gcc版本:

gcc --version
gcc (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

1 个答案:

答案 0 :(得分:1)

问题是,我的cmake项目在构建树中设置了一大堆CXX标志,我需要取消设置才能使其正常工作。