Files
TheRock/rocm-systems/projects/rocprofiler-systems/source/lib/rocprof-sys/library/causal/delay.cpp
T
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

203 lines
4.8 KiB
C++

// Copyright (c) Advanced Micro Devices, Inc.
// SPDX-License-Identifier: MIT
#include "library/causal/delay.hpp"
#include "common/units.hpp"
#include "core/state.hpp"
#include "core/utility.hpp"
#include "library/causal/components/causal_gotcha.hpp"
#include "library/causal/experiment.hpp"
#include "library/causal/sampling.hpp"
#include "library/runtime.hpp"
#include "library/thread_data.hpp"
#include "library/thread_info.hpp"
#include "library/tracing.hpp"
#include <cstdint>
#include "logger/debug.hpp"
#include <timemory/components/macros.hpp>
#include <timemory/mpl/concepts.hpp>
#include <timemory/mpl/types.hpp>
#include <timemory/process/threading.hpp>
#include <atomic>
#include <chrono>
#include <random>
namespace rocprofsys
{
namespace causal
{
namespace
{
auto&
get_delay_data()
{
using thread_data_t = thread_data<identity<std::int64_t>, delay>;
static auto& _v = thread_data_t::construct(
construct_on_init{}, []() { return delay::get_global().load(); });
return _v;
}
std::int64_t
compute_sleep_for_overhead()
{
using random_engine_t = std::mt19937_64;
auto _engine = random_engine_t{ std::random_device{}() };
auto _dist = std::uniform_int_distribution<std::int64_t>{ 0, 5 };
size_t _ntot = 250;
size_t _nwarm = 50;
auto _stats = tim::statistics<double>{};
for(size_t i = 0; i < _ntot; ++i)
{
auto _val = _dist(_engine);
std::int64_t _beg = tracing::now();
std::this_thread::sleep_for(std::chrono::nanoseconds{ _val });
std::int64_t _end = tracing::now();
if(i < _nwarm) continue;
auto _diff = (_end - _beg);
if(_diff < _val)
{
throw std::runtime_error(
fmt::format("Error! sleep_for({}) [nanoseconds] >= {}", _val, _diff));
}
_stats += (_diff - _val);
}
LOG_TRACE("[causal] overhead of std::this_thread::sleep_for(...) "
"invocation = {} usec +/- {} usec",
_stats.get_mean() / units::usec, _stats.get_stddev() / units::usec);
tim::manager::instance()->add_metadata([_stats](auto& ar) {
ar(tim::cereal::make_nvp("causal thread sleep overhead [nsec]", _stats));
});
(void) get_delay_data();
return _stats.get_mean();
}
std::int64_t sleep_for_overhead = 0;
} // namespace
void
delay::setup()
{
static std::once_flag _once{};
std::call_once(_once, []() { sleep_for_overhead = compute_sleep_for_overhead(); });
}
void
delay::process()
{
if(!is_local_available()) return;
if(causal::experiment::is_active())
{
if(get_global() < get_local())
{
get_global() += (get_local() - get_global());
}
else if(get_global() > get_local())
{
::rocprofsys::causal::sampling::pause();
auto _beg = tracing::now();
std::this_thread::sleep_for(
std::chrono::nanoseconds{ get_global() - get_local() });
get_local() += (tracing::now() - _beg);
::rocprofsys::causal::sampling::resume();
}
}
else
{
get_local() = get_global();
}
}
void
delay::credit()
{
if(!is_local_available()) return;
auto _diff = get_global() - get_local();
if(_diff > 0)
{
get_local() += _diff;
}
}
void
delay::preblock()
{
if(!is_local_available()) return;
auto _diff = get_global() - get_local();
if(_diff > 0)
{
get_local() += _diff;
}
}
void
delay::postblock(std::int64_t _preblock_global_delay_value)
{
if(!is_local_available()) return;
get_local() += (get_global() - _preblock_global_delay_value);
}
std::int64_t
delay::sync()
{
auto _v = get_global().load(std::memory_order_seq_cst);
if(get_delay_data()) get_delay_data()->fill(_v);
return _v;
}
std::atomic<std::int64_t>&
delay::get_global()
{
static auto _v = std::atomic<std::int64_t>{ 0 };
return _v;
}
static void
thr_init()
{
static thread_local auto _thr_init = []() {
using thread_data_t = thread_data<identity<std::int64_t>, delay>;
thread_data_t::construct(construct_on_thread{ threading::get_id() },
delay::get_global().load());
return true;
}();
(void) _thr_init; // To make compiler happy.
}
bool
delay::is_local_available()
{
thr_init();
auto& _data = get_delay_data();
return _data != nullptr;
}
std::int64_t&
delay::get_local(std::int64_t _tid)
{
thr_init();
auto& _data = get_delay_data();
if(_data == nullptr)
{
throw std::runtime_error("No data: get_delay_data() returned nullptr");
}
return _data->at(_tid);
}
std::uint64_t
delay::compute_total_delay(std::uint64_t _baseline)
{
return get_global().load() - _baseline;
}
} // namespace causal
} // namespace rocprofsys