summaryrefslogtreecommitdiff
path: root/parallel-libs/streamexecutor/include/streamexecutor/HostMemory.h
blob: cea98f4ed741a174641e23838c2ccda5388047f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//===-- HostMemory.h - Types for registered host memory ---------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// This file defines types that represent registered host memory buffers. Host
/// memory must be registered to participate in asynchronous copies to or from
/// device memory.
///
//===----------------------------------------------------------------------===//

#ifndef STREAMEXECUTOR_HOSTMEMORY_H
#define STREAMEXECUTOR_HOSTMEMORY_H

#include <cassert>
#include <cstddef>
#include <type_traits>

#include "llvm/ADT/ArrayRef.h"

namespace streamexecutor {

class Device;
template <typename ElemT> class RegisteredHostMemory;

/// A mutable slice of registered host memory.
///
/// The memory is registered in the sense of
/// streamexecutor::Device::registerHostMemory.
///
/// Holds a reference to an underlying registered host memory buffer. Must not
/// be used after the underlying buffer is freed or unregistered.
template <typename ElemT> class MutableRegisteredHostMemorySlice {
public:
  using ElementTy = ElemT;

  MutableRegisteredHostMemorySlice(RegisteredHostMemory<ElemT> &Registered)
      : MutableArrayRef(Registered.getPointer(), Registered.getElementCount()) {
  }

  ElemT *getPointer() const { return MutableArrayRef.data(); }
  size_t getElementCount() const { return MutableArrayRef.size(); }

  /// Chops off the first DropCount elements of the slice.
  LLVM_ATTRIBUTE_UNUSED_RESULT
  MutableRegisteredHostMemorySlice slice(size_t DropCount) const {
    return MutableRegisteredHostMemorySlice(MutableArrayRef.slice(DropCount));
  }

  /// Chops off the first DropCount elements of the slice and keeps the next
  /// TakeCount elements.
  LLVM_ATTRIBUTE_UNUSED_RESULT
  MutableRegisteredHostMemorySlice slice(size_t DropCount,
                                         size_t TakeCount) const {
    return MutableRegisteredHostMemorySlice(
        MutableArrayRef.slice(DropCount, TakeCount));
  }

  /// Chops off the last DropCount elements of the slice.
  LLVM_ATTRIBUTE_UNUSED_RESULT
  MutableRegisteredHostMemorySlice drop_back(size_t DropCount) const {
    return MutableRegisteredHostMemorySlice(
        MutableArrayRef.drop_back(DropCount));
  }

private:
  MutableRegisteredHostMemorySlice(llvm::MutableArrayRef<ElemT> MutableArrayRef)
      : MutableArrayRef(MutableArrayRef) {}

  llvm::MutableArrayRef<ElemT> MutableArrayRef;
};

/// An immutable slice of registered host memory.
///
/// The memory is registered in the sense of
/// streamexecutor::Device::registerHostMemory.
///
/// Holds a reference to an underlying registered host memory buffer. Must not
/// be used after the underlying buffer is freed or unregistered.
template <typename ElemT> class RegisteredHostMemorySlice {
public:
  using ElementTy = ElemT;

  RegisteredHostMemorySlice(const RegisteredHostMemory<ElemT> &Registered)
      : ArrayRef(Registered.getPointer(), Registered.getElementCount()) {}

  RegisteredHostMemorySlice(
      MutableRegisteredHostMemorySlice<ElemT> MutableSlice)
      : ArrayRef(MutableSlice.getPointer(), MutableSlice.getElementCount()) {}

  const ElemT *getPointer() const { return ArrayRef.data(); }
  size_t getElementCount() const { return ArrayRef.size(); }

  /// Chops off the first N elements of the slice.
  LLVM_ATTRIBUTE_UNUSED_RESULT
  RegisteredHostMemorySlice slice(size_t N) const {
    return RegisteredHostMemorySlice(ArrayRef.slice(N));
  }

  /// Chops off the first N elements of the slice and keeps the next M elements.
  LLVM_ATTRIBUTE_UNUSED_RESULT
  RegisteredHostMemorySlice slice(size_t N, size_t M) const {
    return RegisteredHostMemorySlice(ArrayRef.slice(N, M));
  }

  /// Chops off the last N elements of the slice.
  LLVM_ATTRIBUTE_UNUSED_RESULT
  RegisteredHostMemorySlice drop_back(size_t N) const {
    return RegisteredHostMemorySlice(ArrayRef.drop_back(N));
  }

private:
  llvm::ArrayRef<ElemT> ArrayRef;
};

namespace internal {

/// Helper function to unregister host memory.
///
/// This is a thin wrapper around streamexecutor::Device::unregisterHostMemory.
/// It is defined so this operation can be performed from the destructor of the
/// template class RegisteredHostMemory without including Device.h in this
/// header and creating a header inclusion cycle.
void destroyRegisteredHostMemoryInternals(Device *TheDevice, void *Pointer);

} // namespace internal

/// Registered host memory that knows how to unregister itself upon destruction.
///
/// The memory is registered in the sense of
/// streamexecutor::Device::registerHostMemory.
///
/// ElemT is the type of element stored in the host buffer.
template <typename ElemT> class RegisteredHostMemory {
public:
  using ElementTy = ElemT;

  RegisteredHostMemory(Device *TheDevice, ElemT *Pointer, size_t ElementCount)
      : TheDevice(TheDevice), Pointer(Pointer), ElementCount(ElementCount) {
    assert(TheDevice != nullptr && "cannot construct a "
                                   "RegisteredHostMemoryBase with a null "
                                   "platform device");
  }

  RegisteredHostMemory(const RegisteredHostMemory &) = delete;
  RegisteredHostMemory &operator=(const RegisteredHostMemory &) = delete;

  RegisteredHostMemory(RegisteredHostMemory &&Other)
      : TheDevice(Other.TheDevice), Pointer(Other.Pointer),
        ElementCount(Other.ElementCount) {
    Other.TheDevice = nullptr;
    Other.Pointer = nullptr;
  }

  RegisteredHostMemory &operator=(RegisteredHostMemory &&Other) {
    TheDevice = Other.TheDevice;
    Pointer = Other.Pointer;
    ElementCount = Other.ElementCount;
    Other.TheDevice = nullptr;
    Other.Pointer = nullptr;
  }

  ~RegisteredHostMemory() {
    internal::destroyRegisteredHostMemoryInternals(TheDevice, Pointer);
  }

  ElemT *getPointer() { return static_cast<ElemT *>(Pointer); }
  const ElemT *getPointer() const { return static_cast<ElemT *>(Pointer); }
  size_t getElementCount() const { return ElementCount; }

  /// Creates an immutable slice for the entire contents of this memory.
  RegisteredHostMemorySlice<ElemT> asSlice() const {
    return RegisteredHostMemorySlice<ElemT>(*this);
  }

  /// Creates a mutable slice for the entire contents of this memory.
  MutableRegisteredHostMemorySlice<ElemT> asSlice() {
    return MutableRegisteredHostMemorySlice<ElemT>(*this);
  }

private:
  Device *TheDevice;
  void *Pointer;
  size_t ElementCount;
};

} // namespace streamexecutor

#endif // STREAMEXECUTOR_HOSTMEMORY_H