Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:12:17

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 // CUDA plugin include(s).
0010 #include "Acts/Plugins/Cuda/Utilities/StreamWrapper.hpp"
0011 
0012 #include "ErrorCheck.cuh"
0013 #include "StreamHandlers.cuh"
0014 
0015 // CUDA include(s).
0016 #include <cuda_runtime.h>
0017 
0018 namespace Acts {
0019 namespace Cuda {
0020 
0021 StreamWrapper::StreamWrapper(void* stream, bool ownsStream)
0022     : m_stream(stream), m_ownsStream(ownsStream) {}
0023 
0024 StreamWrapper::StreamWrapper(StreamWrapper&& parent)
0025     : m_stream(parent.m_stream), m_ownsStream(parent.m_ownsStream) {
0026   parent.m_stream = nullptr;
0027   parent.m_ownsStream = false;
0028 }
0029 
0030 StreamWrapper::~StreamWrapper() {
0031   // Destroy the stream, if we still hold it.
0032   if (m_stream && m_ownsStream) {
0033     ACTS_CUDA_ERROR_CHECK(cudaStreamDestroy(getStreamFrom(*this)));
0034   }
0035 }
0036 
0037 StreamWrapper& StreamWrapper::operator=(StreamWrapper&& rhs) {
0038   // Check whether anything needs to be done.
0039   if (this == &rhs) {
0040     return *this;
0041   }
0042 
0043   // Destroy the current stream, if we hold one.
0044   if (m_stream && m_ownsStream) {
0045     ACTS_CUDA_ERROR_CHECK(cudaStreamDestroy(getStreamFrom(*this)));
0046   }
0047 
0048   // Perform the move.
0049   m_stream = rhs.m_stream;
0050   m_ownsStream = rhs.m_ownsStream;
0051   rhs.m_stream = nullptr;
0052   rhs.m_ownsStream = false;
0053 
0054   // Return this object.
0055   return *this;
0056 }
0057 
0058 void StreamWrapper::synchronize() const {
0059   // Use CUDA to wait for all tasks to finish in the stream.
0060   ACTS_CUDA_ERROR_CHECK(cudaStreamSynchronize(getStreamFrom(*this)));
0061   return;
0062 }
0063 
0064 StreamWrapper createStreamFor(const Acts::Cuda::Info::Device& device) {
0065   // Create the stream for the selected device.
0066   ACTS_CUDA_ERROR_CHECK(cudaSetDevice(device.id));
0067   cudaStream_t stream = nullptr;
0068   ACTS_CUDA_ERROR_CHECK(
0069       cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
0070 
0071   // Return the new object.
0072   return StreamWrapper(stream);
0073 }
0074 
0075 }  // namespace Cuda
0076 }  // namespace Acts