From 845a037f0fab3e7dddea7c5e7a05ac75e988e770 Mon Sep 17 00:00:00 2001 From: Toby Davis Date: Fri, 20 Oct 2023 12:21:09 +0100 Subject: [PATCH] Updates to Python Bindings: --- CMakeLists.txt | 190 +++++---------------------- cmake/pythonAPI.cmake | 0 librapid/bindings/__init__.py | 1 + librapid/bindings/python/__init__.py | 1 + librapid/bindings/python/test.cpp | 24 ++++ setup.py | 1 - 6 files changed, 56 insertions(+), 161 deletions(-) create mode 100644 cmake/pythonAPI.cmake create mode 100644 librapid/bindings/__init__.py create mode 100644 librapid/bindings/python/__init__.py create mode 100644 librapid/bindings/python/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 88d774548..b658e0bd4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,8 +83,36 @@ file(GLOB_RECURSE LIBRAPID_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/librapid/cxxblas/*.cxx" # Source files ) -set(module_name "librapid") -add_library(${module_name} STATIC ${LIBRAPID_SOURCES}) +if (${SKBUILD}) + message(STATUS "[ LIBRAPID ] Building for Python") + set(module_name "_librapid") + + set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE) + + message(STATUS "[ LIBRAPID ] Cloning PyBind11") + FetchContent_Declare( + pybind11 + GIT_REPOSITORY https://github.com/pybind/pybind11.git + ) + FetchContent_MakeAvailable(pybind11) + + file(GLOB_RECURSE PYTHON_SOURCES + "${CMAKE_CURRENT_SOURCE_DIR}/librapid/bindings/python/*.hpp" # Header files + "${CMAKE_CURRENT_SOURCE_DIR}/librapid/bindings/python/*.cpp" # Source files + ) + + pybind11_add_module( + ${module_name} MODULE + + ${LIBRAPID_SOURCES} + ${PYTHON_SOURCES} + ) + + install(TARGETS ${module_name} DESTINATION .) +else() + set(module_name "librapid") + add_library(${module_name} STATIC ${LIBRAPID_SOURCES}) +endif() # clang-format off target_compile_definitions(${module_name} PUBLIC LIBRAPID_MAJOR=${LIBRAPID_MAJOR}) @@ -172,163 +200,6 @@ if (LIBRAPID_USE_BLAS) configure_blas() endif () -## If LIBRAPID_MKL_CONFIG_PATH is set, use that instead of the default -#if (LIBRAPID_MKL_CONFIG_PATH) -# message(STATUS "[ LIBRAPID ] Using MKLConfig.cmake from ${LIBRAPID_MKL_CONFIG_PATH}") -# include(${LIBRAPID_MKL_CONFIG_PATH}) -# message(STATUS "[ LIBRAPID ] Linking against MKL") -# message(STATUS "[ LIBRAPID ] MKL Libraries: ${MKL_LIBRARIES}") -# message(STATUS "[ LIBRAPID ] MKL Include Directories: ${MKL_INCLUDE}") -# target_link_libraries(${module_name} PUBLIC MKL::MKL) -# target_include_directories(${module_name} PUBLIC ${MKL_INCLUDE}) -# target_compile_definitions(${module_name} PUBLIC LIBRAPID_HAS_BLAS) -# set(LIBRAPID_HAS_BLAS ON) -# set_blas_definition("MKL") -#endif () -# -#if (LIBRAPID_GET_BLAS AND NOT LIBRAPID_HAS_BLAS) -# message(STATUS "[ LIBRAPID ] Downloading OpenBLAS Build...") -# -# FetchContent_Declare( -# BuildOpenBLAS -# GIT_REPOSITORY https://github.com/LibRapid/BuildOpenBLAS.git -# ) -# -# FetchContent_MakeAvailable(BuildOpenBLAS) -# -# set(BLAS_FOUND TRUE) -# set(LIBRAPID_USE_BLAS TRUE) -# -# if (${IS_WINDOWS}) -# # Use openblas-windows-latest -# set(BLAS_LIBRARIES "${FETCHCONTENT_BASE_DIR}/buildopenblas-src/openblas-windows-latest/lib/openblas.lib") -# elseif (${IS_MACOS}) -# # Use openblas-macos-latest -# set(BLAS_LIBRARIES "${FETCHCONTENT_BASE_DIR}/buildopenblas-src/openblas-macos-latest/lib/libopenblas.a") -# else () # Linux and other systems -# # Use openblas-ubuntu-latest -# set(BLAS_LIBRARIES "${FETCHCONTENT_BASE_DIR}/buildopenblas-src/openblas-ubuntu-latest/lib/libopenblas.a") -# endif () -#endif () -# -## See if BLAS should be linked against -#if (LIBRAPID_USE_BLAS AND NOT LIBRAPID_HAS_BLAS) -# if (${BLAS_FOUND}) -# message(STATUS "[ LIBRAPID ] BLAS located was ${BLAS_LIBRARIES}") -# -# list(GET ${BLAS_LIBRARIES} 0 LIBRAPID_BLAS) -# -# if (NOT ${LIBRAPID_BLAS}) -# set(LIBRAPID_BLAS ${BLAS_LIBRARIES}) -# endif () -# -# message(STATUS "[ LIBRAPID ] Using BLAS (" ${LIBRAPID_BLAS} ")") -# -# get_filename_component(filepath ${LIBRAPID_BLAS} DIRECTORY) -# get_filename_component(filename ${LIBRAPID_BLAS} NAME) -# -# # Attempt to identify which BLAS library is being used and -# # set the appropriate compile definition -# set_blas_definition_from_file(filename) -# -# # Copy include files -# set(inc_path "${filepath}/../include") -# message(STATUS "[ LIBRAPID ] Checking path ${inc_path} for include files") -# FILE(GLOB_RECURSE files "${filepath}/..") -# message(STATUS "[ LIBRAPID ] Information: ${files}") -# if (NOT (EXISTS ${inc_path})) -# message(STATUS "[ LIBRAPID ] Could not locate include path for BLAS") -# endif () -# -# set(has_cblas OFF) -# -# if (EXISTS "${inc_path}/openblas") -# FILE(GLOB_RECURSE include_files "${inc_path}/openblas/*.*") -# foreach (file IN LISTS include_files) -# get_filename_component(inc_file ${file} NAME) -# if (${inc_file} STREQUAL "cblas.h") -# set(has_cblas ON) -# endif () -# endforeach () -# else () -# FILE(GLOB_RECURSE include_files "${inc_path}/*.*") -# foreach (file IN LISTS include_files) -# get_filename_component(inc_file ${file} NAME) -# if (${inc_file} STREQUAL "cblas.h") -# set(has_cblas ON) -# endif () -# endforeach () -# endif () -# -# if (${has_cblas}) -# if (EXISTS "${inc_path}/openblas") -# FILE(GLOB_RECURSE include_files "${inc_path}/openblas/*.*") -# foreach (file IN LISTS include_files) -# message(STATUS "[ LIBRAPID ] Found OpenBLAS include file " ${file}) -# get_filename_component(inc_file ${file} NAME) -# configure_file(${file} "${CMAKE_CURRENT_SOURCE_DIR}/librapid/blas/${inc_file}" COPYONLY) -# endforeach () -# endif () -# # else () -# -# FILE(GLOB_RECURSE include_files "${inc_path}/*.*") -# foreach (file IN LISTS include_files) -# message(STATUS "[ LIBRAPID ] Found include file " ${file}) -# get_filename_component(inc_file ${file} NAME) -# configure_file(${file} "${CMAKE_CURRENT_SOURCE_DIR}/librapid/blas/${inc_file}" COPYONLY) -# endforeach () -# -# # endif () -# -# # Copy library files -# get_filename_component(lib_name ${LIBRAPID_BLAS} NAME) -# # message(STATUS "[ LIBRAPID ] Found library file ${lib_name}") -# configure_file(${LIBRAPID_BLAS} "${CMAKE_CURRENT_SOURCE_DIR}/librapid/blas/${lib_name}" COPYONLY) -# endif () -# -# # Copy binary files if on Windows -# if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") -# set(bin_path "${filepath}/../bin") -# if (NOT (EXISTS ${bin_path})) -# message(WARNING "Could not locate folder for BLAS") -# else () -# FILE(GLOB_RECURSE include_files "${bin_path}/*.dll") -# foreach (file IN LISTS include_files) -# message(STATUS "[ LIBRAPID ] Found binary file " ${file}) -# get_filename_component(filename ${file} NAME) -# configure_file(${file} "${CMAKE_CURRENT_SOURCE_DIR}/librapid/blas/${filename}" COPYONLY) -# endforeach () -# -# FILE(GLOB_RECURSE bin_files "${CMAKE_CURRENT_SOURCE_DIR}/*.dll") -# foreach (file IN LISTS bin_files) -# message(STATUS "[ LIBRAPID ] Found packaged binary file " ${file}) -# get_filename_component(filename ${file} NAME) -# configure_file(${file} "${CMAKE_CURRENT_SOURCE_DIR}/librapid/blas/${filename}" COPYONLY) -# endforeach () -# endif () -# endif () -# -# # Add the compile definition so LibRapid knows it has BLAS -# if (${has_cblas}) -# # Link the required library -# target_link_libraries(${module_name} PUBLIC -# "${CMAKE_CURRENT_SOURCE_DIR}/librapid/blas/${lib_name}" -# ) -# -# target_include_directories(${module_name} PUBLIC -# "${CMAKE_CURRENT_SOURCE_DIR}/librapid/blas" -# ) -# -# target_compile_definitions(${module_name} PUBLIC LIBRAPID_HAS_BLAS) -# set(LIBRAPID_HAS_BLAS true) -# else () -# message(WARNING "Although BLAS was found, no cblas.h file was found, so BLAS support is not enabled") -# endif () -# else () -# message(WARNING "BLAS support was requested but a valid BLAS interface was not found") -# endif () -#endif () - # Check if CUDA should be used if (LIBRAPID_USE_CUDA) find_package(CUDAToolkit QUIET) @@ -630,7 +501,6 @@ if (IS_WINDOWS AND LIBRAPID_NO_WINDOWS_H) NOWINOFFSETS NOWINMESSAGES ) - endif () if (LIBRAPID_GET_FFTW) diff --git a/cmake/pythonAPI.cmake b/cmake/pythonAPI.cmake new file mode 100644 index 000000000..e69de29bb diff --git a/librapid/bindings/__init__.py b/librapid/bindings/__init__.py new file mode 100644 index 000000000..54168be44 --- /dev/null +++ b/librapid/bindings/__init__.py @@ -0,0 +1 @@ +print("YOYOYO WAZZUP") \ No newline at end of file diff --git a/librapid/bindings/python/__init__.py b/librapid/bindings/python/__init__.py new file mode 100644 index 000000000..fb0263081 --- /dev/null +++ b/librapid/bindings/python/__init__.py @@ -0,0 +1 @@ +print("Hello from _librapid") \ No newline at end of file diff --git a/librapid/bindings/python/test.cpp b/librapid/bindings/python/test.cpp new file mode 100644 index 000000000..2d53c34ee --- /dev/null +++ b/librapid/bindings/python/test.cpp @@ -0,0 +1,24 @@ +#define LIBRAPID_ASSERT + +#include +#include +#include +#include + +namespace py = pybind11; +namespace lrc = librapid; + +// Docstring for the module +std::string moduleDocstring = "A highly-optimised Python library for numeric calculations"; + +PYBIND11_MODULE(_librapid, module) { + module.doc() = moduleDocstring; + + module.def("test", [](uint64_t n) { + if (n & 1) { + return 3 * n + 1; + } else { + return n / 2; + } + }); +} diff --git a/setup.py b/setup.py index 316e6862f..4ae014b09 100644 --- a/setup.py +++ b/setup.py @@ -103,7 +103,6 @@ def __lt__(self, other): description="A highly optimised C++ library for high-performance computing", long_description=long_description, long_description_content_type="text/markdown", - # packages=ic(["librapid"] + ["librapid." + mod for mod in find_packages("librapid/python")]), packages=ic(["librapid." + mod for mod in find_packages("librapid")]), package_dir={"": "librapid"}, cmake_args=cmakeArgs,