marian/CMakeLists.txt

346 lines
13 KiB
CMake
Raw Normal View History

2016-05-07 23:56:23 +03:00
cmake_minimum_required(VERSION 3.5.1)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
2018-11-29 06:30:27 +03:00
if (POLICY CMP0074)
cmake_policy(SET CMP0074 NEW) # CMake 3.12
endif ()
2018-01-24 05:35:55 +03:00
project(marian CXX C)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(BUILD_ARCH native CACHE STRING "Compile for this CPU architecture.")
2017-06-14 15:06:20 +03:00
2017-07-04 16:06:26 +03:00
# Custom CMake options
2018-03-01 15:36:27 +03:00
option(COMPILE_CPU "Compile CPU version" ON)
2018-03-03 05:06:26 +03:00
option(COMPILE_CUDA "Compile GPU version" ON)
2018-12-07 08:55:12 +03:00
option(USE_SENTENCEPIECE "Download and compile SentencePiece" OFF)
option(USE_STATIC_LIBS "Link statically against non-system libs" OFF)
option(USE_CUDNN "Use CUDNN library" OFF)
2018-07-01 10:15:30 +03:00
option(USE_NCCL "Use NCCL library" ON)
2018-02-26 09:08:25 +03:00
option(USE_MPI "Use MPI library" OFF)
2018-12-07 08:55:12 +03:00
option(COMPILE_EXAMPLES "Compile examples" OFF)
option(COMPILE_TESTS "Compile tests" OFF)
option(COMPILE_SERVER "Compile marian-server" ON)
option(USE_FBGEMM "Use FBGEMM" ON)
2017-07-04 16:06:26 +03:00
# Project versioning
find_package(Git QUIET)
2017-07-13 15:02:43 +03:00
include(GetVersionFromFile)
2017-07-04 16:06:26 +03:00
message(STATUS "Project name: ${PROJECT_NAME}")
message(STATUS "Project version: ${PROJECT_VERSION_STRING_FULL}")
2017-01-21 18:11:40 +03:00
2018-10-03 03:02:52 +03:00
execute_process(COMMAND git submodule update --init --recursive --no-fetch
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
2019-02-05 10:07:00 +03:00
if(NOT CMAKE_BUILD_TYPE)
message(WARNING "CMAKE_BUILD_TYPE not set; setting to Release")
set(CMAKE_BUILD_TYPE "Release")
endif()
2018-12-13 00:33:04 +03:00
# Set compilation flags
if(MSVC)
# These are used in src/CMakeLists.txt on a per-target basis
list(APPEND ALL_WARNINGS /WX; /W4;)
# Disabled bogus warnings for CPU intrincics:
# C4310: cast truncates constant value
# C4324: 'marian::cpu::int16::`anonymous-namespace'::ScatterPut': structure was padded due to alignment specifier
set(DISABLE_GLOBALLY "/wd\"4310\" /wd\"4324\"")
2019-02-05 10:07:00 +03:00
2018-12-13 00:45:57 +03:00
set(INTRINSICS "/arch:AVX")
# Or maybe use these?
# set(INTRINSICS "/arch:AVX2")
# set(INTRINSICS "/arch:AVX512")
2018-12-13 00:33:04 +03:00
set(CMAKE_CXX_FLAGS "/EHsc /DWIN32 /D_WINDOWS /DUNICODE /D_UNICODE /D_CRT_NONSTDC_NO_WARNINGS /D_CRT_SECURE_NO_WARNINGS ${DISABLE_GLOBALLY}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} /MT /O2 ${INTRINSICS} /Zi /MP /GL /DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} /MTd /Od /Ob0 ${INTRINSICS} /RTC1 /Zi /D_DEBUG")
2018-12-13 00:45:57 +03:00
# ignores warning LNK4049: locally defined symbol free imported - this comes from zlib
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /LTCG:incremental /INCREMENTAL:NO /NODEFAULTLIB:MSVCRT /ignore:4049")
2018-12-13 00:33:04 +03:00
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /LTCG:incremental")
2019-02-05 10:07:00 +03:00
find_library(SHLWAPI Shlwapi.lib)
2018-12-13 00:33:04 +03:00
set(EXT_LIBS ${EXT_LIBS} SHLWAPI)
else(MSVC)
# Detect support CPU instrinsics for the current platform. This will
# only by used with BUILD_ARCH=native. For overridden BUILD_ARCH we
# minimally use -msse4.1. This seems to work with MKL.
set(INTRINSICS "")
if(BUILD_ARCH STREQUAL "native")
message(STATUS "Checking support for CPU intrinsics")
include(FindSSE)
if(SSE2_FOUND)
message(STATUS "SSE2 support found")
set(INTRINSICS "${INTRINSICS} -msse2")
endif(SSE2_FOUND)
if(SSE3_FOUND)
message(STATUS "SSE3 support found")
set(INTRINSICS "${INTRINSICS} -msse3")
endif(SSE3_FOUND)
if(SSE4_1_FOUND)
message(STATUS "SSE4.1 support found")
set(INTRINSICS "${INTRINSICS} -msse4.1")
endif(SSE4_1_FOUND)
if(AVX_FOUND)
message(STATUS "AVX support found")
set(INTRINSICS "${INTRINSICS} -mavx")
endif(AVX_FOUND)
if(AVX2_FOUND)
message(STATUS "AVX2 support found")
set(INTRINSICS "${INTRINSICS} -mavx2")
endif(AVX2_FOUND)
if(AVX512_FOUND)
message(STATUS "AVX512 support found")
set(INTRINSICS "${INTRINSICS} -mavx512f")
list(APPEND INTRINSICS_NVCC -Xcompiler\ -mavx512f)
endif(AVX512_FOUND)
else()
set(INTRINSICS "-msse4.1")
endif()
2018-12-07 08:24:33 +03:00
if(USE_FBGEMM)
set(EXT_LIBS ${EXT_LIBS} fbgemm dl)
add_definitions(-DUSE_FBGEMM=1)
endif(USE_FBGEMM)
2018-11-29 04:27:12 +03:00
set(DISABLE_GLOBALLY "-Wno-unused-result")
# These are used in src/CMakeLists.txt on a per-target basis
list(APPEND ALL_WARNINGS -Wall; -Werror; -Wno-unused-result; -Wno-deprecated; -Wno-pragmas; -Wno-unused-parameter; -Wextra; -Wno-unused-function;
-Wno-unused-value; -Wno-unknown-pragmas; -Wno-sign-compare; -Wno-missing-field-initializers;)
2018-11-29 04:27:12 +03:00
# This warning does not exist prior to gcc 5.0
2018-11-29 06:30:27 +03:00
if(CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.0)
list(APPEND ALL_WARNINGS -Wsuggest-override)
endif()
2018-11-29 04:27:12 +03:00
set(CMAKE_CXX_FLAGS "-std=c++11 -pthread -Wl,--no-as-needed -fPIC ${DISABLE_GLOBALLY} -march=${BUILD_ARCH} ${INTRINSICS}")
set(CMAKE_CXX_FLAGS_RELEASE "-Ofast -m64 -funroll-loops -ffinite-math-only -g -rdynamic")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -rdynamic")
set(CMAKE_CXX_FLAGS_SLIM "-Ofast -m64 -funroll-loops -ffinite-math-only -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_PROFILE "${CMAKE_CXX_FLAGS_RELEASE} -pg")
set(CMAKE_CXX_FLAGS_PROFGEN "${CMAKE_CXX_FLAGS_RELEASE} -fprofile-generate -fprofile-correction")
set(CMAKE_CXX_FLAGS_PROFUSE "${CMAKE_CXX_FLAGS_RELEASE} -fprofile-use -fprofile-correction")
# these need to be set separately
set(CMAKE_C_FLAGS "-pthread -Wl,--no-as-needed -fPIC ${DISABLE_GLOBALLY} -march=${BUILD_ARCH} ${INTRINSICS}")
set(CMAKE_C_FLAGS_RELEASE "-O3 -m64 -funroll-loops -ffinite-math-only -g -rdynamic")
set(CMAKE_C_FLAGS_DEBUG "-O0 -g -rdynamic")
set(CMAKE_C_FLAGS_SLIM "-O3 -m64 -funroll-loops -ffinite-math-only -DNDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_RELEASE} -pg")
set(CMAKE_C_FLAGS_PROFGEN "${CMAKE_C_FLAGS_RELEASE} -fprofile-generate -fprofile-correction")
set(CMAKE_C_FLAGS_PROFUSE "${CMAKE_C_FLAGS_RELEASE} -fprofile-use -fprofile-correction")
endif(MSVC)
2018-10-08 02:09:29 +03:00
# Downloading SentencePiece if requested and set to compile with it.
# Requires all the dependencies imposed by SentencePiece
if(USE_SENTENCEPIECE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_SENTENCEPIECE")
LIST(APPEND CUDA_NVCC_FLAGS -DUSE_SENTENCEPIECE; )
2018-11-24 10:12:55 +03:00
set(EXT_LIBS ${EXT_LIBS} sentencepiece sentencepiece_train)
2018-10-08 02:09:29 +03:00
endif()
# Find packages
2018-03-03 05:06:26 +03:00
set(EXT_LIBS ${EXT_LIBS} ${CMAKE_DL_LIBS})
if(COMPILE_CUDA)
2018-12-07 07:43:44 +03:00
if(USE_STATIC_LIBS)
2018-12-10 06:53:57 +03:00
# link statically to stdlib libraries
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
# look for libraries that have .a suffix
2018-12-07 07:43:44 +03:00
set(_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
if(WIN32)
list(INSERT CMAKE_FIND_LIBRARY_SUFFIXES 0 .lib .a)
else()
set(CMAKE_FIND_LIBRARY_SUFFIXES .a _static.a)
endif()
endif()
2018-09-14 12:04:19 +03:00
find_package(CUDA "8.0")
2017-07-04 16:06:26 +03:00
if(CUDA_FOUND)
2019-07-19 17:13:18 +03:00
# CUDA >= 10.0 requires CMake >= 3.12.2
if((CUDA_VERSION VERSION_EQUAL "10.0" OR CUDA_VERSION VERSION_GREATER "10.0") AND (CMAKE_VERSION VERSION_LESS "3.12.2"))
message(WARNING "On some Unix systems CUDA 10.0+ requires CMake 3.12.2+; you use CMake ${CMAKE_VERSION}")
2019-07-19 17:13:18 +03:00
endif()
2018-12-07 07:43:44 +03:00
if(USE_STATIC_LIBS)
find_library(CUDA_culibos_LIBRARY NAMES culibos PATHS ${CUDA_TOOLKIT_ROOT_DIR}/lib64)
set(EXT_LIBS ${EXT_LIBS} ${CUDA_curand_LIBRARY} ${CUDA_cusparse_LIBRARY} ${CUDA_culibos_LIBRARY} ${CUDA_CUBLAS_LIBRARIES})
2018-12-07 08:24:33 +03:00
message(STATUS "Found CUDA libraries: ${CUDA_curand_LIBRARY} ${CUDA_cusparse_LIBRARY} ${CUDA_culibos_LIBRARY} ${CUDA_CUBLAS_LIBRARIES}")
2018-12-07 07:43:44 +03:00
else(USE_STATIC_LIBS)
set(EXT_LIBS ${EXT_LIBS} ${CUDA_curand_LIBRARY} ${CUDA_cusparse_LIBRARY} ${CUDA_CUBLAS_LIBRARIES})
2018-12-07 08:24:33 +03:00
message(STATUS "Found CUDA libraries: ${CUDA_curand_LIBRARY} ${CUDA_cusparse_LIBRARY} ${CUDA_CUBLAS_LIBRARIES}")
2019-07-19 17:13:18 +03:00
endif(USE_STATIC_LIBS)
2018-02-25 07:11:02 +03:00
if(USE_CUDNN)
find_package(CUDNN "7.0")
if(CUDNN_FOUND)
include_directories(${CUDNN_INCLUDE_DIRS})
set(EXT_LIBS ${EXT_LIBS} ${CUDNN_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCUDNN")
LIST(APPEND CUDA_NVCC_FLAGS -DCUDNN; )
2018-03-01 15:36:27 +03:00
endif(CUDNN_FOUND)
endif(USE_CUDNN)
2018-06-25 09:07:41 +03:00
2018-03-03 05:06:26 +03:00
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCUDA_FOUND")
2018-03-05 04:50:21 +03:00
list(APPEND CUDA_NVCC_FLAGS -DCUDA_FOUND; )
2018-09-27 16:52:39 +03:00
if(MSVC)
list(APPEND CUDA_NVCC_FLAGS -DBOOST_PP_VARIADICS=0; )
endif()
2018-11-23 03:20:05 +03:00
if(USE_NCCL)
add_library(nccl STATIC IMPORTED)
2018-12-07 03:35:33 +03:00
set(EXT_LIBS ${EXT_LIBS} nccl)
2019-02-05 10:07:00 +03:00
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_NCCL")
LIST(APPEND CUDA_NVCC_FLAGS -DUSE_NCCL; )
2018-11-23 03:20:05 +03:00
endif(USE_NCCL)
2019-07-19 17:13:18 +03:00
if(USE_STATIC_LIBS)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
endif()
2018-12-07 07:43:44 +03:00
2018-03-03 05:06:26 +03:00
else(CUDA_FOUND)
2018-09-14 12:04:19 +03:00
message(FATAL_ERROR "CUDA has not been found, set -DCOMPILE_CUDA=off to avoid this check and to compile the CPU version only")
2017-07-04 16:06:26 +03:00
endif(CUDA_FOUND)
2018-03-03 05:06:26 +03:00
else(COMPILE_CUDA)
message(WARNING "COMPILE_CUDA=off : Building only CPU version")
endif(COMPILE_CUDA)
2018-09-27 16:52:39 +03:00
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
2018-09-04 17:44:04 +03:00
list(APPEND CUDA_NVCC_FLAGS --default-stream per-thread; -O0; -g; -arch=sm_30; -gencode=arch=compute_30,code=sm_30; -gencode=arch=compute_50,code=sm_50; -gencode=arch=compute_52,code=sm_52; -gencode=arch=compute_60,code=sm_60; -gencode=arch=compute_61,code=sm_61; -gencode=arch=compute_61,code=compute_61 ;)
2017-06-17 22:22:27 +03:00
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
2018-09-04 17:44:04 +03:00
list(APPEND CUDA_NVCC_FLAGS --default-stream per-thread; -O3; -g; --use_fast_math; -arch=sm_30; -gencode=arch=compute_30,code=sm_30; -gencode=arch=compute_50,code=sm_50; -gencode=arch=compute_52,code=sm_52; -gencode=arch=compute_60,code=sm_60; -gencode=arch=compute_61,code=sm_61; -gencode=arch=compute_61,code=compute_61 ;)
2017-06-17 22:22:27 +03:00
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")
2018-09-27 16:52:39 +03:00
if(NOT MSVC)
2018-11-29 04:27:12 +03:00
# @TODO: add warnings here too
list(APPEND CUDA_NVCC_FLAGS -ccbin ${CMAKE_C_COMPILER}; -std=c++11; -Xcompiler\ -fPIC; -Xcompiler\ -Wno-unused-result; -Xcompiler\ -Wno-deprecated; -Xcompiler\ -Wno-pragmas; -Xcompiler\ -Wno-unused-value; -Xcompiler\ -Werror;)
else()
list(APPEND CUDA_NVCC_FLAGS -Xcompiler\ /FS; )
2018-09-04 17:44:04 +03:00
endif()
2017-06-17 22:22:27 +03:00
2017-07-04 16:06:26 +03:00
list(REMOVE_DUPLICATES CUDA_NVCC_FLAGS)
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
if(USE_STATIC_LIBS)
set(_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
if(WIN32)
list(INSERT CMAKE_FIND_LIBRARY_SUFFIXES 0 .lib .a)
else()
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
endif()
endif()
2018-09-27 16:52:39 +03:00
if(NOT WIN32)
find_package(Tcmalloc)
if(Tcmalloc_FOUND)
include_directories(${Tcmalloc_INCLUDE_DIR})
set(EXT_LIBS ${EXT_LIBS} ${Tcmalloc_LIBRARIES})
else(Tcmalloc_FOUND)
message(WARNING "Cannot find TCMalloc library. Continuing.")
endif(Tcmalloc_FOUND)
endif()
2017-11-02 13:33:47 +03:00
2018-02-26 09:08:25 +03:00
if(USE_MPI)
2018-03-01 15:36:27 +03:00
find_package(MPI 2.0)
if(MPI_FOUND)
include_directories(${MPI_INCLUDE_PATH})
set(EXT_LIBS ${EXT_LIBS} ${MPI_LIBRARIES})
add_definitions(-DMPI_FOUND=1)
endif(MPI_FOUND)
2018-02-26 09:08:25 +03:00
endif(USE_MPI)
2018-03-01 15:36:27 +03:00
if(COMPILE_CPU)
find_package(MKL)
if(MKL_FOUND)
include_directories(${MKL_INCLUDE_DIR})
set(EXT_LIBS ${EXT_LIBS} ${MKL_LIBRARIES})
add_definitions(-DBLAS_FOUND=1 -DMKL_FOUND=1)
else(MKL_FOUND)
set(BLA_VENDOR "OpenBLAS")
find_package(BLAS)
if(BLAS_FOUND)
include_directories(${BLAS_INCLUDE_DIR})
set(EXT_LIBS ${EXT_LIBS} ${BLAS_LIBRARIES})
add_definitions(-DBLAS_FOUND=1)
endif(BLAS_FOUND)
endif(MKL_FOUND)
endif(COMPILE_CPU)
2018-02-22 04:44:04 +03:00
2018-11-10 21:47:04 +03:00
set(BOOST_COMPONENTS timer iostreams filesystem system chrono)
2018-04-19 01:14:10 +03:00
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
add_definitions(-DUSE_BOOST_REGEX=1)
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} regex)
2018-12-07 08:24:33 +03:00
message(STATUS "Using boost::regex")
2018-04-19 01:14:10 +03:00
else()
2018-12-07 08:24:33 +03:00
message(STATUS "Using std::regex")
2018-04-19 01:14:10 +03:00
endif()
if(COMPILE_SERVER)
2018-01-09 17:18:57 +03:00
find_package(OpenSSL)
if(OpenSSL_FOUND)
message(STATUS "Found OpenSSL")
include_directories(${OPENSSL_INCLUDE_DIR})
set(EXT_LIBS ${EXT_LIBS} ${OPENSSL_CRYPTO_LIBRARY})
else(OpenSSL_FOUND)
message(WARNING "Cannot find OpenSSL library. Not compiling server.")
2018-01-13 22:24:03 +03:00
set(COMPILE_SERVER "off")
2018-01-09 17:18:57 +03:00
endif(OpenSSL_FOUND)
endif(COMPILE_SERVER)
2017-08-19 10:01:31 +03:00
if(USE_STATIC_LIBS)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
endif()
if(USE_STATIC_LIBS)
set(Boost_USE_STATIC_LIBS ON)
endif()
2017-08-19 15:49:55 +03:00
find_package(Boost COMPONENTS ${BOOST_COMPONENTS})
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
set(EXT_LIBS ${EXT_LIBS} ${Boost_LIBRARIES})
set(EXT_LIBS ${EXT_LIBS} ${ZLIB_LIBRARIES}) # hack for static compilation
2017-08-19 15:49:55 +03:00
else(Boost_FOUND)
2017-11-02 13:33:47 +03:00
message(SEND_ERROR "Cannot find Boost libraries. Terminating.")
2017-08-19 15:49:55 +03:00
endif(Boost_FOUND)
2017-06-14 15:06:20 +03:00
if(COMPILE_TESTS)
2017-07-13 15:02:43 +03:00
enable_testing()
2017-06-14 15:06:20 +03:00
endif(COMPILE_TESTS)
2018-02-25 04:56:33 +03:00
if(COMPILE_EXAMPLES)
add_definitions(-DCOMPILE_EXAMPLES=1)
endif(COMPILE_EXAMPLES)
2017-07-04 16:06:26 +03:00
# Generate project_version.h to reflect our version number
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/common/project_version.h.in
2018-10-25 04:01:45 +03:00
${CMAKE_CURRENT_SOURCE_DIR}/src/common/project_version.h @ONLY)
2017-07-04 16:06:26 +03:00
# Compile source files
2016-05-07 23:56:23 +03:00
include_directories(${marian_SOURCE_DIR}/src)
add_subdirectory(src)
2016-09-16 19:28:00 +03:00
2017-07-04 16:06:26 +03:00
2017-06-14 15:06:20 +03:00
# Add a target to generate API documentation with Doxygen
2016-09-16 19:28:00 +03:00
find_package(Doxygen)
if(DOXYGEN_FOUND)
2017-07-13 15:02:43 +03:00
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in
${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_target(doc
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen" VERBATIM
)
2016-09-16 19:28:00 +03:00
endif(DOXYGEN_FOUND)
2018-09-16 19:50:53 +03:00