From 7aff37256a3a82da124c5752fabaac45d72a626b Mon Sep 17 00:00:00 2001 From: kaihsin Date: Tue, 3 Dec 2024 01:04:09 -0500 Subject: [PATCH 1/9] tmp --- CMakeLists.txt | 21 ++++++++++++++++----- pyproject.toml | 1 + src/cpp/include/{ => cytnx_core}/test.hpp | 4 ++-- src/cpp/pybind/main.cpp | 4 +++- src/cpp/src/test.cpp | 5 +++-- 5 files changed, 25 insertions(+), 10 deletions(-) rename src/cpp/include/{ => cytnx_core}/test.hpp (52%) diff --git a/CMakeLists.txt b/CMakeLists.txt index d5dffc2..630eec6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,8 +71,7 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/include/ COMPONENT headers FILES_MATCHING PATTERN "*.h*") -export(EXPORT cytnx_targets FILE ${CMAKE_CURRENT_BINARY_DIR}/CytnxTargets.cmake NAMESPACE cytnx_core::) -export(PACKAGE cytnx_core) + # ##################################################################### # Pybind @@ -80,8 +79,20 @@ export(PACKAGE cytnx_core) set(PYBIND11_FINDPYTHON ON) find_package(pybind11 CONFIG REQUIRED) -pybind11_add_module(pycytnx MODULE src/cpp/pybind/main.cpp) -target_link_libraries(pycytnx PUBLIC cytnx_core) +pybind11_add_module(pycytnx_core SHARED src/cpp/pybind/main.cpp) +target_link_libraries(pycytnx_core PUBLIC cytnx_core) #install(TARGETS pycytnx DESTINATION ${SKBUILD_PROJECT_NAME}) -install(TARGETS pycytnx DESTINATION ${CMAKE_INSTALL_PREFIX}/cytnx_core) +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/cytnx_core + DESTINATION ${CMAKE_INSTALL_PREFIX} +) +install(TARGETS pycytnx_core + LIBRARY + DESTINATION ${CMAKE_INSTALL_PREFIX}/cytnx_core + COMPONENT libraries +) message(STATUS " skbuild Installation Prefix: ${SKBUILD_PROJECT_NAME}") +message(STATUS " skbuild Installation Prefix: ${SKBUILD_HEADERS_DIR}") + + +export(EXPORT cytnx_targets FILE ${CMAKE_CURRENT_BINARY_DIR}/CytnxTargets.cmake NAMESPACE cytnx_core::) +export(PACKAGE cytnx_core) diff --git a/pyproject.toml b/pyproject.toml index 956df6e..d0c3dd3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ dependencies = [] [tool.scikit-build] minimum-version = "build-system.requires" build-dir = "build/{wheel_tag}" +build.tool-args = ["-j4"] [tool.black] line-length = 88 diff --git a/src/cpp/include/test.hpp b/src/cpp/include/cytnx_core/test.hpp similarity index 52% rename from src/cpp/include/test.hpp rename to src/cpp/include/cytnx_core/test.hpp index e8345bc..de09796 100644 --- a/src/cpp/include/test.hpp +++ b/src/cpp/include/cytnx_core/test.hpp @@ -1,9 +1,9 @@ #pragma once -#include +#include namespace cytnx_core { -void test(); +std::string test(); } diff --git a/src/cpp/pybind/main.cpp b/src/cpp/pybind/main.cpp index 9a57ecd..f01883b 100644 --- a/src/cpp/pybind/main.cpp +++ b/src/cpp/pybind/main.cpp @@ -1,6 +1,8 @@ +#include #include +#include -std::string hello_from_bin() { return "Hello from Kai!"; } +std::string hello_from_bin() { return cytnx_core::test(); } namespace py = pybind11; diff --git a/src/cpp/src/test.cpp b/src/cpp/src/test.cpp index c3e8fcb..9cb0127 100644 --- a/src/cpp/src/test.cpp +++ b/src/cpp/src/test.cpp @@ -1,4 +1,5 @@ -#include "test.hpp" +#include "cytnx_core/test.hpp" #include +#include -void test() { std::cout << "Hello from test" << std::endl; } +std::string test() { return "Hello from test"; } From 55e13e91c49e82c74399b8fe6947754be6b3b84a Mon Sep 17 00:00:00 2001 From: kaihsin Date: Tue, 3 Dec 2024 02:34:34 -0500 Subject: [PATCH 2/9] fixed linking, binding module name --- CMakeLists.txt | 19 +++++++------------ src/cpp/pybind/main.cpp | 2 +- src/cpp/src/CMakeLists.txt | 1 - src/cpp/src/test.cpp | 4 +++- src/cytnx_core/__init__.py | 2 +- 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 630eec6..d93853e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,8 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/include/ FILES_MATCHING PATTERN "*.h*") +export(EXPORT cytnx_targets FILE ${CMAKE_CURRENT_BINARY_DIR}/CytnxTargets.cmake NAMESPACE cytnx_core::) +export(PACKAGE cytnx_core) # ##################################################################### # Pybind @@ -79,20 +81,13 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/include/ set(PYBIND11_FINDPYTHON ON) find_package(pybind11 CONFIG REQUIRED) -pybind11_add_module(pycytnx_core SHARED src/cpp/pybind/main.cpp) -target_link_libraries(pycytnx_core PUBLIC cytnx_core) -#install(TARGETS pycytnx DESTINATION ${SKBUILD_PROJECT_NAME}) +pybind11_add_module(_core MODULE src/cpp/pybind/main.cpp) +target_link_libraries(_core PUBLIC cytnx_core) + install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/cytnx_core DESTINATION ${CMAKE_INSTALL_PREFIX} ) -install(TARGETS pycytnx_core - LIBRARY - DESTINATION ${CMAKE_INSTALL_PREFIX}/cytnx_core - COMPONENT libraries -) +install(TARGETS _core LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/cytnx_core) +#install(TARGETS pycytnx DESTINATION ${SKBUILD_PROJECT_NAME}) message(STATUS " skbuild Installation Prefix: ${SKBUILD_PROJECT_NAME}") message(STATUS " skbuild Installation Prefix: ${SKBUILD_HEADERS_DIR}") - - -export(EXPORT cytnx_targets FILE ${CMAKE_CURRENT_BINARY_DIR}/CytnxTargets.cmake NAMESPACE cytnx_core::) -export(PACKAGE cytnx_core) diff --git a/src/cpp/pybind/main.cpp b/src/cpp/pybind/main.cpp index f01883b..de76e29 100644 --- a/src/cpp/pybind/main.cpp +++ b/src/cpp/pybind/main.cpp @@ -6,7 +6,7 @@ std::string hello_from_bin() { return cytnx_core::test(); } namespace py = pybind11; -PYBIND11_MODULE(pycytnx, m) { +PYBIND11_MODULE(_core, m) { m.doc() = "pybind11 hello module"; m.def("hello_from_bin", &hello_from_bin, R"pbdoc( diff --git a/src/cpp/src/CMakeLists.txt b/src/cpp/src/CMakeLists.txt index 7c96618..762a393 100644 --- a/src/cpp/src/CMakeLists.txt +++ b/src/cpp/src/CMakeLists.txt @@ -1,6 +1,5 @@ target_sources_local(cytnx_core PRIVATE - # put cpp files here test.cpp diff --git a/src/cpp/src/test.cpp b/src/cpp/src/test.cpp index 9cb0127..5167bb1 100644 --- a/src/cpp/src/test.cpp +++ b/src/cpp/src/test.cpp @@ -1,5 +1,7 @@ -#include "cytnx_core/test.hpp" +#include #include #include +namespace cytnx_core { std::string test() { return "Hello from test"; } +} // namespace cytnx_core diff --git a/src/cytnx_core/__init__.py b/src/cytnx_core/__init__.py index b919c0f..e581d39 100644 --- a/src/cytnx_core/__init__.py +++ b/src/cytnx_core/__init__.py @@ -1,4 +1,4 @@ -from cytnx_core.pycytnx import hello_from_bin +from cytnx_core._core import hello_from_bin def hello() -> str: From 5f1f9f9a9923fa868f195496d389374ee73cff71 Mon Sep 17 00:00:00 2001 From: kaihsin Date: Tue, 3 Dec 2024 02:38:38 -0500 Subject: [PATCH 3/9] update readme --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 84b6a64..2286d8d 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,12 @@ Running pytest: - cmake 3.15+ (see CMakeList.txt, default 3.20) * most of the deps should be able to install via pypi. + + +## Compile directly the C++ package + +```bash + $mkdir build + $cd build + $cmake ../ -DCMAKE_INSTALL_PREFIX= +``` From 8dcad97f1f267b2d307c4bc33d62454da23e4b38 Mon Sep 17 00:00:00 2001 From: kaihsin Date: Fri, 20 Dec 2024 04:42:58 -0500 Subject: [PATCH 4/9] add type and device --- README.md | 4 + pyproject.toml | 4 +- src/cpp/include/cytnx_core/Device.hpp | 46 ++ src/cpp/include/cytnx_core/Type.hpp | 415 ++++++++++++++++++ src/cpp/include/cytnx_core/cytnx_core.hpp | 13 + src/cpp/include/cytnx_core/errors/base.hpp | 76 ++++ src/cpp/include/cytnx_core/errors/cuda.hpp | 177 ++++++++ .../include/cytnx_core/errors/cytnx_error.hpp | 7 + src/cpp/include/cytnx_core/test.hpp | 9 - src/cpp/pybind/main.cpp | 120 ++++- src/cpp/src/CMakeLists.txt | 6 +- src/cpp/src/Device.cpp | 112 +++++ src/cpp/src/Type.cpp | 59 +++ src/cpp/src/test.cpp | 7 - src/cytnx_core/__init__.py | 7 +- src/cytnx_core/_core/__init__.pyi | 31 ++ src/cytnx_core/_core/device/__init__.pyi | 9 + src/cytnx_core/py.typed | 0 src/cytnx_core/pycytnx.pyi | 3 - test/test_device.py | 17 + test/test_simple.py | 8 - test/test_type.py | 16 + uv.lock | 334 ++++++++------ 23 files changed, 1304 insertions(+), 176 deletions(-) create mode 100644 src/cpp/include/cytnx_core/Device.hpp create mode 100644 src/cpp/include/cytnx_core/Type.hpp create mode 100644 src/cpp/include/cytnx_core/cytnx_core.hpp create mode 100644 src/cpp/include/cytnx_core/errors/base.hpp create mode 100644 src/cpp/include/cytnx_core/errors/cuda.hpp create mode 100644 src/cpp/include/cytnx_core/errors/cytnx_error.hpp delete mode 100644 src/cpp/include/cytnx_core/test.hpp create mode 100644 src/cpp/src/Device.cpp create mode 100644 src/cpp/src/Type.cpp delete mode 100644 src/cpp/src/test.cpp create mode 100644 src/cytnx_core/_core/__init__.pyi create mode 100644 src/cytnx_core/_core/device/__init__.pyi delete mode 100644 src/cytnx_core/py.typed delete mode 100644 src/cytnx_core/pycytnx.pyi create mode 100644 test/test_device.py delete mode 100644 test/test_simple.py create mode 100644 test/test_type.py diff --git a/README.md b/README.md index 2286d8d..80f3ea9 100644 --- a/README.md +++ b/README.md @@ -43,3 +43,7 @@ Running pytest: $cd build $cmake ../ -DCMAKE_INSTALL_PREFIX= ``` + +## For DEV: + +1. Please add corresponding .pyi for binded objects/modules to comply with linting. diff --git a/pyproject.toml b/pyproject.toml index d0c3dd3..0de6cd4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,9 @@ authors = [ { name = "kaihsin", email = "kaihsinwu@gmail.com" } ] requires-python = ">=3.10" -dependencies = [] +dependencies = [ + "numpy>=2.2.0", +] [tool.scikit-build] minimum-version = "build-system.requires" diff --git a/src/cpp/include/cytnx_core/Device.hpp b/src/cpp/include/cytnx_core/Device.hpp new file mode 100644 index 0000000..8abf278 --- /dev/null +++ b/src/cpp/include/cytnx_core/Device.hpp @@ -0,0 +1,46 @@ +#ifndef CYTNX_DEVICE_H_ +#define CYTNX_DEVICE_H_ + +#include +#include + +namespace cytnx_core { + +/// @cond +struct __device { + enum __pybind_device { cpu = -1, cuda = 0 }; +}; + +class Device_class { +public: + enum : int { cpu = -1, cuda = 0 }; + int Ngpus; + int Ncpus; + std::vector> CanAccessPeer; + Device_class(); + void print_property(); + std::string getname(const int &device_id); + ~Device_class(); + // void cudaDeviceSynchronize(); +}; +/// @endcond + +/** + * @brief data on which devices. + * + * @details This is the variable about the data on which devices .\n + * You can use it as following: + * \code + * int device = Device.cpu; + * \endcode + * The supported enumerations are as following: + * + * enumeration | description + * --------------|-------------------- + * cpu | -1, on cpu + * cuda | 0, on cuda + */ +extern Device_class Device; +} // namespace cytnx_core + +#endif // CYTNX_DEVICE_H_ diff --git a/src/cpp/include/cytnx_core/Type.hpp b/src/cpp/include/cytnx_core/Type.hpp new file mode 100644 index 0000000..35b3f56 --- /dev/null +++ b/src/cpp/include/cytnx_core/Type.hpp @@ -0,0 +1,415 @@ +#ifndef CYTNX_TYPE_H_ +#define CYTNX_TYPE_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include // also brings in cuComplex.h + +#define MKL_Complex8 std::complex +#define MKL_Complex16 std::complex + +#ifdef UNI_MKL +#include +typedef MKL_INT blas_int; +#else +typedef int32_t blas_int; +#endif + +// @cond +namespace cytnx_core { + +template using vec3d = std::vector>>; + +template using vec2d = std::vector>; + +typedef double cytnx_double; +typedef float cytnx_float; +typedef uint64_t cytnx_uint64; +typedef uint32_t cytnx_uint32; +typedef uint16_t cytnx_uint16; +typedef int64_t cytnx_int64; +typedef int32_t cytnx_int32; +typedef int16_t cytnx_int16; +typedef size_t cytnx_size_t; +typedef std::complex cytnx_complex64; +typedef std::complex cytnx_complex128; +typedef bool cytnx_bool; + +namespace internal { +template struct is_complex_impl : std::false_type {}; + +template struct is_complex_impl> : std::true_type {}; + +template struct is_complex_floating_point_impl : std::false_type {}; + +template +struct is_complex_floating_point_impl> + : std::is_floating_point {}; + +template +constexpr std::size_t index_in_tuple_helper() { + static_assert(I < std::tuple_size_v, "Type not found!"); + if constexpr (std::is_same_v>) { + return I; + } else { + return index_in_tuple_helper(); + } +} + +} // namespace internal + +// helper metafunction to transform a variant into another variant via a +// transform template alias +template class Transform> +struct make_variant_from_transform; + +template