cmake 生成的cuda 好處就是跨平臺,少去各種配置。
1. CMakeLists.txt 程式碼如下:
# CMakeLists.txt for G4CU project
project(test_cuda_project)
# required cmake version
cmake_minimum_required(VERSION 2.8)
# packages
find_package(CUDA)
if(${CUDA_FOUND})
include_directories(${CUDA_INCLUDE_DIRS})
link_directories($ENV{CUDA_PATH}/lib/x64)
else(${CUDA_FOUND})
MESSAGE(STATUS "cuda not found!")
endif(${CUDA_FOUND})
# nvcc flags
#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)
# 新增要編譯的庫
#add_library(gpu SHARED ${CURRENT_HEADERS} ${CURRENT_SOURCES})
set_source_files_properties(test.cpp PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
file(GLOB_RECURSE CURRENT_HEADERS *.h *.hpp *.cuh)
file(GLOB CURRENT_SOURCES *.cpp *.cu .cc)
source_group("Include" FILES ${CURRENT_HEADERS})
source_group("Source" FILES ${CURRENT_SOURCES})
CUDA_ADD_EXECUTABLE(test ${CURRENT_HEADERS} ${CURRENT_SOURCES})
target_link_libraries(test cuda.lib cudart_static.lib)
複製程式碼
2. nvcc 是cuda的編譯器,有興趣的自己看看。
nvcc test.cpp main -x=cu
複製程式碼
下面這句非常關鍵,就是告訴編譯器這個檔案有cuda的語法,不然會無法編譯通過。
set_source_files_properties(test.cpp PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)複製程式碼
link_directories($ENV{CUDA_PATH}/lib/x64) 複製程式碼
$ENV{CUDA_PATH} 是環境變數,一般在安裝cuda會自動新增。複製程式碼
3. 連結庫
target_link_libraries(test_cuda_project cuda.lib cudart_static.lib)複製程式碼
新增專案的連結庫,一般 新增 cudart_static.lib就夠了,如果一些cuda的api需要其他庫,可以手動加進去,重新cmake.
4. 生成專案
在 vs2015下
cmake -G "Visual Studio 14 2015 Win64" .複製程式碼
在 linux 沒試過,估計
cmake . 複製程式碼
5. 測試程式碼
#include <iostream>
#include <cuda_runtime_api.h>
bool InitCUDA()
{
int count;
cudaGetDeviceCount(&count);//獲得cuda裝置的數量
if (count == 0)
{
std::cout << "There is no device.\n";
return false;
}
int i;
for (i = 0; i < count; i++)
{
cudaDeviceProp prop;//cuda裝置屬性物件
if (cudaGetDeviceProperties(&prop, i) == cudaSuccess)
{
std::cout << "device name:" << prop.name << "\n";
std::cout << " 1 compute value:" << prop.major << "\t" << "2 compute value:" << prop.minor << "\n";
std::cout << "hz:" << prop.clockRate << "\n";
std::cout << "processor nums:" << prop.multiProcessorCount << "\n";
std::cout << "GPU is support muti p run:" << prop.concurrentKernels << "\n";
}
}
cudaSetDevice(i);//啟動裝置
return true;
}
int main()
{
if (!InitCUDA())
{
return 0;
}
_sleep(10000);
std::cout << "cuda !\n";
return 0;
}
複製程式碼