Skip to content

Commit d8f2ad4

Browse files
author
Alexander Strack
committed
Add Python bindings for HPX backend
1 parent 93b6872 commit d8f2ad4

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed

bindings/Python/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Authors: Alexander Van Craen, Marcel Breyer
1+
## Authors: Alexander Van Craen, Marcel Breyer, Alexander Strack
22
## Copyright (C): 2018-today The PLSSVM project - All Rights Reserved
33
## License: This file is part of the PLSSVM project which is released under the MIT license.
44
## See the LICENSE.md file in the project root for full license information.
@@ -67,6 +67,9 @@ endif ()
6767
if (TARGET ${PLSSVM_OPENMP_BACKEND_LIBRARY_NAME})
6868
list(APPEND PLSSVM_PYTHON_BINDINGS_SOURCES ${CMAKE_CURRENT_LIST_DIR}/backends/openmp_csvm.cpp)
6969
endif ()
70+
if (TARGET ${PLSSVM_HPX_BACKEND_LIBRARY_NAME})
71+
list(APPEND PLSSVM_PYTHON_BINDINGS_SOURCES ${CMAKE_CURRENT_LIST_DIR}/backends/hpx_csvm.cpp)
72+
endif ()
7073
if (TARGET ${PLSSVM_STDPAR_BACKEND_LIBRARY_NAME})
7174

