File indexing completed on 2025-08-27 08:47:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #pragma once
0019
0020 #include <cstdint>
0021 #include <functional>
0022 #include <memory>
0023 #include <string>
0024
0025 #include "arrow/io/type_fwd.h"
0026 #include "arrow/result.h"
0027 #include "arrow/status.h"
0028 #include "arrow/type_fwd.h"
0029 #include "arrow/util/compare.h"
0030 #include "arrow/util/macros.h"
0031 #include "arrow/util/visibility.h"
0032
0033 namespace arrow {
0034
0035 class MemoryManager;
0036
0037
0038
0039
0040
0041
0042
0043 class ARROW_EXPORT Device : public std::enable_shared_from_this<Device>,
0044 public util::EqualityComparable<Device> {
0045 public:
0046 virtual ~Device();
0047
0048
0049
0050
0051
0052
0053 virtual const char* type_name() const = 0;
0054
0055
0056
0057
0058
0059 virtual std::string ToString() const = 0;
0060
0061
0062 virtual bool Equals(const Device&) const = 0;
0063
0064
0065
0066
0067
0068 virtual int64_t device_id() const { return -1; }
0069
0070
0071
0072
0073
0074 bool is_cpu() const { return is_cpu_; }
0075
0076
0077
0078
0079
0080
0081 virtual std::shared_ptr<MemoryManager> default_memory_manager() = 0;
0082
0083
0084 virtual DeviceAllocationType device_type() const = 0;
0085
0086 class SyncEvent;
0087
0088
0089
0090
0091
0092
0093 class ARROW_EXPORT Stream {
0094 public:
0095 using release_fn_t = std::function<void(void*)>;
0096
0097 virtual ~Stream() = default;
0098
0099 virtual const void* get_raw() const { return stream_.get(); }
0100
0101
0102
0103
0104
0105 virtual Status WaitEvent(const SyncEvent&) = 0;
0106
0107
0108 virtual Status Synchronize() const = 0;
0109
0110 protected:
0111 explicit Stream(void* stream, release_fn_t release_stream)
0112 : stream_{stream, release_stream} {}
0113
0114 std::unique_ptr<void, release_fn_t> stream_;
0115 };
0116
0117 virtual Result<std::shared_ptr<Stream>> MakeStream() { return NULLPTR; }
0118
0119
0120
0121
0122
0123
0124 virtual Result<std::shared_ptr<Stream>> MakeStream(
0125 unsigned int ARROW_ARG_UNUSED(flags)) {
0126 return NULLPTR;
0127 }
0128
0129
0130
0131
0132
0133
0134
0135 virtual Result<std::shared_ptr<Stream>> WrapStream(
0136 void* ARROW_ARG_UNUSED(device_stream),
0137 Stream::release_fn_t ARROW_ARG_UNUSED(release_fn)) {
0138 return NULLPTR;
0139 }
0140
0141
0142 class ARROW_EXPORT SyncEvent {
0143 public:
0144 using release_fn_t = std::function<void(void*)>;
0145
0146 virtual ~SyncEvent() = default;
0147
0148 void* get_raw() { return sync_event_.get(); }
0149
0150
0151 virtual Status Wait() = 0;
0152
0153
0154
0155 virtual Status Record(const Stream&) = 0;
0156
0157 protected:
0158
0159
0160
0161 explicit SyncEvent(void* sync_event, release_fn_t release_sync_event)
0162 : sync_event_{sync_event, release_sync_event} {}
0163
0164 std::unique_ptr<void, release_fn_t> sync_event_;
0165 };
0166
0167 protected:
0168 ARROW_DISALLOW_COPY_AND_ASSIGN(Device);
0169 explicit Device(bool is_cpu = false) : is_cpu_(is_cpu) {}
0170
0171 bool is_cpu_;
0172 };
0173
0174
0175
0176
0177
0178
0179 class ARROW_EXPORT MemoryManager : public std::enable_shared_from_this<MemoryManager> {
0180 public:
0181 virtual ~MemoryManager();
0182
0183
0184 const std::shared_ptr<Device>& device() const { return device_; }
0185
0186
0187
0188
0189
0190 bool is_cpu() const { return device_->is_cpu(); }
0191
0192
0193
0194
0195
0196
0197 virtual Result<std::shared_ptr<io::RandomAccessFile>> GetBufferReader(
0198 std::shared_ptr<Buffer> buf) = 0;
0199
0200
0201
0202
0203
0204
0205
0206
0207 virtual Result<std::shared_ptr<io::OutputStream>> GetBufferWriter(
0208 std::shared_ptr<Buffer> buf) = 0;
0209
0210
0211
0212
0213 virtual Result<std::unique_ptr<Buffer>> AllocateBuffer(int64_t size) = 0;
0214
0215
0216
0217
0218 static Result<std::shared_ptr<Buffer>> CopyBuffer(
0219 const std::shared_ptr<Buffer>& source, const std::shared_ptr<MemoryManager>& to);
0220
0221
0222
0223
0224
0225 static Result<std::unique_ptr<Buffer>> CopyNonOwned(
0226 const Buffer& source, const std::shared_ptr<MemoryManager>& to);
0227
0228
0229
0230
0231 static Result<std::shared_ptr<Buffer>> ViewBuffer(
0232 const std::shared_ptr<Buffer>& source, const std::shared_ptr<MemoryManager>& to);
0233
0234
0235 static Status CopyBufferSliceToCPU(const std::shared_ptr<Buffer>& buf, int64_t offset,
0236 int64_t length, uint8_t* out_data);
0237
0238
0239
0240
0241
0242
0243
0244 virtual Result<std::shared_ptr<Device::SyncEvent>> MakeDeviceSyncEvent();
0245
0246
0247
0248
0249
0250
0251 virtual Result<std::shared_ptr<Device::SyncEvent>> WrapDeviceSyncEvent(
0252 void* sync_event, Device::SyncEvent::release_fn_t release_sync_event);
0253
0254 protected:
0255 ARROW_DISALLOW_COPY_AND_ASSIGN(MemoryManager);
0256
0257 explicit MemoryManager(const std::shared_ptr<Device>& device) : device_(device) {}
0258
0259
0260
0261
0262
0263
0264 virtual Result<std::shared_ptr<Buffer>> CopyBufferFrom(
0265 const std::shared_ptr<Buffer>& buf, const std::shared_ptr<MemoryManager>& from);
0266 virtual Result<std::shared_ptr<Buffer>> CopyBufferTo(
0267 const std::shared_ptr<Buffer>& buf, const std::shared_ptr<MemoryManager>& to);
0268 virtual Result<std::unique_ptr<Buffer>> CopyNonOwnedFrom(
0269 const Buffer& buf, const std::shared_ptr<MemoryManager>& from);
0270 virtual Result<std::unique_ptr<Buffer>> CopyNonOwnedTo(
0271 const Buffer& buf, const std::shared_ptr<MemoryManager>& to);
0272 virtual Result<std::shared_ptr<Buffer>> ViewBufferFrom(
0273 const std::shared_ptr<Buffer>& buf, const std::shared_ptr<MemoryManager>& from);
0274 virtual Result<std::shared_ptr<Buffer>> ViewBufferTo(
0275 const std::shared_ptr<Buffer>& buf, const std::shared_ptr<MemoryManager>& to);
0276
0277 std::shared_ptr<Device> device_;
0278 };
0279
0280
0281
0282
0283 class ARROW_EXPORT CPUDevice : public Device {
0284 public:
0285 const char* type_name() const override;
0286 std::string ToString() const override;
0287 bool Equals(const Device&) const override;
0288 DeviceAllocationType device_type() const override { return DeviceAllocationType::kCPU; }
0289
0290 std::shared_ptr<MemoryManager> default_memory_manager() override;
0291
0292
0293 static std::shared_ptr<Device> Instance();
0294
0295
0296
0297
0298 static std::shared_ptr<MemoryManager> memory_manager(MemoryPool* pool);
0299
0300 protected:
0301 CPUDevice() : Device(true) {}
0302 };
0303
0304 class ARROW_EXPORT CPUMemoryManager : public MemoryManager {
0305 public:
0306 Result<std::shared_ptr<io::RandomAccessFile>> GetBufferReader(
0307 std::shared_ptr<Buffer> buf) override;
0308 Result<std::shared_ptr<io::OutputStream>> GetBufferWriter(
0309 std::shared_ptr<Buffer> buf) override;
0310
0311 Result<std::unique_ptr<Buffer>> AllocateBuffer(int64_t size) override;
0312
0313
0314 MemoryPool* pool() const { return pool_; }
0315
0316 protected:
0317 CPUMemoryManager(const std::shared_ptr<Device>& device, MemoryPool* pool)
0318 : MemoryManager(device), pool_(pool) {}
0319
0320 static std::shared_ptr<MemoryManager> Make(const std::shared_ptr<Device>& device,
0321 MemoryPool* pool = default_memory_pool());
0322
0323 Result<std::shared_ptr<Buffer>> CopyBufferFrom(
0324 const std::shared_ptr<Buffer>& buf,
0325 const std::shared_ptr<MemoryManager>& from) override;
0326 Result<std::shared_ptr<Buffer>> CopyBufferTo(
0327 const std::shared_ptr<Buffer>& buf,
0328 const std::shared_ptr<MemoryManager>& to) override;
0329 Result<std::unique_ptr<Buffer>> CopyNonOwnedFrom(
0330 const Buffer& buf, const std::shared_ptr<MemoryManager>& from) override;
0331 Result<std::unique_ptr<Buffer>> CopyNonOwnedTo(
0332 const Buffer& buf, const std::shared_ptr<MemoryManager>& to) override;
0333 Result<std::shared_ptr<Buffer>> ViewBufferFrom(
0334 const std::shared_ptr<Buffer>& buf,
0335 const std::shared_ptr<MemoryManager>& from) override;
0336 Result<std::shared_ptr<Buffer>> ViewBufferTo(
0337 const std::shared_ptr<Buffer>& buf,
0338 const std::shared_ptr<MemoryManager>& to) override;
0339
0340 MemoryPool* pool_;
0341
0342 friend std::shared_ptr<MemoryManager> CPUDevice::memory_manager(MemoryPool* pool);
0343 ARROW_FRIEND_EXPORT friend std::shared_ptr<MemoryManager> default_cpu_memory_manager();
0344 };
0345
0346
0347
0348
0349
0350
0351 ARROW_EXPORT
0352 std::shared_ptr<MemoryManager> default_cpu_memory_manager();
0353
0354 using DeviceMapper =
0355 std::function<Result<std::shared_ptr<MemoryManager>>(int64_t device_id)>;
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370 ARROW_EXPORT
0371 Status RegisterDeviceMapper(DeviceAllocationType device_type, DeviceMapper mapper);
0372
0373
0374
0375
0376
0377
0378
0379 ARROW_EXPORT
0380 Result<DeviceMapper> GetDeviceMapper(DeviceAllocationType device_type);
0381
0382 }