Skip to content

Commit

Permalink
Refactoring get_cpu_pinning()
Browse files Browse the repository at this point in the history
  • Loading branch information
wangleis committed Feb 4, 2024
1 parent 3ec8c18 commit 5e48f82
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 26 deletions.
38 changes: 24 additions & 14 deletions src/plugins/intel_cpu/src/cpu_map_scheduling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "cpu_streams_calculation.hpp"
#include "openvino/core/parallel.hpp"
#include "openvino/runtime/system_conf.hpp"
#include "openvino/runtime/threading/cpu_streams_info.hpp"

namespace ov {
namespace intel_cpu {
Expand Down Expand Up @@ -70,28 +71,37 @@ std::vector<std::vector<int>> apply_hyper_threading(bool& input_ht_hint,
}

bool get_cpu_pinning(bool& input_value,
const bool input_changed,
const int num_streams,
const Config::LatencyThreadingMode latency_threading_mode,
const std::vector<std::vector<int>>& proc_type_table) {
int result_value;
int num_sockets = get_default_latency_streams(latency_threading_mode);
bool latency = num_streams <= num_sockets && num_streams > 0;
const bool& input_changed,
const std::vector<std::vector<int>>& streams_info_table) {
bool result_value;

#if defined(__APPLE__)
result_value = false;
#else
if (input_changed) {
result_value = input_value;
} else {
# if defined(_WIN32)
result_value = false;
# else
result_value = true;
if (proc_type_table[0][EFFICIENT_CORE_PROC] > 0 &&
proc_type_table[0][EFFICIENT_CORE_PROC] < proc_type_table[0][ALL_PROC]) {
result_value = latency ? false : true;
for (size_t n = 0; n < streams_info_table.size(); n++) {
if (streams_info_table[n][PROC_TYPE] == ALL_PROC) {
size_t m = n + 1;
while ((m < streams_info_table.size()) && (0 == streams_info_table[m][NUMBER_OF_STREAMS])) {
if (EFFICIENT_CORE_PROC == streams_info_table[m][PROC_TYPE]) {
result_value = false;
break;
} else {
m++;
}
}
}
}
}
#if (OV_THREAD == OV_THREAD_TBB || OV_THREAD == OV_THREAD_TBB_AUTO)
# if defined(__APPLE__)
result_value = false;
# endif
}
#endif

input_value = result_value;

return result_value;
Expand Down
10 changes: 3 additions & 7 deletions src/plugins/intel_cpu/src/cpu_map_scheduling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,12 @@ std::vector<std::vector<int>> apply_hyper_threading(bool& input_ht_hint,
* @brief whether pinning cpu cores according to enableCpuPinning property
* @param[in] input_type indicate value of property enableCpuPinning.
* @param[in] input_changed indicate if value is set by user.
* @param[in] num_streams number of streams
* @param[in] latency_threading_mode is the scope of candidate processors per stream for latency hint
* @param[in] proc_type_table candidate processors available at this time
* @param[in] streams_info_table indicate streams detail of this model
* @return whether pinning threads to cpu cores
*/
bool get_cpu_pinning(bool& input_value,
const bool input_changed,
const int num_streams,
const Config::LatencyThreadingMode latency_threading_mode,
const std::vector<std::vector<int>>& proc_type_table);
const bool& input_changed,
const std::vector<std::vector<int>>& streams_info_table);

} // namespace intel_cpu
} // namespace ov
10 changes: 5 additions & 5 deletions src/plugins/intel_cpu/src/cpu_streams_calculation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,7 @@ std::vector<std::vector<int>> generate_stream_info(const int streams,
config.changedHyperThreading,
ov::util::to_string(config.hintPerfMode),
proc_type_table);
executor_config._cpu_reservation = get_cpu_pinning(config.enableCpuPinning,
config.changedCpuPinning,
streams,
config.latencyThreadingMode,
proc_type_table);

if (-1 == preferred_nthreads_per_stream) {
model_prefer_threads = get_model_prefer_threads(streams, proc_type_table, model, config);
}
Expand All @@ -553,6 +549,10 @@ std::vector<std::vector<int>> generate_stream_info(const int streams,
ov::util::to_string(config.hintPerfMode),
config.latencyThreadingMode,
proc_type_table);

executor_config._cpu_reservation =
get_cpu_pinning(config.enableCpuPinning, config.changedCpuPinning, executor_config._streams_info_table);

return proc_type_table;
}

