File indexing completed on 2025-01-18 09:54:50
0001
0002
0003
0004
0005
0006
0007
0008 #pragma once
0009
0010 #include "corecel/DeviceRuntimeApi.hh"
0011
0012 #include "corecel/Assert.hh"
0013 #include "corecel/Macros.hh"
0014 #include "corecel/Types.hh"
0015
0016 namespace celeritas
0017 {
0018
0019 #if !CELER_USE_DEVICE
0020 struct MockStream_st;
0021 template<class Pointer>
0022 struct MockMemoryResource
0023 {
0024 virtual Pointer do_allocate(std::size_t, std::size_t) = 0;
0025
0026 virtual void do_deallocate(Pointer, std::size_t, std::size_t) = 0;
0027 };
0028 #endif
0029
0030
0031
0032
0033
0034 template<class Pointer>
0035 #if CELER_USE_DEVICE
0036 class AsyncMemoryResource final : public thrust::mr::memory_resource<Pointer>
0037 #else
0038 class AsyncMemoryResource final : public MockMemoryResource<Pointer>
0039 #endif
0040 {
0041 public:
0042
0043
0044 using pointer = Pointer;
0045 #if CELER_USE_DEVICE
0046 using StreamT = CELER_DEVICE_PREFIX(Stream_t);
0047 #else
0048 using StreamT = MockStream_st*;
0049 #endif
0050
0051
0052
0053 explicit AsyncMemoryResource(StreamT stream) : stream_{stream} {}
0054
0055
0056 AsyncMemoryResource() = default;
0057
0058
0059 pointer do_allocate(std::size_t bytes, std::size_t) final;
0060
0061
0062 void do_deallocate(pointer p, std::size_t, std::size_t) final;
0063
0064 private:
0065 StreamT stream_{nullptr};
0066 };
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 class Stream
0077 {
0078 public:
0079
0080
0081 #if CELER_USE_DEVICE
0082 using StreamT = CELER_DEVICE_PREFIX(Stream_t);
0083 #else
0084 using StreamT = MockStream_st*;
0085 #endif
0086 using ResourceT = AsyncMemoryResource<void*>;
0087
0088
0089 public:
0090
0091 static bool async();
0092
0093
0094 Stream();
0095
0096
0097 Stream(std::nullptr_t) {}
0098
0099
0100 ~Stream();
0101
0102
0103 Stream(Stream const&) = delete;
0104 Stream& operator=(Stream const&) = delete;
0105 Stream(Stream&&) noexcept;
0106 Stream& operator=(Stream&&) noexcept;
0107 void swap(Stream& other) noexcept;
0108
0109
0110 StreamT get() const { return stream_; }
0111
0112
0113 ResourceT& memory_resource() { return memory_resource_; }
0114
0115
0116 void* malloc_async(std::size_t bytes) const;
0117
0118
0119 void free_async(void* ptr) const;
0120
0121 private:
0122 StreamT stream_{nullptr};
0123 ResourceT memory_resource_;
0124 };
0125
0126
0127 }