-
Notifications
You must be signed in to change notification settings - Fork 18
/
CMakeLists.txt
executable file
·88 lines (68 loc) · 2.53 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
cmake_minimum_required(VERSION 3.5)
project(needle C CXX)
cmake_policy(SET CMP0146 OLD)
# find correct version of Python
execute_process(COMMAND python3-config --prefix
OUTPUT_VARIABLE Python_ROOT_DIR)
find_package(Python COMPONENTS Development Interpreter REQUIRED)
include_directories(${Python_INCLUDE_DIRS})
# find pybind
execute_process(COMMAND python3 -m pybind11 --cmakedir
RESULT_VARIABLE __pybind_exit_code
OUTPUT_VARIABLE __pybind_path
OUTPUT_STRIP_TRAILING_WHITESPACE)
find_package(pybind11 PATHS ${__pybind_path})
if(NOT MSVC)
set(CMAKE_CXX_FLAGS "-std=c++11 -O2 -march=native ${CMAKE_CXX_FLAGS}")
set(CMAKE_CUDA_STANDARD 14)
else()
set(CMAKE_CXX_FLAGS "/std:c++11 -O2 -march=native ${CMAKE_CXX_FLAGS}")
set(CMAKE_CUDA_STANDARD 14)
endif()
include_directories(SYSTEM ${pybind11_INCLUDE_DIRS})
list(APPEND LINKER_LIBS ${pybind11_LIBRARIES})
###################
### CPU BACKEND ###
###################
add_library(ndarray_backend_cpu MODULE src/ndarray_backend_cpu.cc)
target_link_libraries(ndarray_backend_cpu PUBLIC ${LINKER_LIBS})
pybind11_extension(ndarray_backend_cpu)
pybind11_strip(ndarray_backend_cpu)
# directly output to ffi folder
set_target_properties(ndarray_backend_cpu
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/python/needle/backend_ndarray
CXX_VISIBILITY_PRESET "hidden"
)
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set_property(TARGET ndarray_backend_cpu PROPERTY LINK_OPTIONS -undefined dynamic_lookup)
endif()
####################
### CUDA BACKEND ###
####################
find_package(CUDA)
if(CUDA_FOUND)
message(STATUS "Found cuda, building cuda backend")
include_directories(SYSTEM ${CUDA_INCLUDE_DIRS})
list(APPEND LINKER_LIBS ${CUDA_CUDART_LIBRARY})
# invoke nvidia smi to detect if we really have a GPU
execute_process(COMMAND "nvidia-smi" ERROR_QUIET RESULT_VARIABLE NV_RET)
if(NV_RET EQUAL "0")
CUDA_SELECT_NVCC_ARCH_FLAGS(ARCH_FLAGS Auto)
else()
# set to 3.7 the flag of K80
CUDA_SELECT_NVCC_ARCH_FLAGS(ARCH_FLAGS 3.7)
endif()
# set arch flags properly
CUDA_ADD_LIBRARY(ndarray_backend_cuda MODULE src/ndarray_backend_cuda.cu OPTIONS ${ARCH_FLAGS})
target_link_libraries(ndarray_backend_cuda ${LINKER_LIBS})
pybind11_extension(ndarray_backend_cuda)
pybind11_strip(ndarray_backend_cuda)
# directly output to ffi folder
set_target_properties(ndarray_backend_cuda
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/python/needle/backend_ndarray
CXX_VISIBILITY_PRESET "hidden"
CUDA_VISIBILITY_PRESET "hidden"
)
endif()