Skip to content

Commit

Permalink
Fixes a bug with releasing the GIL and python 3.12 in the rir builder…
Browse files Browse the repository at this point in the history
… part of libroom. Related to wjakob/nanobind#377
  • Loading branch information
fakufaku committed Apr 25, 2024
1 parent d38ef8a commit 9e77929
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
9 changes: 3 additions & 6 deletions pyroomacoustics/libroom_src/libroom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,7 @@ PYBIND11_MODULE(libroom, m) {
m.def("dist_line_point", &dist_line_point,
"Computes the distance between a point and an infinite line");

m.def("rir_builder", &rir_builder, "RIR builder",
py::call_guard<py::gil_scoped_release>());
m.def("delay_sum", &delay_sum, "Delay and sum",
py::call_guard<py::gil_scoped_release>());
m.def("fractional_delay", &fractional_delay, "Fractional delays",
py::call_guard<py::gil_scoped_release>());
m.def("rir_builder", &rir_builder, "RIR builder");
m.def("delay_sum", &delay_sum, "Delay and sum");
m.def("fractional_delay", &fractional_delay, "Fractional delays");
}
21 changes: 16 additions & 5 deletions pyroomacoustics/libroom_src/rir_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ constexpr T get_pi() {
template <class T>
void threaded_rir_builder_impl(
py::array_t<T, py::array::c_style | py::array::forcecast> rir,
const py::array_t<T, py::array::c_style | py::array::forcecast> time,
const py::array_t<T, py::array::c_style | py::array::forcecast> alpha,
const py::array_t<T, py::array::c_style | py::array::forcecast> &time,
const py::array_t<T, py::array::c_style | py::array::forcecast> &alpha,
const py::array_t<int, py::array::c_style | py::array::forcecast>
visibility,
&visibility,
int fs, size_t fdl, size_t lut_gran, size_t num_threads) {

auto pi = get_pi<T>();

// accessors for the arrays
Expand Down Expand Up @@ -110,6 +111,9 @@ void threaded_rir_builder_impl(
std::vector<T> rir_out(num_threads * rir_len);
size_t block_size = size_t(std::ceil(double(n_times) / double(num_threads)));

// relase the GIL from here on
py::gil_scoped_release release;

// build the RIR
ThreadPool pool(num_threads);
std::vector<std::future<void>> results;
Expand Down Expand Up @@ -166,9 +170,10 @@ void threaded_rir_builder_impl(
for (auto &&result : sum_results) result.get();
}

void rir_builder(py::buffer rir, const py::buffer time, const py::buffer alpha,
const py::buffer visibility, int fs, size_t fdl,
void rir_builder(py::buffer rir, const py::buffer &time, const py::buffer &alpha,
const py::buffer &visibility, int fs, size_t fdl,
size_t lut_gran, size_t num_threads) {

// dispatch to correct implementation depending on input type
auto buf = pybind11::array::ensure(rir);
if (py::isinstance<py::array_t<float>>(buf)) {
Expand Down Expand Up @@ -214,6 +219,9 @@ void threaded_delay_sum_impl(
std::vector<T> out_buffers(num_threads * out_len, 0);
size_t block_size = size_t(std::ceil(double(n_irs) / double(num_threads)));

// release the GIL from here on
py::gil_scoped_release release;

// build the RIR
ThreadPool pool(num_threads);
std::vector<std::future<void>> results;
Expand Down Expand Up @@ -315,6 +323,9 @@ void threaded_fractional_delay_impl(
// divide into equal size blocks for thread processing
size_t block_size = size_t(std::ceil(double(n_times) / double(num_threads)));

// relase the GIL from here on
py::gil_scoped_release release;

// build the RIR
ThreadPool pool(num_threads);
std::vector<std::future<void>> results;
Expand Down
4 changes: 2 additions & 2 deletions pyroomacoustics/libroom_src/rir_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace py = pybind11;

void rir_builder(py::buffer rir, const py::buffer time, const py::buffer alpha,
const py::buffer visibility, int fs, size_t fdl,
void rir_builder(py::buffer rir, const py::buffer &time, const py::buffer &alpha,
const py::buffer &visibility, int fs, size_t fdl,
size_t lut_gran, size_t num_threads);

void delay_sum(const py::buffer irs, const py::buffer delays, py::buffer output,
Expand Down
2 changes: 1 addition & 1 deletion pyroomacoustics/tests/test_build_rir.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def measure_runtime(dtype=np.float32, num_threads=4):
td = np.round(tt).astype(np.int32)
tf = (tt - td).astype(dtype)
irs = np.zeros((tt.shape[0], fdl), dtype=dtype)
fractional_delay(irs, tf, 20, num_threads)
libroom.fractional_delay(irs, tf, 20, num_threads)
irs *= alpha[:, None]
libroom.delay_sum(irs, td, rir, num_threads)
tock_2steps = (time.perf_counter() - tick) / n_repeat
Expand Down

0 comments on commit 9e77929

Please sign in to comment.