Skip to content

Commit 21e132c

Browse files
authored
add polling example using stdexec (#62)
* add polling example using stdexec * harmonize printouts between polling examamples
1 parent ffb29bb commit 21e132c

5 files changed

Lines changed: 322 additions & 13 deletions

File tree

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ if(BUILD_CUDA AND BUILD_TBB)
8181
target_link_libraries(exec_reco_stdexec PRIVATE CoroutineTests CUDA::cudart TBB::tbb STDEXEC::stdexec)
8282
add_executable(exec_delegate_stdexec exec_delegate.cpp)
8383
target_link_libraries(exec_delegate_stdexec PRIVATE CoroutineTests CUDA::cudart TBB::tbb STDEXEC::stdexec)
84+
add_executable(exec_event_poll_stdexec exec_event_poll.cpp)
85+
target_link_libraries(exec_event_poll_stdexec PRIVATE CoroutineTests CUDA::cudart TBB::tbb STDEXEC::stdexec)
8486
endif()
8587
if(BUILD_CAPY)
8688
add_executable(capy_reco capy_reco.cpp)

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,6 @@ These examples are a modification of [reconstruction examples](#reconstruction)
182182

183183
## Event poll
184184

185-
Link: [alien_event_poll.cpp](alien_event_poll.cpp), [capy_event_poll.cpp](capy_event_poll.cpp)
185+
Link: [alien_event_poll.cpp](alien_event_poll.cpp), [exec_event_poll.cpp](exec_event_poll.cpp), [capy_event_poll.cpp](capy_event_poll.cpp)
186186

187187
These examples are a variant of [delegate examples](#delegate) in which awaiting completion of CUDA operations is done by repeatedly querying the event state instead of using a callback as in earlier examples. All the queries are executed by a specific thread.

examples/alien_event_poll.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ struct Retry {
5050
void await_resume() const noexcept {}
5151
};
5252

53-
tool::Task<void> poll(cudaEvent_t event) {
53+
tool::Task<void> poll(cudaEvent_t event, std::string_view parent) {
5454
auto status = cudaSuccess;
55-
log() << "Polling for event completion..." << std::endl;
55+
log(parent) << "Polling for event completion..." << std::endl;
5656
while ((status = cudaEventQuery(event)) == cudaErrorNotReady) {
57-
log() << "Event not ready, retrying..." << std::endl;
57+
log(parent) << "Event not ready, retrying..." << std::endl;
5858
co_await Retry{};
5959
}
6060
ERROR_CHECK_CUDA(status);
61+
log(parent) << "Event completed successfully" << std::endl;
6162
}
6263

6364
subtool::Task<DeviceBuffer<int>> clusterization(
@@ -83,7 +84,7 @@ subtool::Task<DeviceBuffer<int>> clusterization(
8384
ERROR_CHECK_CUDA(cudaEventRecord(event, stream));
8485
}));
8586

86-
co_await schedule_on(delegation_scheduler, poll(event));
87+
co_await schedule_on(delegation_scheduler, poll(event, self));
8788

8889
auto nClusters = 0;
8990
for (auto v : h_cells)
@@ -136,7 +137,7 @@ subtool::Task<DeviceBuffer<int>> seeding(
136137
ERROR_CHECK_CUDA(cudaEventRecord(event, stream));
137138
}));
138139

139-
co_await schedule_on(delegation_scheduler, poll(event));
140+
co_await schedule_on(delegation_scheduler, poll(event, self));
140141

141142
int nSeeds = 0;
142143
for (auto v : h_clusters)
@@ -200,7 +201,7 @@ tool::Task<tool::StatusCode> reconstruct(
200201
ERROR_CHECK_CUDA(cudaEventRecord(event, stream));
201202
}));
202203

203-
co_await schedule_on(delegation_scheduler, poll(event));
204+
co_await schedule_on(delegation_scheduler, poll(event, self));
204205

205206
log(self) << "Finishing reconstruction" << std::endl;
206207
co_return tool::StatusCode::SUCCESS;

examples/capy_event_poll.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ struct Retry {
5151
void await_resume() const noexcept {}
5252
};
5353

54-
boost::capy::task<void> poll(cudaEvent_t event) {
54+
boost::capy::task<void> poll(cudaEvent_t event, std::string_view parent) {
5555
auto status = cudaSuccess;
56-
log() << "Polling for event completion..." << std::endl;
56+
log(parent) << "Polling for event completion..." << std::endl;
5757
while ((status = cudaEventQuery(event)) == cudaErrorNotReady) {
58-
log() << "Event not ready, retrying..." << std::endl;
58+
log(parent) << "Event not ready, retrying..." << std::endl;
5959
co_await Retry{};
6060
}
6161
ERROR_CHECK_CUDA(status);
62+
log(parent) << "Event completed successfully" << std::endl;
6263
}
6364

6465
boost::capy::task<DeviceBuffer<int>> clusterization(
@@ -81,7 +82,8 @@ boost::capy::task<DeviceBuffer<int>> clusterization(
8182
ERROR_CHECK_CUDA(cudaEventRecord(event, stream));
8283
}));
8384

84-
co_await boost::capy::run(delegation_thread.get_executor())(poll(event));
85+
co_await boost::capy::run(delegation_thread.get_executor())(
86+
poll(event, self));
8587

8688
auto nClusters = 0;
8789
for (auto v : h_cells)
@@ -129,7 +131,8 @@ boost::capy::task<DeviceBuffer<int>> seeding(
129131
ERROR_CHECK_CUDA(cudaEventRecord(event, stream));
130132
}));
131133

132-
co_await boost::capy::run(delegation_thread.get_executor())(poll(event));
134+
co_await boost::capy::run(delegation_thread.get_executor())(
135+
poll(event, self));
133136

134137
int nSeeds = 0;
135138
for (auto v : h_clusters)
@@ -188,7 +191,8 @@ boost::capy::task<tools::StatusCode> reconstruct(
188191
ERROR_CHECK_CUDA(cudaEventRecord(event, stream));
189192
}));
190193

191-
co_await boost::capy::run(delegation_thread.get_executor())(poll(event));
194+
co_await boost::capy::run(delegation_thread.get_executor())(
195+
poll(event, self));
192196

193197
log(self) << "Finishing reconstruction" << std::endl;
194198
co_return tools::StatusCode::SUCCESS;

0 commit comments

Comments
 (0)