-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
105 lines (87 loc) · 3.18 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
cmake_minimum_required(VERSION 3.2)
project(needle C CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# 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})
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()
# add pybind11
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/pybind11/include)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/pybind11)
# add openmp
find_package(OpenMP QUIET)
if(OpenMP_FOUND)
message (STATUS "OpenMP_FOUND = ${OpenMP_FOUND}")
message (STATUS "OpenMP_CXX_FOUND = ${OpenMP_CXX_FOUND}")
list(APPEND LINKER_LIBS_CPU OpenMP::OpenMP_CXX)
include(CMakePrintHelpers)
cmake_print_properties(
TARGETS
OpenMP::OpenMP_CXX
PROPERTIES
INTERFACE_COMPILE_OPTIONS
INTERFACE_INCLUDE_DIRECTORIES
INTERFACE_LINK_LIBRARIES
)
endif()
#add onednn
set(USE_DNNL ON)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/backend_cpu_dnnl/backend_cpu_dnnl_config.h.in cpu_dnnl_config.h)
if(USE_DNNL STREQUAL "ON")
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/backend_cpu_dnnl)
list(APPEND LINKER_LIBS_CPU ndarray_backend_cpu_dnnl)
endif()
###################
### CPU BACKEND ###
###################
add_library(ndarray_backend_cpu MODULE src/ndarray_backend_cpu.cc)
target_link_libraries(ndarray_backend_cpu PUBLIC ${LINKER_LIBS_CPU})
pybind11_extension(ndarray_backend_cpu)
pybind11_strip(ndarray_backend_cpu)
target_include_directories(ndarray_backend_cpu PUBLIC "${PROJECT_BINARY_DIR}")
# 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_GPU ${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_GPU})
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()