Skip to content

Commit 9458638

Browse files
Affinity mask helper
Signed-off-by: Bartosz Dunajski <[email protected]>
1 parent 171a614 commit 9458638

File tree

6 files changed

+87
-41
lines changed

6 files changed

+87
-41
lines changed

level_zero/core/source/driver/driver_handle_imp.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,25 @@ ze_result_t DriverHandleImp::initialize(std::vector<std::unique_ptr<NEO::Device>
158158
}
159159
}
160160

161+
const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
162+
auto rootDeviceEnvironment = neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[rootDeviceIndex].get();
163+
161164
if (enableProgramDebugging) {
162165
if (neoDevice->getDebugger() != nullptr) {
163166
NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr,
164167
"%s", "Source Level Debugger cannot be used with Environment Variable enabling program debugging.\n");
165168
UNRECOVERABLE_IF(neoDevice->getDebugger() != nullptr && enableProgramDebugging);
166169
}
167-
neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[neoDevice->getRootDeviceIndex()]->debugger = DebuggerL0::create(neoDevice.get());
170+
rootDeviceEnvironment->debugger = DebuggerL0::create(neoDevice.get());
168171
}
169172

170-
this->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex());
171-
this->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()});
173+
this->rootDeviceIndices.insert(rootDeviceIndex);
174+
this->deviceBitfields.insert({rootDeviceIndex, neoDevice->getDeviceBitfield()});
172175

173176
auto pNeoDevice = neoDevice.release();
174-
auto device = Device::create(this, pNeoDevice, pNeoDevice->getExecutionEnvironment()->rootDeviceEnvironments[pNeoDevice->getRootDeviceIndex()]->deviceAffinityMask, false, &returnValue);
177+
178+
auto subDevicesMask = static_cast<uint32_t>(rootDeviceEnvironment->deviceAffinityMask.getGenericSubDevicesMask().to_ulong());
179+
auto device = Device::create(this, pNeoDevice, subDevicesMask, false, &returnValue);
175180
this->devices.push_back(device);
176181

177182
multiOsContextDriver |= device->isMultiDeviceCapable();

shared/source/device/device.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ SubDevice *Device::createEngineInstancedSubDevice(uint32_t subDeviceIndex, aub_s
6666
}
6767

