Files
rocm-systems/source/lib/rocprofiler-sdk/ompt/ompt.cpp
T
Madsen, Jonathan 00c46fd5e5 SDK: OMPT Support (#22)
* Ability to select alternative compiler per file

Implementation of ompt interface to rocprofiler SDK. task_create and task_schedule are not supported.

Misc updates

Update OpenMP target sample

- samples/ompt -> samples/openmp_target
- fix sample test of openmp-target
- reorganize files

Rework OpenMP implementation

Minor OpenMP implementation cleanup

Rename samples/openmp_target CMake targets

Add tests/bin/openmp

- OpenMP target test app in tests/bin/openmp/target

Format samples/openmp_target CMakeLists.txt

Misc lib/rocprofiler-sdk/openmp cleanup

- fix includes
- convert_arg

Update openmp.def.cpp

- tweak includes
- remove lots of temporary variables

Update samples

- common::get_callback_id_names() -> common::get_callback_tracing_names()
- add kernel dispatch, memory copy, scratch memory buffered tracing to openmp target sample

Fix code object operation names

- add "CODE_OBJECT_" prefix

Update include/rocprofiler-sdk/openmp/api_id.h

- remove spurious comment

Miscellaneous openmp updates

- similar API for openmp_begin and openmp_end
- move implementations of ompt callbacks to openmp.cpp
- ompt_{thread_begin,thread_end,parallel_begin,parallel_end}_callbacks are openmp_events

[SWDEV-484495] Fix int truncation in CSV output (#1098)

CSV output truncates doubles to ints when it shouldn't. Derived metrics
are (mostly) doubles and lose precision (or become worthless) if treated
as an int. Converted these to double to match the format we return from
rocprof-sdk.

Co-authored-by: Benjamin Welton <ben@amd.com>

Update limit for max counter records in rocprof-tool (#1073)

A fixed sized std::array is used to store counter records in rocprofiler SDK. This limit was breached in SWDEV-484742. Upping the limit to 512 to be less likely to reach this limit again.

adding proxy ompt_data_t * arguments

fixes for proxy pointers

- Implement proxy ompt_data_t* pointers for clients
- Add ompt_data_t* arguments back to callback API
- Modify openmp sample to illustrate use of proxy pointers

formatting

SWDEV-467350: Skipping tool counter iteration for unsupported hardware (#1083)

Fixing some accumulate metrics (#1089)

* Fixing some accumulate metrics

* Fixing some more accumulate metrics

---------

Co-authored-by: Benjamin Welton <bewelton@amd.com>

updating rocprofv3 help options (#1113)

* updating rocprofv3 help options

* updating CHANGELOG

Fixing installed pacakge tests in CI (#1119)

* Fixing installed pacakge tests in CI

* Formatted rocprofv3.py with black formatter

SWDEV-488948: PC Sampling - Correlation class to provide some thread safety. Adding multithread tests. (#1112)

* SWDEV-488948: PC Sampling - Correlation class to provide some thread safety. Adding multithread tests.

* Update source/lib/rocprofiler-sdk/pc_sampling/parser/correlation.hpp

Co-authored-by: Vladimir Indic <139573562+vlaindic@users.noreply.github.com>

* Update source/lib/rocprofiler-sdk/pc_sampling/parser/correlation.hpp

Co-authored-by: Vladimir Indic <139573562+vlaindic@users.noreply.github.com>

* Adding backlog for codeobj changes

* Formatting

* Update source/lib/rocprofiler-sdk/pc_sampling/code_object.hpp

Co-authored-by: Vladimir Indic <139573562+vlaindic@users.noreply.github.com>

* Update source/lib/rocprofiler-sdk/pc_sampling/code_object.hpp

Co-authored-by: Vladimir Indic <139573562+vlaindic@users.noreply.github.com>

---------

Co-authored-by: Vladimir Indic <139573562+vlaindic@users.noreply.github.com>

SWDEV-487621: Fixes for metric definitions (#1118)

* Fixes for metric definitions

* Removing gfx8

* Update changelog

* Fixing unit tests

* Small fixes

* Fix for write size

Fix PSDB change (#1120)

Reverts change to `source/include/rocprofiler-sdk/callback_tracing.h`
from commit 9b2ece76c3

clang-18 build fix for RCCL (#1123)

Removes ambiguity on const usage, which clang-18 complains about
(preventing build with warn error).

mem copy direction field update (#1124)

Adding Node-id for debugging with log level trace (#1090)

fix botched rebase

Per Jonathan to remove -rdynamic warning so CI will continue

pedantic formatting

Correct the package name of rocprofiler-sdk (#1126)

* Correct the package name of rocprofiler-sdk

ROCM VERSION(for ex: 60300) was missing in the package name.
Added the same

* Use cmake cache string while setting the variable for ROCm Version

* correct the cmake-format

---------

Co-authored-by: Ranjith Ramakrishnan <Ranjith.Ramakrishnan@amd.com>

Fixing kokkosp tool library packaging (#1121)

* Fixing kokkosp tool library packaging

* Update source/lib/rocprofiler-sdk-tool/kokkosp/CMakeLists.txt

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update CMakeLists.txt

* Update CMakeLists.txt

* Component Requirement in CPack

* Adding package dependency

* Update CMakeLists.txt

* Update rocprofiler_config_packaging.cmake

* Fix rocprofiler-sdk-tool-kokkosp BUILD/INSTALL RPATH

- CMAKE_INSTALL_LIBDIR doesn't help

* Add BUILD/INSTALL RPATH to rocprofv3-trigger-list-metrics

- fixes packaging issues

* Update packaging

- core depends on rocprofiler-sdk-roctx
- add CPACK_DEBIAN_PACKAGE_SHLIBDEPS_PRIVATE_DIRS to resolve inter-package dependencies

* Fix package depends version format

* Improve tests/rocprofv3/summary/validate logging

* Update CI workflow

- prioritize roctx package in Install Packages step

* Remove setting <package-name>_VERSION in config.cmake.in

- this is automatically handled by existence of <package-name>-config-version.cmake

* Update rocprofiler-sdk-config.cmake

- relax find_package versioning requirements to same major and minor version

* Update rocprofiler-sdk-config.cmake

- relax find_package versioning requirements (remove EXACT, specify range)

* Tweak CI workflow

* Update perfetto_reader.py

- better handle failure to load trace processor

* Misc cleanup for config packaging

* Update config packaging

* Update config packaging

* Revert perfetto for core-rpm packages

* Revert perfetto for core-rpm packages

- perfetto < 0.9.0

* Tweak tests/rocprofv3/summary/validate.py

- reorder some checks

---------

Co-authored-by: Ammar Elwazir <aelwazir@useocpm2m-387-013.amd.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com>

Clang Warning Fixes (#1131)

Builds prevented on clang-18

Adding start and end timestamp columns in csv (#1128)

* Adding start and end timestamp columns in csv

* Adding assert check for the counter timestamps

---------

Co-authored-by: Gopesh Bhardwaj <gopesh.bhardwaj@amd.com>

rocprofv3: docs and help menu updates (#1129)

* doc updates

* Correcting ROCtx information

* Making ROCTx string consistent

* missing occurence

Renamed agent profiling service to device counting service (#1132)

* Renamed agent profiling service to device counting service

Name more aptly represents what agent profiling did (device wide
counter collection). Conversion of existing user code can be
performed by the following find/sed command:

find . -type f -exec sed -i 's/rocprofiler_agent_profile_callback_t/rocprofiler_device_counting_service_callback_t/g; s/rocprofiler_configure_agent_profile_counting_service/rocprofiler_configure_device_counting_service/g; s/agent_profile.h/device_counting_service.h/g; s/rocprofiler_sample_agent_profile_counting_service/rocprofiler_sample_device_counting_service/g' {} +

* Converted dispatch profile to dispatch counting service

* Debug for functioal counters test

* Minor changes for CI

* Minor fix

* More fixes for CI

* Update evaluate_ast.cpp

---------

Co-authored-by: Benjamin Welton <ben@amd.com>

Testing updated RPM dockers (#1136)

* Testing updated RPM dockers

* Trying to fix PSDB for test package dependency

Agent Profiling Fixes for Broken/Improper API Usage (#1122)

Prevent's multiple setups of agent profiling on the same agent.

Fixes agent read context to only read agents that were setup.

Prevent copy of agent profiling internal data struct and reset
hsa_signal on move to prevent inadvertant delete.

Simplifying PR template (#1139)

Implementation of ompt interface to rocprofiler SDK. task_create and task_schedule are not supported.

Fixing installed pacakge tests in CI (#1119)

* Fixing installed pacakge tests in CI

* Formatted rocprofv3.py with black formatter

Fix PSDB change (#1120)

Reverts change to `source/include/rocprofiler-sdk/callback_tracing.h`
from commit 9b2ece76c3

delete unused files

added arguments to some OMPT buffter records

* Fix cmake issues

Remove rocprofiler_ompt_finalize_tool

- a public API function is not necessary: should just finalize rocprofiler-sdk

Fix duplicate ROCPROFILER_{BUFFER,CALLBACK}_TRACING_KIND_STRING

Add lib/rocprofiler-sdk/ompt.hpp

- declares rocprofiler::sdk::finalize_ompt

Remove change to tests/rocprofv3/summary/conftest.py

Add set_fini_status(1) back to registration.cpp

Deleted uneeded files

Incoporate OpenMP code and sample

Fix merge issues with amd-staging

Add push_correlation_id for OpenMP tasking; improve debugability

fixup bad merge

* Suppress OpenMP data race

* Fix openmp_target sample

* Enum and struct name changes + source code reorg

- remove mix of ompt and openmp
  - opted for ompt
- changes made for consistency
  - ompt_api -> ompt
  - openmp_api -> ompt
  - OPENMP -> OMPT

* Update tests and more renaming

- dest_device_num -> dst_device_num
- src_addr -> src_address
- dest_addr -> dst_address
- remove info_type::begin
- require OMP_TARGET_OFFLOAD

* Update openmp-target test/sample env and labels

* Formatting

* Tweaks to cmake for openmp target

- Disable for thread sanitizers due to preloading issue

* OpenMP target cmake updates

- remove gfx1010 (fails on mi300)
- OPENMP_GPU_TARGETS

* Remove device_unload and target_map_emi support

- these are never supported by AMD OpenMP compilers

* Update CI workflow

- exclude openmp-target tests from navi3 and vega20

---------

Co-authored-by: Larry Meadows <Lawrence.Meadows@amd.com>
Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com>
2024-12-05 22:48:19 -06:00

1163 lines
45 KiB
C++

// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// 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/or sell
// 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.
#include "lib/rocprofiler-sdk/ompt/ompt.hpp"
#include "lib/common/logging.hpp"
#include "lib/common/string_entry.hpp"
#include "lib/common/utility.hpp"
#include "lib/rocprofiler-sdk/context/correlation_id.hpp"
#include "lib/rocprofiler-sdk/tracing/fwd.hpp"
#include "lib/rocprofiler-sdk/tracing/tracing.hpp"
#include <rocprofiler-sdk/buffer.h>
#include <rocprofiler-sdk/buffer_tracing.h>
#include <rocprofiler-sdk/callback_tracing.h>
#include <rocprofiler-sdk/external_correlation.h>
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/ompt.h>
#include <rocprofiler-sdk/ompt/api_args.h>
#include <rocprofiler-sdk/ompt/omp-tools.h>
#include <glog/logging.h>
#include <cstddef>
#include <cstdint>
#include <unordered_map>
#include <utility>
namespace rocprofiler
{
namespace ompt
{
namespace
{
ompt_table&
get_table();
struct ompt_table_lookup
{
using type = ompt_table;
auto& operator()(type& _v) const { return _v; }
auto& operator()(type* _v) const { return *_v; }
auto& operator()() const { return (*this)(get_table()); }
};
} // namespace
} // namespace ompt
} // namespace rocprofiler
#define ROCPROFILER_LIB_ROCPROFILER_OMPT_OMPT_CPP_IMPL 1
#include "ompt.def.cpp"
#undef ROCPROFILER_LIB_ROCPROFILER_OMPT_OMPT_CPP_IMPL
namespace rocprofiler
{
namespace ompt
{
namespace
{
auto&
get_ompt_state_stack()
{
// for callbacks that don't have a place to stash context, we assume
// a per-thread stack. otherwise we stash the saved state in the ompt_data_t field.
static thread_local auto _v = tracing::small_vector_t<ompt_save_state*, 8>{};
return _v;
}
auto*
get_ompt_data_proxy()
{
static auto*& _v = common::static_object<ompt_data_proxy>::construct();
return _v;
}
// Macros for access to appropriate ompt_data_t* proxy
#define CLIENT(name) (CHECK_NOTNULL(get_ompt_data_proxy())->get_client_ptr(name))
#define INTERNAL(name) (CHECK_NOTNULL(get_ompt_data_proxy())->get_internal_ptr(name))
void
ompt_thread_begin_callback(ompt_thread_t thread_type, ompt_data_t* thread_data)
{
ompt_impl<ROCPROFILER_OMPT_ID_thread_begin>::event(thread_type, CLIENT(thread_data));
}
void
ompt_thread_end_callback(ompt_data_t* thread_data)
{
ompt_impl<ROCPROFILER_OMPT_ID_thread_end>::event(CLIENT(thread_data));
}
void
ompt_parallel_begin_callback(ompt_data_t* encountering_task_data,
const ompt_frame_t* encountering_task_frame,
ompt_data_t* parallel_data,
unsigned int requested_parallelism,
int flags,
const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_parallel_begin>::event(CLIENT(encountering_task_data),
encountering_task_frame,
CLIENT(parallel_data),
requested_parallelism,
flags,
codeptr_ra);
}
void
ompt_parallel_end_callback(ompt_data_t* parallel_data,
ompt_data_t* encountering_task_data,
int flags,
const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_parallel_end>::event(
CLIENT(parallel_data), CLIENT(encountering_task_data), flags, codeptr_ra);
}
void
ompt_task_create_callback(ompt_data_t* encountering_task_data,
const ompt_frame_t* encountering_task_frame,
ompt_data_t* new_task_data,
int flags,
int has_dependences,
const void* codeptr_ra)
{
auto* corr_id =
ompt_impl<ROCPROFILER_OMPT_ID_task_create>::event_common(CLIENT(encountering_task_data),
encountering_task_frame,
CLIENT(new_task_data),
flags,
has_dependences,
codeptr_ra);
auto* state = new ompt_task_save_state{corr_id, flags};
INTERNAL(new_task_data)->ptr = state;
context::pop_latest_correlation_id(corr_id);
}
void
ompt_task_schedule_callback(ompt_data_t* prior_task_data,
ompt_task_status_t prior_task_status,
ompt_data_t* next_task_data)
{
auto* corr_id = ompt_impl<ROCPROFILER_OMPT_ID_task_schedule>::event_common(
CLIENT(prior_task_data), prior_task_status, CLIENT(next_task_data));
context::pop_latest_correlation_id(corr_id);
corr_id->sub_ref_count();
auto* pprior = INTERNAL(prior_task_data);
auto* pnext = INTERNAL(next_task_data);
assert(pprior != nullptr);
auto* state_prior = reinterpret_cast<ompt_task_save_state*>(pprior->ptr);
auto* state_next = pnext ? reinterpret_cast<ompt_task_save_state*>(pnext->ptr) : nullptr;
auto* prior_corrid = context::get_latest_correlation_id();
if(state_prior->corr_id == prior_corrid && state_prior->task_flags != 0)
{
// pop the current correlation ID (for the prior_task)
assert((state_prior->task_flags & 0xFF) == ompt_task_explicit);
context::pop_latest_correlation_id(prior_corrid);
}
if(state_next && (state_next->task_flags & 0xFF) == ompt_task_explicit)
{
// push the next correlation ID (for the next_task)
context::push_correlation_id(state_next->corr_id);
}
if(prior_task_status == ompt_task_yield || prior_task_status == ompt_task_detach ||
prior_task_status == ompt_task_switch)
return;
// the prior task is done
assert(state_prior->task_flags != 0);
if(prior_task_status == ompt_task_complete)
{
// FIXME? do we need to decrement the ref count
// state_prior->corr_id->sub_ref_count();
delete state_prior;
pprior->ptr = nullptr;
}
}
void
ompt_implicit_task_callback(ompt_scope_endpoint_t endpoint,
ompt_data_t* parallel_data,
ompt_data_t* task_data,
unsigned int actual_parallelism,
unsigned int index,
int flags)
{
if(endpoint == ompt_scope_begin)
{
ompt_impl<ROCPROFILER_OMPT_ID_implicit_task>::begin(INTERNAL(task_data),
endpoint,
CLIENT(parallel_data),
CLIENT(task_data),
actual_parallelism,
index,
flags);
}
else if(endpoint == ompt_scope_end)
{
ompt_impl<ROCPROFILER_OMPT_ID_implicit_task>::end(INTERNAL(task_data),
endpoint,
CLIENT(parallel_data),
CLIENT(task_data),
actual_parallelism,
index,
flags);
}
else
{
ROCP_FATAL << "endpoint in implicit_task is not begin or end: " << endpoint;
}
}
void
ompt_device_initialize_callback(int device_num,
const char* type,
ompt_device_t* device,
ompt_function_lookup_t lookup,
const char* documentation)
{
ompt_impl<ROCPROFILER_OMPT_ID_device_initialize>::event(
device_num, type, device, lookup, documentation);
}
void
ompt_device_finalize_callback(int device_num)
{
ompt_impl<ROCPROFILER_OMPT_ID_device_finalize>::event(device_num);
}
void
ompt_device_load_callback(int device_num,
const char* filename,
int64_t offset_in_file,
void* vma_in_file,
size_t bytes,
void* host_addr,
void* device_addr,
uint64_t module_id)
{
ompt_impl<ROCPROFILER_OMPT_ID_device_load>::event(device_num,
filename,
offset_in_file,
vma_in_file,
bytes,
host_addr,
device_addr,
module_id);
}
// void
// ompt_device_unload_callback(int device_num, uint64_t module_id)
// {
// ompt_impl<ROCPROFILER_OMPT_ID_device_unload>::event(device_num, module_id);
// }
void
ompt_sync_region_wait_callback(ompt_sync_region_t kind,
ompt_scope_endpoint_t endpoint,
ompt_data_t* parallel_data,
ompt_data_t* task_data,
const void* codeptr_ra)
{
if(endpoint == ompt_scope_begin)
{
ompt_impl<ROCPROFILER_OMPT_ID_sync_region_wait>::begin(
nullptr, kind, endpoint, CLIENT(parallel_data), CLIENT(task_data), codeptr_ra);
}
else if(endpoint == ompt_scope_end)
{
ompt_impl<ROCPROFILER_OMPT_ID_sync_region_wait>::end(
nullptr, kind, endpoint, CLIENT(parallel_data), CLIENT(task_data), codeptr_ra);
}
else
{
ROCP_FATAL << "endpoint in sync_region_wait is not begin or end: " << endpoint;
}
}
void
ompt_mutex_released_callback(ompt_mutex_t kind, ompt_wait_id_t wait_id, const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_mutex_released>::event(kind, wait_id, codeptr_ra);
}
void
ompt_dependences_callback(ompt_data_t* task_data, const ompt_dependence_t* deps, int ndeps)
{
ompt_impl<ROCPROFILER_OMPT_ID_dependences>::event(CLIENT(task_data), deps, ndeps);
}
void
ompt_task_dependence_callback(ompt_data_t* src_task_data, ompt_data_t* sink_task_data)
{
ompt_impl<ROCPROFILER_OMPT_ID_task_dependence>::event(CLIENT(src_task_data),
CLIENT(sink_task_data));
}
void
ompt_work_callback(ompt_work_t work_type,
ompt_scope_endpoint_t endpoint,
ompt_data_t* parallel_data,
ompt_data_t* task_data,
uint64_t count,
const void* codeptr_ra)
{
if(endpoint == ompt_scope_begin)
{
ompt_impl<ROCPROFILER_OMPT_ID_work>::begin(nullptr,
work_type,
endpoint,
CLIENT(parallel_data),
CLIENT(task_data),
count,
codeptr_ra);
}
else if(endpoint == ompt_scope_end)
{
ompt_impl<ROCPROFILER_OMPT_ID_work>::end(nullptr,
work_type,
endpoint,
CLIENT(parallel_data),
CLIENT(task_data),
count,
codeptr_ra);
}
else
{
ROCP_FATAL << "endpoint in work is not begin or end: " << endpoint;
}
}
void
ompt_masked_callback(ompt_scope_endpoint_t endpoint,
ompt_data_t* parallel_data,
ompt_data_t* task_data,
const void* codeptr_ra)
{
if(endpoint == ompt_scope_begin)
{
ompt_impl<ROCPROFILER_OMPT_ID_masked>::begin(
nullptr, endpoint, CLIENT(parallel_data), CLIENT(task_data), codeptr_ra);
}
else if(endpoint == ompt_scope_end)
{
ompt_impl<ROCPROFILER_OMPT_ID_masked>::end(
nullptr, endpoint, CLIENT(parallel_data), CLIENT(task_data), codeptr_ra);
}
else
{
ROCP_FATAL << "endpoint in masked is not begin or end: " << endpoint;
}
}
void
ompt_target_map_callback(ompt_id_t target_id,
unsigned int nitems,
void** host_addr,
void** device_addr,
size_t* bytes,
unsigned int* mapping_flags,
const void* codeptr_ra)
{
common::consume_args(
target_id, nitems, host_addr, device_addr, bytes, mapping_flags, codeptr_ra);
}
void
ompt_sync_region_callback(ompt_sync_region_t kind,
ompt_scope_endpoint_t endpoint,
ompt_data_t* parallel_data,
ompt_data_t* task_data,
const void* codeptr_ra)
{
if(endpoint == ompt_scope_begin)
{
ompt_impl<ROCPROFILER_OMPT_ID_sync_region>::begin(
nullptr, kind, endpoint, CLIENT(parallel_data), CLIENT(task_data), codeptr_ra);
}
else if(endpoint == ompt_scope_end)
{
ompt_impl<ROCPROFILER_OMPT_ID_sync_region>::end(
nullptr, kind, endpoint, CLIENT(parallel_data), CLIENT(task_data), codeptr_ra);
}
else
{
ROCP_FATAL << "endpoint in sync_region is not begin or end: " << endpoint;
}
}
void
ompt_lock_init_callback(ompt_mutex_t kind,
unsigned int hint,
unsigned int impl,
ompt_wait_id_t wait_id,
const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_lock_init>::event(kind, hint, impl, wait_id, codeptr_ra);
}
void
ompt_lock_destroy_callback(ompt_mutex_t kind, ompt_wait_id_t wait_id, const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_lock_destroy>::event(kind, wait_id, codeptr_ra);
}
void
ompt_mutex_acquire_callback(ompt_mutex_t kind,
unsigned int hint,
unsigned int impl,
ompt_wait_id_t wait_id,
const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_mutex_acquire>::event(kind, hint, impl, wait_id, codeptr_ra);
}
void
ompt_mutex_acquired_callback(ompt_mutex_t kind, ompt_wait_id_t wait_id, const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_mutex_acquired>::event(kind, wait_id, codeptr_ra);
}
void
ompt_nest_lock_callback(ompt_scope_endpoint_t endpoint,
ompt_wait_id_t wait_id,
const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_nest_lock>::event(endpoint, wait_id, codeptr_ra);
}
void
ompt_flush_callback(ompt_data_t* thread_data, const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_flush>::event(CLIENT(thread_data), codeptr_ra);
}
void
ompt_cancel_callback(ompt_data_t* task_data, int flags, const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_cancel>::event(CLIENT(task_data), flags, codeptr_ra);
}
void
ompt_reduction_callback(ompt_sync_region_t kind,
ompt_scope_endpoint_t endpoint,
ompt_data_t* parallel_data,
ompt_data_t* task_data,
const void* codeptr_ra)
{
if(endpoint == ompt_scope_begin)
{
ompt_impl<ROCPROFILER_OMPT_ID_reduction>::begin(
nullptr, kind, endpoint, CLIENT(parallel_data), CLIENT(task_data), codeptr_ra);
}
else if(endpoint == ompt_scope_end)
{
ompt_impl<ROCPROFILER_OMPT_ID_reduction>::end(
nullptr, kind, endpoint, CLIENT(parallel_data), CLIENT(task_data), codeptr_ra);
}
else
{
ROCP_FATAL << "endpoint in reduction is not begin or end: " << endpoint;
}
}
void
ompt_dispatch_callback(ompt_data_t* parallel_data,
ompt_data_t* task_data,
ompt_dispatch_t kind,
ompt_data_t instance)
{
ompt_impl<ROCPROFILER_OMPT_ID_dispatch>::event(
CLIENT(parallel_data), CLIENT(task_data), kind, instance);
}
void
ompt_target_emi_callback(ompt_target_t kind,
ompt_scope_endpoint_t endpoint,
int device_num,
ompt_data_t* task_data,
ompt_data_t* target_task_data,
ompt_data_t* target_data,
const void* codeptr_ra)
{
if(endpoint == ompt_scope_begin)
{
ompt_impl<ROCPROFILER_OMPT_ID_target_emi>::begin(INTERNAL(target_data),
kind,
endpoint,
device_num,
CLIENT(task_data),
CLIENT(target_task_data),
CLIENT(target_data),
codeptr_ra);
}
else if(endpoint == ompt_scope_end)
{
ompt_impl<ROCPROFILER_OMPT_ID_target_emi>::end(INTERNAL(target_data),
kind,
endpoint,
device_num,
CLIENT(task_data),
CLIENT(target_task_data),
CLIENT(target_data),
codeptr_ra);
}
else
{
ROCP_FATAL << "endpoint in target_emi is not begin or end: " << endpoint;
}
}
void
ompt_target_data_op_emi_callback(ompt_scope_endpoint_t endpoint,
ompt_data_t* target_task_data,
ompt_data_t* target_data,
ompt_id_t* host_op_id,
ompt_target_data_op_t optype,
void* src_address,
int src_device_num,
void* dst_address,
int dst_device_num,
size_t bytes,
const void* codeptr_ra)
{
auto* _host_op_data = reinterpret_cast<ompt_data_t*>(host_op_id);
if(endpoint == ompt_scope_begin)
{
ompt_impl<ROCPROFILER_OMPT_ID_target_data_op_emi>::begin(INTERNAL(_host_op_data),
endpoint,
CLIENT(target_task_data),
CLIENT(target_data),
CLIENT(_host_op_data),
optype,
src_address,
src_device_num,
dst_address,
dst_device_num,
bytes,
codeptr_ra);
}
else if(endpoint == ompt_scope_end)
{
ompt_impl<ROCPROFILER_OMPT_ID_target_data_op_emi>::end(INTERNAL(_host_op_data),
endpoint,
CLIENT(target_task_data),
CLIENT(target_data),
CLIENT(_host_op_data),
optype,
src_address,
src_device_num,
dst_address,
dst_device_num,
bytes,
codeptr_ra);
}
else
{
ROCP_FATAL << "endpoint in target_data_op_emi is not begin or end: " << endpoint;
}
}
void
ompt_target_submit_emi_callback(ompt_scope_endpoint_t endpoint,
ompt_data_t* target_data,
ompt_id_t* host_op_id,
unsigned int requested_num_teams)
{
auto* _host_op_data = reinterpret_cast<ompt_data_t*>(host_op_id);
if(endpoint == ompt_scope_begin)
{
ompt_impl<ROCPROFILER_OMPT_ID_target_submit_emi>::begin(INTERNAL(_host_op_data),
endpoint,
CLIENT(target_data),
CLIENT(_host_op_data),
requested_num_teams);
}
else if(endpoint == ompt_scope_end)
{
ompt_impl<ROCPROFILER_OMPT_ID_target_submit_emi>::end(INTERNAL(_host_op_data),
endpoint,
CLIENT(target_data),
CLIENT(_host_op_data),
requested_num_teams);
}
else
{
ROCP_FATAL << "endpoint in target_submit_emi is not begin or end: " << endpoint;
}
(void) target_data;
}
// void
// ompt_target_map_emi_callback(ompt_data_t* target_data,
// unsigned int nitems,
// void** host_addr,
// void** device_addr,
// size_t* bytes,
// unsigned int* mapping_flags,
// const void* codeptr_ra)
// {
// common::consume_args(
// target_data, nitems, host_addr, device_addr, bytes, mapping_flags, codeptr_ra);
// }
void
ompt_error_callback(ompt_severity_t severity,
const char* message,
size_t length,
const void* codeptr_ra)
{
ompt_impl<ROCPROFILER_OMPT_ID_error>::event(severity, message, length, codeptr_ra);
}
#undef CLIENT
#undef INTERNAL
// The ompt callback table
ompt_table ompt_callback_table = {
ompt_thread_begin_callback,
ompt_thread_end_callback,
ompt_parallel_begin_callback,
ompt_parallel_end_callback,
ompt_task_create_callback,
ompt_task_schedule_callback,
ompt_implicit_task_callback,
ompt_device_initialize_callback,
ompt_device_finalize_callback,
ompt_device_load_callback,
// ompt_device_unload_callback,
ompt_sync_region_wait_callback,
ompt_mutex_released_callback,
ompt_dependences_callback,
ompt_task_dependence_callback,
ompt_work_callback,
ompt_masked_callback,
ompt_target_map_callback,
ompt_sync_region_callback,
ompt_lock_init_callback,
ompt_lock_destroy_callback,
ompt_mutex_acquire_callback,
ompt_mutex_acquired_callback,
ompt_nest_lock_callback,
ompt_flush_callback,
ompt_cancel_callback,
ompt_reduction_callback,
ompt_dispatch_callback,
ompt_target_emi_callback,
ompt_target_data_op_emi_callback,
ompt_target_submit_emi_callback,
// ompt_target_map_emi_callback,
ompt_error_callback,
};
ompt_table&
get_table()
{
return ompt_callback_table;
}
void
rocprof_ompt_cb_interface(rocprofiler_ompt_callback_functions_t& cb_functions)
{
ompt_impl<ROCPROFILER_OMPT_ID_callback_functions>::event(cb_functions);
}
} // namespace
ompt_data_t*
proxy_data_ptr(ompt_data_t* realptr)
{
return (get_ompt_data_proxy())->get_client_ptr(realptr);
}
// special case fake callback to send the ompt cb function pointers
template <>
struct ompt_info<ROCPROFILER_OMPT_ID_callback_functions>
{
static constexpr auto callback_domain_idx = ompt_domain_info::callback_domain_idx;
static constexpr auto buffered_domain_idx = ompt_domain_info::buffered_domain_idx;
static constexpr auto operation_idx = ROCPROFILER_OMPT_ID_callback_functions;
static constexpr auto name = "omp_callback_functions";
static constexpr bool unsupported = false;
static constexpr auto begin = -1;
using this_type = ompt_info<ROCPROFILER_OMPT_ID_callback_functions>;
using base_type = ompt_impl<ROCPROFILER_OMPT_ID_callback_functions>;
static constexpr auto offset() { return -1; }
template <typename DataT>
static auto& get_api_data_args(DataT& _data)
{
return _data.callback_functions;
}
};
// These implement the callbacks for OMPT
template <size_t OpIdx>
template <typename... Args>
void
ompt_impl<OpIdx>::begin(ompt_data_t* data, Args... args)
{
using info_type = ompt_info<OpIdx>;
ROCP_TRACE << __FUNCTION__ << " :: " << info_type::name;
constexpr auto external_corr_id_domain_idx =
ompt_domain_info::external_correlation_id_domain_idx;
constexpr auto ref_count = 2;
auto thr_id = common::get_tid();
auto callback_contexts = tracing::callback_context_data_vec_t{};
auto buffered_contexts = tracing::buffered_context_data_vec_t{};
auto external_corr_ids = tracing::external_correlation_id_map_t{};
tracing::populate_contexts(info_type::callback_domain_idx,
info_type::buffered_domain_idx,
info_type::operation_idx,
callback_contexts,
buffered_contexts,
external_corr_ids);
auto* corr_id = tracing::correlation_service::construct(ref_count);
auto internal_corr_id = corr_id->internal;
tracing::populate_external_correlation_ids(external_corr_ids,
thr_id,
external_corr_id_domain_idx,
info_type::operation_idx,
internal_corr_id);
// invoke the callbacks
if(!callback_contexts.empty())
{
auto tracer_data = common::init_public_api_struct(callback_ompt_data_t{});
set_data_args(info_type::get_api_data_args(tracer_data.args), std::forward<Args>(args)...);
tracing::execute_phase_enter_callbacks(callback_contexts,
thr_id,
internal_corr_id,
external_corr_ids,
info_type::callback_domain_idx,
info_type::operation_idx,
tracer_data);
}
// enter callback may update the external correlation id field
tracing::update_external_correlation_ids(
external_corr_ids, thr_id, external_corr_id_domain_idx);
// stash the state
ompt_save_state* state = new ompt_save_state{.thr_id = thr_id,
.start_timestamp = 0,
.operation_idx = info_type::operation_idx,
.corr_id = corr_id,
.external_corr_ids = external_corr_ids,
.callback_contexts = callback_contexts,
.buffered_contexts = buffered_contexts};
if(data)
data->ptr = state;
else
get_ompt_state_stack().emplace_back(state);
// decrement the reference count before returning
corr_id->sub_ref_count();
state->start_timestamp = common::timestamp_ns();
}
template <size_t OpIdx>
template <typename... Args>
void
ompt_impl<OpIdx>::end(ompt_data_t* data, Args... args)
{
using info_type = ompt_info<OpIdx>;
ROCP_TRACE << __FUNCTION__ << " :: " << info_type::name;
// END PART OF OMPT CALLBACK
auto end_timestamp = common::timestamp_ns();
ompt_save_state* state = nullptr;
if(data != nullptr)
state = static_cast<ompt_save_state*>(data->ptr);
else
state = get_ompt_state_stack().pop_back_val();
assert(state != nullptr);
ROCP_FATAL_IF(state->operation_idx != info_type::operation_idx)
<< "Mismatch of OMPT operation: begin=" << state->operation_idx
<< ", end=" << info_type::operation_idx;
auto& callback_contexts = state->callback_contexts;
auto& buffered_contexts = state->buffered_contexts;
auto external_corr_ids = state->external_corr_ids;
auto* corr_id = state->corr_id;
auto internal_corr_id = corr_id->internal;
ROCP_FATAL_IF(common::get_tid() != state->thr_id)
<< "MIsmatch of OMPT begin/end thread id: "
<< " current=" << common::get_tid() << ", expected= " << state->thr_id;
if(!callback_contexts.empty())
{
auto tracer_data = common::init_public_api_struct(callback_ompt_data_t{});
set_data_args(info_type::get_api_data_args(tracer_data.args), std::forward<Args>(args)...);
tracing::execute_phase_exit_callbacks(callback_contexts,
external_corr_ids,
info_type::callback_domain_idx,
info_type::operation_idx,
tracer_data);
}
if(!buffered_contexts.empty())
{
auto buffer_record = common::init_public_api_struct(buffer_ompt_record_t{});
if constexpr(OpIdx == ROCPROFILER_OMPT_ID_target_emi ||
OpIdx == ROCPROFILER_OMPT_ID_target_data_op_emi ||
OpIdx == ROCPROFILER_OMPT_ID_target_submit_emi)
{
auto tracer_data = common::init_public_api_struct(callback_ompt_data_t{});
set_data_args(info_type::get_api_data_args(tracer_data.args),
std::forward<Args>(args)...);
if constexpr(OpIdx == ROCPROFILER_OMPT_ID_target_emi)
{
buffer_record.target.kind = tracer_data.args.target_emi.kind;
buffer_record.target.device_num = tracer_data.args.target_emi.device_num;
buffer_record.target.task_id = tracer_data.args.target_emi.task_data->value;
buffer_record.target.target_id = tracer_data.args.target_emi.target_data->value;
buffer_record.target.codeptr_ra = tracer_data.args.target_emi.codeptr_ra;
}
else if constexpr(OpIdx == ROCPROFILER_OMPT_ID_target_data_op_emi)
{
buffer_record.target_data_op.host_op_id =
tracer_data.args.target_data_op_emi.host_op_id->value;
buffer_record.target_data_op.optype = tracer_data.args.target_data_op_emi.optype;
buffer_record.target_data_op.src_device_num =
tracer_data.args.target_data_op_emi.src_device_num;
buffer_record.target_data_op.dst_device_num =
tracer_data.args.target_data_op_emi.dst_device_num;
buffer_record.target_data_op.reserved = 0;
buffer_record.target_data_op.bytes = tracer_data.args.target_data_op_emi.bytes;
buffer_record.target_data_op.codeptr_ra =
tracer_data.args.target_data_op_emi.codeptr_ra;
}
else if constexpr(OpIdx == ROCPROFILER_OMPT_ID_target_submit_emi)
{
buffer_record.target_kernel.device_num = 0; // FIXME
buffer_record.target_kernel.requested_num_teams =
tracer_data.args.target_submit_emi.requested_num_teams;
buffer_record.target_kernel.host_op_id =
tracer_data.args.target_submit_emi.host_op_id->value;
}
}
buffer_record.start_timestamp = state->start_timestamp;
buffer_record.end_timestamp = end_timestamp;
tracing::execute_buffer_record_emplace(buffered_contexts,
state->thr_id,
internal_corr_id,
external_corr_ids,
info_type::buffered_domain_idx,
info_type::operation_idx,
buffer_record);
}
// decrement the reference count after usage in the callback/buffers
state->corr_id->sub_ref_count();
context::pop_latest_correlation_id(state->corr_id);
delete state;
if(data) data->ptr = nullptr;
}
template <size_t OpIdx>
template <typename... Args>
context::correlation_id*
ompt_impl<OpIdx>::event_common(Args... args)
{
using info_type = ompt_info<OpIdx>;
ROCP_TRACE << __FUNCTION__ << " :: " << info_type::name;
constexpr auto external_corr_id_domain_idx =
ompt_domain_info::external_correlation_id_domain_idx;
constexpr auto ref_count = 1;
auto thr_id = common::get_tid();
auto callback_contexts = tracing::callback_context_data_vec_t{};
auto buffered_contexts = tracing::buffered_context_data_vec_t{};
auto external_corr_ids = tracing::external_correlation_id_map_t{};
tracing::populate_contexts(info_type::callback_domain_idx,
info_type::buffered_domain_idx,
info_type::operation_idx,
callback_contexts,
buffered_contexts,
external_corr_ids);
auto buffer_record = common::init_public_api_struct(buffer_ompt_record_t{});
auto tracer_data = common::init_public_api_struct(callback_ompt_data_t{});
auto* corr_id = tracing::correlation_service::construct(ref_count);
uint64_t internal_corr_id = corr_id->internal;
tracing::populate_external_correlation_ids(external_corr_ids,
thr_id,
external_corr_id_domain_idx,
info_type::operation_idx,
internal_corr_id);
// invoke the callbacks
if(!callback_contexts.empty())
{
set_data_args(info_type::get_api_data_args(tracer_data.args), std::forward<Args>(args)...);
tracing::execute_phase_none_callbacks(callback_contexts,
thr_id,
internal_corr_id,
external_corr_ids,
info_type::callback_domain_idx,
info_type::operation_idx,
tracer_data);
}
tracing::update_external_correlation_ids(
external_corr_ids, thr_id, external_corr_id_domain_idx);
if(!buffered_contexts.empty())
{
buffer_record.start_timestamp = common::timestamp_ns();
buffer_record.end_timestamp = buffer_record.start_timestamp;
tracing::execute_buffer_record_emplace(buffered_contexts,
thr_id,
internal_corr_id,
external_corr_ids,
info_type::buffered_domain_idx,
info_type::operation_idx,
buffer_record);
}
return corr_id;
}
template <size_t OpIdx>
template <typename... Args>
void
ompt_impl<OpIdx>::event(Args&&... args)
{
auto corr_id = ompt_impl<OpIdx>::event_common(std::forward<Args>(args)...);
context::pop_latest_correlation_id(corr_id);
corr_id->sub_ref_count();
}
namespace
{
template <typename Tp>
decltype(auto)
convert_arg(Tp&& _arg)
{
using type = common::mpl::unqualified_type_t<Tp>;
if constexpr(common::mpl::is_string_type<type>::value)
{
if(!_arg) return std::remove_reference_t<Tp>(_arg);
return common::get_string_entry(std::string_view{_arg})->c_str();
}
else
return std::forward<Tp>(_arg);
}
template <size_t OpIdx, size_t... OpIdxTail>
void
get_ids(std::vector<uint32_t>& _id_list, std::index_sequence<OpIdx, OpIdxTail...>)
{
auto _idx = ompt_info<OpIdx>::operation_idx;
if(_idx < ompt_domain_info::last) _id_list.emplace_back(_idx);
if constexpr(sizeof...(OpIdxTail) > 0) get_ids(_id_list, std::index_sequence<OpIdxTail...>{});
}
template <size_t OpIdx, size_t... OpIdxTail>
const char*
name_by_id(const uint32_t id, std::index_sequence<OpIdx, OpIdxTail...>)
{
if(OpIdx == id) return ompt_info<OpIdx>::name;
if constexpr(sizeof...(OpIdxTail) > 0)
return name_by_id(id, std::index_sequence<OpIdxTail...>{});
else
return nullptr;
}
bool
should_enable_callback(rocprofiler_callback_tracing_kind_t _callback_domain,
rocprofiler_buffer_tracing_kind_t _buffered_domain,
int _operation)
{
// we loop over all the *registered* contexts and see if any of them, at any point in time,
// might require callback or buffered API tracing
for(const auto& itr : context::get_registered_contexts())
{
if(!itr) continue;
// if there is a callback tracer enabled for the given domain and op, we need to wrap
if(itr->callback_tracer && itr->callback_tracer->domains(_callback_domain) &&
itr->callback_tracer->domains(_callback_domain, _operation))
return true;
// if there is a buffered tracer enabled for the given domain and op, we need to wrap
if(itr->buffered_tracer && itr->buffered_tracer->domains(_buffered_domain) &&
itr->buffered_tracer->domains(_buffered_domain, _operation))
return true;
}
return false;
}
template <size_t OpIdx>
void
update_table(ompt_update_func f, std::integral_constant<size_t, OpIdx>)
{
auto _info = ompt_info<OpIdx>();
if(_info.unsupported)
{
ROCP_INFO << "OMPT operation not supported: " << _info.name;
return;
}
// check to see if there are any contexts which enable this operation in the OMPT API domain
if(!should_enable_callback(
_info.callback_domain_idx, _info.buffered_domain_idx, _info.operation_idx))
return;
ROCP_TRACE << "updating table entry for " << _info.name;
// Register this callback for OMPT at init time.
auto& _func = _info.get_table_func();
auto* _ompt_cb = reinterpret_cast<ompt_callback_t*>(&_func);
f(_info.name, _ompt_cb, _info.ompt_idx);
}
template <size_t OpIdx, size_t... OpIdxTail>
void
update_table(ompt_update_func f, std::index_sequence<OpIdx, OpIdxTail...>)
{
update_table(f, std::integral_constant<size_t, OpIdx>{});
if constexpr(sizeof...(OpIdxTail) > 0) update_table(f, std::index_sequence<OpIdxTail...>{});
}
} // namespace
template <size_t OpIdx>
template <typename DataArgsT, typename... Args>
void
ompt_impl<OpIdx>::set_data_args(DataArgsT& _data_args, Args... args)
{
if constexpr(sizeof...(Args) == 0)
_data_args.no_args.empty = '\0';
else
_data_args = DataArgsT{convert_arg(args)...};
}
// check out the assembly here... this compiles to a switch statement
const char*
name_by_id(uint32_t id)
{
return name_by_id(id, std::make_index_sequence<ompt_domain_info::last>{});
}
std::vector<uint32_t>
get_ids()
{
constexpr auto last_api_id = ompt_domain_info::last;
auto _data = std::vector<uint32_t>{};
_data.reserve(last_api_id);
get_ids(_data, std::make_index_sequence<last_api_id>{});
return _data;
}
template <typename DataT, size_t OpIdx, size_t... OpIdxTail>
void
iterate_args(const uint32_t id,
const DataT& data,
rocprofiler_callback_tracing_operation_args_cb_t func,
int32_t max_deref,
void* user_data,
std::index_sequence<OpIdx, OpIdxTail...>)
{
if(OpIdx == id)
{
using info_type = ompt_info<OpIdx>;
auto&& arg_list = info_type::as_arg_list(data, max_deref);
auto&& arg_addr = info_type::as_arg_addr(data);
for(size_t i = 0; i < std::min(arg_list.size(), arg_addr.size()); ++i)
{
auto ret = func(info_type::callback_domain_idx, // kind
id, // operation
i, // arg_number
arg_addr.at(i), // arg_value_addr
arg_list.at(i).indirection_level, // indirection
arg_list.at(i).type, // arg_type
arg_list.at(i).name, // arg_name
arg_list.at(i).value.c_str(), // arg_value_str
arg_list.at(i).dereference_count, // num deref in str
user_data);
if(ret != 0) break;
}
return;
}
if constexpr(sizeof...(OpIdxTail) > 0)
iterate_args(id, data, func, max_deref, user_data, std::index_sequence<OpIdxTail...>{});
}
void
update_callback(rocprofiler_ompt_callback_functions_t& cb_functions)
{
auto _info = ompt_info<ROCPROFILER_OMPT_ID_callback_functions>();
if(should_enable_callback(
_info.callback_domain_idx, _info.buffered_domain_idx, _info.operation_idx))
rocprof_ompt_cb_interface(cb_functions);
}
void
iterate_args(uint32_t id,
const rocprofiler_callback_tracing_ompt_data_t& data,
rocprofiler_callback_tracing_operation_args_cb_t callback,
int32_t max_deref,
void* user_data)
{
if(callback)
iterate_args(id,
data,
callback,
max_deref,
user_data,
std::make_index_sequence<ompt::ompt_domain_info::ompt_last>{});
}
void
update_table(ompt_update_func f)
{
update_table(f, std::make_index_sequence<ompt::ompt_domain_info::ompt_last>{});
}
} // namespace ompt
} // namespace rocprofiler