Skip to content

Commit

Permalink
Updates to Python Bindings:
Browse files Browse the repository at this point in the history
  • Loading branch information
Pencilcaseman committed Oct 20, 2023
1 parent 00d7a40 commit 845a037
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 161 deletions.
190 changes: 30 additions & 160 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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 <bin> 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)
Expand Down Expand Up @@ -630,7 +501,6 @@ if (IS_WINDOWS AND LIBRAPID_NO_WINDOWS_H)
NOWINOFFSETS
NOWINMESSAGES
)

endif ()

if (LIBRAPID_GET_FFTW)
Expand Down
Empty file added cmake/pythonAPI.cmake
Empty file.
1 change: 1 addition & 0 deletions librapid/bindings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("YOYOYO WAZZUP")
1 change: 1 addition & 0 deletions librapid/bindings/python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello from _librapid")
24 changes: 24 additions & 0 deletions librapid/bindings/python/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#define LIBRAPID_ASSERT

#include <librapid/librapid.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>

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;
}
});
}
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 845a037

Please sign in to comment.