6868
bool Device::genericSubDevicesAllowed() {
69-
auto deviceMask = executionEnvironment->rootDeviceEnvironments[getRootDeviceIndex()]->deviceAffinityMask;
69+
auto deviceMask = executionEnvironment->rootDeviceEnvironments[getRootDeviceIndex()]->deviceAffinityMask.getGenericSubDevicesMask();
7070
uint32_t subDeviceCount = HwHelper::getSubDevicesCount(&getHardwareInfo());
7171
deviceBitfield = maxNBitValue(subDeviceCount);
7272
deviceBitfield &= deviceMask;

shared/source/execution_environment/execution_environment.cpp

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "shared/source/built_ins/built_ins.h"
1111
#include "shared/source/built_ins/sip.h"
1212
#include "shared/source/execution_environment/root_device_environment.h"
13+
#include "shared/source/helpers/affinity_mask.h"
1314
#include "shared/source/helpers/hw_helper.h"
1415
#include "shared/source/memory_manager/memory_manager.h"
1516
#include "shared/source/memory_manager/os_agnostic_memory_manager.h"
@@ -91,33 +92,31 @@ void ExecutionEnvironment::parseAffinityMask() {
9192
return;
9293
}
9394

94-
std::vector<std::vector<bool>> affinityMaskBitSet(rootDeviceEnvironments.size());
95-
for (uint32_t i = 0; i < affinityMaskBitSet.size(); i++) {
96-
auto hwInfo = rootDeviceEnvironments[i]->getHardwareInfo();
97-
affinityMaskBitSet[i].resize(HwHelper::getSubDevicesCount(hwInfo));
98-
}
95+
const uint32_t numRootDevices = static_cast<uint32_t>(rootDeviceEnvironments.size());
96+
97+
std::vector<AffinityMaskHelper> affinityMaskHelper(numRootDevices);
9998

10099
size_t pos = 0;
101100
while (pos < affinityMaskString.size()) {
102101
size_t posNextDot = affinityMaskString.find_first_of(".", pos);
103102
size_t posNextComma = affinityMaskString.find_first_of(",", pos);
104103
std::string rootDeviceString = affinityMaskString.substr(pos, std::min(posNextDot, posNextComma) - pos);
105104
uint32_t rootDeviceIndex = static_cast<uint32_t>(std::stoul(rootDeviceString, nullptr, 0));
106-
if (rootDeviceIndex < rootDeviceEnvironments.size()) {
105+
if (rootDeviceIndex < numRootDevices) {
106+
auto hwInfo = rootDeviceEnvironments[rootDeviceIndex]->getHardwareInfo();
107+
auto subDevicesCount = HwHelper::getSubDevicesCount(hwInfo);
108+
107109
pos += rootDeviceString.size();
108110
if (posNextDot != std::string::npos &&
109111
affinityMaskString.at(pos) == '.' && posNextDot < posNextComma) {
110112
pos++;
111113
std::string subDeviceString = affinityMaskString.substr(pos, posNextComma - pos);
112114
uint32_t subDeviceIndex = static_cast<uint32_t>(std::stoul(subDeviceString, nullptr, 0));
113-
auto hwInfo = rootDeviceEnvironments[rootDeviceIndex]->getHardwareInfo();
114-
if (subDeviceIndex < HwHelper::getSubDevicesCount(hwInfo)) {
115-
affinityMaskBitSet[rootDeviceIndex][subDeviceIndex] = true;
115+
if (subDeviceIndex < subDevicesCount) {
116+
affinityMaskHelper[rootDeviceIndex].enableGenericSubDevice(subDeviceIndex);
116117
}
117118
} else {
118-
std::fill(affinityMaskBitSet[rootDeviceIndex].begin(),
119-
affinityMaskBitSet[rootDeviceIndex].end(),
120-
true);
119+
affinityMaskHelper[rootDeviceIndex].enableAllGenericSubDevices(subDevicesCount);
121120
}
122121
}
123122
if (posNextComma == std::string::npos) {
@@ -126,31 +125,13 @@ void ExecutionEnvironment::parseAffinityMask() {
126125
pos = posNextComma + 1;
127126
}
128127

129-
uint32_t offset = 0;
130-
uint32_t affinityMask = 0;
131-
for (uint32_t i = 0; i < affinityMaskBitSet.size(); i++) {
132-
for (uint32_t j = 0; j < affinityMaskBitSet[i].size(); j++) {
133-
if (affinityMaskBitSet[i][j] == true) {
134-
affinityMask |= (1UL << offset);
135-
}
136-
offset++;
137-
}
138-
}
139-
140-
uint32_t currentMaskOffset = 0;
141128
std::vector<std::unique_ptr<RootDeviceEnvironment>> filteredEnvironments;
142-
for (size_t i = 0u; i < this->rootDeviceEnvironments.size(); i++) {
143-
auto hwInfo = rootDeviceEnvironments[i]->getHardwareInfo();
144-
145-
uint32_t currentDeviceMask = (affinityMask >> currentMaskOffset) & ((1UL << HwHelper::getSubDevicesCount(hwInfo)) - 1);
146-
bool isDeviceExposed = currentDeviceMask > 0;
147-
148-
currentMaskOffset += HwHelper::getSubDevicesCount(hwInfo);
149-
if (!isDeviceExposed) {
129+
for (uint32_t i = 0u; i < numRootDevices; i++) {
130+
if (!affinityMaskHelper[i].isDeviceEnabled()) {
150131
continue;
151132
}
152133

153-
rootDeviceEnvironments[i]->deviceAffinityMask = currentDeviceMask;
134+
rootDeviceEnvironments[i]->deviceAffinityMask = affinityMaskHelper[i];
154135
filteredEnvironments.emplace_back(rootDeviceEnvironments[i].release());
155136
}
156137

shared/source/execution_environment/root_device_environment.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#pragma once
99
#include "shared/source/built_ins/sip_kernel_type.h"
10+
#include "shared/source/helpers/affinity_mask.h"
1011
#include "shared/source/helpers/options.h"
1112

1213
#include <cstdint>
@@ -33,8 +34,6 @@ class SipKernel;
3334
class SWTagsManager;
3435
struct HardwareInfo;
3536

36-
constexpr uint32_t allSubDevicesActive = std::numeric_limits<uint32_t>::max();
37-
3837
struct RootDeviceEnvironment {
3938
protected:
4039
std::unique_ptr<HardwareInfo> hwInfo;
@@ -74,7 +73,7 @@ struct RootDeviceEnvironment {
7473
std::unique_ptr<SWTagsManager> tagsManager;
7574
ExecutionEnvironment &executionEnvironment;
7675

77-
uint32_t deviceAffinityMask = allSubDevicesActive;
76+
AffinityMaskHelper deviceAffinityMask{true};
7877

7978
private:
8079
std::mutex mtx;

shared/source/helpers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(NEO_CORE_HELPERS
88
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
99
${CMAKE_CURRENT_SOURCE_DIR}/abort.h
1010
${CMAKE_CURRENT_SOURCE_DIR}/address_patch.h
11+
${CMAKE_CURRENT_SOURCE_DIR}/affinity_mask.h
1112
${CMAKE_CURRENT_SOURCE_DIR}/aligned_memory.h
1213
${CMAKE_CURRENT_SOURCE_DIR}/api_specific_config.h
1314
${CMAKE_CURRENT_SOURCE_DIR}/array_count.h
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (C) 2021 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
10+
#include "shared/source/helpers/constants.h"
11+
#include "shared/source/helpers/debug_helpers.h"
12+
13+
#include <array>
14+
#include <bitset>
15+
#include <vector>
16+
17+
namespace NEO {
18+
19+
class AffinityMaskHelper {
20+
public:
21+
using AffinityMaskContainer = std::vector<std::bitset<32>>;
22+
23+
AffinityMaskHelper(bool allSubdevicesActive) {
24+
if (!allSubdevicesActive) {
25+
return;
26+
}
27+
28+
constexpr size_t maxInitialSubdeviceCount = 4;
29+
30+
enableAllGenericSubDevices(maxInitialSubdeviceCount);
31+
}
32+
33+
AffinityMaskHelper() : AffinityMaskHelper(false) {}
34+
35+
void enableGenericSubDevice(uint32_t subDeviceIndex) {
36+
subDevicesWithEnginesMasks.resize(subDeviceIndex + 1);
37+
38+
genericSubDevicesMask.set(subDeviceIndex);
39+
subDevicesWithEnginesMasks[subDeviceIndex] = std::numeric_limits<uint32_t>::max();
40+
}
41+
42+
void enableAllGenericSubDevices(uint32_t subDeviceCount) {
43+
for (uint32_t i = 0; i < subDeviceCount; i++) {
44+
enableGenericSubDevice(i);
45+
}
46+
}
47+
48+
DeviceBitfield getGenericSubDevicesMask() const {
49+
return genericSubDevicesMask;
50+
}
51+
52+
bool isDeviceEnabled() const {
53+
return genericSubDevicesMask.any();
54+
}
55+
56+
protected:
57+
AffinityMaskContainer subDevicesWithEnginesMasks;
58+
DeviceBitfield genericSubDevicesMask = 0;
59+
};
60+
} // namespace NEO

0 commit comments

Comments
 (0)