Skip to content

Commit

Permalink
code review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Divyajyoti Ukirde <[email protected]>
  • Loading branch information
diukirde committed Sep 19, 2024
1 parent fad88e5 commit 3a32ca5
Showing 1 changed file with 40 additions and 49 deletions.
89 changes: 40 additions & 49 deletions src/runtime_src/core/common/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,29 @@ set_cpu_affinity(std::thread& thread)

std::string cpus = xrt_core::config::detail::get_string_value("Runtime.cpu_affinity","default");
if (cpus=="default")
all=true;
else {
boost::trim_if(cpus,boost::is_any_of("{}"));
using tokenizer=boost::tokenizer<boost::char_separator<char> >;
boost::char_separator<char> sep(", ");
auto max_cpus = std::thread::hardware_concurrency();
CPU_ZERO(&cpuset);
for (auto& tok : tokenizer(cpus,sep)) {
auto cpu = std::stoul(tok);
if (cpu < max_cpus) {
XRT_DEBUG(std::cout,"adding cpu #",cpu," to affinity mask\n");
CPU_SET(cpu,&cpuset);
}
else {
xrt_core::message::send(xrt_core::message::severity_level::warning,"XRT", "Ignoring cpu affinity since cpu #" + tok + " is out of range\n");
all=true;
}
return;

boost::trim_if(cpus,boost::is_any_of("{}"));
using tokenizer=boost::tokenizer<boost::char_separator<char> >;
boost::char_separator<char> sep(", ");
auto max_cpus = std::thread::hardware_concurrency();
CPU_ZERO(&cpuset);
for (auto& tok : tokenizer(cpus,sep)) {
auto cpu = std::stoul(tok);
if (cpu < max_cpus) {
XRT_DEBUG(std::cout,"adding cpu #",cpu," to affinity mask\n");
CPU_SET(cpu,&cpuset);
}
else {
xrt_core::message::send(xrt_core::message::severity_level::warning,"XRT", "Ignoring cpu affinity since cpu #" + tok + " is out of range\n");
return;
}
}
}

if (all)
return;

if (pthread_setaffinity_np(thread.native_handle(),sizeof(cpu_set_t),&cpuset)) {
if (pthread_setaffinity_np(thread.native_handle(),sizeof(cpu_set_t),&cpuset))
throw std::runtime_error("error calling pthread_setaffinity_np");
}

}

#else
Expand All @@ -143,44 +139,39 @@ static void
set_cpu_affinity(std::thread& thread)
{
static bool initialized = false;
static DWORD_PTR affinityMask = 0;
static DWORD_PTR affinity_mask = 0;
static bool all = false;

if (!initialized) {
initialized = true;

std::string cpus = xrt_core::config::detail::get_string_value("Runtime.cpu_affinity", "default");
if (cpus == "default") {
all = true;
}
else {
boost::trim_if(cpus, boost::is_any_of("{}"));
using tokenizer = boost::tokenizer<boost::char_separator<char>>;
boost::char_separator<char> sep(", ");
auto max_cpus = GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
for (auto& tok : tokenizer(cpus, sep)) {
auto cpu = std::stoul(tok);
cpu = static_cast<DWORD_PTR>(cpu);
auto one = static_cast<DWORD_PTR>(1U);
if (cpu < max_cpus) {
XRT_DEBUG(std::cout, "adding cpu #", cpu, " to affinity mask\n");
affinityMask |= (one << cpu); // Set the respective bit for the CPU
}
else {
xrt_core::message::send(xrt_core::message::severity_level::warning, "XRT", "Ignoring cpu affinity since cpu #" + tok + " is out of range\n");
all = true;
}
if (cpus == "default")
return;

boost::trim_if(cpus, boost::is_any_of("{}"));
using tokenizer = boost::tokenizer<boost::char_separator<char>>;
boost::char_separator<char> sep(", ");
auto max_cpus = std::thread::hardware_concurrency();
for (auto& tok : tokenizer(cpus, sep)) {
auto cpu = std::stoul(tok);
cpu = static_cast<DWORD_PTR>(cpu);
auto one = static_cast<DWORD_PTR>(1U);
if (cpu < max_cpus) {
XRT_DEBUG(std::cout, "adding cpu #", cpu, " to affinity mask\n");
affinity_mask |= (one << cpu); // Set the respective bit for the CPU
}
else {
xrt_core::message::send(xrt_core::message::severity_level::warning, "XRT", "Ignoring cpu affinity since cpu #" + tok + " is out of range\n");
return;
}
}
}

if (all)
return;

HANDLE threadHandle = thread.native_handle();
if (!SetThreadAffinityMask(threadHandle, affinityMask)) {
HANDLE thread_handle = thread.native_handle();
if (!SetThreadAffinityMask(thread_handle, affinity_mask))
throw std::runtime_error("error calling SetThreadAffinityMask");
}

}

#endif
Expand Down

0 comments on commit 3a32ca5

Please sign in to comment.