Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
mberaha committed Sep 24, 2023
2 parents 4cb9727 + a5a93eb commit 548e9ff
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 192 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ cmake-build-debug/
# lib/_deps/ folder
lib/_deps/
# .old folders
src/hierarchies/updaters/.old/
test/.old/
src/hierarchies/updaters/.old/
examples/gamma_hierarchy/.old/
# .env file
.env
140 changes: 55 additions & 85 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ cmake_minimum_required(VERSION 3.20.0)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
message("CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}")

# Check if ccache is present to speed up compilation time
find_program(CCACHE_PROGRAM ccache)
if (CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()

# Project name
project(bayesmix)

# Set cmake variables
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release)

set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -funroll-loops -ftree-vectorize -Wno-deprecated")
set(CMAKE_CXX_FLAGS_DEBUG "-Og")
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)
set(CMAKE_FIND_PACKAGE_TARGETS_GLOBAL TRUE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Require PkgConfig
find_package(PkgConfig REQUIRED)
Expand All @@ -32,64 +35,38 @@ set(FETCHCONTENT_BASE_DIR ${CMAKE_CURRENT_LIST_DIR}/lib/_deps)
set(FETCHCONTENT_QUIET OFF)
set(FETCHCONTENT_UPDATES_DISCONNECTED ON)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_TESTING OFF)

# Make bayesmix-dev/math available
message(CHECK_START "Fetching math")
FetchContent_Declare(math
GIT_REPOSITORY "https://github.com/bayesmix-dev/math.git"
GIT_TAG "develop"
)
FetchContent_MakeAvailable(math)
message(CHECK_PASS "fetched")

# TBB CMake integration
message(STATUS "Using math TBB")
# Define TBB_ROOT Folder
set(TBB_ROOT ${math_SOURCE_DIR}/lib/tbb)
file(COPY ${math_SOURCE_DIR}/lib/tbb_2020.3/ DESTINATION ${TBB_ROOT})
# Build TBB Library with CMake Integration
include(${TBB_ROOT}/cmake/TBBBuild.cmake)
list(APPEND MAKE_ARGS "tbb_build_dir=${TBB_ROOT}")
list(APPEND MAKE_ARGS "tbb_build_prefix=tbb")
tbb_build(TBB_ROOT ${TBB_ROOT} CONFIG_DIR TBB_DIR MAKE_ARGS ${MAKE_ARGS})
# Require TBB library (for compile / link options)
find_package(TBB)

# Check if Protobuf is present in system
find_package(Protobuf)
if (NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND AND NOT TARGET protobuf::libprotobuf)

# if (${CMAKE_VERSION} VERSION_LESS "3.20.0")
# message(FATAL_ERROR
# "Your cmake version is too old: either install a newer version (>=3.20)"
# " or install google's protocol buffer (protobuf) library by hand.")
# endif()

message(CHECK_START "Fetching Protobuf")
list(APPEND CMAKE_MESSAGE_INDENT " ")

option(protobuf_BUILD_TESTS "" OFF)
set(protobuf_BUILD_EXPORT OFF)
set(protobuf_MSVC_STATIC_RUNTIME OFF)
FetchContent_Declare(protobuf
GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
GIT_TAG "v3.14.0"
GIT_SUBMODULES ""
SOURCE_SUBDIR cmake)
FetchContent_MakeAvailable(protobuf)

list(POP_BACK CMAKE_MESSAGE_INDENT)
message(CHECK_PASS "fetched")
message("Protobuf_FOUND ${Protobuf_FOUND}")
message(" --> PROTOBUF LIB: ${PROTOBUF_LIBRARIES}")
message(" --> PROTOBUF INCLUDE: ${PROTOBUF_INCLUDE_DIRS}")
message(" --> PROTOBUF VERSION: ${Protobuf_VERSION}")
message(" --> PROTOBUF Found: ${Protobuf_FOUND}")
# Set BASEPATH variable
set(BASEPATH "${CMAKE_CURRENT_LIST_DIR}")

