Skip to content

Commit 0ae874f

Browse files
committed
Do not expose the environment related functions in the Python bindings but automatically handle it during module import and cleanup. NO user action required anymore.
1 parent a797616 commit 0ae874f

File tree

7 files changed

+11
-145
lines changed

7 files changed

+11
-145
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,9 +805,6 @@ Roughly the same can be achieved using our Python bindings with the following Py
805805
import plssvm
806806
from sklearn.metrics import classification_report
807807

808-
# correctly initialize and finalize environments
809-
environment_guard = plssvm.environment.ScopeGuard()
810-
811808
try:
812809
# create a new C-SVM parameter set, explicitly overriding the default kernel function
813810
params = plssvm.Parameter(kernel_type=plssvm.KernelFunctionType.POLYNOMIAL)

bindings/Python/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ set(PLSSVM_PYTHON_BINDINGS_SOURCES
4141
${CMAKE_CURRENT_LIST_DIR}/classification_types.cpp
4242
${CMAKE_CURRENT_LIST_DIR}/csvm.cpp
4343
${CMAKE_CURRENT_LIST_DIR}/data_set.cpp
44-
${CMAKE_CURRENT_LIST_DIR}/environment.cpp
4544
${CMAKE_CURRENT_LIST_DIR}/file_format_types.cpp
4645
${CMAKE_CURRENT_LIST_DIR}/gamma.cpp
4746
${CMAKE_CURRENT_LIST_DIR}/kernel_function_types.cpp

bindings/Python/README.md

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
- [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, plssvm.kokkos.CSVM](#plssvmopenmpcsvm-plssvmhpxcsvm-plssvmcudacsvm-plssvmhipcsvm-plssvmopenclcsvm-plssvmsyclcsvm-plssvmdpcppcsvm-plssvmadaptivecppcsvm-plssvmkokkoscsvm)
1414
- [plssvm.Model](#plssvmmodel)
1515
- [plssvm.Version](#plssvmversion)
16-
- [plssvm.environment.ScopeGuard](#plssvmenvironmentscopeguard)
1716
- [plssvm.detail.tracking.PerformanceTracker](#plssvmdetailtrackingperformancetracker)
1817
- [plssvm.detail.tracking.Events](#plssvmdetailtrackingevent-plssvmdetailtrackingevents)
1918
- [Free functions](#free-functions)
@@ -198,7 +197,6 @@ The following table lists all PLSSVM enumerations exposed on the Python side:
198197
| `ClassificationType` | `OAA`, `OAO` | The different supported multi-class classification strategies (default: `LIBSVM`). |
199198
| `BackendType` | `AUTOMATIC`, `OPENMP`, `HPX`, `CUDA`, `HIP`, `OPENCL`, `SYCL`, `KOKKOS` | The different supported backends (default: `AUTOMATIC`). If `AUTOMATIC` is provided, the selected backend depends on the used target platform. |
200199
| `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. |
201-
| `Status` | `UNINITIALIZED`, `INITIALIZED`, `FINALIZED`, `UNNECESSARY` | The different environment status values. **Note**: located in the `plssvm.environment` module. | |
202200

203201
If a SYCL implementation is available, additional enumerations are available:
204202

@@ -343,9 +341,8 @@ If the most performant backend should be used, it is sufficient to use `plssvm.C
343341
`sycl_implementation_type` to choose between DPC++ and AdaptiveCpp as SYCL implementations
344342
and `sycl_kernel_invocation_type` to choose between the two different SYCL kernel invocation types.
345343

346-
**Note**: if the backend type is `plssvm.BackendType.HPX` it is necessary to initialize and finalize the HPX runtime.
347-
The runtime can be manually managed using `plssvm.environment.initialize()` and `plssvm.environment.finalize()`.
348-
We recommend utilizing `plssvm.environment.ScopeGuard()` to manage the lifetime of the HPX runtime automatically.
344+
**Note**: if the backend type is `plssvm.BackendType.HPX` or `plssvm.BackendType.Kokkos` special initialization and finalization functions must be called.
345+
However, this is **automatically** handled by our Python bindings on the module import and cleanup.
349346

350347
| methods | description |
351348
|----------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
@@ -443,19 +440,6 @@ A class encapsulating the version information of the used PLSSVM installation.
443440
| `minor : int` | The minor PLSSVM version. |
444441
| `patch : int` | The patch PLSSVM version. |
445442

446-
#### `plssvm.environment.ScopeGuard`
447-
448-
The environmental scope guard can be used to automatically finalize all necessary backend environments when it goes out of scope.
449-
450-
| constructors | description |
451-
|-----------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
452-
| `ScopeGuard([backends={}])` | Create a new scope guard initializing all available backend environments. If a list of backends is provided, only initializes these backends. |
453-
| `ScopeGuard(argc, argv, [backends={}])` | Create a new scope guard initializing all available backend environments using the provided command line arguments. If a list of backends is provided, only initializes these backends. |
454-
455-
| methods | description |
456-
|--------------|-----------------------------------------------------------------------------------------------------------------------------------|
457-
| `backends()` | Return all initialized backends. All backends returned by this function will be finalized when the scope guard goes out of scope. |
458-
459443
#### `plssvm.detail.tracking.PerformanceTracker`
460444

461445
A submodule used to track various performance statistics like runtimes, but also the used setup and hyperparameters.
@@ -549,15 +533,6 @@ If a stdpar implementation is available, additional free functions are available
549533
|-------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|
550534
| `list_available_stdpar_implementations()` | List all available stdpar implementations (determined during PLSSVM's build step; currently always guaranteed to be only one implementation). |
551535

552-
Additional free functions are available under `plssvm.environment.`.
553-
554-
| function | description |
555-
|-----------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
556-
| `get_backend_status(backend)` | Return the current environment status of the provided backend. |
557-
| `initialize([backends={}])` | Initialize all available backend environments. If a list of backends is provided, only initializes these backends. |
558-
| `initialize(argc, argv, [backends={}])` | Initialize all available backend environments using the provided command line arguments. If a list of backends is provided, only initializes these backends. |
559-
| `finalize([backends={}])` | Finalize all available backend environments. If a list of backends is provided, only finalizes these backends. |
560-
561536
### Exceptions
562537

563538
The PLSSVM Python3 bindings define a few new exception types:
@@ -576,6 +551,5 @@ The PLSSVM Python3 bindings define a few new exception types:
576551
| `MatrixError` | If something went wrong in the internal matrix class. **Note**: shouldn't occur in user code. |
577552
| `KernelLaunchResourcesError` | If something went wrong during a kernel launch due to insufficient ressources. |
578553
| `ClassificationReportError` | If something in the classification report went wrong. **Note**: shouldn't occur in user code. |
579-
| `EnvironmentError` | If something during environment initialization or finalization went wrong. |
580554

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

bindings/Python/environment.cpp

Lines changed: 0 additions & 105 deletions
This file was deleted.

bindings/Python/main.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* See the LICENSE.md file in the project root for full license information.
88
*/
99

10+
#include "plssvm/environment.hpp" // plssvm::environment::{initialize, finalize}
1011
#include "plssvm/exceptions/exceptions.hpp" // plssvm::exception
1112

1213
#include "pybind11/pybind11.h" // PYBIND11_MODULE, py::module_, py::exception, py::register_exception_translator
@@ -32,7 +33,6 @@ void init_parameter(py::module_ &);
3233
void init_model(py::module_ &);
3334
void init_data_set(py::module_ &);
3435
void init_version(py::module_ &);
35-
void init_environment(py::module_ &);
3636
void init_exceptions(py::module_ &, const py::exception<plssvm::exception> &);
3737
void init_csvm(py::module_ &);
3838
void init_openmp_csvm(py::module_ &, const py::exception<plssvm::exception> &);
@@ -48,6 +48,14 @@ void init_sklearn(py::module_ &);
4848
PYBIND11_MODULE(plssvm, m) {
4949
m.doc() = "Parallel Least Squares Support Vector Machine";
5050

51+
// automatically initialize the environments
52+
plssvm::environment::initialize();
53+
54+
// automatically finalize the environments
55+
m.add_object("_cleanup", py::capsule([]() {
56+
plssvm::environment::finalize();
57+
}));
58+
5159
// register PLSSVM base exception
5260
static py::exception<plssvm::exception> base_exception(m, "PLSSVMError");
5361
py::register_exception_translator([](std::exception_ptr p) {
@@ -81,7 +89,6 @@ PYBIND11_MODULE(plssvm, m) {
8189
init_model(m);
8290
init_data_set(m);
8391
init_version(m);
84-
init_environment(m);
8592
init_exceptions(m, base_exception);
8693
init_csvm(m);
8794

examples/python/main.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import plssvm
22
from sklearn.metrics import classification_report
33

4-
# correctly initialize and finalize environments
5-
environment_guard = plssvm.environment.ScopeGuard()
6-
74
try:
85
# create a new C-SVM parameter set, explicitly overriding the default kernel function
96
params = plssvm.Parameter(kernel_type=plssvm.KernelFunctionType.POLYNOMIAL)

examples/python/sklearn_like_svc.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from sklearn.datasets import make_classification
22
import plssvm
33

4-
# correctly initialize and finalize environments
5-
environment_guard = plssvm.environment.ScopeGuard()
6-
74
num_samples = 2**8
85
num_features = 2**6
96

0 commit comments

Comments
 (0)