Skip to content

Commit ef74e7c

Browse files
Initial changes for XDP runlist
Signed-off-by: Rahul Bramandlapalli <rbramand@amd.com>
1 parent 3dd9f86 commit ef74e7c

File tree

3 files changed

+150
-34
lines changed

3 files changed

+150
-34
lines changed

src/runtime_src/core/common/api/hw_context_int.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// This file defines implementation extensions to the XRT XCLBIN APIs.
99
#include "core/include/xrt/xrt_hw_context.h"
1010
#include "core/include/xrt/experimental/xrt_module.h"
11+
#include "core/include/xrt/xrt_kernel.h"
1112

1213
#include <cstdint>
1314

@@ -70,6 +71,23 @@ get_scratchpad_mem_buf(const xrt::hw_context& hwctx, size_t size_per_col);
7071
void
7172
dump_scratchpad_mem(const xrt::hw_context& hwctx);
7273

74+
// APIs used by XDP to register init and exit runs
75+
// These runs will be used to initialize AI array and collect profile/trace data
76+
// These runs are added to runlist at the beginning and end respectively
77+
XRT_CORE_COMMON_EXPORT
78+
void
79+
register_xdp_init_run(const xrt::hw_context& ctx, const xrt::run& run);
80+
81+
XRT_CORE_COMMON_EXPORT
82+
void
83+
register_xdp_exit_run(const xrt::hw_context& ctx, const xrt::run& run);
84+
85+
const std::vector<xrt::run>&
86+
get_xdp_init_runs(const xrt::hw_context& ctx);
87+
88+
const std::vector<xrt::run>&
89+
get_xdp_exit_runs(const xrt::hw_context& ctx);
90+
7391
}} // hw_context_int, xrt_core
7492

7593
#endif

src/runtime_src/core/common/api/xrt_hw_context.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "core/common/time.h"
1313
#include "core/common/utils.h"
1414
#include "core/include/xrt/xrt_hw_context.h"
15+
#include "core/include/xrt/xrt_kernel.h"
1516
#include "core/include/xrt/experimental/xrt_ext.h"
1617
#include "core/include/xrt/experimental/xrt_module.h"
1718
#include "bo_int.h"
@@ -152,6 +153,11 @@ class hw_context_impl : public std::enable_shared_from_this<hw_context_impl>
152153
// are saved to a scratchpad memory allocated specifically for that context.
153154
std::once_flag m_scratchpad_init_flag; // used for thread safe lazy init of scratchpad
154155
xrt::bo m_scratchpad_buf;
156+
// Vector of XDP runs to be added at beginning and end of runlist
157+
// These runs initializes/configures AI array and collects the profile/trace data
158+
std::vector<xrt::run> m_xdp_init_runs;
159+
std::vector<xrt::run> m_xdp_exit_runs;
160+
155161
std::shared_ptr<xrt_core::usage_metrics::base_logger> m_usage_logger =
156162
xrt_core::usage_metrics::get_usage_metrics_logger();
157163
bool m_elf_flow = false;
@@ -465,6 +471,33 @@ class hw_context_impl : public std::enable_shared_from_this<hw_context_impl>
465471
msg.append(dump_file_name);
466472
xrt_core::message::send(xrt_core::message::severity_level::debug, "xrt_hw_context", msg);
467473
}
474+
475+
// Callback Functions for XDP to register init and exit runs
476+
// that are added to xrt::runlist at start and end respectively
477+
void
478+
register_xdp_init_run(const xrt::run& run)
479+
{
480+
m_xdp_init_runs.push_back(run);
481+
}
482+
483+
void
484+
register_xdp_exit_run(const xrt::run& run)
485+
{
486+
m_xdp_exit_runs.push_back(run);
487+
}
488+
489+
// Functions to get XDP init and exit run vectors
490+
const std::vector<xrt::run>&
491+
get_xdp_init_runs() const
492+
{
493+
return m_xdp_init_runs;
494+
}
495+
496+
const std::vector<xrt::run>&
497+
get_xdp_exit_runs() const
498+
{
499+
return m_xdp_exit_runs;
500+
}
468501
};
469502

