gpt4 book ai didi

c++ - 使用 Cmake 编写简单的 CUDA 程序

转载 作者:行者123 更新时间:2023-11-28 05:01:26 26 4
gpt4 key购买 nike

我最近一直在处理 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 一样,它起作用了。

编辑:让 VERBOSE=ON 运行:

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.

最佳答案

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

关于c++ - 使用 Cmake 编写简单的 CUDA 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45840202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com