|
3 | 3 | #include <cuda_runtime.h> |
4 | 4 | #include <mutex> |
5 | 5 |
|
| 6 | +#ifdef CUDA_STREAM_POOL |
| 7 | +#include <deque> |
| 8 | +#include <mutex> |
| 9 | +#include <unordered_map> |
| 10 | +#endif |
| 11 | + |
6 | 12 | uint32_t cuda_get_device() { |
7 | 13 | int device; |
8 | 14 | check_cuda_error(cudaGetDevice(&device)); |
@@ -109,18 +115,89 @@ void cuda_event_destroy(cudaEvent_t event, uint32_t gpu_index) { |
109 | 115 | check_cuda_error(cudaEventDestroy(event)); |
110 | 116 | } |
111 | 117 |
|
| 118 | +#ifdef CUDA_STREAM_POOL |
| 119 | +struct CudaBoundStream |
| 120 | +{ |
| 121 | + cudaStream_t stream; |
| 122 | + uint32_t gpu_index; |
| 123 | +}; |
| 124 | + |
| 125 | +class CudaStreamPool |
| 126 | +{ |
| 127 | + std::vector<CudaBoundStream> poolCompute; |
| 128 | + std::vector<CudaBoundStream> poolTransfer; |
| 129 | + |
| 130 | + std::mutex mutex_pools; |
| 131 | + |
| 132 | + size_t nextStream = 0; |
| 133 | + |
| 134 | + const size_t MAX_STREAMS = 16; |
| 135 | + |
| 136 | +public: |
| 137 | + cudaStream_t create_stream(uint32_t gpu_index) |
| 138 | + { |
| 139 | + std::lock_guard<std::mutex> lock(mutex_pools); |
| 140 | + if (poolCompute.empty()) |
| 141 | + { |
| 142 | + poolCompute.reserve(MAX_STREAMS); |
| 143 | + |
| 144 | + cuda_set_device(gpu_index); |
| 145 | + for (size_t i = 0; i < MAX_STREAMS; i++) |
| 146 | + { |
| 147 | + cudaStream_t stream; |
| 148 | + check_cuda_error(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking)); |
| 149 | + poolCompute.push_back(CudaBoundStream{stream, gpu_index}); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + PANIC_IF_FALSE(gpu_index == poolCompute[nextStream].gpu_index, "Bad gpu in stream pool"); |
| 154 | + cudaStream_t res = poolCompute[nextStream].stream; |
| 155 | + nextStream = (nextStream + 1) % poolCompute.size(); |
| 156 | + return res; |
| 157 | + } |
| 158 | + |
| 159 | + void destroy_stream(cudaStream_t stream, uint32_t gpu_index) |
| 160 | + { |
| 161 | + //do nothing |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | + |
| 166 | +class CudaMultiStreamPool { |
| 167 | + std::unordered_map<uint32_t, CudaStreamPool> per_gpu_pools; |
| 168 | + std::mutex pools_mutex; // for creation of the mem managers |
| 169 | + |
| 170 | +public: |
| 171 | + CudaStreamPool &get(uint32_t gpu_index) { |
| 172 | + std::lock_guard<std::mutex> guard(pools_mutex); |
| 173 | + return per_gpu_pools[gpu_index]; // creates it if it does not exist |
| 174 | + } |
| 175 | +}; |
| 176 | + |
| 177 | +CudaMultiStreamPool gCudaStreamPool; |
| 178 | +#endif |
| 179 | + |
| 180 | + |
112 | 181 | /// Unsafe function to create a CUDA stream, must check first that GPU exists |
113 | 182 | cudaStream_t cuda_create_stream(uint32_t gpu_index) { |
| 183 | +#ifdef CUDA_STREAM_POOL |
| 184 | + return gCudaStreamPool.get(gpu_index).create_stream(gpu_index); |
| 185 | +#else |
114 | 186 | cuda_set_device(gpu_index); |
115 | 187 | cudaStream_t stream; |
116 | 188 | check_cuda_error(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking)); |
117 | 189 | return stream; |
| 190 | +#endif |
118 | 191 | } |
119 | 192 |
|
120 | 193 | /// Unsafe function to destroy CUDA stream, must check first the GPU exists |
121 | 194 | void cuda_destroy_stream(cudaStream_t stream, uint32_t gpu_index) { |
| 195 | +#ifdef CUDA_STREAM_POOL |
| 196 | + gCudaStreamPool.get(gpu_index).destroy_stream(stream, gpu_index); |
| 197 | +#else |
122 | 198 | cuda_set_device(gpu_index); |
123 | 199 | check_cuda_error(cudaStreamDestroy(stream)); |
| 200 | +#endif |
124 | 201 | } |
125 | 202 |
|
126 | 203 | void cuda_synchronize_stream(cudaStream_t stream, uint32_t gpu_index) { |
|
0 commit comments