1. 参考博客:
1. 【CUDA】CUDA编译:https://blog.csdn.net/u011285477/article/details/104408296
2. 项目文件夹结构:
c_cuda/
cuda/
CMakeLists.txt
foo.cu
foo.cuh
CMakeLists.txt
main.cpp
3. 完整代码:
1. c_cuda/CMakeLists.txt:
# CMakeLists.txt for G4CU project
project(project)
# required cmake version
cmake_minimum_required(VERSION 2.8)
add_subdirectory(cuda)
set (EXTRA_LIBS ${EXTRA_LIBS} gpu)
ADD_EXECUTABLE(project main.cpp)
target_link_libraries (project ${EXTRA_LIBS})
2. c_cuda/main.cpp:
#include <stdio.h>
#include <iostream>
extern "C" void useCUDA();
int main()
{
std::cout<<"Using C++"<<std::endl;
useCUDA();
return 0;
}
3. c_cuda/cuda/CMakeLists.txt:
# CMakeLists.txt for G4CU project
project(gpu)
# required cmake version
cmake_minimum_required(VERSION 2.8)
# packages
find_package(CUDA)
#include_directories ("${PROJECT_SOURCE_DIR}")
# nvcc flags
# set(CUDA_NVCC_FLAGS -O3;-G;-g;-allow-unsupported-compiler)
set(CUDA_NVCC_FLAGS -G;-g;-allow-unsupported-compiler)
#set(CUDA_NVCC_FLAGS -gencode arch=compute_20,code=sm_20;-G;-g)
#set(CUDA_NVCC_FLAGS -gencode arch=compute_52,code=sm_52;-G;-g)
file(GLOB_RECURSE CURRENT_HEADERS *.h *.hpp *.cuh)
file(GLOB CURRENT_SOURCES *.cpp *.cu)
source_group("Include" FILES ${CURRENT_HEADERS})
source_group("Source" FILES ${CURRENT_SOURCES})
#cuda_add_library(gpu SHARED ${CURRENT_HEADERS} ${CURRENT_SOURCES})
cuda_add_library(gpu STATIC ${CURRENT_HEADERS} ${CURRENT_SOURCES})
4. c_cuda/cuda/foo.cu:
#include "foo.cuh"
#define CHECK(res) { if(res != cudaSuccess){printf("Error :%s:%d , ", __FILE__,__LINE__); \
printf("code : %d , reason : %s \n", res,cudaGetErrorString(res));exit(-1);}}
__global__ void foo()
{
printf("CUDA!\n");
}
void useCUDA()
{
foo<<<1,5>>>();
CHECK(cudaDeviceSynchronize());
}
5. c_cuda/cuda/foo.cuh:
#ifndef FOO_CUH
#define FOO_CUH
#include <stdio.h>
extern "C"
void useCUDA();
#endif
4. 使用 Visual Studio 2022 打开 c_cuda 工程:
1. 双击桌面图标 "Visual Studio 2022"
2. 点击 "继续但无需代码( W ) ->"
3. 点击左上角 "文件" --》"打开"--》"CMake",选中 c_cuda/CMakeLists.txt,点击 "打开"
4. 等就绪后,没有报错信息,c_cuda 下生成了 out\build\x64-Debug 文件夹:
5. 点击 Visual Studio 2022 顶部导航栏的 "生成" --》"全部生成",成功生成了 project.exe:
6. 执行 project.exe:
可以将 xxx 中的:
__global__ void foo()
{
printf("CUDA!\n");
}
修改为 :
__global__ void foo()
{
while( true ){
printf("CUDA!\n");
}
}
重新编译、运行,鼠标移动到屏幕下方,右键 --》"任务管理器"--》切换到 "性能" tab页,发现确实是在吃 GPU。
可能报错的解决方法:
1. 如果报如下错:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include\crt/host_config.h(153):
fatal error C1189: #error: -- unsupported Microsoft Visual Studio version!
Only the versions between 2017 and 2022 (inclusive) are supported!
The nvcc flag '-allow-unsupported-compiler' can be used to override this version check;
however, using an unsupported host compiler may cause compilation failure or incorrect run time execution.
Use at your own risk.
可能是因为 c_cuda/cuda/CMakeLists.txt 中的 set(CUDA_NVCC_FLAGS 后面没加 -allow-unsupported-compiler 标记
2. 如果点击 "生成" --》"全部生成" 时,报如下错:
可能是因为 c_cuda/cuda/CMakeLists.txt 中的 set(CUDA_NVCC_FLAGS 后面加了 -O3 标记,去掉就行了