我使用示例create cuda library with cmake创建了一个基于cuda的小型库。该库包含一个名为CudaImage的类,该类应在图像上运行Cuda内核。
CMakeLists如下:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(cudaMPbTools LANGUAGES CXX CUDA)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../common
)
add_library(cudaMPbTools STATIC
cudaimage.cu
cudaimage.h
cvector.h
cvector.cpp
)
target_link_libraries(cudaMPbTools libCommon)
target_compile_features(cudaMPbTools PUBLIC cxx_std_11)
set_target_properties( cudaMPbTools
PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
cudaimage.h包含以下代码:
class CudaImage {
public:
CudaImage(unsigned char* image_data, int image_width, int image_height, int scale);
private:
__device__
void addToHistoArray(int val, int i, int j);
};
库可以使用cmake正常编译,但是现在我试图在库外的main.cpp中使用CudaImage类:
CudaImage cudaImg(img1.data, img1.cols, img1.rows, 10);
但这会在主项目中生成编译错误,因为它无法识别cudaimage.h中的 device 关键字。
主项目的CMakeLists.txt类似于:
cmake_minimum_required (VERSION 3.10)
project (mPb)
cmake_policy(SET CMP0020 NEW)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../common
${CMAKE_CURRENT_SOURCE_DIR}/../cudaMPbTools
${Qt5Core_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS})
set(MPB_SRCS
main.cpp
pbdetector.cpp
)
set(MPB_HEADR
pbdetector.h)
add_executable(mPb ${MPB_SRCS} ${MPB_HEADR})
target_link_libraries(mPb
libCommon
${OpenCV_LIBS}
cudaMPbTools)
qt5_use_modules(mPb Core)
有人可以就此事提供建议吗?主项目中的CMakeLists.txt是否正确?
更新我从类CudaImage中提取了函数addToHistoArray,并声明如下:
__device__ void addToHistoArray(struct CVector* dHalfDiscInfluencePoints, int totalHalfInfluencePoints, unsigned int** dHistograms, int image_width, int image_height, int scale, int arcno, int val, int i, int j);
,我仍然无法编译我的项目。我也更新了github中的源代码。
答案 0 :(得分:1)
__device__ void CudaImage::addToHistoArray(int val, int i, int j)
很可疑。成员函数的全部要点是它具有一个this
指针,即CudaImage*
。那当然是主机指针(CudaImage
位于CPU上)。那么,您的__device__
(GPU)函数试图使用主机指针做什么?!
不编译是幸运的副作用。大概addToHistoArray
不需要CudaImage*
,因此它可能是cudaimage.cu
内部的自由函数
答案 1 :(得分:1)
就像MSalters所说的那样,问题在于,要从GPU调用 device 方法以在GPU上执行。我不确定这就是您的意思。如果没有,您应该阅读以下内容: Difference between global and device functions
(对不起,我无法评论帖子)
答案 2 :(得分:0)
我找到了一个可以编译我的项目的解决方案。它要求以下条件:
CMake文件的外观适用于该库:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(cudaMPbTools)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../common
)
find_package(CUDA)
cuda_add_library(cudaMPbTools
cudaimage.cu
cudaimage.h
cvector.h
cvector.cpp
STATIC
)
target_link_libraries(cudaMPbTools libCommon ${CUDA_LIBRARIES})
target_compile_features(cudaMPbTools PUBLIC cxx_std_11)
对于项目:
cmake_minimum_required (VERSION 3.10)
project (mPb)
find_package(CUDA)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../common
${CMAKE_CURRENT_SOURCE_DIR}/../cudaMPbTools
${Qt5Core_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
${CUDA_INCLUDE_DIRS})
set(MPB_SRCS
main.cpp
pbdetector.cpp
)
set(MPB_HEADR
pbdetector.h)
cuda_add_executable(mPb ${MPB_SRCS} ${MPB_HEADR})
target_link_libraries(mPb
libCommon
${OpenCV_LIBS}
cudaMPbTools)
qt5_use_modules(mPb Core)