diff --git a/src/gpu/communication/UniformGPUScheme.h b/src/gpu/communication/UniformGPUScheme.h index 231b03294bd9d58e7cf7a970c56b8a05ddb2ab88..f622e302292519b0a64f856cf4a7701abad3873b 100644 --- a/src/gpu/communication/UniformGPUScheme.h +++ b/src/gpu/communication/UniformGPUScheme.h @@ -73,6 +73,52 @@ namespace communication { * When running multiple \ref UniformGPUScheme concurrently, different MPI tags * have to be used for the schemes: the tag can be passed in the constructor. */ +template<typename Stencil> +class GPUStreamRAII { + public: + GPUStreamRAII() { + for (uint_t i = 0; i < Stencil::Q; ++i) + WALBERLA_GPU_CHECK(gpuStreamCreate(&streams_[i])) + + } + + ~GPUStreamRAII() { + for (uint_t i = 0; i < Stencil::Q; ++i) { + WALBERLA_GPU_CHECK(gpuStreamDestroy(streams_[i])); + } + } + + // Delete copy constructor and copy assignment + GPUStreamRAII(const GPUStreamRAII&) = delete; + GPUStreamRAII& operator=(const GPUStreamRAII&) = delete; + + GPUStreamRAII(GPUStreamRAII&& other) noexcept { + for (uint_t i = 0; i < Stencil::Q; ++i) { + streams_[i] = other.streams_[i]; + other.streams_[i] = nullptr; + } + } + + GPUStreamRAII& operator=(GPUStreamRAII&& other) noexcept { + if (this != &other) { + for (uint_t i = 0; i < Stencil::Q; ++i) { + if (streams_[i]) { + WALBERLA_GPU_CHECK(gpuStreamDestroy(streams_[i])); + } + streams_[i] = other.streams_[i]; + other.streams_[i] = nullptr; + } + } + return *this; + } + + //gpuStream_t get(uint_t index) const { return streams_[index]; } + + + private: + std::array<gpuStream_t, Stencil::Q> streams_; +}; + template<typename Stencil> class UniformGPUScheme { @@ -88,11 +134,12 @@ namespace communication { const bool sendDirectlyFromGPU = false, const bool useLocalCommunication = true, const int tag = 5432 ); - ~UniformGPUScheme() + /*~UniformGPUScheme() { for (uint_t i = 0; i < Stencil::Q; ++i) - WALBERLA_GPU_CHECK(gpuStreamDestroy(streams_[i])) - } + WALBERLA_GPU_CHECK(gpuStreamDestroy(ravi_[i])) + }*/ + ~UniformGPUScheme() = default; void addPackInfo( const shared_ptr<GeneratedGPUPackInfo> &pi ); @@ -135,10 +182,15 @@ namespace communication { Set<SUID> requiredBlockSelectors_; Set<SUID> incompatibleBlockSelectors_; - std::array<gpuStream_t, Stencil::Q> streams_; + //std::array<gpuStream_t, Stencil::Q> streams_; // array of RAII wrappers + std::array<GPUStreamRAII<Stencil>, Stencil::Q> streams_; + }; + + + } // namespace communication } // namespace gpu } // namespace walberla