470503
} // xrt
@@ -532,6 +565,31 @@ dump_scratchpad_mem(const xrt::hw_context& hwctx)
532565
return hwctx.get_handle()->dump_scratchpad_mem();
533566
}
534567

568+
void
569+
register_xdp_init_run(const xrt::hw_context& ctx, const xrt::run& run)
570+
{
571+
ctx.get_handle()->register_xdp_init_run(run);
572+
}
573+
574+
XRT_CORE_COMMON_EXPORT
575+
void
576+
register_xdp_exit_run(const xrt::hw_context& ctx, const xrt::run& run)
577+
{
578+
ctx.get_handle()->register_xdp_exit_run(run);
579+
}
580+
581+
const std::vector<xrt::run>&
582+
get_xdp_init_runs(const xrt::hw_context& ctx)
583+
{
584+
return ctx.get_handle()->get_xdp_init_runs();
585+
}
586+
587+
const std::vector<xrt::run>&
588+
get_xdp_exit_runs(const xrt::hw_context& ctx)
589+
{
590+
return ctx.get_handle()->get_xdp_exit_runs();
591+
}
592+
535593
} // xrt_core::hw_context_int
536594

537595
////////////////////////////////////////////////////////////////

src/runtime_src/core/common/api/xrt_kernel.cpp

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,46 @@ class runlist_impl
33503350
}
33513351
}
33523352

3353+
void
3354+
add_run_helper(xrt::run run)
3355+
{
3356+
// Get the potentially throwing action out of the way first
3357+
auto runidx = m_runlist.size();
3358+
m_runlist.reserve(runidx + 1);
3359+
m_bos.reserve(runidx + 1);
3360+
3361+
auto execbuf = get_cmd_chain_for_run_at_index(runidx);
3362+
auto [cmd, pkt] = unpack(execbuf);
3363+
auto chain_data = get_ert_cmd_chain_data(pkt);
3364+
3365+
auto run_impl = run.get_handle();
3366+
auto run_cmd = run_impl->get_cmd();
3367+
auto run_bo = run_cmd->get_exec_bo();
3368+
auto run_bo_props = run_bo->get_properties();
3369+
3370+
auto data_idx = chain_data->command_count;
3371+
chain_data->data[data_idx] = run_bo_props.kmhdl;
3372+
3373+
// Let shim handle binding of run_bo arguments to the command
3374+
// that cahins the run_bo. This allows pinning if necessary.
3375+
// May throw, but so far no state change, so still safe.
3376+
cmd->bind_at(data_idx, run_bo, 0, run_bo_props.size);
3377+
3378+
// Once a run object is added to a list it will be in a state that
3379+
// makes it impossible to add to another list or to same list
3380+
// twice. This state is managed by the run object itself by
3381+
// recording this runlist with the run object, but it doesn't
3382+
// proctect against caller manually controlling the run object,
3383+
// which is undefined behavior. No exceptions after this point.
3384+
run_impl->set_runlist(this); // throws or changes state of run
3385+
3386+
// Non throwing state change
3387+
chain_data->command_count++;
3388+
pkt->count += sizeof(uint64_t) / word_size; // account for added command
3389+
m_runlist.push_back(std::move(run)); // move of shared_ptr is noexcept
3390+
m_bos.push_back(run_bo); // ptr noexcept
3391+
}
3392+
33533393
// Wait for runlist to complete, then check each chained command
33543394
// submitted to determine potential error within chunk. Locate the
33553395
// first failing command if any and mark all subsequent commands as
@@ -3454,41 +3494,19 @@ class runlist_impl
34543494
if (m_state != state::idle)
34553495
throw xrt_core::error("runlist must be idle before adding run objects, current state: " + state_to_string(m_state));
34563496

