37#include <initializer_list>
42#if defined(__CUDACC__)
44#include <cuda_runtime.h>
45#elif defined(__HIPCC__)
46#include <hip/hip_runtime.h>
47#include <hip/hip_runtime.h>
65#ifndef CS_DISPATCH_QUEUE_FORCE_SYNC
66#define CS_DISPATCH_QUEUE_FORCE_SYNC 0
84#if defined(__CUDACC__)
86#elif defined(__HIPCC__)
89 std::chrono::steady_clock::time_point;
98#if defined(__CUDACC__)
100#elif defined(__HIPCC__)
112#if defined(__CUDACC__) || defined(__HIPCC__)
115 other.event_impl =
nullptr;
123#if defined(__CUDACC__) || defined(__HIPCC__)
126#if defined(__CUDACC__)
128#elif defined(__HIPCC__)
134 other.event_impl =
nullptr;
150#if defined(__CUDACC__)
156#elif defined(__HIPCC__)
173#if defined(__CUDACC__)
175#elif defined(__HIPCC__)
223 return ~(*event_ptr);
258#if defined(__CUDACC__)
259 cudaStream_t new_stream;
260 cudaStreamCreate(&new_stream);
261 context_.set_stream(new_stream);
262 cudaEventRecord(~start_event, context_.stream());
263#elif defined(__HIPCC__)
264 hipStream_t new_stream;
265 hipStreamCreate(&new_stream);
266 context_.set_stream(new_stream);
267 hipEventRecord(~start_event, context_.stream());
269 ~start_event = std::chrono::steady_clock::now();
277#if defined(__CUDACC__)
278 cudaStreamWaitEvent(context_.stream(), ~event);
279#elif defined(__HIPCC__)
280 hipStreamWaitEvent(context_.stream(), ~event);
289#if defined(__CUDACC__) || defined(__HIPCC__)
290 for (
auto const &event : sync_events) {
307#if defined(__CUDACC__)
308 cudaEventRecord(~end_event, context_.stream());
309#elif defined(__HIPCC__)
310 hipEventRecord(~end_event, context_.stream());
312 ~end_event = std::chrono::steady_clock::now();
314 return { end_event };
345#if defined(__CUDACC__)
346 cudaStreamDestroy(context_.stream());
347#elif defined(__HIPCC__)
348 hipStreamDestroy(context_.stream());
354template <
class FunctionType,
class... Args>
371#if defined(__CUDACC__) || defined(__HIPCC__)
372 std::tuple<FunctionType, args_tuple_t>;
374 std::tuple<FunctionType>;
388#if defined(__CUDACC__) || defined(__HIPCC__)
391 data_tuple_(std::move(function))
398#if defined(__CUDACC__)
400#elif defined(__HIPCC__)
407#if defined(__CUDACC__)
410 std::get<1>(data_tuple_) =
args_tuple_t{ std::move(args)... };
413 return cudaLaunchHostFunc
417 [](
void *data_tuple_ptr) ->
void {
418 auto &[f, args_tuple] = *(
data_tuple_t *)(data_tuple_ptr);
419 std::apply(f, args_tuple);
426 std::get<0>(data_tuple_)(args...);
429#elif defined(__HIPCC__)
432 std::get<1>(data_tuple_) =
args_tuple_t{ std::move(args)... };
435 return hipLaunchHostFunc
439 [](
void *data_tuple_ptr) ->
void {
440 auto &[f, args_tuple] = *(
data_tuple_t *)(data_tuple_ptr);
441 std::apply(f, args_tuple);
448 std::get<0>(data_tuple_)(args...);
452 std::get<0>(data_tuple_)(args...);
478 template <
class F,
class... Args>
485 std::forward<Args>(args)...);
490 template <
class F,
class... Args>
493 std::initializer_list<cs_event_ref>
const &sync_events,
501 std::forward<Args>(args)...);
506 template <
class M,
class F,
class... Args>
513 std::forward<Args>(args)...);
518 template <
class M,
class F,
class... Args>
521 std::initializer_list<cs_event_ref>
const &sync_events,
529 std::forward<Args>(args)...);
534 template <
class M,
class F,
class... Args>
541 std::forward<Args>(args)...);
546 template <
class M,
class F,
class... Args>
549 std::initializer_list<cs_event_ref>
const &sync_events,
557 std::forward<Args>(args)...);
562 template <
class T,
class F,
class... Args>
570 std::forward<Args>(args)...);
575 template <
class T,
class F,
class... Args>
579 std::initializer_list<cs_event_ref>
const &sync_events,
589 std::forward<Args>(args)...);
594 template <
class T,
class R,
class F,
class... Args>
603 std::forward<Args>(args)...);
608 template <
class T,
class R,
class F,
class... Args>
611 std::initializer_list<cs_event_ref>
const &sync_events,
623 std::forward<Args>(args)...);
630 template <
class FunctionType,
class... Args>
632 single_task(std::initializer_list<cs_event_ref>
const &sync_events,
633 FunctionType &&host_function,
637 std::move(host_function),
639 new_task.add_dependency(sync_events);
640 new_task.launch(std::forward<Args>(args)...);
641 new_task.record_end_event();
646 template <
class FunctionType,
class... Args>
651 std::move(host_function),
653 new_task.launch(std::forward<Args>(args)...);
654 new_task.record_end_event();
669#if defined(__CUDACC__) || defined(__HIPCC__)
672 std::chrono::microseconds;
674 std::chrono::steady_clock::duration;
695#if defined(__CUDACC__)
699 cudaEventElapsedTime(&result_ms, ~start, ~end);
701#elif defined(__HIPCC__)
705 hipEventElapsedTime(&result_ms, ~start, ~end);
708 return ~end - ~start;
auto parallel_for_reduce_sum(cs_lnum_t n, T &sum, F &&f, Args &&... args)
Definition: cs_dispatch.h:2169
auto parallel_for_reduce(cs_lnum_t n, T &result, R &reducer, F &&f, Args &&... args)
Definition: cs_dispatch.h:2195
auto parallel_for_b_faces(const M *m, F &&f, Args &&... args)
Definition: cs_dispatch.h:2127
auto parallel_for_i_faces(const M *m, F &&f, Args &&... args)
Definition: cs_dispatch.h:2099
auto parallel_for(cs_lnum_t n, F &&f, Args &&... args)
Definition: cs_dispatch.h:2146
void wait(void)
Wait (synchronize) until launched computations have finished.
Definition: cs_dispatch.h:2211
Definition: cs_dispatch.h:2288
Definition: cs_dispatch_queue.h:472
cs_task parallel_for_i_faces(const M *m, F &&f, Args &&...args)
Definition: cs_dispatch_queue.h:508
cs_task parallel_for_i_faces(const M *m, std::initializer_list< cs_event_ref > const &sync_events, F &&f, Args &&...args)
Definition: cs_dispatch_queue.h:520
cs_host_task< FunctionType, std::remove_reference_t< Args >... > single_task(std::initializer_list< cs_event_ref > const &sync_events, FunctionType &&host_function, Args &&...args)
Definition: cs_dispatch_queue.h:632
cs_host_task< FunctionType, std::remove_reference_t< Args >... > single_task(FunctionType &&host_function, Args &&...args)
Initiates a single thread task that runs on the host.
Definition: cs_dispatch_queue.h:648
cs_task parallel_for_reduce(cs_lnum_t n, std::initializer_list< cs_event_ref > const &sync_events, T &r, R &reducer, F &&f, Args &&...args)
Definition: cs_dispatch_queue.h:610
cs_task parallel_for_reduce_sum(cs_lnum_t n, T &sum, F &&f, Args &&...args)
Definition: cs_dispatch_queue.h:564
cs_task parallel_for(cs_lnum_t n, std::initializer_list< cs_event_ref > const &sync_events, F &&f, Args &&...args)
Definition: cs_dispatch_queue.h:492
cs_task parallel_for_reduce_sum(cs_lnum_t n, std::initializer_list< cs_event_ref > const &sync_events, T &sum, F &&f, Args &&...args)
Definition: cs_dispatch_queue.h:577
cs_task parallel_for_reduce(cs_lnum_t n, T &r, R &reducer, F &&f, Args &&...args)
Definition: cs_dispatch_queue.h:596
cs_task parallel_for_b_faces(const M *m, std::initializer_list< cs_event_ref > const &sync_events, F &&f, Args &&...args)
Definition: cs_dispatch_queue.h:548
cs_task parallel_for_b_faces(const M *m, F &&f, Args &&...args)
Definition: cs_dispatch_queue.h:536
cs_task parallel_for(cs_lnum_t n, F &&f, Args &&...args)
Definition: cs_dispatch_queue.h:480
cs_dispatch_context initializer_context
Context used to initialize tasks.
Definition: cs_dispatch_queue.h:476
Definition: cs_dispatch_queue.h:187
cs_event_ref & operator=(cs_event_ref const &) &=default
cs_event_ref & operator=(cs_event_ref &&) &=default
cs_event_ref(cs_event_ref const &other)=default
typename cs_event::underlying_type underlying_type
Definition: cs_dispatch_queue.h:191
cs_event * operator->()
Arrow operator to access members of the pointed event.
Definition: cs_dispatch_queue.h:206
underlying_type & operator~()
Definition: cs_dispatch_queue.h:221
cs_event_ref(cs_event &event)
Definition: cs_dispatch_queue.h:193
cs_event & operator*()
Dereference operator to access the pointed event.
Definition: cs_dispatch_queue.h:213
cs_event_ref(cs_event_ref &&other)=default
cs_host_task extends cs_device_task to add support for host function tasks.
Definition: cs_dispatch_queue.h:355
cs_host_task & operator=(cs_host_task &&)=default
~cs_host_task()
Wait for task termination.
Definition: cs_dispatch_queue.h:457
cs_host_task(cs_host_task const &)=delete
cs_host_task(FunctionType &&function, cs_dispatch_context context)
Definition: cs_dispatch_queue.h:385
std::tuple< Args... > args_tuple_t
Tuple type for argument storage.
Definition: cs_dispatch_queue.h:366
std::tuple< FunctionType > data_tuple_t
Definition: cs_dispatch_queue.h:374
cs_host_task & operator=(cs_host_task const &)=delete
cs_host_task(cs_host_task &&)=default
void launch(Args... args)
Definition: cs_dispatch_queue.h:405
Definition: cs_dispatch_queue.h:237
~cs_task()
Waits for task termination and destroys the associated CUDA stream.
Definition: cs_dispatch_queue.h:342
cs_task(cs_task &&)=default
cs_event_ref get_end_event()
Return a reference to the end event.
Definition: cs_dispatch_queue.h:336
cs_task(cs_dispatch_context context={})
Create a new task with a given context and initialize a new stream.
Definition: cs_dispatch_queue.h:256
cs_task(cs_task const &)=delete
cs_event_ref get_start_event()
Return a reference to the start event.
Definition: cs_dispatch_queue.h:329
cs_dispatch_context & get_context()
Return a reference to the context.
Definition: cs_dispatch_queue.h:322
cs_event_ref record_end_event()
Record an event from the task and return a cs_event_ref to it.
Definition: cs_dispatch_queue.h:305
void wait()
Wait for task completion.
Definition: cs_dispatch_queue.h:298
cs_task & operator=(cs_task &&)=default
void add_dependency(cs_event_ref event)
Add an event to wait for.
Definition: cs_dispatch_queue.h:275
cs_task & operator=(cs_task const &)=delete
void add_dependency(std::initializer_list< cs_event_ref > const &sync_events)
Definition: cs_dispatch_queue.h:287
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:325
std::chrono::steady_clock::duration cs_event_duration
Duration type for elapsed time between two events.
Definition: cs_dispatch_queue.h:674
cs_event_duration cs_elapsed_time(cs_event_ref start, cs_event_ref end)
Returns elapsed time (in microseconds) between two events.
Definition: cs_dispatch_queue.h:689
static void sum(const cs_mpi_wrapper &mpi_w, T &first, Vals &... values)
Sum values of a given datatype over a given communicator.
Definition: cs_parall.h:893
Definition: cs_dispatch_queue.h:82
cs_event(cs_event &&other)=default
cs_event(cs_event const &other)=delete
Destructor.
underlying_type & operator~()
Return the underlying implementation.
Definition: cs_dispatch_queue.h:144
cs_event()
Constructor.
Definition: cs_dispatch_queue.h:96
cs_event & operator=(cs_event &&other)=default
void wait()
Wait upon completion.
Definition: cs_dispatch_queue.h:171
cs_event & operator=(cs_event const &)=delete
std::chrono::steady_clock::time_point underlying_type
Definition: cs_dispatch_queue.h:89
underlying_type event_impl
Definition: cs_dispatch_queue.h:92