tatami_python
R bindings to tatami matrices
Loading...
Searching...
No Matches
parallelize.hpp
Go to the documentation of this file.
1#ifndef TATAMI_PYTHON_PARALLELIZE_HPP
2#define TATAMI_PYTHON_PARALLELIZE_HPP
3
7#ifdef TATAMI_PYTHON_PARALLELIZE_UNKNOWN
12#include "pybind11/pybind11.h"
13#include "subpar/subpar.hpp"
14
15#include <optional>
16
17#ifndef TATAMI_PYTHON_SERIALIZE
21#define TATAMI_PYTHON_SERIALIZE ::tatami_python::lock
22#endif
23
29namespace tatami_python {
30
56template<class Function_, class Index_>
57int parallelize(const Function_ fun, const Index_ tasks, int workers) {
58 std::optional<pybind11::gil_scoped_release> ungil;
59 if (PyGILState_Check()) {
60 ungil.emplace();
61 }
62 return subpar::parallelize_range(workers, tasks, std::move(fun));
63}
64
74template<typename Function_>
75void lock(Function_ fun) {
76 std::optional<pybind11::gil_scoped_acquire> gil;
77 if (!PyGILState_Check()) {
78 gil.emplace();
79 }
80 fun();
81}
82
83}
84
88#endif
93#endif
tatami bindings for arbitrary Python matrices.
int parallelize(const Function_ fun, const Index_ tasks, int workers)
Definition parallelize.hpp:57
void lock(Function_ fun)
Definition parallelize.hpp:75