HAMR
The Heterogeneous Accelerator Memory Resource
hamr_stream_impl.h
Go to the documentation of this file.
1 #ifndef hamr_stream_h
2 #define hamr_stream_h
3 
4 ///@file
5 
6 #include "hamr_config.h"
7 
8 #include <cstddef>
9 #include <variant>
10 
11 #if defined(HAMR_ENABLE_CUDA)
12 #include <cuda_runtime.h>
13 #else
14 using cudaStream_t = void*;
15 #endif
16 #if defined(HAMR_ENABLE_HIP)
17 #include <hip/hip_runtime.h>
18 #else
19 using hipStream_t = void*;
20 #endif
21 
22 namespace hamr
23 {
24 
25 /// A wrapper around technology specific streams.
26 /** Streams are used to enable and order concurrent operations on accelerator
27  * devices. The default stream used in hamr is a stream-per-thread where
28  * available. However, note that libraries built seperately will likely use
29  * the default blocking stream and if so explicit specification of the stream
30  * when calling into those libraries is necessary. Note that hamr passes stream
31  * correctly when interfacing with Python. In most cases the hamr API's
32  * requiring a ::stream can be passed the technology specific stream due to
33  * implicit conversion operators implemented here.
34  */
35 class HAMR_EXPORT stream
36 {
37 public:
38  /// constructs a default stream
39  stream() :
40 #if defined(HAMR_ENABLE_CUDA)
41  m_stream(std::in_place_index<1>, cudaStreamPerThread)
42 #elif defined(HAMR_ENABLE_HIP)
43  m_stream(std::in_place_index<2>, hipStreamPerThread)
44 #else
45  m_stream(std::in_place_index<0>, '\0')
46 #endif
47  {}
48 
49  stream(const stream &) = default;
50  stream(stream &&) = default;
51 
52  stream &operator=(const stream &) = default;
53  stream &operator=(stream &&) = default;
54 
55 #if defined(HAMR_ENABLE_CUDA)
56  /// convert to a CUDA stream
57  operator cudaStream_t () const { return this->get_cuda_stream(); }
58 
59  /// assign a CUDA stream
60  stream &operator=(cudaStream_t strm)
61  {
62  m_stream = strm;
63  return *this;
64  }
65 
66  /// Constructs or converts from a CUDA stream
67  stream(const cudaStream_t &strm) : m_stream(std::in_place_index<1>, strm) {}
68 
69  /// Accesses the CUDA stream.
70  cudaStream_t get_cuda_stream() const
71  {
72  const cudaStream_t *cs;
73  if ((cs = std::get_if<1>(&m_stream)))
74  return *cs;
75  return 0; // default stream
76  }
77 #endif
78 #if defined(HAMR_ENABLE_HIP)
79  /// convert to a HIP stream
80  operator hipStream_t () const { return this->get_hip_stream(); }
81 
82  /// assign a HIP stream
83  stream &operator=(hipStream_t strm)
84  {
85  m_stream = strm;
86  return *this;
87  }
88 
89  /// Constructs or converts from a HIP stream
90  stream(hipStream_t &strm) : m_stream(std::in_place_index<2>, strm) {}
91 
92  /// Accesses the HIP stream.
93  hipStream_t get_hip_stream() const
94  {
95  const hipStream_t *hs;
96  if ((hs = std::get_if<2>(&m_stream)))
97  return *hs;
98  return 0; // default stream
99  }
100 #endif
101 
102  /// synchronize the stream
103  int synchronize() const;
104 
105  /// evaluates true if a stream has been set
106  operator bool() const;
107 
108  /// sends the value of the stream to std::cerr
109  void print() const;
110 
111  /// convert the technology specific stream to an integer
112  size_t get_stream();
113 
114 private:
115  std::variant<char, cudaStream_t, hipStream_t> m_stream;
116 };
117 }
118 #endif
hamr::synchronize
void synchronize(PP &&... args)
Definition: hamr_buffer_util.h:174
hamr::stream
A wrapper around technology specific streams.
Definition: hamr_stream.h:35
hamr::stream::stream
stream()
constructs a default stream
Definition: hamr_stream_impl.h:39
hamr
heterogeneous accelerator memory resource
Definition: hamr_buffer.h:13