Skip to content

Commit 9a15398

Browse files
authored
4 cmake install (#10)
* remove submodules * remove extra cd and make it a flag * update installation instructions * added ability to install library
1 parent 2b04f48 commit 9a15398

9 files changed

Lines changed: 149 additions & 17 deletions

File tree

CMakeLists.txt

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,69 @@
11
cmake_minimum_required(VERSION 3.14)
2+
cmake_policy(SET CMP0148 NEW)
23
project(pyscheduler VERSION 0.1.0 LANGUAGES CXX)
34

45
set(CMAKE_CXX_STANDARD 17)
56

6-
add_subdirectory(lib/dlpack)
7-
add_subdirectory(lib/pybind11)
8-
add_subdirectory(lib/concurrentqueue)
7+
8+
find_package(pybind11 REQUIRED)
9+
find_package(concurrentqueue REQUIRED)
910

1011
add_library(${PROJECT_NAME} SHARED "include/pyscheduler/pyscheduler.cpp")
11-
target_link_libraries(pyscheduler PUBLIC
12-
dlpack
13-
concurrentqueue
14-
pybind11::embed
12+
target_link_libraries(pyscheduler PUBLIC pybind11::embed concurrentqueue::concurrentqueue)
13+
target_include_directories(pyscheduler
14+
PUBLIC
15+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
16+
$<INSTALL_INTERFACE:include>
1517
)
16-
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
17-
target_include_directories(${PROJECT_NAME} PUBLIC include)
1818

1919
# Build example programs
2020
option(BUILD_EXAMPLES "Build example programs" OFF)
2121
if(BUILD_EXAMPLES)
2222
add_subdirectory(examples)
2323
endif()
24+
25+
# Install
26+
include(GNUInstallDirs)
27+
include(CMakePackageConfigHelpers)
28+
29+
install(
30+
TARGETS ${PROJECT_NAME}
31+
EXPORT ${PROJECT_NAME}Targets
32+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # .so / .dylib
33+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # .a
34+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # .exe on Windows
35+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # for modern CMake
36+
37+
)
38+
39+
install(
40+
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
41+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
42+
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h"
43+
)
44+
45+
install(
46+
EXPORT ${PROJECT_NAME}Targets
47+
FILE ${PROJECT_NAME}Targets.cmake
48+
NAMESPACE ${PROJECT_NAME}::
49+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
50+
)
51+
52+
write_basic_package_version_file(
53+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
54+
VERSION ${PROJECT_VERSION}
55+
COMPATIBILITY AnyNewerVersion
56+
)
57+
58+
configure_package_config_file(
59+
"${CMAKE_CURRENT_LIST_DIR}/cmake/Config.cmake.in"
60+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
61+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
62+
)
63+
64+
install(
65+
FILES
66+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
67+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
68+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
69+
)

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Optimized as an execution engine for Machine Learning pipelines.
2525
- **Thread Safe Implementation**
2626
Ensures only one Python interpreter is ever initialized per process for pre python3.13.
2727
- [x] Pre Python 3.13
28-
- [ ] Python 3.13+ no-gil sub interpretor
28+
- [ ] Python 3.13+ no-gil sub interpreter
2929
- **Thread‑Pooled Execution**
3030
Uses a high‑performance round robin queue to minimize latency during high throughput workloads
3131
- **Optimized GIL Management**
@@ -47,11 +47,14 @@ Package Dependencies
4747
- [moodycamel::BlockingConcurrentQueue](https://github.com/cameron314/concurrentqueue)
4848
- [dmlc::dlpack](https://github.com/dmlc/dlpack.git)
4949

50+
Can be installed ob Ubuntu using `sudo apt install pybind11-dev libconcurrentqueue-dev libdlpack-dev`
51+
5052
## Installation
5153
1. Initialize submodules `git submodule update --init --recursive`
5254
2. We provide a simple `configure.sh` script to invoke CMake configuration. Use the `-h | --help` flag to see the possible options.
5355
```bash
5456
# Configure a Release build, including examples
5557
./configure.sh -t Release -e
5658
```
57-
3. Optional: install `cmake --install build`
59+
3. Install `sudo cmake --install build`
60+
3. Uninstall `sudo xargs rm < build/install_manifest.txt`

cmake/Config.cmake.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}Targets.cmake")
4+
5+
find_dependency(pybind11)
6+
find_dependency(concurrentqueue)

configure.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ echo " Build examples : $BUILD_EXAMPLES"
4343

4444
# Create build directory
4545
mkdir -p "$BUILD_DIR"
46-
cd "$BUILD_DIR"
4746

4847
# Run CMake configuration
49-
cmake .. -DCMAKE_BUILD_TYPE="$BUILD_TYPE" -DBUILD_EXAMPLES="$BUILD_EXAMPLES"
48+
cmake -B "$BUILD_DIR" -DCMAKE_BUILD_TYPE="$BUILD_TYPE" -DBUILD_EXAMPLES="$BUILD_EXAMPLES" -S .
5049

5150
echo "Configuration complete. You can now run: cmake --build $BUILD_DIR"

include/pyscheduler/pyscheduler.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include "pyscheduler/library_export.hpp"
33

44
#include <atomic>
5-
#include <blockingconcurrentqueue.h>
65
#include <chrono>
6+
#include <moodycamel/blockingconcurrentqueue.h>
77
#include <filesystem>
88
#include <future>
99
#include <iostream>

include/pyscheduler/tensor.hpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "pyscheduler/library_export.hpp"
2+
#include <cuda_runtime.h>
3+
#include <dlpack.h>
4+
#include <memory>
5+
6+
enum class DeviceType {
7+
CPU,
8+
CUDA,
9+
};
10+
11+
template <typename T>
12+
struct DLPackTypeTraits;
13+
14+
// I love explicit template specialization
15+
// NOTE: If you get a compile time, add an entry here:
16+
17+
template <>
18+
struct DLPackTypeTraits<float> {
19+
static constexpr DLDataType dtype = { kDLFloat, 32, 1 };
20+
};
21+
22+
template <>
23+
struct DLPackTypeTraits<double> {
24+
static constexpr DLDataType dtype = { kDLFloat, 64, 1 };
25+
};
26+
27+
template <>
28+
struct DLPackTypeTraits<int64_t> {
29+
static constexpr DLDataType dtype = { kDLInt, 64, 1 };
30+
};
31+
32+
template <>
33+
struct DLPackTypeTraits<int32_t> {
34+
static constexpr DLDataType dtype = { kDLInt, 32, 1 };
35+
};
36+
37+
template <>
38+
struct DLPackTypeTraits<uint8_t> {
39+
static constexpr DLDataType dtype = { kDLUInt, 8, 1 };
40+
};
41+
42+
template <DeviceType Device, typename DataType, size_t... Dims>
43+
std::unique_ptr<DLManagedTensor> createDlpackTensor() {
44+
constexpr int ndim = sizeof...(Dims);
45+
constexpr int64_t num_items = (... * Dims); // C++17 fold expression
46+
47+
// Allocate and set shape
48+
int64_t* shape = new int64_t[ndim]{ Dims... };
49+
50+
// Allocate tensor memory
51+
DataType* data;
52+
if constexpr(Device == DeviceType::CPU) {
53+
data = new T[num_items];
54+
} else if constexpr(Device == DeviceType::CUDA) {
55+
cudaMalloc(&data, num_items * sizeof(T));
56+
}
57+
58+
// Create DLManagedTensor
59+
DLManagedTensor* managed_tensor = new DLManagedTensor();
60+
managed_tensor->dl_tensor.data = data;
61+
managed_tensor->dl_tensor.device = { Device == DeviceType::CPU ? kDLCPU : kDLCUDA, 0 };
62+
managed_tensor->dl_tensor.ndim = ndim;
63+
managed_tensor->dl_tensor.dtype = DLPackTypeTraits<DataType>::dtype;
64+
65+
managed_tensor->dl_tensor.shape = shape;
66+
managed_tensor->dl_tensor.strides = nullptr;
67+
managed_tensor->dl_tensor.byte_offset = 0;
68+
managed_tensor->dl_tensor.shape = shape;
69+
managed_tensor->manager_ctx = nullptr;
70+
71+
tensor->deleter = [](DLManagedTensor* self) {
72+
if(if constexpr Device == DeviceType::GPU)
73+
cudaFree(self->dl_tensor.data);
74+
else
75+
delete[] static_cast<DataType*>(self->dl_tensor.data);
76+
delete[] self->dl_tensor.shape;
77+
delete self;
78+
};
79+
80+
return std::unique_ptr<DLManagedTensor>(managed_tensor);
81+
}

lib/concurrentqueue

Submodule concurrentqueue deleted from 2f09da7

lib/dlpack

Submodule dlpack deleted from 688d2ba

lib/pybind11

Submodule pybind11 deleted from bc4a66d

0 commit comments

Comments
 (0)