forked from iree-org/iree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_manager.cc
177 lines (154 loc) · 6.83 KB
/
device_manager.cc
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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "third_party/mlir_edge/iree/hal/device_manager.h"
#include <algorithm>
#include "third_party/absl/types/source_location.h"
#include "third_party/mlir_edge/iree/base/status.h"
#include "third_party/mlir_edge/iree/base/tracing.h"
#include "third_party/mlir_edge/iree/hal/heap_buffer.h"
namespace iree {
namespace hal {
DeviceManager::DeviceManager() = default;
DeviceManager::~DeviceManager() = default;
Status DeviceManager::RegisterDevice(std::shared_ptr<Device> device) {
IREE_TRACE_SCOPE0("DeviceManager::RegisterDevice");
absl::MutexLock lock(&device_mutex_);
if (std::find(devices_.begin(), devices_.end(), device) != devices_.end()) {
return FailedPreconditionErrorBuilder(ABSL_LOC)
<< "Device already registered";
}
devices_.push_back(std::move(device));
return OkStatus();
}
Status DeviceManager::UnregisterDevice(Device* device) {
IREE_TRACE_SCOPE0("DeviceManager::UnregisterDevice");
absl::MutexLock lock(&device_mutex_);
auto it = std::find_if(devices_.begin(), devices_.end(),
[device](const std::shared_ptr<Device>& other_device) {
return device == other_device.get();
});
if (it == devices_.end()) {
return NotFoundErrorBuilder(ABSL_LOC) << "Device not registered";
}
devices_.erase(it);
return OkStatus();
}
StatusOr<DevicePlacement> DeviceManager::ResolvePlacement(
const PlacementSpec& placement_spec) const {
IREE_TRACE_SCOPE0("DeviceManager::ResolvePlacement");
absl::MutexLock lock(&device_mutex_);
if (devices_.empty()) {
return NotFoundErrorBuilder(ABSL_LOC) << "No devices registered";
}
// TODO(benvanik): multiple devices and placement.
QCHECK_EQ(devices_.size(), 1)
<< "Multiple devices not yet supported (need placement)";
DevicePlacement device_placement;
device_placement.device = devices_.front();
return device_placement;
}
StatusOr<Allocator*> DeviceManager::FindCompatibleAllocator(
MemoryTypeBitfield memory_type, BufferUsageBitfield buffer_usage,
absl::Span<const DevicePlacement> device_placements) const {
IREE_TRACE_SCOPE0("DeviceManager::FindCompatibleAllocator");
if (device_placements.empty()) {
return InvalidArgumentErrorBuilder(ABSL_LOC) << "No placements provided";
}
// Find the first allocator. As we only return an allocator if all placements
// are compatible we'll compare allocator[0] against allocator[1,N].
Allocator* some_allocator = nullptr;
for (const auto& device_placement : device_placements) {
auto* allocator = device_placement.device->allocator();
if (!some_allocator) {
some_allocator = allocator;
continue;
}
// NOTE: as there can be asymmetry between usage restrictions (A can use B
// but B cannot use A) we have to compare both directions.
if (!some_allocator->CanUseBufferLike(allocator, memory_type, buffer_usage,
buffer_usage) ||
!allocator->CanUseBufferLike(some_allocator, memory_type, buffer_usage,
buffer_usage)) {
// Allocators are not compatible.
return NotFoundErrorBuilder(ABSL_LOC)
<< "No single allocator found that is compatible with all "
"placements";
}
}
return some_allocator;
}
StatusOr<ref_ptr<Buffer>> DeviceManager::TryAllocateDeviceVisibleBuffer(
MemoryTypeBitfield memory_type, BufferUsageBitfield buffer_usage,
device_size_t allocation_size,
absl::Span<const DevicePlacement> device_placements) {
IREE_TRACE_SCOPE("DeviceManager::TryAllocateDeviceVisibleBuffer:size", int)
(static_cast<int>(allocation_size));
if (!AnyBitSet(memory_type & MemoryType::kHostLocal)) {
return InvalidArgumentErrorBuilder(ABSL_LOC)
<< "Host-local buffers require the kHostLocal bit: "
<< MemoryTypeString(memory_type);
}
// Strip kDeviceVisible as we conditionally add it based on support.
memory_type &= ~MemoryType::kDeviceVisible;
// Find an allocator that works for device-visible buffers.
// If this fails we'll fall back to allocation a non-device-visible buffer.
auto allocator_or =
FindCompatibleAllocator(memory_type | MemoryType::kDeviceVisible,
buffer_usage, device_placements);
if (allocator_or.ok()) {
return allocator_or.ValueOrDie()->Allocate(
memory_type | MemoryType::kDeviceVisible, buffer_usage,
allocation_size);
}
// Fallback to allocating a host-local buffer.
return HeapBuffer::Allocate(memory_type, buffer_usage, allocation_size);
}
StatusOr<ref_ptr<Buffer>> DeviceManager::AllocateDeviceVisibleBuffer(
MemoryTypeBitfield memory_type, BufferUsageBitfield buffer_usage,
device_size_t allocation_size,
absl::Span<const DevicePlacement> device_placements) {
IREE_TRACE_SCOPE("DeviceManager::AllocateDeviceVisibleBuffer:size", int)
(static_cast<int>(allocation_size));
if (!AnyBitSet(memory_type & MemoryType::kHostLocal)) {
return InvalidArgumentErrorBuilder(ABSL_LOC)
<< "Host-local buffers require the kHostLocal bit: "
<< MemoryTypeString(memory_type);
}
// Always use device-visible.
memory_type |= MemoryType::kDeviceVisible;
// Find an allocator that works for device-visible buffers.
ASSIGN_OR_RETURN(
auto* allocator,
FindCompatibleAllocator(memory_type, buffer_usage, device_placements));
return allocator->Allocate(memory_type, buffer_usage, allocation_size);
}
StatusOr<ref_ptr<Buffer>> DeviceManager::AllocateDeviceLocalBuffer(
MemoryTypeBitfield memory_type, BufferUsageBitfield buffer_usage,
device_size_t allocation_size,
absl::Span<const DevicePlacement> device_placements) {
IREE_TRACE_SCOPE("DeviceManager::AllocateDeviceLocalBuffer:size", int)
(static_cast<int>(allocation_size));
if (!AnyBitSet(memory_type & MemoryType::kDeviceLocal)) {
return InvalidArgumentErrorBuilder(ABSL_LOC)
<< "Device-local buffers require the kDeviceLocal bit: "
<< MemoryTypeString(memory_type);
}
// Find an allocator that works for device-local buffers.
ASSIGN_OR_RETURN(
auto* allocator,
FindCompatibleAllocator(memory_type, buffer_usage, device_placements));
return allocator->Allocate(memory_type, buffer_usage, allocation_size);
}
} // namespace hal
} // namespace iree