7275
# AdaptiveCpp stdpar only support on the CPU when using our Python bindings
@@ -124,4 +127,4 @@ target_compile_options(${PLSSVM_BASE_LIBRARY_NAME} PUBLIC $<$<COMPILE_LANG_AND_I
124127
target_compile_options(${PLSSVM_BASE_LIBRARY_NAME} PUBLIC -fPIC)
125128

126129
# append pybind11 bindings library to installed targets
127-
append_local_and_parent(PLSSVM_TARGETS_TO_INSTALL ${PLSSVM_PYTHON_BINDINGS_LIBRARY_NAME})
130+
append_local_and_parent(PLSSVM_TARGETS_TO_INSTALL ${PLSSVM_PYTHON_BINDINGS_LIBRARY_NAME})

bindings/Python/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- [plssvm.Parameter](#plssvmparameter)
1111
- [plssvm.DataSet](#plssvmdataset)
1212
- [plssvm.CSVM](#plssvmcsvm)
13-
- [plssvm.openmp.CSVM, plssvm.stdpar.CSVM, plssvm.cuda.CSVM, plssvm.hip.CSVM, plssvm.opencl.CSVM, plssvm.sycl.CSVM, plssvm.dpcpp.CSVM, plssvm.adaptivecpp.CSVM](#plssvmopenmpcsvm-plssvmcudacsvm-plssvmhipcsvm-plssvmopenclcsvm-plssvmsyclcsvm-plssvmdpcppcsvm-plssvmadaptivecppcsvm)
13+
- [plssvm.openmp.CSVM, plssvm.hpx.CSVM, plssvm.stdpar.CSVM, plssvm.cuda.CSVM, plssvm.hip.CSVM, plssvm.opencl.CSVM, plssvm.sycl.CSVM, plssvm.dpcpp.CSVM, plssvm.adaptivecpp.CSVM](#plssvmopenmpcsvm-plssvmhpxcsvm-plssvmcudacsvm-plssvmhipcsvm-plssvmopenclcsvm-plssvmsyclcsvm-plssvmdpcppcsvm-plssvmadaptivecppcsvm)
1414
- [plssvm.Model](#plssvmmodel)
1515
- [plssvm.Version](#plssvmversion)
1616
- [plssvm.detail.tracking.PerformanceTracker](#plssvmdetailtrackingperformancetracker)
@@ -195,7 +195,7 @@ The following table lists all PLSSVM enumerations exposed on the Python side:
195195
| `FileFormatType` | `LIBSVM`, `ARFF` | The different supported file format types (default: `LIBSVM`). |
196196
| `GammaCoefficientType` | `AUTOMATIC`, `SCALE` | The different modes for the dynamic gamma calculation (default: `AUTOMATIC`). |
197197
| `ClassificationType` | `OAA`, `OAO` | The different supported multi-class classification strategies (default: `LIBSVM`). |
198-
| `BackendType` | `AUTOMATIC`, `OPENMP`, `CUDA`, `HIP`, `OPENCL`, `SYCL` | The different supported backends (default: `AUTOMATIC`). If `AUTOMATIC` is provided, the selected backend depends on the used target platform. |
198+
| `BackendType` | `AUTOMATIC`, `OPENMP`, `HPX`, `CUDA`, `HIP`, `OPENCL`, `SYCL` | The different supported backends (default: `AUTOMATIC`). If `AUTOMATIC` is provided, the selected backend depends on the used target platform. |
199199
| `VerbosityLevel` | `QUIET`, `LIBSVM`, `TIMING`, `FULL` | The different supported log levels (default: `FULL`). `QUIET` means no output, `LIBSVM` output that is as conformant as possible with LIBSVM's output, `TIMING` all timing related outputs, and `FULL` everything. Can be combined via bit-wise operations. |
200200

201201
If a SYCL implementation is available, additional enumerations are available:
@@ -347,7 +347,7 @@ and `sycl_kernel_invocation_type` to choose between the two different SYCL kerne
347347
| `score(model)` | Score the model with respect to itself returning its accuracy. |
348348
| `score(model, data_set)` | Score the model given the provided data set returning its accuracy. |
349349

350-
#### `plssvm.openmp.CSVM`, `plssvm.stdpar.CSVM`, plssvm.cuda.CSVM`, `plssvm.hip.CSVM`, `plssvm.opencl.CSVM`, `plssvm.sycl.CSVM`, `plssvm.dpcpp.CSVM`, `plssvm.adaptivecpp.CSVM`
350+
#### `plssvm.openmp.CSVM`, `plssvm.hpx.CSVM`, `plssvm.stdpar.CSVM`, plssvm.cuda.CSVM`, `plssvm.hip.CSVM`, `plssvm.opencl.CSVM`, `plssvm.sycl.CSVM`, `plssvm.dpcpp.CSVM`, `plssvm.adaptivecpp.CSVM`
351351

352352
These classes represent the backend specific CSVMs.
353353
**Note**: they are only available if the respective backend has been enabled during PLSSVM's build step.
@@ -533,4 +533,4 @@ The PLSSVM Python3 bindings define a few new exception types:
533533
| `GPUDevicePtrError` | If something went wrong in one of the backend's GPU device pointers. **Note**: shouldn't occur in user code. |
534534
| `MatrixError` | If something went wrong in the internal matrix class. **Note**: shouldn't occur in user code. |
535535

536-
Depending on the available backends, additional `BackendError`s are also available (e.g., `plssvm.cuda.BackendError`).
536+
Depending on the available backends, additional `BackendError`s are also available (e.g., `plssvm.cuda.BackendError`).

bindings/Python/backend_types.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @author Alexander Van Craen
33
* @author Marcel Breyer
4+
* @author Alexander Strack
45
* @copyright 2018-today The PLSSVM project - All Rights Reserved
56
* @license This file is part of the PLSSVM project which is released under the MIT license.
67
* See the LICENSE.md file in the project root for full license information.
@@ -20,6 +21,7 @@ void init_backend_types(py::module_ &m) {
2021
py::enum_<plssvm::backend_type>(m, "BackendType")
2122
.value("AUTOMATIC", plssvm::backend_type::automatic, "the default backend; depends on the specified target platform")
2223
.value("OPENMP", plssvm::backend_type::openmp, "OpenMP to target CPUs only (currently no OpenMP target offloading support)")
24+
.value("HPX", plssvm::backend_type::hpx, "HPX to target CPUs only (currently no GPU executor support)")
2325
.value("STDPAR", plssvm::backend_type::stdpar, "C++ standard parallelism to target CPUs and GPUs from different vendors based on the used stdpar implementation; supported implementations are: nvhpc (nvc++), roc-stdpar, AdaptiveCpp, Intel LLVM (icpx), and GNU GCC + TBB")
2426
.value("CUDA", plssvm::backend_type::cuda, "CUDA to target NVIDIA GPUs only")
2527
.value("HIP", plssvm::backend_type::hip, "HIP to target AMD and NVIDIA GPUs")
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @author Alexander Van Craen
3+
* @author Marcel Breyer
4+
* @author Alexander Strack
5+
* @copyright 2018-today The PLSSVM project - All Rights Reserved
6+
* @license This file is part of the PLSSVM project which is released under the MIT license.
7+
* See the LICENSE.md file in the project root for full license information.
8+
*/
9+
10+
#include "plssvm/backends/HPX/csvm.hpp" // plssvm::hpx::csvm
11+
#include "plssvm/backends/HPX/exceptions.hpp" // plssvm::hpx::backend_exception
12+
#include "plssvm/csvm.hpp" // plssvm::csvm
13+
#include "plssvm/exceptions/exceptions.hpp" // plssvm::exception
14+
#include "plssvm/parameter.hpp" // plssvm::parameter
15+
#include "plssvm/target_platforms.hpp" // plssvm::target_platform
16+
17+
#include "bindings/Python/utility.hpp" // check_kwargs_for_correctness, convert_kwargs_to_parameter, register_py_exception
18+
19+
#include "pybind11/pybind11.h" // py::module_, py::class_, py::init
20+
#include "pybind11/stl.h" // support for STL types
21+
22+
#include <memory> // std::make_unique
23+
24+
namespace py = pybind11;
25+
26+
void init_hpx_csvm(py::module_ &m, const py::exception<plssvm::exception> &base_exception) {
27+
// use its own submodule for the HPX CSVM bindings
28+
py::module_ hpx_module = m.def_submodule("hpx", "a module containing all HPX backend specific functionality");
29+
30+
// bind the CSVM using the HPX backend
31+
py::class_<plssvm::hpx::csvm, plssvm::csvm>(hpx_module, "CSVM")
32+
.def(py::init<>(), "create an SVM with the automatic target platform and default parameter object")
33+
.def(py::init<plssvm::parameter>(), "create an SVM with the automatic target platform and provided parameter object")
34+
.def(py::init<plssvm::target_platform>(), "create an SVM with the provided target platform and default parameter object")
35+
.def(py::init<plssvm::target_platform, plssvm::parameter>(), "create an SVM with the provided target platform and parameter object")
36+
.def(py::init([](const py::kwargs &args) {
37+
// check for valid keys
38+
check_kwargs_for_correctness(args, { "kernel_type", "degree", "gamma", "coef0", "cost" });
39+
// if one of the value keyword parameter is provided, set the respective value
40+
const plssvm::parameter params = convert_kwargs_to_parameter(args);
41+
// create CSVM with the default target platform
42+
return std::make_unique<plssvm::hpx::csvm>(params);
43+
}),
44+
"create an SVM with the default target platform and keyword arguments")
45+
.def(py::init([](const plssvm::target_platform target, const py::kwargs &args) {
46+
// check for valid keys
47+
check_kwargs_for_correctness(args, { "kernel_type", "degree", "gamma", "coef0", "cost" });
48+
// if one of the value keyword parameter is provided, set the respective value
49+
const plssvm::parameter params = convert_kwargs_to_parameter(args);
50+
// create CSVM with the provided target platform
51+
return std::make_unique<plssvm::hpx::csvm>(target, params);
52+
}),
53+
"create an SVM with the provided target platform and keyword arguments");
54+
55+
// register HPX backend specific exceptions
56+
register_py_exception<plssvm::hpx::backend_exception>(hpx_module, "BackendError", base_exception);
57+
}

bindings/Python/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @author Alexander Van Craen
33
* @author Marcel Breyer
4+
* @author Alexander Strack
45
* @copyright 2018-today The PLSSVM project - All Rights Reserved
56
* @license This file is part of the PLSSVM project which is released under the MIT license.
67
* See the LICENSE.md file in the project root for full license information.
@@ -34,6 +35,7 @@ void init_version(py::module_ &);
3435
void init_exceptions(py::module_ &, const py::exception<plssvm::exception> &);
3536
void init_csvm(py::module_ &);
3637
void init_openmp_csvm(py::module_ &, const py::exception<plssvm::exception> &);
38+
void init_hpx_csvm(py::module_ &, const py::exception<plssvm::exception> &);
3739
void init_stdpar_csvm(py::module_ &, const py::exception<plssvm::exception> &);
3840
void init_cuda_csvm(py::module_ &, const py::exception<plssvm::exception> &);
3941
void init_hip_csvm(py::module_ &, const py::exception<plssvm::exception> &);
@@ -84,6 +86,9 @@ PYBIND11_MODULE(plssvm, m) {
8486
#if defined(PLSSVM_HAS_OPENMP_BACKEND)
8587
init_openmp_csvm(m, base_exception);
8688
#endif
89+
#if defined(PLSSVM_HAS_HPX_BACKEND)
90+
init_hpx_csvm(m, base_exception);
91+
#endif
8792
#if defined(PLSSVM_HAS_STDPAR_BACKEND)
8893
init_stdpar_csvm(m, base_exception);
8994
#endif

0 commit comments

Comments
 (0)