Skip to content

Commit

Permalink
Add python binding for Mersenne-Twister (#1942)
Browse files Browse the repository at this point in the history
  • Loading branch information
flomnes authored Feb 22, 2024
1 parent b181904 commit 86f4764
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ Makefile
*.layout
src/.vs

src/config.h
src/config/include
src/config/include/antares/config/config.h

# Yuni
src/ext/yuni/src/ProfileBuild.cmake
Expand Down
7 changes: 6 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ message(STATUS "Build OR-Tools: ${BUILD_ORTOOLS}")
option(BUILD_MINIZIP "Build minizip" OFF)
message(STATUS "Build minizip: ${BUILD_MINIZIP}")

option(BUILD_MERSENNE_TWISTER_PYBIND11 "Build pybind11 bindings for Mersenne-Twister" OFF)
if (${BUILD_MERSENNE_TWISTER_PYBIND11})
find_package(pybind11 REQUIRED)
endif()

#Define install directory
if (NOT DEPS_INSTALL_DIR)
SET(DEPS_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../rte-antares-deps-${CMAKE_BUILD_TYPE})
Expand Down Expand Up @@ -459,4 +464,4 @@ endif()
include(CPack)

#Last. We need all target defined
add_subdirectory(packaging)
add_subdirectory(packaging)
10 changes: 9 additions & 1 deletion src/libs/antares/mersenne-twister/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ target_include_directories(${PROJ}

install(DIRECTORY include/antares
DESTINATION "include"
)
)

if (${BUILD_MERSENNE_TWISTER_PYBIND11})
pybind11_add_module(mersenne_twister_pybind11 mersenne_twister_pybind11.cpp)
target_link_libraries(mersenne_twister_pybind11
PRIVATE
${PROJ}
yuni-static-core)
endif()
15 changes: 15 additions & 0 deletions src/libs/antares/mersenne-twister/mersenne_twister_pybind11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <pybind11/pybind11.h>
#include <antares/mersenne-twister/mersenne-twister.h>

namespace py = pybind11;

PYBIND11_MODULE(mersenne_twister_pybind11, m) {
using namespace Antares;
py::class_<Antares::MersenneTwister>(m, "mersenne_twister")
.def(py::init<>()) // default constructor
.def("reset", py::overload_cast<uint>(&MersenneTwister::reset))
.def("next", &MersenneTwister::next)
.def_static("min", &MersenneTwister::min)
.def_static("max", &MersenneTwister::max);
}

0 comments on commit 86f4764

Please sign in to comment.