-
Notifications
You must be signed in to change notification settings - Fork 958
Expand file tree
/
Copy pathdevice_allocator.cpp
More file actions
58 lines (48 loc) · 1.52 KB
/
device_allocator.cpp
File metadata and controls
58 lines (48 loc) · 1.52 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/runtime/core/device_allocator.h>
#include <executorch/runtime/platform/assert.h>
namespace executorch {
namespace runtime {
DeviceAllocatorRegistry& DeviceAllocatorRegistry::instance() {
static DeviceAllocatorRegistry registry;
return registry;
}
void DeviceAllocatorRegistry::register_allocator(
etensor::DeviceType type,
DeviceAllocator* alloc) {
auto index = static_cast<size_t>(type);
ET_CHECK_MSG(
index < etensor::kNumDeviceTypes,
"Invalid device type: %d",
static_cast<int>(type));
ET_CHECK_MSG(
allocators_[index] == nullptr,
"Allocator already registered for device type: %d",
static_cast<int>(type));
allocators_[index] = alloc;
}
DeviceAllocator* DeviceAllocatorRegistry::get_allocator(
etensor::DeviceType type) {
auto index = static_cast<size_t>(type);
if (index >= etensor::kNumDeviceTypes) {
return nullptr;
}
return allocators_[index];
}
// Convenience free functions
void register_device_allocator(
etensor::DeviceType type,
DeviceAllocator* alloc) {
DeviceAllocatorRegistry::instance().register_allocator(type, alloc);
}
DeviceAllocator* get_device_allocator(etensor::DeviceType type) {
return DeviceAllocatorRegistry::instance().get_allocator(type);
}
} // namespace runtime
} // namespace executorch