Skip to content

Commit 776b2fc

Browse files
committed
Refactor, GIL management
1 parent b88ee54 commit 776b2fc

File tree

7 files changed

+59
-183
lines changed

7 files changed

+59
-183
lines changed

source/framework/python/source/algorithm/miscellaneous_operation/uniform-2.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,21 @@ using namespace pybind11::literals;
1212
namespace lue::framework {
1313
namespace {
1414

15-
// Step 3: Call the algorithm
15+
// Step 4: Release the GIL and call the algorithm
16+
template<typename Element, Rank rank>
17+
auto uniform(
18+
StaticShape<rank> const& array_shape,
19+
StaticShape<rank> const& partition_shape,
20+
Element const& min_value,
21+
Element const& max_value) -> PartitionedArray<Element, rank>
22+
{
23+
pybind11::gil_scoped_release release{};
24+
25+
return value_policies::uniform(array_shape, partition_shape, min_value, max_value);
26+
}
27+
28+
29+
// Step 3: Convert between Python and C++ types
1630
template<typename Element, Rank rank>
1731
auto uniform(
1832
StaticShape<rank> const& array_shape,
@@ -24,12 +38,11 @@ namespace lue::framework {
2438

2539
if constexpr (arithmetic_element_supported<Element>)
2640
{
27-
result = pybind11::cast(
28-
value_policies::uniform(
29-
array_shape,
30-
partition_shape,
31-
pybind11::cast<Element>(min_value),
32-
pybind11::cast<Element>(max_value)));
41+
result = pybind11::cast(value_policies::uniform(
42+
array_shape,
43+
partition_shape,
44+
pybind11::cast<Element>(min_value),
45+
pybind11::cast<Element>(max_value)));
3346
}
3447

3548
return result;
@@ -143,11 +156,10 @@ namespace lue::framework {
143156

144157
if (dynamic_array_shape.size() != dynamic_partition_shape.size())
145158
{
146-
throw std::runtime_error(
147-
std::format(
148-
"Rank of array shape and partition shape must be equal ({} != {})",
149-
dynamic_array_shape.size(),
150-
dynamic_partition_shape.size()));
159+
throw std::runtime_error(std::format(
160+
"Rank of array shape and partition shape must be equal ({} != {})",
161+
dynamic_array_shape.size(),
162+
dynamic_partition_shape.size()));
151163
}
152164
}
153165

source/framework/python/source/command_line.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,23 @@ namespace lue {
77

88
CommandLine::CommandLine()
99
{
10-
// The goal here is (only) to set _argc/_argv for the
11-
// HPX runtime to use
12-
13-
pybind11::gil_scoped_acquire acquire;
14-
15-
pybind11::object sys{pybind11::module_::import("sys")};
16-
pybind11::list argv_py{sys.attr("argv")};
10+
// The goal here is (only) to set _argc/_argv for the HPX runtime to use
11+
{
12+
pybind11::gil_scoped_acquire acquire{};
13+
pybind11::object sys{pybind11::module_::import("sys")};
14+
pybind11::list argv_py{sys.attr("argv")};
1715

18-
_argc = static_cast<int>(argv_py.size());
16+
_argc = static_cast<int>(argv_py.size());
1917

20-
_argument_strings.resize(_argc);
21-
_argument_pointers.resize(_argument_strings.size() + 1);
18+
_argument_strings.resize(_argc);
19+
_argument_pointers.resize(_argument_strings.size() + 1);
2220

23-
for (int i = 0; i < _argc; ++i)
24-
{
25-
pybind11::str arg_py{argv_py[i]};
26-
_argument_strings[i] = arg_py;
27-
_argument_pointers[i] = _argument_strings[i].data();
21+
for (int i = 0; i < _argc; ++i)
22+
{
23+
pybind11::str arg_py{argv_py[i]};
24+
_argument_strings[i] = arg_py;
25+
_argument_pointers[i] = _argument_strings[i].data();
26+
}
2827
}
2928

3029
lue_assert(_argument_pointers.size() == static_cast<std::size_t>(_argc + 1));

source/framework/python/source/command_line.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#pragma once
2-
#include "lue/py/configure.hpp"
3-
#include <functional>
4-
#include <memory>
52
#include <string>
63
#include <vector>
74

source/framework/python/source/hpx_runtime.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "hpx_runtime.hpp"
2-
#include "ostream.hpp"
32
#include <hpx/hpx_start.hpp>
43
#include <pybind11/pybind11.h>
54

@@ -18,8 +17,7 @@ namespace lue {
1817

1918
hpx::init_params params{};
2019
params.cfg = _configuration;
21-
params.mode = hpx::runtime_mode::default_;
22-
// params.mode = hpx::runtime_mode::console;
20+
params.mode = hpx::runtime_mode::console;
2321

2422
if (!hpx::start(start_function, _command_line.argc(), _command_line.argv(), params))
2523
{
@@ -55,7 +53,7 @@ namespace lue {
5553

5654

5755
// Main HPX thread, does nothing but wait for the application to exit
58-
int HPXRuntime::hpx_main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
56+
auto HPXRuntime::hpx_main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) -> int
5957
{
6058
// Store a pointer to the runtime here.
6159
_runtime = hpx::get_runtime_ptr();
@@ -68,14 +66,6 @@ namespace lue {
6866

6967
_startup_condition_variable.notify_one();
7068

71-
// Redirect all console output to Python's stdout
72-
std::unique_ptr<phylanx::bindings::scoped_ostream_redirect> stream;
73-
74-
{
75-
pybind11::gil_scoped_acquire acquire;
76-
stream = std::unique_ptr<phylanx::bindings::scoped_ostream_redirect>{};
77-
}
78-
7969
// Now, wait for destructor to be called.
8070
{
8171
std::unique_lock<hpx::spinlock> lock(_mutex);
@@ -85,11 +75,6 @@ namespace lue {
8575
}
8676
}
8777

88-
{
89-
pybind11::gil_scoped_acquire acquire;
90-
stream.reset();
91-
}
92-
9378
// Tell the runtime it's OK to exit
9479
return hpx::finalize();
9580
}

source/framework/python/source/hpx_runtime.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@ namespace lue {
1313

1414
HPXRuntime(std::vector<std::string> const& configuration);
1515

16+
HPXRuntime(HPXRuntime const& other) = delete;
17+
18+
HPXRuntime(HPXRuntime&& other) = delete;
19+
1620
~HPXRuntime();
1721

22+
auto operator=(HPXRuntime const& other) -> HPXRuntime& = delete;
23+
24+
auto operator=(HPXRuntime&& other) -> HPXRuntime& = delete;
25+
1826
protected:
1927

20-
int hpx_main(int argc, char* argv[]);
28+
auto hpx_main(int argc, char* argv[]) -> int;
2129

2230
private:
2331

source/framework/python/source/ostream.hpp

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

source/framework/python/source/submodule.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <pybind11/numpy.h>
88
#include <pybind11/stl.h>
99

10+
#include <hpx/algorithm.hpp>
11+
1012

1113
namespace lue::framework {
1214
namespace {
@@ -16,11 +18,10 @@ namespace lue::framework {
1618

1719
void start_hpx_runtime(std::vector<std::string> const& configuration)
1820
{
19-
// Iff the pointer to the runtime is not pointing to an instance,
20-
// instantiate one. This will start the HPX runtime.
21+
// Iff the pointer to the runtime is not pointing to an instance, instantiate one. This will
22+
// start the HPX runtime.
2123
if (runtime == nullptr)
2224
{
23-
pybind11::gil_scoped_release release;
2425
runtime = new HPXRuntime{configuration};
2526
}
2627
}
@@ -34,7 +35,6 @@ namespace lue::framework {
3435
{
3536
HPXRuntime* r = runtime;
3637
runtime = nullptr;
37-
pybind11::gil_scoped_release release;
3838
delete r;
3939
}
4040
}
@@ -108,11 +108,12 @@ namespace lue::framework {
108108

109109
gdal::register_gdal_drivers();
110110

111-
submodule.def("start_hpx_runtime", &start_hpx_runtime);
112-
113-
submodule.def("stop_hpx_runtime", &stop_hpx_runtime);
114-
115-
submodule.def("on_root_locality", &on_root_locality);
111+
submodule.def(
112+
"start_hpx_runtime", &start_hpx_runtime, pybind11::call_guard<pybind11::gil_scoped_release>());
113+
submodule.def(
114+
"stop_hpx_runtime", &stop_hpx_runtime, pybind11::call_guard<pybind11::gil_scoped_release>());
115+
submodule.def(
116+
"on_root_locality", &on_root_locality, pybind11::call_guard<pybind11::gil_scoped_release>());
116117

117118
bind_hpx(submodule);
118119

0 commit comments

Comments
 (0)