Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57b4fe5

Browse files
authored
Add files via upload
1 parent 55c5d10 commit 57b4fe5

28 files changed

+2476
-0
lines changed

cmake/ConfigGen.cmake

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
################################################################################################
3+
# Helper function to get all list items that begin with given prefix
4+
# Usage:
5+
# caffe_get_items_with_prefix(<prefix> <list_variable> <output_variable>)
6+
function(caffe_get_items_with_prefix prefix list_variable output_variable)
7+
set(__result "")
8+
foreach(__e ${${list_variable}})
9+
if(__e MATCHES "^${prefix}.*")
10+
list(APPEND __result ${__e})
11+
endif()
12+
endforeach()
13+
set(${output_variable} ${__result} PARENT_SCOPE)
14+
endfunction()
15+
16+
################################################################################################
17+
# Function for generation Caffe build- and install- tree export config files
18+
# Usage:
19+
# caffe_generate_export_configs()
20+
function(caffe_generate_export_configs)
21+
set(install_cmake_suffix "share/Caffe")
22+
23+
if(NOT HAVE_CUDA)
24+
set(HAVE_CUDA FALSE)
25+
endif()
26+
27+
if(NOT HAVE_CUDNN)
28+
set(HAVE_CUDNN FALSE)
29+
endif()
30+
31+
# ---[ Configure build-tree CaffeConfig.cmake file ]---
32+
33+
configure_file("cmake/Templates/CaffeConfig.cmake.in" "${PROJECT_BINARY_DIR}/CaffeConfig.cmake" @ONLY)
34+
35+
# Add targets to the build-tree export set
36+
export(TARGETS caffe caffeproto FILE "${PROJECT_BINARY_DIR}/CaffeTargets.cmake")
37+
export(PACKAGE Caffe)
38+
39+
# ---[ Configure install-tree CaffeConfig.cmake file ]---
40+
41+
configure_file("cmake/Templates/CaffeConfig.cmake.in" "${PROJECT_BINARY_DIR}/cmake/CaffeConfig.cmake" @ONLY)
42+
43+
# Install the CaffeConfig.cmake and export set to use with install-tree
44+
install(FILES "${PROJECT_BINARY_DIR}/cmake/CaffeConfig.cmake" DESTINATION ${install_cmake_suffix})
45+
install(EXPORT CaffeTargets DESTINATION ${install_cmake_suffix})
46+
47+
# ---[ Configure and install version file ]---
48+
49+
# TODO: Lines below are commented because Caffe doesn't declare its version in headers.
50+
# When the declarations are added, modify `caffe_extract_caffe_version()` macro and uncomment
51+
52+
# configure_file(cmake/Templates/CaffeConfigVersion.cmake.in "${PROJECT_BINARY_DIR}/CaffeConfigVersion.cmake" @ONLY)
53+
# install(FILES "${PROJECT_BINARY_DIR}/CaffeConfigVersion.cmake" DESTINATION ${install_cmake_suffix})
54+
endfunction()
55+
56+

