Skip to content

Commit e6fb836

Browse files
authored
Merge pull request NVIDIA#1455 from lucteo/get-system-scheduler-api
Use `get_system_scheduler()` to obtain the system scheduler.
2 parents ac27beb + 533ee60 commit e6fb836

File tree

4 files changed

+25
-90
lines changed

4 files changed

+25
-90
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
build*/
2+
.build*/
23
.cache
34
.vscode/*
45
!.vscode/launch.json

include/exec/system_context.hpp

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,8 @@ namespace exec {
7171
template <stdexec::sender _S, std::integral _Size, class _Fn>
7272
class system_bulk_sender;
7373

74-
/// Provides a view on some global underlying execution context supporting parallel forward progress.
75-
class system_context {
76-
public:
77-
/// Initializes the system context with the default implementation.
78-
system_context();
79-
~system_context() = default;
80-
81-
system_context(const system_context&) = delete;
82-
system_context(system_context&&) = delete;
83-
system_context& operator=(const system_context&) = delete;
84-
system_context& operator=(system_context&&) = delete;
85-
86-
// Returns a scheduler that can add work to the underlying execution context.
87-
system_scheduler get_scheduler();
88-
89-
/// Returns the maximum number of threads the context may support; this is just a hint.
90-
size_t max_concurrency() const noexcept;
91-
92-
private:
93-
/// The actual implementation of the system context.
94-
system_context_replaceability::system_scheduler* __impl_{nullptr};
95-
};
74+
/// Returns a scheduler that can add work to the underlying execution context.
75+
system_scheduler get_system_scheduler();
9676

9777
/// The execution domain of the system_scheduler, used for the purposes of customizing
9878
/// sender algorithms such as `bulk`.
@@ -540,20 +520,13 @@ namespace exec {
540520
_Fn __fun_;
541521
};
542522

543-
inline system_context::system_context() {
544-
__impl_ = system_context_replaceability::query_system_context<
523+
inline system_scheduler get_system_scheduler() {
524+
auto __impl = system_context_replaceability::query_system_context<
545525
system_context_replaceability::system_scheduler>();
546-
if (!__impl_) {
526+
if (!__impl) {
547527
throw std::runtime_error{"No system context implementation found"};
548528
}
549-
}
550-
551-
inline system_scheduler system_context::get_scheduler() {
552-
return system_scheduler{__impl_};
553-
}
554-
555-
inline size_t system_context::max_concurrency() const noexcept {
556-
return std::thread::hardware_concurrency();
529+
return system_scheduler{__impl};
557530
}
558531

559532
inline auto system_scheduler::query(stdexec::get_forward_progress_guarantee_t) const noexcept

test/exec/test_system_context.cpp

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,70 +33,41 @@
3333

3434
namespace ex = stdexec;
3535

36-
TEST_CASE("system_context has default ctor and dtor", "[types][system_scheduler]") {
37-
STATIC_REQUIRE(std::is_default_constructible_v<exec::system_context>);
38-
STATIC_REQUIRE(std::is_destructible_v<exec::system_context>);
39-
}
40-
41-
TEST_CASE("system_context is not copyable nor movable", "[types][system_scheduler]") {
42-
STATIC_REQUIRE_FALSE(std::is_copy_constructible_v<exec::system_context>);
43-
STATIC_REQUIRE_FALSE(std::is_move_constructible_v<exec::system_context>);
44-
}
45-
4636
TEST_CASE("system_context can return a scheduler", "[types][system_scheduler]") {
47-
auto sched = exec::system_context{}.get_scheduler();
37+
auto sched = exec::get_system_scheduler();
4838
STATIC_REQUIRE(ex::scheduler<decltype(sched)>);
4939
}
5040

51-
TEST_CASE("can query max concurrency from system_context", "[types][system_scheduler]") {
52-
exec::system_context ctx;
53-
size_t max_concurrency = ctx.max_concurrency();
54-
REQUIRE(max_concurrency >= 1);
55-
}
56-
5741
TEST_CASE("system scheduler is not default constructible", "[types][system_scheduler]") {
58-
auto sched = exec::system_context{}.get_scheduler();
42+
auto sched = exec::get_system_scheduler();
5943
using sched_t = decltype(sched);
6044
STATIC_REQUIRE(!std::is_default_constructible_v<sched_t>);
6145
STATIC_REQUIRE(std::is_destructible_v<sched_t>);
6246
}
6347

6448
TEST_CASE("system scheduler is copyable and movable", "[types][system_scheduler]") {
65-
auto sched = exec::system_context{}.get_scheduler();
49+
auto sched = exec::get_system_scheduler();
6650
using sched_t = decltype(sched);
6751
STATIC_REQUIRE(std::is_copy_constructible_v<sched_t>);
6852
STATIC_REQUIRE(std::is_move_constructible_v<sched_t>);
6953
}
7054

7155
TEST_CASE("a copied scheduler is equal to the original", "[types][system_scheduler]") {
72-
exec::system_context ctx;
73-
auto sched1 = ctx.get_scheduler();
56+
auto sched1 = exec::get_system_scheduler();
7457
auto sched2 = sched1;
7558
REQUIRE(sched1 == sched2);
7659
}
7760

7861
TEST_CASE(
79-
"two schedulers obtained from the same system_context are equal",
80-
"[types][system_scheduler]") {
81-
exec::system_context ctx;
82-
auto sched1 = ctx.get_scheduler();
83-
auto sched2 = ctx.get_scheduler();
84-
REQUIRE(sched1 == sched2);
85-
}
86-
87-
TEST_CASE(
88-
"compare two schedulers obtained from different system_context objects",
62+
"two schedulers obtained from get_system_scheduler() are equal",
8963
"[types][system_scheduler]") {
90-
exec::system_context ctx1;
91-
auto sched1 = ctx1.get_scheduler();
92-
exec::system_context ctx2;
93-
auto sched2 = ctx2.get_scheduler();
94-
// TODO: clarify the result of this in the paper
64+
auto sched1 = exec::get_system_scheduler();
65+
auto sched2 = exec::get_system_scheduler();
9566
REQUIRE(sched1 == sched2);
9667
}
9768

9869
TEST_CASE("system scheduler can produce a sender", "[types][system_scheduler]") {
99-
auto snd = ex::schedule(exec::system_context{}.get_scheduler());
70+
auto snd = ex::schedule(exec::get_system_scheduler());
10071
using sender_t = decltype(snd);
10172

10273
STATIC_REQUIRE(ex::sender<sender_t>);
@@ -105,17 +76,15 @@ TEST_CASE("system scheduler can produce a sender", "[types][system_scheduler]")
10576
}
10677

10778
TEST_CASE("trivial schedule task on system context", "[types][system_scheduler]") {
108-
exec::system_context ctx;
109-
exec::system_scheduler sched = ctx.get_scheduler();
79+
exec::system_scheduler sched = exec::get_system_scheduler();
11080

11181
ex::sync_wait(ex::schedule(sched));
11282
}
11383

11484
TEST_CASE("simple schedule task on system context", "[types][system_scheduler]") {
11585
std::thread::id this_id = std::this_thread::get_id();
11686
std::thread::id pool_id{};
117-
exec::system_context ctx;
118-
exec::system_scheduler sched = ctx.get_scheduler();
87+
exec::system_scheduler sched = exec::get_system_scheduler();
11988

12089
auto snd = ex::then(ex::schedule(sched), [&] { pool_id = std::this_thread::get_id(); });
12190

@@ -127,14 +96,12 @@ TEST_CASE("simple schedule task on system context", "[types][system_scheduler]")
12796
}
12897

12998
TEST_CASE("simple schedule forward progress guarantee", "[types][system_scheduler]") {
130-
exec::system_context ctx;
131-
exec::system_scheduler sched = ctx.get_scheduler();
99+
exec::system_scheduler sched = exec::get_system_scheduler();
132100
REQUIRE(ex::get_forward_progress_guarantee(sched) == ex::forward_progress_guarantee::parallel);
133101
}
134102

135103
TEST_CASE("get_completion_scheduler", "[types][system_scheduler]") {
136-
exec::system_context ctx;
137-
exec::system_scheduler sched = ctx.get_scheduler();
104+
exec::system_scheduler sched = exec::get_system_scheduler();
138105
REQUIRE(ex::get_completion_scheduler<ex::set_value_t>(ex::get_env(ex::schedule(sched))) == sched);
139106
REQUIRE(
140107
ex::get_completion_scheduler<ex::set_stopped_t>(ex::get_env(ex::schedule(sched))) == sched);
@@ -144,8 +111,7 @@ TEST_CASE("simple chain task on system context", "[types][system_scheduler]") {
144111
std::thread::id this_id = std::this_thread::get_id();
145112
std::thread::id pool_id{};
146113
std::thread::id pool_id2{};
147-
exec::system_context ctx;
148-
exec::system_scheduler sched = ctx.get_scheduler();
114+
exec::system_scheduler sched = exec::get_system_scheduler();
149115

150116
auto snd = ex::then(ex::schedule(sched), [&] { pool_id = std::this_thread::get_id(); });
151117
auto snd2 = ex::then(std::move(snd), [&] { pool_id2 = std::this_thread::get_id(); });
@@ -161,8 +127,7 @@ TEST_CASE("simple chain task on system context", "[types][system_scheduler]") {
161127

162128
// TODO: fix this test. This also makes tsan and asan unhappy.
163129
// TEST_CASE("checks stop_token before starting the work", "[types][system_scheduler]") {
164-
// exec::system_context ctx;
165-
// exec::system_scheduler sched = ctx.get_scheduler();
130+
// exec::system_scheduler sched = exec::get_system_scheduler();
166131

167132
// exec::async_scope scope;
168133
// scope.request_stop();
@@ -185,8 +150,7 @@ TEST_CASE("simple bulk task on system context", "[types][system_scheduler]") {
185150
std::thread::id this_id = std::this_thread::get_id();
186151
constexpr size_t num_tasks = 16;
187152
std::thread::id pool_ids[num_tasks];
188-
exec::system_context ctx;
189-
exec::system_scheduler sched = ctx.get_scheduler();
153+
exec::system_scheduler sched = exec::get_system_scheduler();
190154

191155
auto bulk_snd = ex::bulk(ex::schedule(sched), num_tasks, [&](unsigned long id) {
192156
pool_ids[id] = std::this_thread::get_id();
@@ -207,8 +171,7 @@ TEST_CASE("simple bulk chaining on system context", "[types][system_scheduler]")
207171
std::thread::id pool_id{};
208172
std::thread::id propagated_pool_ids[num_tasks];
209173
std::thread::id pool_ids[num_tasks];
210-
exec::system_context ctx;
211-
exec::system_scheduler sched = ctx.get_scheduler();
174+
exec::system_scheduler sched = exec::get_system_scheduler();
212175

213176
auto snd = ex::then(ex::schedule(sched), [&] {
214177
pool_id = std::this_thread::get_id();
@@ -268,8 +231,7 @@ TEST_CASE("can change the implementation of system context", "[types][system_sch
268231

269232
std::thread::id this_id = std::this_thread::get_id();
270233
std::thread::id pool_id{};
271-
exec::system_context ctx;
272-
exec::system_scheduler sched = ctx.get_scheduler();
234+
exec::system_scheduler sched = exec::get_system_scheduler();
273235

274236
auto snd = ex::then(ex::schedule(sched), [&] { pool_id = std::this_thread::get_id(); });
275237

test/exec/test_system_context_replaceability.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ TEST_CASE(
5858
"[system_scheduler][replaceability]") {
5959
std::thread::id this_id = std::this_thread::get_id();
6060
std::thread::id pool_id{};
61-
exec::system_context ctx;
62-
exec::system_scheduler sched = ctx.get_scheduler();
61+
exec::system_scheduler sched = exec::get_system_scheduler();
6362

6463
auto snd = ex::then(ex::schedule(sched), [&] { pool_id = std::this_thread::get_id(); });
6564

0 commit comments

Comments
 (0)