Expand Down
121 changes: 121 additions & 0 deletions src/plugins/intel_cpu/tests/unit/streams_info/cpu_pinning_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <gtest/gtest.h>

#include "common_test_utils/test_common.hpp"
#include "cpu_map_scheduling.hpp"
#include "openvino/runtime/system_conf.hpp"

using namespace testing;
using namespace ov;

namespace {

struct CpuPinningTestCase {
bool input_cpu_pinning;
bool input_changed;
std::vector<std::vector<int>> input_stream_info_table;
bool output_cpu_pinning;
};

class CpuPinningTests : public ov::test::TestsCommon,
public testing::WithParamInterface<std::tuple<CpuPinningTestCase>> {
public:
void SetUp() override {
auto test_data = std::get<0>(GetParam());

auto test_output = ov::intel_cpu::get_cpu_pinning(test_data.input_cpu_pinning,
test_data.input_changed,
test_data.input_stream_info_table);

ASSERT_EQ(test_data.output_cpu_pinning, test_data.input_cpu_pinning);
ASSERT_EQ(test_data.output_cpu_pinning, test_output);
}
};

TEST_P(CpuPinningTests, CpuPinning) {}

CpuPinningTestCase cpu_pinning_macos_mock_set_true = {
true, // param[in]: simulated settting for cpu pinning property
true, // param[in]: simulated settting for user changing cpu pinning property
{{1, MAIN_CORE_PROC, 20, 0, 0}}, // param[in]: simulated setting for current streams infor table
false, // param[expected out]: simulated setting for expected output
};
CpuPinningTestCase cpu_pinning_macos_mock_set_false = {
false,
true,
{{1, MAIN_CORE_PROC, 20, 0, 0}},
false,
};
CpuPinningTestCase cpu_pinning_macos_mock_set_default = {
true,
false,
{{1, MAIN_CORE_PROC, 20, 0, 0}},
false,
};
CpuPinningTestCase cpu_pinning_win_mock_set_true = {
true,
true,
{{1, MAIN_CORE_PROC, 20, 0, 0}},
true,
};
CpuPinningTestCase cpu_pinning_win_mock_set_false = {
false,
true,
{{1, MAIN_CORE_PROC, 20, 0, 0}},
false,
};
CpuPinningTestCase cpu_pinning_win_mock_set_default = {
true,
false,
{{1, MAIN_CORE_PROC, 20, 0, 0}},
false,
};
CpuPinningTestCase cpu_pinning_linux_mock_set_true = {
true,
true,
{{1, MAIN_CORE_PROC, 20, 0, 0}},
true,
};
CpuPinningTestCase cpu_pinning_linux_mock_set_false = {
false,
true,
{{1, MAIN_CORE_PROC, 20, 0, 0}},
false,
};
CpuPinningTestCase cpu_pinning_linux_mock_set_default = {
false,
false,
{{1, MAIN_CORE_PROC, 20, 0, 0}},
true,
};
CpuPinningTestCase cpu_pinning_linux_mock_set_default_2 = {
true,
false,
{{1, ALL_PROC, 14, 0, 0}, {0, MAIN_CORE_PROC, 6, 0, 0}, {0, EFFICIENT_CORE_PROC, 8, 0, 0}},
false,
};

#if defined(__linux__)
INSTANTIATE_TEST_SUITE_P(smoke_CpuPinning,
CpuPinningTests,
::testing::Values(cpu_pinning_linux_mock_set_true,
cpu_pinning_linux_mock_set_false,
cpu_pinning_linux_mock_set_default,
cpu_pinning_linux_mock_set_default_2));
#elif defined(_WIN32)
INSTANTIATE_TEST_SUITE_P(smoke_CpuPinning,
CpuPinningTests,
::testing::Values(cpu_pinning_win_mock_set_true,
cpu_pinning_win_mock_set_false,
cpu_pinning_win_mock_set_default));
#else
INSTANTIATE_TEST_SUITE_P(smoke_CpuPinning,
CpuPinningTests,
::testing::Values(cpu_pinning_macos_mock_set_true,
cpu_pinning_macos_mock_set_false,
cpu_pinning_macos_mock_set_default));
#endif
} // namespace

0 comments on commit 5e48f82

Please sign in to comment.