Skip to content

Commit c6e81d3

Browse files
Simplify parsing affinity mask
Signed-off-by: Bartosz Dunajski <[email protected]>
1 parent 9458638 commit c6e81d3

File tree

8 files changed

+59
-42
lines changed

8 files changed

+59
-42
lines changed

opencl/source/helpers/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ set(RUNTIME_SRCS_HELPERS_BASE
4545
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/queue_helpers.cpp
4646
${CMAKE_CURRENT_SOURCE_DIR}/queue_helpers.h
4747
${CMAKE_CURRENT_SOURCE_DIR}/sampler_helpers.h
48-
${CMAKE_CURRENT_SOURCE_DIR}/string_helpers.h
4948
${CMAKE_CURRENT_SOURCE_DIR}/surface_formats.cpp
5049
${CMAKE_CURRENT_SOURCE_DIR}/surface_formats.h
5150
${CMAKE_CURRENT_SOURCE_DIR}/task_information.cpp

opencl/source/program/create.inl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2020 Intel Corporation
2+
* Copyright (C) 2017-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -8,10 +8,10 @@
88
#include "shared/source/debug_settings/debug_settings_manager.h"
99
#include "shared/source/device/device.h"
1010
#include "shared/source/helpers/constants.h"
11+
#include "shared/source/helpers/string_helpers.h"
1112

1213
#include "opencl/source/cl_device/cl_device.h"
1314
#include "opencl/source/context/context.h"
14-
#include "opencl/source/helpers/string_helpers.h"
1515
#include "opencl/source/platform/platform.h"
1616
#include "opencl/source/program/program.h"
1717

@@ -65,7 +65,7 @@ T *Program::create(
6565
size_t combinedStringSize = 0;
6666
T *program = nullptr;
6767

68-
auto retVal = createCombinedString(
68+
auto retVal = StringHelpers::createCombinedString(
6969
combinedString,
7070
combinedStringSize,
7171
count,

opencl/test/unit_test/utilities/file_logger_tests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 Intel Corporation
2+
* Copyright (C) 2019-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -8,9 +8,9 @@
88
#pragma once
99

1010
#include "shared/source/helpers/file_io.h"
11+
#include "shared/source/helpers/string_helpers.h"
1112
#include "shared/source/utilities/directory.h"
1213

13-
#include "opencl/source/helpers/string_helpers.h"
1414
#include "opencl/source/utilities/logger.h"
1515

1616
#include <map>

shared/source/execution_environment/execution_environment.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "shared/source/execution_environment/root_device_environment.h"
1313
#include "shared/source/helpers/affinity_mask.h"
1414
#include "shared/source/helpers/hw_helper.h"
15+
#include "shared/source/helpers/string_helpers.h"
1516
#include "shared/source/memory_manager/memory_manager.h"
1617
#include "shared/source/memory_manager/os_agnostic_memory_manager.h"
1718
#include "shared/source/os_interface/os_environment.h"
@@ -84,8 +85,9 @@ void ExecutionEnvironment::prepareRootDeviceEnvironments(uint32_t numRootDevices
8485
}
8586
}
8687
}
88+
8789
void ExecutionEnvironment::parseAffinityMask() {
88-
auto affinityMaskString = DebugManager.flags.ZE_AFFINITY_MASK.get();
90+
const auto &affinityMaskString = DebugManager.flags.ZE_AFFINITY_MASK.get();
8991

9092
if (affinityMaskString.compare("default") == 0 ||
9193
affinityMaskString.empty()) {
@@ -96,33 +98,25 @@ void ExecutionEnvironment::parseAffinityMask() {
9698

9799
std::vector<AffinityMaskHelper> affinityMaskHelper(numRootDevices);
98100

99-
size_t pos = 0;
100-
while (pos < affinityMaskString.size()) {
101-
size_t posNextDot = affinityMaskString.find_first_of(".", pos);
102-
size_t posNextComma = affinityMaskString.find_first_of(",", pos);
103-
std::string rootDeviceString = affinityMaskString.substr(pos, std::min(posNextDot, posNextComma) - pos);
104-
uint32_t rootDeviceIndex = static_cast<uint32_t>(std::stoul(rootDeviceString, nullptr, 0));
101+
auto affinityMaskEntries = StringHelpers::split(affinityMaskString, ",");
102+
103+
for (const auto &entry : affinityMaskEntries) {
104+
auto subEntries = StringHelpers::split(entry, ".");
105+
uint32_t rootDeviceIndex = StringHelpers::toUint32t(subEntries[0]);
106+
105107
if (rootDeviceIndex < numRootDevices) {
106108
auto hwInfo = rootDeviceEnvironments[rootDeviceIndex]->getHardwareInfo();
107109
auto subDevicesCount = HwHelper::getSubDevicesCount(hwInfo);
108110

109-
pos += rootDeviceString.size();
110-
if (posNextDot != std::string::npos &&
111-
affinityMaskString.at(pos) == '.' && posNextDot < posNextComma) {
112-
pos++;
113-
std::string subDeviceString = affinityMaskString.substr(pos, posNextComma - pos);
114-
uint32_t subDeviceIndex = static_cast<uint32_t>(std::stoul(subDeviceString, nullptr, 0));
111+
if (subEntries.size() == 2) {
112+
uint32_t subDeviceIndex = StringHelpers::toUint32t(subEntries[1]);
115113
if (subDeviceIndex < subDevicesCount) {
116114
affinityMaskHelper[rootDeviceIndex].enableGenericSubDevice(subDeviceIndex);
117115
}
118116
} else {
119117
affinityMaskHelper[rootDeviceIndex].enableAllGenericSubDevices(subDevicesCount);
120118
}
121119
}
122-
if (posNextComma == std::string::npos) {
123-
break;
124-
}
125-
pos = posNextComma + 1;
126120
}
127121

128122
std::vector<std::unique_ptr<RootDeviceEnvironment>> filteredEnvironments;

shared/source/helpers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ set(NEO_CORE_HELPERS
9696
${CMAKE_CURRENT_SOURCE_DIR}/state_compute_mode_helper.h
9797
${CMAKE_CURRENT_SOURCE_DIR}/stdio.h
9898
${CMAKE_CURRENT_SOURCE_DIR}/string.h
99+
${CMAKE_CURRENT_SOURCE_DIR}/string_helpers.h
99100
${CMAKE_CURRENT_SOURCE_DIR}/surface_format_info.h
100101
${CMAKE_CURRENT_SOURCE_DIR}/timestamp_offsets.h
101102
${CMAKE_CURRENT_SOURCE_DIR}/timestamp_packet.cpp

shared/source/helpers/affinity_mask.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <array>
1414
#include <bitset>
15+
#include <string>
1516
#include <vector>
1617

1718
namespace NEO {
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2020 Intel Corporation
2+
* Copyright (C) 2017-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -14,7 +14,8 @@
1414
#include <cstring>
1515
#include <string>
1616

17-
const int maximalStackSizeSizes = 16;
17+
namespace StringHelpers {
18+
constexpr int maximalStackSizeSizes = 16;
1819

1920
inline int createCombinedString(
2021
std::string &dstString,
@@ -28,7 +29,7 @@ inline int createCombinedString(
2829
retVal = CL_INVALID_VALUE;
2930
}
3031

31-
using SourceSizesT = StackVec<size_t, maximalStackSizeSizes>;
32+
using SourceSizesT = StackVec<size_t, StringHelpers::maximalStackSizeSizes>;
3233
SourceSizesT localSizes;
3334

3435
if (retVal == CL_SUCCESS) {
@@ -61,3 +62,25 @@ inline int createCombinedString(
6162

6263
return retVal;
6364
}
65+
66+
inline std::vector<std::string> split(const std::string &input, const char *delimiter) {
67+
std::vector<std::string> outVector;
68+
size_t pos = 0;
69+
70+
while (pos < input.size()) {
71+
size_t nextDelimiter = input.find_first_of(delimiter, pos);
72+
outVector.emplace_back(input.substr(pos, std::min(nextDelimiter, input.size()) - pos));
73+
74+
pos = nextDelimiter;
75+
if (pos != std::string::npos) {
76+
pos++;
77+
}
78+
}
79+
80+
return outVector;
81+
}
82+
83+
inline uint32_t toUint32t(const std::string &input) {
84+
return static_cast<uint32_t>(std::stoul(input, nullptr, 0));
85+
}
86+
} // namespace StringHelpers

shared/test/common/helpers/string_to_hash_tests.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
*/
77

88
#include "shared/source/helpers/hash.h"
9-
10-
#include "opencl/source/helpers/string_helpers.h"
9+
#include "shared/source/helpers/string_helpers.h"
1110

1211
#include "gtest/gtest.h"
1312

@@ -21,7 +20,7 @@ TEST(CreateCombinedStrings, GivenSingleStringWhenCreatingCombinedStringThenDstSt
2120
auto srcStrings = &pSrcString;
2221
size_t lengths = strlen(srcString);
2322

24-
auto retVal = createCombinedString(
23+
auto retVal = StringHelpers::createCombinedString(
2524
dstString,
2625
dstStringSizeInBytes,
2726
1,
@@ -41,7 +40,7 @@ TEST(CreateCombinedStrings, GivenNullLengthWhenCreatingCombinedStringThenDstStri
4140
const char *pSrcString = srcString;
4241
auto srcStrings = &pSrcString;
4342

44-
auto retVal = createCombinedString(
43+
auto retVal = StringHelpers::createCombinedString(
4544
dstString,
4645
dstStringSizeInBytes,
4746
1,
@@ -60,7 +59,7 @@ TEST(CreateCombinedStrings, GivenZeroLengthWhenCreatingCombinedStringThenDstStri
6059
auto srcStrings = &pSrcString;
6160
size_t lengths = 0;
6261

63-
auto retVal = createCombinedString(
62+
auto retVal = StringHelpers::createCombinedString(
6463
dstString,
6564
dstStringSizeInBytes,
6665
1,
@@ -81,7 +80,7 @@ TEST(CreateCombinedStrings, GivenMultiStringWhenCreatingCombinedStringThenDstStr
8180
auto srcStrings = &srcString[0];
8281
size_t lengths[2] = {strlen(srcString[0]), strlen(srcString[1])};
8382

84-
auto retVal = createCombinedString(
83+
auto retVal = StringHelpers::createCombinedString(
8584
dstString,
8685
dstStringSizeInBytes,
8786
2,
@@ -100,7 +99,7 @@ TEST(CreateCombinedStrings, GivenMultiStringAndNullLengthWhenCreatingCombinedStr
10099
combined += srcString[1];
101100
auto srcStrings = &srcString[0];
102101

103-
auto retVal = createCombinedString(
102+
auto retVal = StringHelpers::createCombinedString(
104103
dstString,
105104
dstStringSizeInBytes,
106105
2,
@@ -120,7 +119,7 @@ TEST(CreateCombinedStrings, GivenMultiStringAndZeroLengthWhenCreatingCombinedStr
120119
auto srcStrings = &srcString[0];
121120
size_t lengths[2] = {0, strlen(srcString[1])};
122121

123-
auto retVal = createCombinedString(
122+
auto retVal = StringHelpers::createCombinedString(
124123
dstString,
125124
dstStringSizeInBytes,
126125
2,
@@ -140,7 +139,7 @@ TEST(CreateCombinedStrings, GivenMultipleStringsIncludingOneWithErrorWhenCreatin
140139
std::string combined(expString[0]);
141140
combined += expString[1];
142141

143-
auto retVal = createCombinedString(
142+
auto retVal = StringHelpers::createCombinedString(
144143
dstString,
145144
dstStringSizeInBytes,
146145
2,
@@ -160,15 +159,15 @@ TEST(CreateCombinedStrings, GivenInvalidInputWhenCreatingCombinedStringThenInval
160159
const char *srcStrings[2] = {srcString[0], srcString[1]};
161160
size_t lengths[2] = {0, strlen(srcString[1])};
162161

163-
auto retVal = createCombinedString(
162+
auto retVal = StringHelpers::createCombinedString(
164163
dstString,
165164
dstStringSizeInBytes,
166165
0,
167166
srcStrings,
168167
lengths);
169168
EXPECT_EQ(CL_INVALID_VALUE, retVal);
170169

171-
retVal = createCombinedString(
170+
retVal = StringHelpers::createCombinedString(
172171
dstString,
173172
dstStringSizeInBytes,
174173
1,
@@ -177,7 +176,7 @@ TEST(CreateCombinedStrings, GivenInvalidInputWhenCreatingCombinedStringThenInval
177176
EXPECT_EQ(CL_INVALID_VALUE, retVal);
178177

179178
srcStrings[0] = nullptr;
180-
retVal = createCombinedString(
179+
retVal = StringHelpers::createCombinedString(
181180
dstString,
182181
dstStringSizeInBytes,
183182
2,
@@ -190,17 +189,17 @@ TEST(CreateCombinedStrings, GivenMultipleStringThatCountIsHigherThanMaximalStack
190189
std::string dstString;
191190
size_t dstStringSizeInBytes = 0;
192191
const char *defaultString = "hello";
193-
const char *srcString[maximalStackSizeSizes + 2];
192+
const char *srcString[StringHelpers::maximalStackSizeSizes + 2];
194193
std::string combinedString;
195-
for (int i = 0; i < maximalStackSizeSizes + 2; i++) {
194+
for (int i = 0; i < StringHelpers::maximalStackSizeSizes + 2; i++) {
196195
srcString[i] = defaultString;
197196
combinedString += defaultString;
198197
}
199198

200-
auto retVal = createCombinedString(
199+
auto retVal = StringHelpers::createCombinedString(
201200
dstString,
202201
dstStringSizeInBytes,
203-
maximalStackSizeSizes + 2,
202+
StringHelpers::maximalStackSizeSizes + 2,
204203
srcString,
205204
nullptr);
206205

0 commit comments

Comments
 (0)