Commit 839acd3e authored by Davis King's avatar Davis King

Setup CMake and related things to build dlib with CUDA support.

parent caf87fa4
...@@ -58,6 +58,8 @@ if (NOT TARGET dlib) ...@@ -58,6 +58,8 @@ if (NOT TARGET dlib)
"Disable this if you don't want to use a BLAS library" ) "Disable this if you don't want to use a BLAS library" )
set (DLIB_USE_LAPACK_STR set (DLIB_USE_LAPACK_STR
"Disable this if you don't want to use a LAPACK library" ) "Disable this if you don't want to use a LAPACK library" )
set (DLIB_USE_CUDA_STR
"Disable this if you don't want to use NVIDIA CUDA" )
set (DLIB_PNG_SUPPORT_STR set (DLIB_PNG_SUPPORT_STR
"Disable this if you don't want to link against libpng" ) "Disable this if you don't want to link against libpng" )
set (DLIB_JPEG_SUPPORT_STR set (DLIB_JPEG_SUPPORT_STR
...@@ -81,6 +83,7 @@ if (NOT TARGET dlib) ...@@ -81,6 +83,7 @@ if (NOT TARGET dlib)
option(DLIB_USE_BLAS ${DLIB_USE_BLAS_STR} ON) option(DLIB_USE_BLAS ${DLIB_USE_BLAS_STR} ON)
option(DLIB_USE_LAPACK ${DLIB_USE_LAPACK_STR} ON) option(DLIB_USE_LAPACK ${DLIB_USE_LAPACK_STR} ON)
option(DLIB_USE_CUDA ${DLIB_USE_CUDA_STR} ON)
option(DLIB_PNG_SUPPORT ${DLIB_PNG_SUPPORT_STR} ON) option(DLIB_PNG_SUPPORT ${DLIB_PNG_SUPPORT_STR} ON)
option(DLIB_JPEG_SUPPORT ${DLIB_JPEG_SUPPORT_STR} ON) option(DLIB_JPEG_SUPPORT ${DLIB_JPEG_SUPPORT_STR} ON)
option(DLIB_LINK_WITH_SQLITE3 ${DLIB_LINK_WITH_SQLITE3_STR} ON) option(DLIB_LINK_WITH_SQLITE3 ${DLIB_LINK_WITH_SQLITE3_STR} ON)
...@@ -368,6 +371,44 @@ if (NOT TARGET dlib) ...@@ -368,6 +371,44 @@ if (NOT TARGET dlib)
endif() endif()
if (DLIB_USE_CUDA)
find_package(CUDA)
if (CUDA_FOUND)
if (CUDA_VERSION_STRING LESS "7.0")
message(FATAL_ERROR "dlib requires CUDA version 7.0 or higher")
endif()
message(STATUS "Looking for cuDNN install...")
# Look for cudnn, we will look in the same place as other CUDA
# libraries and also a few other places as well.
find_path(cudnn_include cudnn.h
HINTS ${CUDA_INCLUDE_DIRS}
PATHS /usr/local/include
)
get_filename_component(cudnn_hint_path ${CUDA_CUBLAS_LIBRARIES} PATH)
find_library(cudnn cudnn
HINTS ${cudnn_hint_path}
PATHS /usr/local/lib64
/usr/local/cuda/lib64
/usr/local/cuda/lib
/usr/local/lib
)
mark_as_advanced(cudnn cudnn_include)
endif()
if (CUDA_FOUND AND cudnn AND cudnn_include)
message(STATUS "Found cuDNN: " ${cudnn})
set(source_files ${source_files} dnn/cuda.cu )
set(dlib_needed_libraries ${dlib_needed_libraries} ${CUDA_CUBLAS_LIBRARIES} ${cudnn})
include_directories(${cudnn_include})
else()
set(DLIB_USE_CUDA OFF CACHE STRING ${DLIB_USE_BLAS_STR} FORCE )
message(STATUS "cuDNN NOT FOUND. DLIB WILL NOT USE CUDA")
endif()
endif()
if (DLIB_LINK_WITH_SQLITE3) if (DLIB_LINK_WITH_SQLITE3)
find_library(sqlite sqlite3) find_library(sqlite sqlite3)
...@@ -398,12 +439,22 @@ if (NOT TARGET dlib) ...@@ -398,12 +439,22 @@ if (NOT TARGET dlib)
mark_as_advanced(fftw fftw_path) mark_as_advanced(fftw fftw_path)
endif() endif()
add_library(dlib STATIC ${source_files} )
target_link_libraries(dlib ${dlib_needed_libraries} )
# Tell CMake to build dlib via add_library()/cuda_add_library()
if (DLIB_USE_CUDA)
cuda_add_library(dlib STATIC ${source_files} )
else()
add_library(dlib STATIC ${source_files} )
endif()
target_link_libraries(dlib ${dlib_needed_libraries} )
if (UNIX AND NOT DLIB_IN_PROJECT_BUILD) if (UNIX AND NOT DLIB_IN_PROJECT_BUILD)
add_library(dlib-shared SHARED ${source_files} ) if (DLIB_USE_CUDA)
target_link_libraries(dlib-shared ${dlib_needed_libraries} ) cuda_add_library(dlib-shared SHARED ${source_files} )
else()
add_library(dlib-shared SHARED ${source_files} )
endif()
target_link_libraries(dlib-shared ${dlib_needed_libraries} )
endif() endif()
endif () ##### end of if NOT DLIB_ISO_CPP_ONLY ########################################################## endif () ##### end of if NOT DLIB_ISO_CPP_ONLY ##########################################################
...@@ -488,6 +539,12 @@ if (NOT TARGET dlib) ...@@ -488,6 +539,12 @@ if (NOT TARGET dlib)
remove_global_define(DLIB_USE_FFTW) remove_global_define(DLIB_USE_FFTW)
endif() endif()
if (DLIB_USE_CUDA AND NOT DLIB_ISO_CPP_ONLY)
add_global_define(DLIB_USE_CUDA)
else()
remove_global_define(DLIB_USE_CUDA)
endif()
if (DLIB_USE_BLAS AND blas_found) if (DLIB_USE_BLAS AND blas_found)
add_global_define(DLIB_USE_BLAS) add_global_define(DLIB_USE_BLAS)
......
...@@ -10,12 +10,15 @@ ...@@ -10,12 +10,15 @@
//#define ENABLE_ASSERTS // asserts always enabled //#define ENABLE_ASSERTS // asserts always enabled
//#define DLIB_DISABLE_ASSERTS // asserts always disabled //#define DLIB_DISABLE_ASSERTS // asserts always disabled
//#define DLIB_ISO_CPP_ONLY
//#define DLIB_NO_GUI_SUPPORT
//#define DLIB_ENABLE_STACK_TRACE
// You should also consider telling dlib to link against libjpeg, libpng, fftw, CUDA, and a
// You should also consider telling dlib to link against libjpeg, libpng, fftw, and a BLAS // BLAS and LAPACK library. To do this you need to uncomment the following #defines.
// and LAPACK library. To do this you need to uncomment the following #defines.
// #define DLIB_JPEG_SUPPORT // #define DLIB_JPEG_SUPPORT
// #define DLIB_PNG_SUPPORT // #define DLIB_PNG_SUPPORT
// #define DLIB_USE_FFTW // #define DLIB_USE_FFTW
// #define DLIB_USE_BLAS // #define DLIB_USE_BLAS
// #define DLIB_USE_LAPACK // #define DLIB_USE_LAPACK
// #define DLIB_USE_CUDA
...@@ -14,10 +14,11 @@ ...@@ -14,10 +14,11 @@
#cmakedefine DLIB_NO_GUI_SUPPORT #cmakedefine DLIB_NO_GUI_SUPPORT
#cmakedefine DLIB_ENABLE_STACK_TRACE #cmakedefine DLIB_ENABLE_STACK_TRACE
// You should also consider telling dlib to link against libjpeg, libpng, fftw, and a BLAS // You should also consider telling dlib to link against libjpeg, libpng, fftw, CUDA, and a
// and LAPACK library. To do this you need to uncomment the following #defines. // BLAS and LAPACK library. To do this you need to uncomment the following #defines.
#cmakedefine DLIB_JPEG_SUPPORT #cmakedefine DLIB_JPEG_SUPPORT
#cmakedefine DLIB_PNG_SUPPORT #cmakedefine DLIB_PNG_SUPPORT
#cmakedefine DLIB_USE_FFTW #cmakedefine DLIB_USE_FFTW
#cmakedefine DLIB_USE_BLAS #cmakedefine DLIB_USE_BLAS
#cmakedefine DLIB_USE_LAPACK #cmakedefine DLIB_USE_LAPACK
#cmakedefine DLIB_USE_CUDA
#include <stdlib.h>
#include <stdio.h>
#define CHECK(call) \
{ \
const cudaError_t error = call; \
if (error != cudaSuccess) \
{ \
fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__); \
fprintf(stderr, "code: %d, reason: %s\n", error, \
cudaGetErrorString(error)); \
exit(1); \
} \
}
__global__ void helloFromGPU()
{
printf("Hello World from GPU!\n");
}
void hello_cuda()
{
printf("Hello World from CPU!\n");
helloFromGPU<<<1, 10>>>();
CHECK(cudaDeviceReset());
}
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
#include "tensor.h" #include "tensor.h"
// TODO, remove this cruft
void hello_cuda();
namespace dlib namespace dlib
{ {
namespace cuda namespace cuda
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment