-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathocloc_supported_devices_helper.cpp
309 lines (265 loc) · 13.1 KB
/
ocloc_supported_devices_helper.cpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright (C) 2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/offline_compiler/source/ocloc_supported_devices_helper.h"
#include "shared/offline_compiler/source/ocloc_api.h"
#include "shared/offline_compiler/source/ocloc_interface.h"
#include "shared/source/device_binary_format/yaml/yaml_parser.h"
#include "shared/source/helpers/product_config_helper.h"
#include <cstring>
#include <memory>
#include <set>
#include <sstream>
#include <vector>
namespace Ocloc {
using namespace NEO;
SupportedDevicesHelper::SupportedDevicesData SupportedDevicesHelper::collectSupportedDevicesData(
const std::vector<DeviceAotInfo> &enabledDevices) const {
SupportedDevicesData data;
// Populate IP Versions, Device Infos, Acronyms
data.deviceIpVersions.reserve(enabledDevices.size());
data.deviceInfos.reserve(enabledDevices.size());
data.acronyms.reserve(enabledDevices.size());
for (const auto &device : enabledDevices) {
data.deviceIpVersions.push_back(device.aotConfig.value);
data.deviceInfos.reserve((*device.deviceIds).size());
for (const auto &deviceId : *device.deviceIds) {
data.deviceInfos.push_back({deviceId, device.aotConfig.revision, device.aotConfig.value});
}
data.acronyms.reserve(device.deviceAcronyms.size());
for (const auto &acronym : device.deviceAcronyms) {
data.acronyms.push_back({acronym.data(), device.aotConfig.value});
}
}
// Populate Family groups
std::map<AOT::FAMILY, std::vector<uint32_t>> groupedDevices;
for (const auto &device : enabledDevices) {
groupedDevices[device.family].push_back(device.aotConfig.value);
}
data.familyGroups.reserve(groupedDevices.size());
for (const auto &entry : groupedDevices) {
data.familyGroups.push_back({productConfigHelper->getAcronymFromAFamily(entry.first).data(), entry.second});
}
// Populate Release groups
std::map<AOT::RELEASE, std::vector<uint32_t>> groupedReleases;
for (const auto &device : enabledDevices) {
groupedReleases[device.release].push_back(device.aotConfig.value);
}
for (const auto &entry : groupedReleases) {
auto name = productConfigHelper->getAcronymFromARelease(entry.first);
if (!name.empty()) {
data.releaseGroups.push_back({name.data(), entry.second});
}
}
return data;
}
std::string SupportedDevicesHelper::serialize(std::string_view oclocName, const SupportedDevicesData &data) const {
std::ostringstream oss;
oss << oclocName << ":\n";
// DeviceIpVersions
oss << " " << SupportedDevicesYamlConstants::deviceIpVersions << ":\n";
for (const auto &ipVersion : data.deviceIpVersions) {
oss << " - 0x" << std::hex << ipVersion << "\n";
}
// IpToDevRevId
oss << " " << SupportedDevicesYamlConstants::ipToDevRevId << ":\n";
for (const auto &device : data.deviceInfos) {
oss << " - " << SupportedDevicesYamlConstants::ip << ": 0x" << std::hex << device.ipVersion << "\n";
oss << " " << SupportedDevicesYamlConstants::revisionId << ": " << std::dec << device.revisionId << "\n";
oss << " " << SupportedDevicesYamlConstants::deviceId << ": 0x" << std::hex << device.deviceId << "\n";
}
// Acronym
oss << " " << SupportedDevicesYamlConstants::acronym << ":\n";
for (const auto &[acronym, ipVersion] : data.acronyms) {
oss << " " << acronym << ": 0x" << std::hex << ipVersion << "\n";
}
// FamilyGroups
oss << " " << SupportedDevicesYamlConstants::familyGroups << ":\n";
for (const auto &[family, ipVersions] : data.familyGroups) {
oss << " " << family << ": [";
for (size_t i = 0; i < ipVersions.size(); ++i) {
oss << "0x" << std::hex << ipVersions[i];
if (i < ipVersions.size() - 1)
oss << ", ";
}
oss << "]\n";
}
// ReleaseGroups
oss << " " << SupportedDevicesYamlConstants::releaseGroups << ":\n";
for (const auto &[release, ipVersions] : data.releaseGroups) {
oss << " " << release << ": [";
for (size_t i = 0; i < ipVersions.size(); ++i) {
oss << "0x" << std::hex << ipVersions[i];
if (i < ipVersions.size() - 1)
oss << ", ";
}
oss << "]\n";
}
return oss.str();
}
std::map<std::string, SupportedDevicesHelper::SupportedDevicesData> SupportedDevicesHelper::deserialize(std::string_view yamlString) const {
std::map<std::string, SupportedDevicesData> result;
NEO::Yaml::YamlParser parser;
std::string errReason, warning;
if (!parser.parse(yamlString.data(), errReason, warning)) {
DEBUG_BREAK_IF(true);
return result;
}
const auto *root = parser.getRoot();
for (const auto &oclocVersionNode : parser.createChildrenRange(*root)) {
std::string oclocVersion = parser.readKey(oclocVersionNode).str();
SupportedDevicesData &data = result[oclocVersion];
const auto *ipVersionsNode = parser.getChild(oclocVersionNode, SupportedDevicesYamlConstants::deviceIpVersions.data());
if (ipVersionsNode) {
for (const auto &ipNode : parser.createChildrenRange(*ipVersionsNode)) {
uint32_t ipVersion;
if (parser.readValueChecked(ipNode, ipVersion)) {
data.deviceIpVersions.push_back(ipVersion);
}
}
}
const auto *deviceIdsNode = parser.getChild(oclocVersionNode, SupportedDevicesYamlConstants::ipToDevRevId.data());
if (deviceIdsNode) {
for (const auto &deviceNode : parser.createChildrenRange(*deviceIdsNode)) {
DeviceInfo info;
const auto *deviceIdNode = parser.getChild(deviceNode, SupportedDevicesYamlConstants::deviceId.data());
const auto *revisionIdNode = parser.getChild(deviceNode, SupportedDevicesYamlConstants::revisionId.data());
const auto *ipNode = parser.getChild(deviceNode, SupportedDevicesYamlConstants::ip.data());
if (deviceIdNode && revisionIdNode && ipNode) {
parser.readValueChecked(*deviceIdNode, info.deviceId);
parser.readValueChecked(*revisionIdNode, info.revisionId);
parser.readValueChecked(*ipNode, info.ipVersion);
data.deviceInfos.push_back(info);
}
}
}
const auto *acronymNode = parser.getChild(oclocVersionNode, SupportedDevicesYamlConstants::acronym.data());
if (acronymNode) {
for (const auto &acrNode : parser.createChildrenRange(*acronymNode)) {
std::string acronym = parser.readKey(acrNode).str();
uint32_t ipVersion;
if (parser.readValueChecked(acrNode, ipVersion)) {
data.acronyms.push_back({acronym, ipVersion});
}
}
}
const auto *familyGroupsNode = parser.getChild(oclocVersionNode, SupportedDevicesYamlConstants::familyGroups.data());
if (familyGroupsNode) {
for (const auto &familyNode : parser.createChildrenRange(*familyGroupsNode)) {
std::string family = parser.readKey(familyNode).str();
std::vector<uint32_t> ipVersions;
for (const auto &ipVersionNode : parser.createChildrenRange(familyNode)) {
uint32_t ipVersion;
if (parser.readValueChecked(ipVersionNode, ipVersion)) {
ipVersions.push_back(ipVersion);
}
}
if (!ipVersions.empty()) {
data.familyGroups.push_back({family, ipVersions});
}
}
}
const auto *releaseGroupsNode = parser.getChild(oclocVersionNode, SupportedDevicesYamlConstants::releaseGroups.data());
if (releaseGroupsNode) {
for (const auto &releaseNode : parser.createChildrenRange(*releaseGroupsNode)) {
std::string release = parser.readKey(releaseNode).str();
std::vector<uint32_t> ipVersions;
for (const auto &ipVersionNode : parser.createChildrenRange(releaseNode)) {
uint32_t ipVersion;
if (parser.readValueChecked(ipVersionNode, ipVersion)) {
ipVersions.push_back(ipVersion);
}
}
if (!ipVersions.empty()) {
data.releaseGroups.push_back({release, ipVersions});
}
}
}
}
return result;
}
std::string SupportedDevicesHelper::mergeAndSerializeWithFormerData(const SupportedDevicesData ¤tData) const {
std::string formerSupportedDevices = getDataFromFormerOcloc();
if (formerSupportedDevices.empty()) {
return serialize(getCurrentOclocName(), currentData);
}
auto formerDataDeserialized = deserialize(formerSupportedDevices);
formerDataDeserialized[getCurrentOclocName()] = currentData;
auto mergedData = mergeOclocData(formerDataDeserialized);
return serialize("ocloc", mergedData);
}
std::string SupportedDevicesHelper::concatAndSerializeWithFormerData(const SupportedDevicesData ¤tData) const {
std::string output = serialize(getCurrentOclocName(), currentData);
std::string formerSupportedDevices = getDataFromFormerOcloc();
if (!formerSupportedDevices.empty()) {
output += "\n" + formerSupportedDevices;
}
return output;
}
SupportedDevicesHelper::SupportedDevicesData SupportedDevicesHelper::mergeOclocData(const std::map<std::string, SupportedDevicesData> &nameDataMap) const {
struct DeviceInfoComparator {
bool operator()(const DeviceInfo &lhs, const DeviceInfo &rhs) const {
return std::tie(lhs.deviceId, lhs.revisionId, lhs.ipVersion) <
std::tie(rhs.deviceId, rhs.revisionId, rhs.ipVersion);
}
};
SupportedDevicesData mergedData;
std::set<uint32_t> uniqueIpVersions;
std::set<DeviceInfo, DeviceInfoComparator> uniqueDeviceInfos;
std::set<std::pair<std::string, uint32_t>> uniqueAcronyms;
std::map<std::string, std::set<uint32_t>> uniqueFamilyGroups;
std::map<std::string, std::set<uint32_t>> uniqueReleaseGroups;
for (const auto &[name, data] : nameDataMap) {
uniqueIpVersions.insert(data.deviceIpVersions.begin(), data.deviceIpVersions.end());
for (const auto &deviceInfo : data.deviceInfos) {
uniqueDeviceInfos.insert(deviceInfo);
}
uniqueAcronyms.insert(data.acronyms.begin(), data.acronyms.end());
for (const auto &[family, ipVersions] : data.familyGroups) {
uniqueFamilyGroups[family].insert(ipVersions.begin(), ipVersions.end());
}
for (const auto &[release, ipVersions] : data.releaseGroups) {
uniqueReleaseGroups[release].insert(ipVersions.begin(), ipVersions.end());
}
}
// Sort DeviceIpVersions (ascending)
mergedData.deviceIpVersions = std::vector<uint32_t>(uniqueIpVersions.begin(), uniqueIpVersions.end());
std::sort(mergedData.deviceIpVersions.begin(), mergedData.deviceIpVersions.end());
// Sort IpToDevRevId (ascending by ipVersion)
mergedData.deviceInfos = std::vector<DeviceInfo>(uniqueDeviceInfos.begin(), uniqueDeviceInfos.end());
std::sort(mergedData.deviceInfos.begin(), mergedData.deviceInfos.end(),
[](const DeviceInfo &a, const DeviceInfo &b) { return a.ipVersion < b.ipVersion; });
// Sort Acronyms (ascending by ipVersion)
mergedData.acronyms = std::vector<std::pair<std::string, uint32_t>>(uniqueAcronyms.begin(), uniqueAcronyms.end());
std::sort(mergedData.acronyms.begin(), mergedData.acronyms.end(),
[](const auto &a, const auto &b) { return a.second < b.second; });
// Sort FamilyGroups (alphabetically by group name)
mergedData.familyGroups.reserve(uniqueFamilyGroups.size());
for (const auto &[family, ipVersions] : uniqueFamilyGroups) {
mergedData.familyGroups.push_back({family, std::vector<uint32_t>(ipVersions.begin(), ipVersions.end())});
}
std::sort(mergedData.familyGroups.begin(), mergedData.familyGroups.end(),
[](const auto &a, const auto &b) { return a.first < b.first; });
// Sort ReleaseGroups (alphabetically by group name)
mergedData.releaseGroups.reserve(uniqueReleaseGroups.size());
for (const auto &[release, ipVersions] : uniqueReleaseGroups) {
mergedData.releaseGroups.push_back({release, std::vector<uint32_t>(ipVersions.begin(), ipVersions.end())});
}
std::sort(mergedData.releaseGroups.begin(), mergedData.releaseGroups.end(),
[](const auto &a, const auto &b) { return a.first < b.first; });
// Sort IP versions within each FAMILY and RELEASE group
for (auto &[_, ipVersions] : mergedData.familyGroups) {
std::sort(ipVersions.begin(), ipVersions.end());
}
for (auto &[_, ipVersions] : mergedData.releaseGroups) {
std::sort(ipVersions.begin(), ipVersions.end());
}
return mergedData;
}
std::string SupportedDevicesHelper::getFormerOclocName() const {
return extractOclocName(getOclocFormerLibName());
}
} // namespace Ocloc