cmake/Cuda.cmake

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
if(CPU_ONLY)
2+
return()
3+
endif()
4+
5+
# Known NVIDIA GPU achitectures Caffe can be compiled for.
6+
# This list will be used for CUDA_ARCH_NAME = All option
7+
set(Caffe_known_gpu_archs "20 21(20) 30 35 50 60 61")
8+
9+
################################################################################################
10+
# A function for automatic detection of GPUs installed (if autodetection is enabled)
11+
# Usage:
12+
# caffe_detect_installed_gpus(out_variable)
13+
function(caffe_detect_installed_gpus out_variable)
14+
if(NOT CUDA_gpu_detect_output)
15+
set(__cufile ${PROJECT_BINARY_DIR}/detect_cuda_archs.cu)
16+
17+
file(WRITE ${__cufile} ""
18+
"#include <cstdio>\n"
19+
"int main()\n"
20+
"{\n"
21+
" int count = 0;\n"
22+
" if (cudaSuccess != cudaGetDeviceCount(&count)) return -1;\n"
23+
" if (count == 0) return -1;\n"
24+
" for (int device = 0; device < count; ++device)\n"
25+
" {\n"
26+
" cudaDeviceProp prop;\n"
27+
" if (cudaSuccess == cudaGetDeviceProperties(&prop, device))\n"
28+
" std::printf(\"%d.%d \", prop.major, prop.minor);\n"
29+
" }\n"
30+
" return 0;\n"
31+
"}\n")
32+
33+
execute_process(COMMAND "${CUDA_NVCC_EXECUTABLE}" "--run" "${__cufile}"
34+
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}/CMakeFiles/"
35+
RESULT_VARIABLE __nvcc_res OUTPUT_VARIABLE __nvcc_out
36+
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
37+
38+
if(__nvcc_res EQUAL 0)
39+
string(REPLACE "2.1" "2.1(2.0)" __nvcc_out "${__nvcc_out}")
40+
set(CUDA_gpu_detect_output ${__nvcc_out} CACHE INTERNAL "Returned GPU architetures from caffe_detect_gpus tool" FORCE)
41+
endif()
42+
endif()
43+
44+
if(NOT CUDA_gpu_detect_output)
45+
message(STATUS "Automatic GPU detection failed. Building for all known architectures.")
46+
set(${out_variable} ${Caffe_known_gpu_archs} PARENT_SCOPE)
47+
else()
48+
set(${out_variable} ${CUDA_gpu_detect_output} PARENT_SCOPE)
49+
endif()
50+
endfunction()
51+
52+
53+
################################################################################################
54+
# Function for selecting GPU arch flags for nvcc based on CUDA_ARCH_NAME
55+
# Usage:
56+
# caffe_select_nvcc_arch_flags(out_variable)
57+
function(caffe_select_nvcc_arch_flags out_variable)
58+
# List of arch names
59+
set(__archs_names "Fermi" "Kepler" "Maxwell" "Pascal" "All" "Manual")
60+
set(__archs_name_default "All")
61+
if(NOT CMAKE_CROSSCOMPILING)
62+
list(APPEND __archs_names "Auto")
63+
set(__archs_name_default "Auto")
64+
endif()
65+
66+
# set CUDA_ARCH_NAME strings (so it will be seen as dropbox in CMake-Gui)
67+
set(CUDA_ARCH_NAME ${__archs_name_default} CACHE STRING "Select target NVIDIA GPU achitecture.")
68+
set_property( CACHE CUDA_ARCH_NAME PROPERTY STRINGS "" ${__archs_names} )
69+
mark_as_advanced(CUDA_ARCH_NAME)
70+
71+
# verify CUDA_ARCH_NAME value
72+
if(NOT ";${__archs_names};" MATCHES ";${CUDA_ARCH_NAME};")
73+
string(REPLACE ";" ", " __archs_names "${__archs_names}")
74+
message(FATAL_ERROR "Only ${__archs_names} architeture names are supported.")
75+
endif()
76+
77+
if(${CUDA_ARCH_NAME} STREQUAL "Manual")
78+
set(CUDA_ARCH_BIN ${Caffe_known_gpu_archs} CACHE STRING "Specify 'real' GPU architectures to build binaries for, BIN(PTX) format is supported")
79+
set(CUDA_ARCH_PTX "50" CACHE STRING "Specify 'virtual' PTX architectures to build PTX intermediate code for")
80+
mark_as_advanced(CUDA_ARCH_BIN CUDA_ARCH_PTX)
81+
else()
82+
unset(CUDA_ARCH_BIN CACHE)
83+
unset(CUDA_ARCH_PTX CACHE)
84+
endif()
85+
86+
if(${CUDA_ARCH_NAME} STREQUAL "Fermi")
87+
set(__cuda_arch_bin "20 21(20)")
88+
elseif(${CUDA_ARCH_NAME} STREQUAL "Kepler")
89+
set(__cuda_arch_bin "30 35")
90+
elseif(${CUDA_ARCH_NAME} STREQUAL "Maxwell")
91+
set(__cuda_arch_bin "50")
92+
elseif(${CUDA_ARCH_NAME} STREQUAL "Pascal")
93+
set(__cuda_arch_bin "60 61")
94+
elseif(${CUDA_ARCH_NAME} STREQUAL "All")
95+
set(__cuda_arch_bin ${Caffe_known_gpu_archs})
96+
elseif(${CUDA_ARCH_NAME} STREQUAL "Auto")
97+
caffe_detect_installed_gpus(__cuda_arch_bin)
98+
else() # (${CUDA_ARCH_NAME} STREQUAL "Manual")
99+
set(__cuda_arch_bin ${CUDA_ARCH_BIN})
100+
endif()
101+
102+
# remove dots and convert to lists
103+
string(REGEX REPLACE "\\." "" __cuda_arch_bin "${__cuda_arch_bin}")
104+
string(REGEX REPLACE "\\." "" __cuda_arch_ptx "${CUDA_ARCH_PTX}")
105+
string(REGEX MATCHALL "[0-9()]+" __cuda_arch_bin "${__cuda_arch_bin}")
106+
string(REGEX MATCHALL "[0-9]+" __cuda_arch_ptx "${__cuda_arch_ptx}")
107+
caffe_list_unique(__cuda_arch_bin __cuda_arch_ptx)
108+
109+
set(__nvcc_flags "")
110+
set(__nvcc_archs_readable "")
111+
112+
# Tell NVCC to add binaries for the specified GPUs
113+
foreach(__arch ${__cuda_arch_bin})
114+
if(__arch MATCHES "([0-9]+)\\(([0-9]+)\\)")
115+
# User explicitly specified PTX for the concrete BIN
116+
list(APPEND __nvcc_flags -gencode arch=compute_${CMAKE_MATCH_2},code=sm_${CMAKE_MATCH_1})
117+
list(APPEND __nvcc_archs_readable sm_${CMAKE_MATCH_1})
118+
else()
119+
# User didn't explicitly specify PTX for the concrete BIN, we assume PTX=BIN
120+
list(APPEND __nvcc_flags -gencode arch=compute_${__arch},code=sm_${__arch})
121+
list(APPEND __nvcc_archs_readable sm_${__arch})
122+
endif()
123+
endforeach()
124+
125+
# Tell NVCC to add PTX intermediate code for the specified architectures
126+
foreach(__arch ${__cuda_arch_ptx})
127+
list(APPEND __nvcc_flags -gencode arch=compute_${__arch},code=compute_${__arch})
128+
list(APPEND __nvcc_archs_readable compute_${__arch})
129+
endforeach()
130+
131+
string(REPLACE ";" " " __nvcc_archs_readable "${__nvcc_archs_readable}")
132+
set(${out_variable} ${__nvcc_flags} PARENT_SCOPE)
133+
set(${out_variable}_readable ${__nvcc_archs_readable} PARENT_SCOPE)
134+
endfunction()
135+
136+
################################################################################################
137+
# Short command for cuda compilation
138+
# Usage:
139+
# caffe_cuda_compile(<objlist_variable> <cuda_files>)
140+
macro(caffe_cuda_compile objlist_variable)
141+
foreach(var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
142+
set(${var}_backup_in_cuda_compile_ "${${var}}")
143+
144+
# we remove /EHa as it generates warnings under windows
145+
string(REPLACE "/EHa" "" ${var} "${${var}}")
146+
147+
endforeach()
148+
149+
if(UNIX OR APPLE)
150+
list(APPEND CUDA_NVCC_FLAGS -Xcompiler -fPIC)
151+
endif()
152+
153+
if(APPLE)
154+
list(APPEND CUDA_NVCC_FLAGS -Xcompiler -Wno-unused-function)
155+
endif()
156+
157+
cuda_compile(cuda_objcs ${ARGN})
158+
159+
foreach(var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
160+
set(${var} "${${var}_backup_in_cuda_compile_}")
161+
unset(${var}_backup_in_cuda_compile_)
162+
endforeach()
163+
164+
set(${objlist_variable} ${cuda_objcs})
165+
endmacro()
166+
167+
################################################################################################
168+
# Short command for cuDNN detection. Believe it soon will be a part of CUDA toolkit distribution.
169+
# That's why not FindcuDNN.cmake file, but just the macro
170+
# Usage:
171+
# detect_cuDNN()
172+
function(detect_cuDNN)
173+
set(CUDNN_ROOT "" CACHE PATH "CUDNN root folder")
174+
175+
find_path(CUDNN_INCLUDE cudnn.h
176+
PATHS ${CUDNN_ROOT} $ENV{CUDNN_ROOT} ${CUDA_TOOLKIT_INCLUDE}
177+
DOC "Path to cuDNN include directory." )
178+
179+
# dynamic libs have different suffix in mac and linux
180+
if(APPLE)
181+
set(CUDNN_LIB_NAME "libcudnn.dylib")
182+
else()
183+
set(CUDNN_LIB_NAME "libcudnn.so")
184+
endif()
185+
186+
get_filename_component(__libpath_hist ${CUDA_CUDART_LIBRARY} PATH)
187+
find_library(CUDNN_LIBRARY NAMES ${CUDNN_LIB_NAME}
188+
PATHS ${CUDNN_ROOT} $ENV{CUDNN_ROOT} ${CUDNN_INCLUDE} ${__libpath_hist} ${__libpath_hist}/../lib
189+
DOC "Path to cuDNN library.")
190+
191+
if(CUDNN_INCLUDE AND CUDNN_LIBRARY)
192+
set(HAVE_CUDNN TRUE PARENT_SCOPE)
193+
set(CUDNN_FOUND TRUE PARENT_SCOPE)
194+
195+
file(READ ${CUDNN_INCLUDE}/cudnn.h CUDNN_VERSION_FILE_CONTENTS)
196+
197+
# cuDNN v3 and beyond
198+
string(REGEX MATCH "define CUDNN_MAJOR * +([0-9]+)"
199+
CUDNN_VERSION_MAJOR "${CUDNN_VERSION_FILE_CONTENTS}")
200+
string(REGEX REPLACE "define CUDNN_MAJOR * +([0-9]+)" "\\1"
201+
CUDNN_VERSION_MAJOR "${CUDNN_VERSION_MAJOR}")
202+
string(REGEX MATCH "define CUDNN_MINOR * +([0-9]+)"
203+
CUDNN_VERSION_MINOR "${CUDNN_VERSION_FILE_CONTENTS}")
204+
string(REGEX REPLACE "define CUDNN_MINOR * +([0-9]+)" "\\1"
205+
CUDNN_VERSION_MINOR "${CUDNN_VERSION_MINOR}")
206+
string(REGEX MATCH "define CUDNN_PATCHLEVEL * +([0-9]+)"
207+
CUDNN_VERSION_PATCH "${CUDNN_VERSION_FILE_CONTENTS}")
208+
string(REGEX REPLACE "define CUDNN_PATCHLEVEL * +([0-9]+)" "\\1"
209+
CUDNN_VERSION_PATCH "${CUDNN_VERSION_PATCH}")
210+
211+
if(NOT CUDNN_VERSION_MAJOR)
212+
set(CUDNN_VERSION "???")
213+
else()
214+
set(CUDNN_VERSION "${CUDNN_VERSION_MAJOR}.${CUDNN_VERSION_MINOR}.${CUDNN_VERSION_PATCH}")
215+
endif()
216+
217+
message(STATUS "Found cuDNN: ver. ${CUDNN_VERSION} found (include: ${CUDNN_INCLUDE}, library: ${CUDNN_LIBRARY})")
218+
219+
string(COMPARE LESS "${CUDNN_VERSION_MAJOR}" 3 cuDNNVersionIncompatible)
220+
if(cuDNNVersionIncompatible)
221+
message(FATAL_ERROR "cuDNN version >3 is required.")
222+
endif()
223+
224+
set(CUDNN_VERSION "${CUDNN_VERSION}" PARENT_SCOPE)
225+
mark_as_advanced(CUDNN_INCLUDE CUDNN_LIBRARY CUDNN_ROOT)
226+
227+
endif()
228+
endfunction()
229+
230+
################################################################################################
231+
### Non macro section
232+
################################################################################################
233+
234+
find_package(CUDA 5.5 QUIET)
235+
find_cuda_helper_libs(curand) # cmake 2.8.7 compartibility which doesn't search for curand
236+
237+
if(NOT CUDA_FOUND)
238+
return()
239+
endif()
240+
241+
set(HAVE_CUDA TRUE)
242+
message(STATUS "CUDA detected: " ${CUDA_VERSION})
243+
list(APPEND Caffe_INCLUDE_DIRS PUBLIC ${CUDA_INCLUDE_DIRS})
244+
list(APPEND Caffe_LINKER_LIBS PUBLIC ${CUDA_CUDART_LIBRARY}
245+
${CUDA_curand_LIBRARY} ${CUDA_CUBLAS_LIBRARIES})
246+
247+
# cudnn detection
248+
if(USE_CUDNN)
249+
detect_cuDNN()
250+
if(HAVE_CUDNN)
251+
list(APPEND Caffe_DEFINITIONS PUBLIC -DUSE_CUDNN)
252+
list(APPEND Caffe_INCLUDE_DIRS PUBLIC ${CUDNN_INCLUDE})
253+
list(APPEND Caffe_LINKER_LIBS PUBLIC ${CUDNN_LIBRARY})
254+
endif()
255+
endif()
256+
257+
# setting nvcc arch flags
258+
caffe_select_nvcc_arch_flags(NVCC_FLAGS_EXTRA)
259+
list(APPEND CUDA_NVCC_FLAGS ${NVCC_FLAGS_EXTRA})
260+
message(STATUS "Added CUDA NVCC flags for: ${NVCC_FLAGS_EXTRA_readable}")
261+
262+
# Boost 1.55 workaround, see https://svn.boost.org/trac/boost/ticket/9392 or
263+
# https://github.com/ComputationalRadiationPhysics/picongpu/blob/master/src/picongpu/CMakeLists.txt
264+
if(Boost_VERSION EQUAL 105500)
265+
message(STATUS "Cuda + Boost 1.55: Applying noinline work around")
266+
# avoid warning for CMake >= 2.8.12
267+
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} \"-DBOOST_NOINLINE=__attribute__((noinline))\" ")
268+
endif()
269+
270+
# disable some nvcc diagnostic that apears in boost, glog, glags, opencv, etc.
271+
foreach(diag cc_clobber_ignored integer_sign_change useless_using_declaration set_but_not_used)
272+
list(APPEND CUDA_NVCC_FLAGS -Xcudafe --diag_suppress=${diag})
273+
endforeach()
274+
275+
# setting default testing device
276+
if(NOT CUDA_TEST_DEVICE)
277+
set(CUDA_TEST_DEVICE -1)
278+
endif()
279+
280+
mark_as_advanced(CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_VERBOSE_BUILD)
281+
mark_as_advanced(CUDA_SDK_ROOT_DIR CUDA_SEPARABLE_COMPILATION)
282+
283+
# Handle clang/libc++ issue
284+
if(APPLE)
285+
caffe_detect_darwin_version(OSX_VERSION)
286+
287+
# OSX 10.9 and higher uses clang/libc++ by default which is incompatible with old CUDA toolkits
288+
if(OSX_VERSION VERSION_GREATER 10.8)
289+
# enabled by default if and only if CUDA version is less than 7.0
290+
caffe_option(USE_libstdcpp "Use libstdc++ instead of libc++" (CUDA_VERSION VERSION_LESS 7.0))
291+
endif()
292+
endif()

0 commit comments

Comments
 (0)