Files
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

193 lines
5.2 KiB
C++

//
// MIT License
// Copyright (c) 2020 Jonathan R. Madsen
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ---------------------------------------------------------------
// Tasking class implementation
//
// Threading.cc
//
#include "PTL/Threading.hh"
#include "PTL/ConsumeParameters.hh"
#include "PTL/Macros.hh"
#if defined(PTL_WINDOWS)
# include <Windows.h>
#endif
#if defined(PTL_MACOS)
# include <sys/sysctl.h>
#endif
#if defined(PTL_LINUX)
# include <fstream>
# include <set>
#endif
#include <cstddef>
#include <thread>
//======================================================================================//
namespace
{
thread_local int ThreadID = PTL::Threading::MASTER_ID;
} // namespace
//======================================================================================//
namespace PTL
{
Pid_t
GetPidId()
{
// In multithreaded mode return Thread ID
return std::this_thread::get_id();
}
//======================================================================================//
unsigned
GetNumberOfCores()
{
return std::thread::hardware_concurrency();
}
//======================================================================================//
unsigned
GetNumberOfPhysicalCpus()
{
#if defined(PTL_MACOS)
int count;
size_t count_len = sizeof(count);
sysctlbyname("hw.physicalcpu", &count, &count_len, nullptr, 0);
return static_cast<unsigned>(count);
#elif defined(PTL_LINUX)
unsigned core_id_count = 0;
std::ifstream ifs("/proc/cpuinfo");
if(ifs)
{
std::set<std::string> core_ids;
while(true)
{
std::string line = {};
getline(ifs, line);
if(!ifs.good())
break;
if(line.find("core id") != std::string::npos)
{
for(std::string itr : { "core id", ":", " ", "\t" })
{
static auto _npos = std::string::npos;
auto _pos = _npos;
while((_pos = line.find(itr)) != _npos)
line = line.replace(_pos, itr.length(), "");
}
core_ids.insert(line);
}
}
core_id_count = static_cast<unsigned>(core_ids.size());
if(core_id_count > 0)
return core_id_count;
}
return GetNumberOfCores();
#else
return GetNumberOfCores();
#endif
}
//======================================================================================//
void
SetThreadId(int value)
{
ThreadID = value;
}
int
GetThreadId()
{
return ThreadID;
}
//======================================================================================//
bool
SetPinAffinity(int _cpu)
{
#if defined(__linux__) || defined(_AIX)
cpu_set_t _cpu_set{};
CPU_ZERO(&_cpu_set);
if(_cpu >= 0)
CPU_SET(_cpu, &_cpu_set);
return (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &_cpu_set) == 0);
#else // Not available for Mac, WIN,...
ConsumeParameters(_cpu);
return true;
#endif
}
//======================================================================================//
bool
SetThreadPriority(int _prio)
{
#if defined(__linux__) || defined(_AIX)
return (pthread_setschedprio(pthread_self(), _prio) == 0);
#else // Not available for Mac, WIN,...
ConsumeParameters(_prio);
return true;
#endif
}
//======================================================================================//
bool
SetPinAffinity(int _cpu, NativeThread& _t)
{
#if defined(__linux__) || defined(_AIX)
cpu_set_t _cpu_set{};
CPU_ZERO(&_cpu_set);
CPU_SET(_cpu, &_cpu_set);
return (pthread_setaffinity_np(static_cast<pthread_t>(_t), sizeof(cpu_set_t),
&_cpu_set) == 0);
#else // Not available for Mac, WIN,...
ConsumeParameters(_cpu, _t);
return true;
#endif
}
//======================================================================================//
bool
SetThreadPriority(int _prio, NativeThread& _t)
{
#if defined(__linux__) || defined(_AIX)
return (pthread_setschedprio(static_cast<pthread_t>(_t), _prio) == 0);
#else
ConsumeParameters(_prio, _t);
return true;
#endif
}
//======================================================================================//
} // namespace PTL