CMS 3D CMS Logo

HostOnlyTask.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_AlpakaInterface_interface_HostOnlyTask_h
2 #define HeterogeneousCore_AlpakaInterface_interface_HostOnlyTask_h
3 
4 #include <functional>
5 #include <memory>
6 
7 #include <alpaka/alpaka.hpp>
8 
9 namespace alpaka {
10 
15  class HostOnlyTask {
16  public:
18 
19  void operator()() const { task_(); }
20 
21  private:
22  std::function<void()> task_;
23  };
24 
25  namespace trait {
26 
27 #ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
28  template <>
30  struct Enqueue<QueueCudaRtNonBlocking, HostOnlyTask> {
31  using TApi = ApiCudaRt;
32 
33  static void CUDART_CB callback(cudaStream_t /*queue*/, cudaError_t /*status*/, void* arg) {
34  //ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(status);
35  std::unique_ptr<HostOnlyTask> pTask(static_cast<HostOnlyTask*>(arg));
36  (*pTask)();
37  }
38 
39  ALPAKA_FN_HOST static auto enqueue(QueueCudaRtNonBlocking& queue, HostOnlyTask task) -> void {
40  auto pTask = std::make_unique<HostOnlyTask>(std::move(task));
41  ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(
42  cudaStreamAddCallback(alpaka::getNativeHandle(queue), callback, static_cast<void*>(pTask.release()), 0u));
43  }
44  };
45 #endif // ALPAKA_ACC_GPU_CUDA_ENABLED
46 
47 #ifdef ALPAKA_ACC_GPU_HIP_ENABLED
48  template <>
50  struct Enqueue<QueueHipRtNonBlocking, HostOnlyTask> {
51  using TApi = ApiHipRt;
52 
53  static void callback(hipStream_t /*queue*/, hipError_t /*status*/, void* arg) {
54  //ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(status);
55  std::unique_ptr<HostOnlyTask> pTask(static_cast<HostOnlyTask*>(arg));
56  (*pTask)();
57  }
58 
59  ALPAKA_FN_HOST static auto enqueue(QueueHipRtNonBlocking& queue, HostOnlyTask task) -> void {
60  auto pTask = std::make_unique<HostOnlyTask>(std::move(task));
61  ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(
62  hipStreamAddCallback(alpaka::getNativeHandle(queue), callback, static_cast<void*>(pTask.release()), 0u));
63  }
64  };
65 #endif // ALPAKA_ACC_GPU_HIP_ENABLED
66 
67  } // namespace trait
68 
69 } // namespace alpaka
70 
71 #endif // HeterogeneousCore_AlpakaInterface_interface_HostOnlyTask_h
HostOnlyTask(std::function< void()> task)
Definition: HostOnlyTask.h:17
A arg
Definition: Factorize.h:31
std::function< void()> task_
Definition: HostOnlyTask.h:22
void operator()() const
Definition: HostOnlyTask.h:19
def move(src, dest)
Definition: eostools.py:511