File indexing completed on 2023-03-17 10:49:12
0001 #ifndef CUDADataFormats_Common_ProductBase_h
0002 #define CUDADataFormats_Common_ProductBase_h
0003
0004 #include <atomic>
0005 #include <memory>
0006
0007 #include "HeterogeneousCore/CUDAUtilities/interface/SharedStreamPtr.h"
0008 #include "HeterogeneousCore/CUDAUtilities/interface/SharedEventPtr.h"
0009
0010 namespace cms {
0011 namespace cuda {
0012 namespace impl {
0013 class ScopedContextBase;
0014 }
0015
0016
0017
0018
0019
0020 class ProductBase {
0021 public:
0022 ProductBase() = default;
0023 ~ProductBase();
0024
0025 ProductBase(const ProductBase&) = delete;
0026 ProductBase& operator=(const ProductBase&) = delete;
0027 ProductBase(ProductBase&& other)
0028 : stream_{std::move(other.stream_)},
0029 event_{std::move(other.event_)},
0030 mayReuseStream_{other.mayReuseStream_.load()},
0031 device_{other.device_} {}
0032 ProductBase& operator=(ProductBase&& other) {
0033 stream_ = std::move(other.stream_);
0034 event_ = std::move(other.event_);
0035 mayReuseStream_ = other.mayReuseStream_.load();
0036 device_ = other.device_;
0037 return *this;
0038 }
0039
0040 bool isValid() const { return stream_.get() != nullptr; }
0041 bool isAvailable() const;
0042
0043 int device() const { return device_; }
0044
0045
0046
0047
0048
0049 cudaStream_t stream() const { return stream_.get(); }
0050
0051
0052
0053
0054
0055 cudaEvent_t event() const { return event_.get(); }
0056
0057 protected:
0058 explicit ProductBase(int device, SharedStreamPtr stream, SharedEventPtr event)
0059 : stream_{std::move(stream)}, event_{std::move(event)}, device_{device} {}
0060
0061 private:
0062 friend class impl::ScopedContextBase;
0063 friend class ScopedContextProduce;
0064
0065
0066 const SharedStreamPtr& streamPtr() const { return stream_; }
0067
0068 bool mayReuseStream() const {
0069 bool expected = true;
0070 bool changed = mayReuseStream_.compare_exchange_strong(expected, false);
0071
0072
0073 return changed;
0074 }
0075
0076
0077
0078 SharedStreamPtr stream_;
0079
0080 SharedEventPtr event_;
0081
0082
0083
0084
0085 mutable std::atomic<bool> mayReuseStream_ = true;
0086
0087
0088 int device_ = -1;
0089 };
0090 }
0091 }
0092
0093 #endif