# Make available bayesmix-dev/math (and TBB)
include(cmake/math.cmake)

# Check math has been populated
if(math_POPULATED)
message(STATUS "math source dir: ${math_SOURCE_DIR}")
message(STATUS "math binary dir: ${math_BINARY_DIR}")
else()
message(FATAL_ERROR "math library required but not found!")
endif()

# Make available protocolbuffers/protobuf (v3.16.0)
include(cmake/protobuf.cmake)

# Check if protobuf is found
if(Protobuf_FOUND)
message(STATUS "Protobuf version : ${Protobuf_VERSION}")
message(STATUS "Protobuf include path : ${Protobuf_INCLUDE_DIRS}")
message(STATUS "Protobuf libraries : ${Protobuf_LIBRARIES}")
message(STATUS "Protobuf compiler libraries : ${Protobuf_PROTOC_LIBRARIES}")
message(STATUS "Protobuf lite libraries : ${Protobuf_LITE_LIBRARIES}")
message(STATUS "Protobuf protoc : ${Protobuf_PROTOC_EXECUTABLE}")
else()
message(FATAL_ERROR "Protobuf library required but not found!")
endif()

# Define project options
option(DISABLE_TESTS
"If tests should be compiled or no" OFF)
option(ENABLE_BENCHMARKS
Expand All @@ -98,32 +75,33 @@ option(ENABLE_DOCS
"If docs should be generated or no" OFF)
option(BUILD_RUN "" ON)

# set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
# Include pathstests
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(BASEPATH "${CMAKE_CURRENT_LIST_DIR}")
set(INCLUDE_PATHS
${BASEPATH}
${math_SOURCE_DIR}
${math_SOURCE_DIR}/lib/boost_1.78.0
${math_SOURCE_DIR}/lib/eigen_3.4.0
${math_SOURCE_DIR}/lib/sundials_6.1.1/include
${TBB_ROOT}/include
${math_SOURCE_DIR}/lib/tbb_2020.3/include
${CMAKE_CURRENT_BINARY_DIR}
${protobuf_SOURCE_DIR}/src
${Protobuf_INCLUDE_DIRS}
)

# Link paths
set(LINK_LIBRARIES
pthread
protobuf::libprotobuf
TBB::tbb
pthread
protobuf::libprotobuf
tbb
OpenMP::OpenMP_CXX
)

# Compiler options
set(COMPILE_OPTIONS -D_REENTRANT -fPIC)

# Compile proto files
file(GLOB ProtoFiles "${BASEPATH}/src/proto/*.proto")
set(PROTO_DIR proto)

foreach(PROTO_FILE IN LISTS ProtoFiles)
message(STATUS "protoc proto(cc): ${PROTO_FILE}")
get_filename_component(PROTO_DIR ${PROTO_FILE} DIRECTORY)
Expand All @@ -145,9 +123,9 @@ foreach(PROTO_FILE IN LISTS ProtoFiles)
list(APPEND PROTO_HDRS ${PROTO_HDR})
list(APPEND PROTO_SRCS ${PROTO_SRC})
endforeach()

SET_SOURCE_FILES_PROPERTIES(${PROTO_SRCS} ${PROTO_HDRS} PROPERTIES GENERATED TRUE)

# Export variables to parent scope if bayesmix is used as dependency library
get_directory_property(HAS_PARENT PARENT_DIRECTORY)
if(HAS_PARENT)
set(BAYESMIX_INCLUDE_PATHS ${INCLUDE_PATHS} PARENT_SCOPE)
Expand All @@ -170,60 +148,52 @@ target_compile_options(bayesmix PUBLIC ${COMPILE_OPTIONS})
# Build static library
add_library(bayesmixlib $<TARGET_OBJECTS:bayesmix>)

# Build run executable
if (BUILD_RUN)
# Build run executable
add_executable(run_mcmc $<TARGET_OBJECTS:bayesmix> executables/run_mcmc.cc)
target_include_directories(run_mcmc PUBLIC ${INCLUDE_PATHS})
target_link_libraries(run_mcmc PUBLIC ${LINK_LIBRARIES})
target_compile_options(run_mcmc PUBLIC ${COMPILE_OPTIONS})
endif()

# Add tests
if (NOT DISABLE_TESTS)
add_subdirectory(test)
endif()

# Add benchmarks
if (ENABLE_BENCHMARKS)
add_subdirectory(benchmarks)
endif()

# Add docs
if (ENABLE_DOCS)
add_subdirectory(docs)
endif()

if (NOT DISABLE_PLOTS)

# Check version (not needed)
if (${CMAKE_VERSION} VERSION_LESS "3.20.0")
message(FATAL_ERROR
"Your cmake version is too old: version >= 3.20.0 is required for plotting.")
# Make available matplotplusplus
include(cmake/matplotplusplus.cmake)
# Check matplotplusplus has been populated
if(matplotplusplus_POPULATED)
message(STATUS "matplotplusplus source dir: ${matplotplusplus_SOURCE_DIR}")
message(STATUS "matplotplusplus binary dir: ${matplotplusplus_BINARY_DIR}")
else()
message(FATAL_ERROR "matplotplusplus library required but not found!")
endif()

# Define patch command to inject
set(matplotplusplus_patch git apply ${CMAKE_CURRENT_LIST_DIR}/resources/patches/matplotplusplus.patch)

# Make matplotplusplus available (+ patch)
message(CHECK_START "Fetching matplotplusplus")
FetchContent_Declare(matplotplusplus
GIT_REPOSITORY "https://github.com/alandefreitas/matplotplusplus"
GIT_TAG "origin/master"
PATCH_COMMAND ${matplotplusplus_patch}
)
FetchContent_MakeAvailable(matplotplusplus)
message(CHECK_PASS "fetched")

# Build plot_mcmc executable
add_executable(plot_mcmc
$<TARGET_OBJECTS:bayesmix>
executables/plot_mcmc.cc
src/plots/plot_utils.h
src/plots/plot_utils.cc
)

target_include_directories(plot_mcmc PUBLIC ${INCLUDE_PATHS} ${matplotplusplus_SOURCE_DIR}/source)
target_link_libraries(plot_mcmc PUBLIC ${LINK_LIBRARIES} matplot)
target_compile_options(plot_mcmc PUBLIC ${COMPILE_OPTIONS})

endif()

# Add examples
if (NOT DISABLE_EXAMPLES)
add_subdirectory(examples)
endif()
111 changes: 111 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Installation

Compiling and building `bayesmix` requires a modern C++ compiler, `git`, the GNU `make` utility, `cmake` (version >= 3.20) and `pkg-config`. Make these packages available varies between operating systems.

**Warning**: Check the version of `cmake` available in your package manager default repository. You may need to install a more recent version in another way. If this is your case, please go to the [official CMake GitHub repository](https://github.com/Kitware/CMake) and follow the installation instructions.

## Requirements - Linux

On Linux systems, the required packages can be installed via the system's default package manager. For instance, in Ubuntu or other Debian based distros it is sufficient to run

```shell
sudo apt install git g++ make cmake pkg-config
```

## Requirements - MacOS

On MacOS, the required packages can be installed via [Homebrew](https://brew.sh/). Once installed, it is sufficient to run

```shell
brew install git g++ make cmake pkg-config
```

## Requirements - Windows

First of all, install `git` via the [Git for Windows](https://gitforwindows.org/) project. Download the [installer](https://github.com/git-for-windows/git/releases/latest) and complete the prompts leaving default choices to install. The Git BASH that comes with this program is the shell we suggest to compile and run `bayesmix`.

On Windows, we also need the installation of a proper C++ toolchain to install the other required packages. `bayesmix` can be successfully compiled and installed with RTools40, RTools42 and RTools43 toolchains. This choice simplified the development on a lightweight `R` interface working on all platforms.

[Rtools](https://cran.r-project.org/bin/windows/Rtools/) is a toolchain bundle used for building `R` packages from source (those that need compilation of C/C++ or Fortran code) and for building `R` itself. Rtools usually consists of Msys2 build tools, GCC/MinGW-w64 compiler toolchain and libraries. These require slightly different steps to configure, so please follow the appropriate steps below.

All toolchains will require updating your `PATH` variable, See [these instructions](https://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/) for details on changing the `PATH` if you are unfamiliar. The following instructions will assume that the default installation directory was used, so be sure to update the paths accordingly if you have chosen a different directory.

### Configure RTools40

Download the [installer](https://github.com/r-windows/rtools-installer/releases/download/2022-02-06/rtools40-x86_64.exe) and complete the prompts to install.
Next, add the following lines to your `PATH`:

```shell
C:\rtools40\usr\bin
C:\rtools40\mingw64\bin
```

The C++ compiler is now available on your system. All the other dependencies can be installed by typing the following commands into one of the shells installed with RTools (e.g. launch the `C:\rtools40\msys.exe` file)

```shell
pacman -Sy mingw-w64-x86_64-make
pacman -Sy mingw-w64-x86_64-cmake
pacman -Sy mingw-w64-x86_64-pkg-config
```

### Configure RTools42 / RTools43

These two versions of RTools toolchain are quite similar. Of course, RTools43 offers newer version of the packages, but in both cases the installation and configuration is identical.
Download either the [RTools42 installer](https://cran.r-project.org/bin/windows/Rtools/rtools42/files/rtools42-5355-5357.exe) or the [RTools43 installer](https://cran.r-project.org/bin/windows/Rtools/rtools43/files/rtools43-5550-5548.exe) and complete the prompts to install.
Next, add the following lines to your `PATH`:

```shell
# RTools42
C:\rtools42\x86_64-w64-mingw32.static.posix\bin
C:\rtools42\ucrt64\bin
C:\rtools42\usr\bin

# RTools43
C:\rtools43\x86_64-w64-mingw32.static.posix\bin
C:\rtools43\ucrt64\bin
C:\rtools43\usr\bin
```

The C++ compiler is now available on your system. All the other dependencies can be installed by typing the following commands into one of the shells installed with RTools (e.g. lauch the `C:\rtools42\msys.exe` or `C:\rtools43\msys.exe` file)

```shell
pacman -Sy mingw-w64-ucrt-x86_64-make
pacman -Sy mingw-w64-ucrt-x86_64-cmake
pacman -Sy mingw-w64-ucrt-x86_64-pkgconf
```

### Important remarks :

- Use the Git BASH shell available with Git for Windows to execute these commands. If `PATH` environment variable has been configured correctly, all requirements will be satisfied.
- In order for `bayesmix` to be properly linked to Intel's TBB library, the absolute path to `tbb` must be added to the User `PATH` variable. This is done automatically during build but to make this change effective user need to close and open a new Git BASH shell.

## Build `bayesmix`

To install and use `bayesmix`, please `cd` to the folder to which you wish to install it, and clone this repository with the following command-line instruction:

```shell
git clone https://github.com/bayesmix-dev/bayesmix.git
```

Then, by using `cd bayesmix`, you will enter the newly downloaded folder.

To build the executable for the main file `run_mcmc.cc`, please use the following list of commands:

```shell
mkdir build
cd build
cmake .. -DDISABLE_TESTS=ON
make run_mcmc
cd ..
```

Finally, to compile unit tests, please use the following commands:

```shell
cd build
cmake ..
make test_bayesmix
cd ..
```

The corresponding executable is located at `build/test/test_bayesmix`.
Loading

0 comments on commit 548e9ff

Please sign in to comment.