3457-
// Get the potentially throwing action out of the way first
3458-
auto runidx = m_runlist.size();
3459-
m_runlist.reserve(runidx + 1);
3460-
m_bos.reserve(runidx + 1);
3461-
3462-
auto execbuf = get_cmd_chain_for_run_at_index(runidx);
3463-
auto [cmd, pkt] = unpack(execbuf);
3464-
auto chain_data = get_ert_cmd_chain_data(pkt);
3465-
3466-
auto run_impl = run.get_handle();
3467-
auto run_cmd = run_impl->get_cmd();
3468-
auto run_bo = run_cmd->get_exec_bo();
3469-
auto run_bo_props = run_bo->get_properties();
3470-
3471-
auto data_idx = chain_data->command_count;
3472-
chain_data->data[data_idx] = run_bo_props.kmhdl;
3473-
3474-
// Let shim handle binding of run_bo arguments to the command
3475-
// that cahins the run_bo. This allows pinning if necessary.
3476-
// May throw, but so far no state change, so still safe.
3477-
cmd->bind_at(data_idx, run_bo, 0, run_bo_props.size);
3478-
3479-
// Once a run object is added to a list it will be in a state that
3480-
// makes it impossible to add to another list or to same list
3481-
// twice. This state is managed by the run object itself by
3482-
// recording this runlist with the run object, but it doesn't
3483-
// proctect against caller manually controlling the run object,
3484-
// which is undefined behavior. No exceptions after this point.
3485-
run_impl->set_runlist(this); // throws or changes state of run
3497+
// Get XDP init runs registered with the hwctx and add to runlist
3498+
// if not already added.
3499+
// These runs initializes AI array for profile/trace data
3500+
// The list can be empty if profile/trace is not enabled
3501+
if (m_runlist.empty()) {
3502+
const auto& xdp_init_runs = xrt_core::hw_context_int::get_xdp_init_runs(m_hwctx);
3503+
for (const auto& init_run : xdp_init_runs) {
3504+
add_run_helper(init_run);
3505+
}
3506+
}
34863507

3487-
// Non throwing state change
3488-
chain_data->command_count++;
3489-
pkt->count += sizeof(uint64_t) / word_size; // account for added command
3490-
m_runlist.push_back(std::move(run)); // move of shared_ptr is noexcept
3491-
m_bos.push_back(run_bo); // ptr noexcept
3508+
// Now add the original run object
3509+
add_run_helper(std::move(run));
34923510
}
34933511

34943512
void
@@ -3500,6 +3518,14 @@ class runlist_impl
35003518
if (m_runlist.empty())
35013519
return;
35023520

3521+
// Add XDP exit runs if any at the end of runlist before submitting
3522+
// These runs collect profile/trace data
3523+
// The list can be empty if profile/trace is not enabled
3524+
const auto& xdp_exit_runs = xrt_core::hw_context_int::get_xdp_exit_runs(m_hwctx);
3525+
for (const auto& exit_run : xdp_exit_runs) {
3526+
add_run_helper(exit_run);
3527+
}
3528+
35033529
// Prep each run object
35043530
for (auto& run : m_runlist)
35053531
run.get_handle()->prep_start();
@@ -3539,6 +3565,20 @@ class runlist_impl
35393565

35403566
// On succesful wait, the runlist becomes idle
35413567
m_state = state::idle;
3568+
3569+
// Remove any XDP exit runs added during execute
3570+
// This is done because runlist can be reused
3571+
// and XDP exit runs should be added at end
3572+
const auto& xdp_exit_runs = xrt_core::hw_context_int::get_xdp_exit_runs(m_hwctx);
3573+
if (xdp_exit_runs.size() > 0) {
3574+
// remove the exit runs from the runlist
3575+
m_runlist.erase(m_runlist.end() - xdp_exit_runs.size(), m_runlist.end());
3576+
3577+
// clear the runlist associated with these exit runs
3578+
for (auto& run : xdp_exit_runs)
3579+
run.get_handle()->clear_runlist();
3580+
}
3581+
35423582
return std::cv_status::no_timeout;
35433583
}
35443584

0 commit comments

Comments
 (0)