project(KMCUDA CXX C CUDA) set(CMAKE_MODULE_PATH ${CMAKE_HOME_DIRECTORY}/cmake)
set (CMAKE_CXX_STANDARD 11) set (CMAKE_CXX_STANDARD_REQUIRED ON) set (CMAKE_CUDA_STANDARD 11) set (CMAKE_CUDA_STANDARD_REQUIRED ON)
find_package(OpenMP REQUIRED) if (MSVC) set (OpenMP_CXX_FLAGS "${OpenMP_CXX_FLAGS} -openmp:experimental") endif() set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
find_package(CUDAToolkit REQUIRED) if (NOT CUDA_ARCH) set(CUDA_ARCH 86) endif() set(CMAKE_CUDA_ARCHITECTURES ${CUDA_ARCH})
# ----------------- python ----------------- # find python find_package(Python 3.6 REQUIRED) # run python and read python include path execute_process(COMMAND python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())" OUTPUT_VARIABLE Python_INCLUDE_DIRS OUTPUT_STRIP_TRAILING_WHITESPACE) message(STATUS "Python_INCLUDE_DIRS: ${Python_INCLUDE_DIRS}") include_directories(${Python_INCLUDE_DIRS}) # if PYTHON_LIBRARIES is defined, use it if(PYTHON_LIBRARIES) set(Python_LIBRARIES ${PYTHON_LIBRARIES}) endif() # if Python_LIBRARIES is not found, warn user if(NOT Python_LIBRARIES) message(WARNING "Python_LIBRARIES not found, please set it manually") endif() message(STATUS "Python_LIBRARIES: ${Python_LIBRARIES}") add_compile_definitions(SWIG_PYTHON_INTERPRETER_NO_DEBUG)
# ----------------- numpy ----------------- # run python and get numpy include path execute_process(COMMAND python -c "import numpy; print(numpy.get_include())" OUTPUT_VARIABLE NUMPY_INCLUDE_DIRS OUTPUT_STRIP_TRAILING_WHITESPACE) message(STATUS "NUMPY_INCLUDE_DIRS: ${NUMPY_INCLUDE_DIRS}") include_directories(${NUMPY_INCLUDE_DIRS}) # download numpy.i if not found if(EXISTS"lib/numpy.i") message(STATUS "numpy.i found") else() message(STATUS "numpy.i not found, downloading...") file(DOWNLOAD https://raw.githubusercontent.com/numpy/numpy/master/tools/swig/numpy.i lib/numpy.i) endif()
# swig if(WRAP_LIB) set_property(SOURCE ./src/libzerogomoku.i PROPERTY CPLUSPLUS ON) swig_add_library(libzerogomoku TYPE SHARED LANGUAGE python SOURCES ./src/libzerogomoku.i ${SOURCES}) swig_link_libraries(libzerogomoku ${Python_LIBRARIES}${TORCH_LIBRARIES})
# The following code block is suggested to be used on Windows. # According to https://github.com/pytorch/pytorch/issues/25457, # the DLLs need to be copied to avoid memory errors. # if (WIN32) # file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll") # add_custom_command(TARGET _libzerogomoku # POST_BUILD # COMMAND ${CMAKE_COMMAND} -E copy_if_different # ${TORCH_DLLS} # $<TARGET_FILE_DIR:_libzerogomoku>) # endif (WIN32) endif()
# unit test if(UNIT_TEST) add_library(test_lib ${SOURCES}) target_link_libraries(test_lib ${TORCH_LIBRARIES})
# run python and get numpy include path execute_process(COMMAND python -c "import numpy; print(numpy.get_include())" OUTPUT_VARIABLE NUMPY_INCLUDE_DIRS OUTPUT_STRIP_TRAILING_WHITESPACE) message(STATUS "NUMPY_INCLUDE_DIRS: ${NUMPY_INCLUDE_DIRS}") include_directories(${NUMPY_INCLUDE_DIRS})
下面的代码自动下载 numpy.i
1 2 3 4 5 6 7
# download numpy.i if not found if(EXISTS"lib/numpy.i") message(STATUS "numpy.i found") else() message(STATUS "numpy.i not found, downloading...") file(DOWNLOAD https://raw.githubusercontent.com/numpy/numpy/master/tools/swig/numpy.i lib/numpy.i) endif()
set_property(SOURCE ./src/libzerogomoku.i PROPERTY CPLUSPLUS ON) swig_add_library(libzerogomoku TYPE SHARED LANGUAGE python SOURCES ./src/libzerogomoku.i ${SOURCES}) swig_link_libraries(libzerogomoku ${Python_LIBRARIES}${TORCH_LIBRARIES})
通过 Python 解释器查找 Python 头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# find python find_package(Python 3.6 REQUIRED) # run python and read python include path execute_process(COMMAND python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())" OUTPUT_VARIABLE Python_INCLUDE_DIRS OUTPUT_STRIP_TRAILING_WHITESPACE) message(STATUS "Python_INCLUDE_DIRS: ${Python_INCLUDE_DIRS}") include_directories(${Python_INCLUDE_DIRS}) # if PYTHON_LIBRARIES is defined, use it if(PYTHON_LIBRARIES) set(Python_LIBRARIES ${PYTHON_LIBRARIES}) endif() # if Python_LIBRARIES is not found, warn user if(NOT Python_LIBRARIES) message(WARNING "Python_LIBRARIES not found, please set it manually") endif() message(STATUS "Python_LIBRARIES: ${Python_LIBRARIES}")
在 Windows 上, 如果生成了调用 PyTorch 的可执行文件, 可能需要把 PyTorch 使用的各种动态链接库复制到可执行文件目录
1 2 3 4 5 6 7 8 9 10 11
# The following code block is suggested to be used on Windows. # According to https://github.com/pytorch/pytorch/issues/25457, # the DLLs need to be copied to avoid memory errors. if (WIN32) file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll") add_custom_command(TARGET _libzerogomoku POST_BUILD COMMAND${CMAKE_COMMAND} -E copy_if_different ${TORCH_DLLS} $<TARGET_FILE_DIR:_libzerogomoku>) endif (WIN32)