Skip to content

Commit

Permalink
Releases GIL as much as possible
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
  • Loading branch information
jeandet committed Mar 7, 2024
1 parent 1ed2ff8 commit 7c28f62
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
22 changes: 15 additions & 7 deletions pycdfpp/buffers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ template <CDF_Types data_t>
[[nodiscard]] py::array make_array(Variable& variable, py::object& obj)
{
// static_assert(data_t != CDF_Types::CDF_CHAR and data_t != CDF_Types::CDF_UCHAR);
return py::array_t<from_cdf_type_t<data_t>>(shape_ssize_t(variable),
strides<from_cdf_type_t<data_t>>(variable), variable.get<from_cdf_type_t<data_t>>().data(),
obj);
from_cdf_type_t<data_t>* ptr = nullptr;
{
py::gil_scoped_release release;
ptr = variable.get<from_cdf_type_t<data_t>>().data();
}
return py::array_t<from_cdf_type_t<data_t>>(
shape_ssize_t(variable), strides<from_cdf_type_t<data_t>>(variable), ptr, obj);
}

template <typename T, typename size_type>
Expand Down Expand Up @@ -155,17 +159,22 @@ template <CDF_Types T>
[[nodiscard]] py::buffer_info impl_make_buffer(cdf::Variable& var)
{
using U = cdf::from_cdf_type_t<T>;
char* ptr = nullptr;
{
py::gil_scoped_release release;
ptr = var.bytes_ptr();
}
if constexpr ((T == CDF_Types::CDF_CHAR) or (T == CDF_Types::CDF_UCHAR))
{
return py::buffer_info(var.bytes_ptr(), /* Pointer to buffer */
return py::buffer_info(ptr, /* Pointer to buffer */
var.shape().back(), /* Size of one scalar */
fmt::format("{}s", var.shape().back()),
static_cast<ssize_t>(std::size(var.shape()) - 1), /* Number of dimensions */
str_shape_ssize_t(var), str_strides<U>(var), true);
}
else
{
return py::buffer_info(var.bytes_ptr(), /* Pointer to buffer */
return py::buffer_info(ptr, /* Pointer to buffer */
sizeof(U), /* Size of one scalar */
py::format_descriptor<U>::format(),
static_cast<ssize_t>(std::size(var.shape())), /* Number of dimensions */
Expand All @@ -177,15 +186,14 @@ template <CDF_Types data_t, bool encode_strings>
[[nodiscard]] py::object make_str_array(py::object& obj)
{
py::module_ np = py::module_::import("numpy");
if constexpr(encode_strings)
if constexpr (encode_strings)
{
return np.attr("char").attr("decode")(py::memoryview(obj));
}
else
{
return np.attr("array")(py::memoryview(obj));
}

}

}
Expand Down
29 changes: 24 additions & 5 deletions pycdfpp/cdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ void def_cdf_loading_functions(T& mod)
[](py::bytes& buffer, bool iso_8859_1_to_utf8)
{
py::buffer_info info(py::buffer(buffer).request());
py::gil_scoped_release release;
return io::load(static_cast<char*>(info.ptr), static_cast<std::size_t>(info.size),
iso_8859_1_to_utf8);
},
Expand All @@ -172,6 +173,7 @@ void def_cdf_loading_functions(T& mod)
py::buffer_info info(buffer.request());
if (info.ndim != 1)
throw std::runtime_error("Incompatible buffer dimension!");
py::gil_scoped_release release;
return io::load(static_cast<char*>(info.ptr), info.shape[0], iso_8859_1_to_utf8, true);
},
py::arg("buffer"), py::arg("iso_8859_1_to_utf8") = false, py::return_value_policy::move,
Expand All @@ -180,7 +182,10 @@ void def_cdf_loading_functions(T& mod)
mod.def(
"load",
[](const char* fname, bool iso_8859_1_to_utf8, bool lazy_load)
{ return io::load(std::string { fname }, iso_8859_1_to_utf8, lazy_load); },
{
py::gil_scoped_release release;
return io::load(std::string { fname }, iso_8859_1_to_utf8, lazy_load);
},
py::arg("fname"), py::arg("iso_8859_1_to_utf8") = false, py::arg("lazy_load") = true,
py::return_value_policy::move);
}
Expand All @@ -196,14 +201,28 @@ void def_cdf_saving_functions(T& mod)

mod.def(
"save",
[](const CDF& cdf, const char* fname) { return io::save(cdf, std::string { fname }); },
[](const CDF& cdf, const char* fname)
{
py::gil_scoped_release release;
return io::save(cdf, std::string { fname });
},
py::arg("cdf"), py::arg("fname"));


py::class_<cdf_bytes>(mod, "_cdf_bytes", py::buffer_protocol())
.def_buffer([](cdf_bytes& b) -> py::buffer_info
{ return py::buffer_info(b.data.data(), std::size(b.data), true); });
.def_buffer(
[](cdf_bytes& b) -> py::buffer_info
{
py::gil_scoped_release release;
return py::buffer_info(b.data.data(), std::size(b.data), true);
});

mod.def(
"save", [](const CDF& cdf) { return cdf_bytes { io::save(cdf) }; }, py::arg("cdf"));
"save",
[](const CDF& cdf)
{
py::gil_scoped_release release;
return cdf_bytes { io::save(cdf) };
},
py::arg("cdf"));
}

0 comments on commit 7c28f62

Please sign in to comment.