Skip to content

Commit e651ea5

Browse files
committed
[Python] Support backend and mem type config for python package
1 parent a741180 commit e651ea5

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@ $ python3 -m unittest discover -v
223223
inside the build directory in the folder with python wrapper `python/pycubool/`.
224224
So, the wrapper will be able to automatically locate required lib file.
225225
226+
### Python package runtime config
227+
228+
**Pycubool** Python-package can be configured before import by the following environment variables:
229+
230+
- `CUBOOL_PATH` - custom path to compiled cubool library object.
231+
Without that variable by default the package will try to find library in the package folder.
232+
233+
- `CUBOOL_BACKEND` - allows to select backend for execution.
234+
By default library selects cuda if present. Pass value `cpu` to force cpu computations,
235+
even if cuda backend is presented and supported for selection.
236+
237+
- `CUBOOL_MEM` - type of the memory to use if run computations in cuda backend.
238+
By default library uses device memory. If pass in the variable value `managed`, then
239+
backend will be configured to use cuda managed memory for resources allocation.
240+
226241
## Usage
227242
228243
The following C++ code snipped demonstrates, how library functions and

cubool/sources/core/library.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ namespace cubool {
278278
<< " sharedMemoryPerBlockKiBs: " << caps.sharedMemoryPerBlockKiBs;
279279
}
280280
else {
281-
stream << "Cuda device is not presented";
281+
stream << "Cuda device is not presented (fallback to cpu backend)";
282282
}
283283

284284
stream << LogStream::cmt;

python/pycubool/wrapper.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Wrapper:
4444

4545
def __init__(self):
4646
self.loaded_dll = None
47+
self.is_managed = Wrapper.__get_use_managed_mem()
48+
self.force_cpu = Wrapper.__get_force_cpu()
4749

4850
try:
4951
# Try from config if present
@@ -62,7 +64,9 @@ def __del__(self):
6264
self.__release_library()
6365

6466
def __setup_library(self):
65-
status = self.loaded_dll.cuBool_Initialize(ctypes.c_uint(bridge.get_init_hints(False, False)))
67+
status = self.loaded_dll.cuBool_Initialize(ctypes.c_uint(bridge.get_init_hints(
68+
force_cpu_backend=self.force_cpu, is_gpu_mem_managed=self.is_managed)))
69+
6670
bridge.check(status)
6771

6872
def __release_library(self):
@@ -76,3 +80,19 @@ def __get_lib_path(cls, prefix):
7680
return prefix / entry
7781

7882
return None
83+
84+
@classmethod
85+
def __get_force_cpu(cls):
86+
try:
87+
memory = os.environ["CUBOOL_BACKEND"]
88+
return True if memory.lower() == "cpu" else False
89+
except KeyError:
90+
return False
91+
92+
@classmethod
93+
def __get_use_managed_mem(cls):
94+
try:
95+
memory = os.environ["CUBOOL_MEM"]
96+
return True if memory.lower() == "managed" else False
97+
except KeyError:
98+
return False

0 commit comments

Comments
 (0)