From dc172bff7406b956895f1e29e94d95cae9832e7c Mon Sep 17 00:00:00 2001 From: gbalduzz Date: Thu, 28 May 2020 20:18:58 +0200 Subject: [PATCH 1/2] added noexcept qualifier to stream move constructor/assignment. --- include/dca/linalg/util/cublas_handle.hpp | 13 +++++++---- include/dca/linalg/util/cuda_stream.hpp | 16 +++++++++---- include/dca/linalg/util/magma_queue.hpp | 24 +++++++++++++++----- include/dca/linalg/util/stream_container.hpp | 8 +++---- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/include/dca/linalg/util/cublas_handle.hpp b/include/dca/linalg/util/cublas_handle.hpp index 2f311df7f..b3401c237 100644 --- a/include/dca/linalg/util/cublas_handle.hpp +++ b/include/dca/linalg/util/cublas_handle.hpp @@ -23,28 +23,33 @@ namespace util { class CublasHandle { public: - CublasHandle() { + CublasHandle() noexcept { cublasStatus_t ret = cublasCreate(&handle_); checkRC(ret); } CublasHandle& operator=(const CublasHandle& other) = delete; - CublasHandle(CublasHandle&& other) { + CublasHandle(CublasHandle&& other) noexcept { std::swap(handle_, other.handle_); } + CublasHandle& operator=(CublasHandle&& other) noexcept { + std::swap(handle_, other.handle_); + return *this; + } + ~CublasHandle() { if (handle_) cublasDestroy(handle_); } - void setStream(cudaStream_t stream) { + void setStream(cudaStream_t stream) noexcept { cublasStatus_t ret = cublasSetStream(handle_, stream); checkRC(ret); } - operator cublasHandle_t() const { + operator cublasHandle_t() const noexcept { return handle_; } diff --git a/include/dca/linalg/util/cuda_stream.hpp b/include/dca/linalg/util/cuda_stream.hpp index ea935e141..0b6045780 100644 --- a/include/dca/linalg/util/cuda_stream.hpp +++ b/include/dca/linalg/util/cuda_stream.hpp @@ -26,17 +26,23 @@ namespace util { class CudaStream { public: - CudaStream() { + CudaStream() noexcept { cudaStreamCreate(&stream_); } CudaStream(const CudaStream& other) = delete; + CudaStream& operator=(const CudaStream& other) = delete; - CudaStream(CudaStream&& other) { + CudaStream(CudaStream&& other) noexcept { std::swap(stream_, other.stream_); } - void sync() const { + CudaStream& operator=(CudaStream&& other) noexcept { + std::swap(stream_, other.stream_); + return *this; + } + + void sync() const noexcept { checkRC(cudaStreamSynchronize(stream_)); } @@ -45,7 +51,7 @@ class CudaStream { cudaStreamDestroy(stream_); } - operator cudaStream_t() const { + operator cudaStream_t() const noexcept { return stream_; } @@ -60,7 +66,7 @@ class CudaStream { public: CudaStream() = default; - void sync() const {} + void sync() const noexcept {} }; #endif // DCA_HAVE_CUDA diff --git a/include/dca/linalg/util/magma_queue.hpp b/include/dca/linalg/util/magma_queue.hpp index decc571b8..21ddf6681 100644 --- a/include/dca/linalg/util/magma_queue.hpp +++ b/include/dca/linalg/util/magma_queue.hpp @@ -23,19 +23,31 @@ namespace util { class MagmaQueue { public: - MagmaQueue() { + MagmaQueue() noexcept { magma_queue_create(&queue_); } + MagmaQueue(const MagmaQueue&) = delete; + MagmaQueue& operator=(const MagmaQueue&) = delete; + + MagmaQueue(MagmaQueue&& rhs) noexcept { + std::swap(queue_, rhs.queue_); + } + + MagmaQueue& operator=(MagmaQueue&& rhs) noexcept { + std::swap(queue_, rhs.queue_); + return *this; + } + ~MagmaQueue() { magma_queue_destroy(queue_); } - inline operator magma_queue_t() { + operator magma_queue_t() const noexcept { return queue_; } - cudaStream_t getStream() const { + cudaStream_t getStream() const noexcept { return magma_queue_get_cuda_stream(queue_); } @@ -43,9 +55,9 @@ class MagmaQueue { magma_queue_t queue_ = nullptr; }; -} // util -} // linalg -} // dca +} // namespace util +} // namespace linalg +} // namespace dca #endif // DCA_HAVE_CUDA #endif // DCA_LINALG_UTIL_MAGMA_QUEUE_HPP diff --git a/include/dca/linalg/util/stream_container.hpp b/include/dca/linalg/util/stream_container.hpp index b472cb8b0..ca0c9458e 100644 --- a/include/dca/linalg/util/stream_container.hpp +++ b/include/dca/linalg/util/stream_container.hpp @@ -28,11 +28,11 @@ class StreamContainer { public: StreamContainer(int max_threads = 0) : streams_(max_threads * streams_per_thread_) {} - int get_max_threads() const { + int get_max_threads() const noexcept { return streams_.size() / streams_per_thread_; } - int get_streams_per_thread() const { + int get_streams_per_thread() const noexcept { return streams_per_thread_; } @@ -46,7 +46,7 @@ class StreamContainer { // Returns the 'stream_id'-th stream associated with thread 'thread_id'. // Preconditions: 0 <= thread_id < get_max_threads(), // 0 <= stream_id < streams_per_thread_. - CudaStream& operator()(int thread_id, int stream_id) { + CudaStream& operator()(int thread_id, int stream_id) noexcept { assert(thread_id >= 0 && thread_id < get_max_threads()); assert(stream_id >= 0 && stream_id < streams_per_thread_); return streams_[stream_id + streams_per_thread_ * thread_id]; @@ -55,7 +55,7 @@ class StreamContainer { // Synchronizes the 'stream_id'-th stream associated with thread 'thread_id'. // Preconditions: 0 <= thread_id < get_max_threads(), // 0 <= stream_id < streams_per_thread_. - void sync(int thread_id, int stream_id) { + void sync(int thread_id, int stream_id) noexcept { operator()(thread_id, stream_id).sync(); } From 4233cb2db88cee0c6b8b0ccf5091971352af8b65 Mon Sep 17 00:00:00 2001 From: gbalduzz Date: Fri, 5 Jun 2020 17:22:20 +0200 Subject: [PATCH 2/2] silenced memory type warning. --- test/unit/linalg/gpu_test_util.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/linalg/gpu_test_util.hpp b/test/unit/linalg/gpu_test_util.hpp index 8fdeb9338..a7e691a57 100644 --- a/test/unit/linalg/gpu_test_util.hpp +++ b/test/unit/linalg/gpu_test_util.hpp @@ -25,7 +25,7 @@ cudaMemoryType PointerType(const ScalarType* ptr) { if (ret == cudaErrorInvalidValue) return cudaMemoryTypeHost; checkRC(ret); - return attributes.memoryType; + return attributes.type; } template