Skip to content

Commit

Permalink
Fix failure
Browse files Browse the repository at this point in the history
Signed-off-by: rbramand <[email protected]>
  • Loading branch information
rbramand committed Nov 11, 2024
1 parent 0996d80 commit f3576d4
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 101 deletions.
2 changes: 2 additions & 0 deletions src/runtime_src/core/common/api/module_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ patch(const xrt::module&, const std::string& argnm, size_t index, const xrt::bo&
// Note that if size passed in is 0, real buffer size required will be returned
// without any patching. This is useful if caller wishes to discover the exact size
// of the control code buffer.
// New ELfs pack multiple control codes info in it, to identify which control code
// to run we use index
XRT_CORE_COMMON_EXPORT
void
patch(const xrt::module&, uint8_t*, size_t*, const std::vector<std::pair<std::string, uint64_t>>*,
Expand Down
69 changes: 20 additions & 49 deletions src/runtime_src/core/common/api/xrt_hw_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#define XCL_DRIVER_DLL_EXPORT // exporting xrt_xclbin.h
#define XRT_CORE_COMMON_SOURCE // in same dll as coreutil

#include "core/include/experimental/xrt_module.h"
#include "core/include/xrt/xrt_hw_context.h"
#include "core/include/experimental/xrt_module.h"
#include "hw_context_int.h"
#include "module_int.h"
#include "xclbin_int.h"
Expand Down Expand Up @@ -78,7 +78,7 @@ class hw_context_impl : public std::enable_shared_from_this<hw_context_impl>

// creation successful, store the module in the map
auto kernel_name = xrt_core::module_int::get_kernel_info(module).props.name;
m_module_map[kernel_name] = std::move(module);
m_module_map.emplace(std::move(kernel_name), std::move(module));
}

std::shared_ptr<hw_context_impl>
Expand Down Expand Up @@ -117,9 +117,8 @@ class hw_context_impl : public std::enable_shared_from_this<hw_context_impl>
auto part_size = xrt_core::module_int::get_partition_size(module);

// create hw ctx handle if not already created
if (m_hdl == nullptr) {
m_module_map[kernel_name] = std::move(module);

if (!m_hdl) {
m_module_map.emplace(std::move(kernel_name), std::move(module));
m_partition_size = part_size;
m_hdl = m_core_device->create_hw_context(m_partition_size, m_cfg_param, m_mode);
return;
Expand Down Expand Up @@ -187,10 +186,10 @@ class hw_context_impl : public std::enable_shared_from_this<hw_context_impl>
xrt::module
get_module(const std::string& kname) const
{
auto itr = m_module_map.find(kname);
if (itr == m_module_map.end())
throw std::runtime_error("no module found with given kernel name in ctx");
return itr->second;
if (auto itr = m_module_map.find(kname); itr != m_module_map.end())
return itr->second;

throw std::runtime_error("no module found with given kernel name in ctx");
}
};

Expand Down Expand Up @@ -241,74 +240,46 @@ get_module(const xrt::hw_context& ctx, const std::string& kname)
// xrt_hwcontext C++ API implmentations (xrt_hw_context.h)
////////////////////////////////////////////////////////////////
namespace xrt {

// common function called with hw ctx created from different ways
static std::shared_ptr<hw_context_impl>
alloc_hwctx_from_cfg(const xrt::device& device, const xrt::uuid& xclbin_id, const xrt::hw_context::cfg_param_type& cfg_param)
post_alloc_hwctx(const std::shared_ptr<hw_context_impl>& handle)
{
XRT_TRACE_POINT_SCOPE(xrt_hw_context);
auto handle = std::make_shared<hw_context_impl>(device.get_handle(), xclbin_id, cfg_param);

// Update device is called with a raw pointer to dyanamically
// link to callbacks that exist in XDP via a C-style interface
// The create_hw_context_from_implementation function is then
// called in XDP create a hw_context to the underlying implementation
xrt_core::xdp::update_device(handle.get());

handle->get_usage_logger()->log_hw_ctx_info(handle.get());

return handle;
}

static std::shared_ptr<hw_context_impl>
alloc_hwctx_from_mode(const xrt::device& device, const xrt::uuid& xclbin_id, xrt::hw_context::access_mode mode)
alloc_hwctx_from_cfg(const xrt::device& device, const xrt::uuid& xclbin_id, const xrt::hw_context::cfg_param_type& cfg_param)
{
XRT_TRACE_POINT_SCOPE(xrt_hw_context);
auto handle = std::make_shared<hw_context_impl>(device.get_handle(), xclbin_id, mode);

// Update device is called with a raw pointer to dyanamically
// link to callbacks that exist in XDP via a C-style interface
// The create_hw_context_from_implementation function is then
// called in XDP create a hw_context to the underlying implementation
xrt_core::xdp::update_device(handle.get());

handle->get_usage_logger()->log_hw_ctx_info(handle.get());
return post_alloc_hwctx(std::make_shared<hw_context_impl>(device.get_handle(), xclbin_id, cfg_param));
}

return handle;
static std::shared_ptr<hw_context_impl>
alloc_hwctx_from_mode(const xrt::device& device, const xrt::uuid& xclbin_id, xrt::hw_context::access_mode mode)
{
XRT_TRACE_POINT_SCOPE(xrt_hw_context);
return post_alloc_hwctx(std::make_shared<hw_context_impl>(device.get_handle(), xclbin_id, mode));
}

static std::shared_ptr<hw_context_impl>
alloc_empty_hwctx(const xrt::device& device, const xrt::hw_context::cfg_param_type& cfg_param, xrt::hw_context::access_mode mode)
{
XRT_TRACE_POINT_SCOPE(xrt_hw_context);
auto handle = std::make_shared<hw_context_impl>(device.get_handle(), cfg_param, mode);

// Update device is called with a raw pointer to dyanamically
// link to callbacks that exist in XDP via a C-style interface
// The create_hw_context_from_implementation function is then
// called in XDP create a hw_context to the underlying implementation
xrt_core::xdp::update_device(handle.get());

handle->get_usage_logger()->log_hw_ctx_info(handle.get());

return handle;
return post_alloc_hwctx(std::make_shared<hw_context_impl>(device.get_handle(), cfg_param, mode));
}

static std::shared_ptr<hw_context_impl>
alloc_hwctx_from_elf(const xrt::device& device, const xrt::elf& elf, const xrt::hw_context::cfg_param_type& cfg_param,
xrt::hw_context::access_mode mode)
{
XRT_TRACE_POINT_SCOPE(xrt_hw_context);
auto handle = std::make_shared<hw_context_impl>(device.get_handle(), elf, cfg_param, mode);

// Update device is called with a raw pointer to dyanamically
// link to callbacks that exist in XDP via a C-style interface
// The create_hw_context_from_implementation function is then
// called in XDP create a hw_context to the underlying implementation
xrt_core::xdp::update_device(handle.get());

handle->get_usage_logger()->log_hw_ctx_info(handle.get());

return handle;
return post_alloc_hwctx(std::make_shared<hw_context_impl>(device.get_handle(), elf, cfg_param, mode));
}

hw_context::
Expand Down
71 changes: 27 additions & 44 deletions src/runtime_src/core/common/api/xrt_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,11 +1083,8 @@ class argument

struct global_type : iarg
{
size_t size; // size in bytes of argument per xclbin

explicit
global_type(size_t bytes = 0)
: size(bytes)
global_type()
{}

std::vector<uint32_t>
Expand Down Expand Up @@ -1182,7 +1179,7 @@ class argument
}
case xarg::argtype::global :
case xarg::argtype::constant :
content = std::make_unique<global_type>(arg.size);
content = std::make_unique<global_type>();
break;
case xarg::argtype::local : // local memory
case xarg::argtype::stream : // stream connection
Expand Down Expand Up @@ -1505,39 +1502,16 @@ class kernel_impl : public std::enable_shared_from_this<kernel_impl>
return data; // no skipping
}

xrt::module
get_module(const xrt::hw_context& ctx, const std::string& kname)
// parse the name passed to kernel constructor to
// extract kernel name and control code index
static std::pair<std::string, uint32_t>
get_kname_ctrl_idx_pair(const std::string& name)
{
// ELF use case, identify module from ctx that has given kernel name and
// get kernel signature from the module to construct kernel args etc
// kernel name will be of format - <kernel_name>:<index>
auto i = kname.find(":");
if (i == std::string::npos) {
// default case - ctrl code 0 will be used
name = kname.substr(0, kname.size());
m_ctrl_code_index = 0;
}
else {
name = kname.substr(0, i);
m_ctrl_code_index = std::stoul(kname.substr(i+1, kname.size()-i-1));
}

return xrt_core::hw_context_int::get_module(ctx, name);
}

const property_type&
get_elf_kernel_properties()
{
// Get kernel info from module
const auto& kernel_info = xrt_core::module_int::get_kernel_info(m_module);
if (name != kernel_info.props.name)
throw std::runtime_error("Kernel name mismatch, incorrect module picked\n");

// set kernel args
for (auto& arg : kernel_info.args)
args.emplace_back(arg);

return kernel_info.props;
// kernel name will be of format - <kernel_name>:<ctrl code index>
if (auto i = name.find(':'); i != std::string::npos)
return std::make_pair(name.substr(0, i), std::stoul(name.substr(i+1, name.size()-i-1)));
else
return std::make_pair(name, 0); // default case - ctrl code 0 will be used
}

static uint32_t
Expand Down Expand Up @@ -1616,15 +1590,21 @@ class kernel_impl : public std::enable_shared_from_this<kernel_impl>
}

kernel_impl(std::shared_ptr<device_type> dev, xrt::hw_context ctx, const std::string& nm)
: device(std::move(dev)) // share ownership
, hwctx(std::move(ctx)) // hw context
, hwqueue(hwctx) // hw queue
, m_module(get_module(hwctx, nm)) // module object with matching kernel name
, properties(get_elf_kernel_properties()) // kernel info present in Elf
: name(get_kname_ctrl_idx_pair(nm).first) // get canonical kernel name
, device(std::move(dev)) // share ownership
, hwctx(std::move(ctx)) // hw context
, hwqueue(hwctx) // hw queue
, m_module(xrt_core::hw_context_int::get_module(ctx, name)) // module obj with matching kernel name
, properties(xrt_core::module_int::get_kernel_info(m_module).props)
, uid(create_uid())
, m_ctrl_code_index(get_kname_ctrl_idx_pair(nm).second) // control code index
{
XRT_DEBUGF("kernel_impl::kernel_impl(%d)\n", uid);

// get kernel info from module and initialize kernel args
for (auto& arg : xrt_core::module_int::get_kernel_info(m_module).args)
args.emplace_back(arg);

// amend args with computed data based on kernel protocol
amend_args();
m_usage_logger->log_kernel_info(device->core_device.get(), hwctx, name, args.size());
Expand Down Expand Up @@ -1992,8 +1972,11 @@ class run_impl
return count++;
}

// This function copies the module into a hw_context. The module
// This function copies the module into a hw_context. The module
// will be associated with hwctx specific memory.
// If module has multiple control codes, index is used to identify
// the control code that needs to be run.
// By default control code at zeroth index is picked
static xrt::module
copy_module(const xrt::module& module, const xrt::hw_context& hwctx, uint32_t ctrl_code_idx)
{
Expand Down Expand Up @@ -3489,7 +3472,7 @@ alloc_kernel(const std::shared_ptr<device_type>& dev,
xrt::kernel::cu_access_mode mode)
{
auto amode = hwctx_access_mode(mode); // legacy access mode to hwctx qos
return std::make_shared<xrt::kernel_impl>(dev, xrt::hw_context{dev->get_xrt_device(), xclbin_id, amode}, name);
return std::make_shared<xrt::kernel_impl>(dev, xrt::hw_context{dev->get_xrt_device(), xclbin_id, amode}, xrt::module{}, name);
}

static std::shared_ptr<xrt::kernel_impl>
Expand Down
25 changes: 19 additions & 6 deletions src/runtime_src/core/common/api/xrt_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,12 @@ class module_elf_aie2p : public module_elf
return std::make_pair(UINT32_MAX, control_packet::get_empty_buf());
}

[[nodiscard]] virtual size_t
get_scratch_pad_mem_size() const override
{
return m_scratch_pad_mem_size;
}

[[nodiscard]] std::pair<uint32_t, const buf&>
get_preempt_save() const override
{
Expand Down Expand Up @@ -1377,8 +1383,9 @@ class module_sram : public module_impl
create_instr_buf(const module_impl* parent)
{
XRT_DEBUGF("-> module_sram::create_instr_buf()\n");
instr_buf data;
std::tie(m_instr_sec_idx, data) = parent->get_instr(m_index);
auto instr_buf_info = parent->get_instr(m_index);
m_instr_sec_idx = instr_buf_info.first;
const instr_buf& data = instr_buf_info.second;
size_t sz = data.size();
if (sz == 0)
throw std::runtime_error("Invalid instruction buffer size");
Expand Down Expand Up @@ -1461,8 +1468,9 @@ class module_sram : public module_impl
void
create_ctrlpkt_buf(const module_impl* parent)
{
control_packet data;
std::tie(m_ctrlpkt_sec_idx, data) = parent->get_ctrlpkt(m_index);
auto ctrl_pkt_info = parent->get_ctrlpkt(m_index);
m_ctrlpkt_sec_idx = ctrl_pkt_info.first;
const control_packet& data = ctrl_pkt_info.second;
size_t sz = data.size();

if (sz == 0) {
Expand Down Expand Up @@ -1663,7 +1671,7 @@ class module_sram : public module_impl
}

public:
module_sram(std::shared_ptr<module_impl> parent, xrt::hw_context hwctx, uint32_t index)
module_sram(std::shared_ptr<module_impl> parent, xrt::hw_context hwctx, uint32_t index = 0)
: module_impl{ parent->get_cfg_uuid() }
, m_parent{ std::move(parent) }
, m_hwctx{ std::move(hwctx) }
Expand Down Expand Up @@ -1788,7 +1796,7 @@ patch(const xrt::module& module, uint8_t* ibuf, size_t* sz, const std::vector<st
auto os_abi = hdl->get_os_abi();

if (os_abi == Elf_Amd_Aie2p || os_abi == Elf_Amd_Aie2p_config) {
const auto& buf_info = hdl->get_instr(idx);
auto buf_info = hdl->get_instr(idx);
patch_index = buf_info.first;
inst = &(buf_info.second);
}
Expand Down Expand Up @@ -1896,6 +1904,11 @@ module(void* userptr, size_t sz, const xrt::uuid& uuid)
: detail::pimpl<module_impl>{ std::make_shared<module_userptr>(userptr, sz, uuid) }
{}

module::
module(const xrt::module& parent, const xrt::hw_context& hwctx)
: detail::pimpl<module_impl>{ std::make_shared<module_sram>(parent.handle, hwctx) }
{}

module::
module(const xrt::module& parent, const xrt::hw_context& hwctx, uint32_t ctrl_code_idx)
: detail::pimpl<module_impl>{ std::make_shared<module_sram>(parent.handle, hwctx, ctrl_code_idx) }
Expand Down
20 changes: 19 additions & 1 deletion src/runtime_src/core/include/experimental/xrt_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ class module : public detail::pimpl<module_impl>
XRT_API_EXPORT
module(void* userptr, size_t sz, const xrt::uuid& uuid);

/**
* module() - Constructor associate module with hardware context
*
* @param parent
* Parent module with instruction buffer to move into hwctx
* @param hwctx
* Hardware context to associate with module
*
* Copy content of existing module into an allocation associated
* with the specified hardware context.
*
* Throws if module is not compatible with hardware context
*/
XRT_API_EXPORT
module(const xrt::module& parent, const xrt::hw_context& hwctx);

/**
* module() - Constructor associate module with hardware context
*
Expand All @@ -90,11 +106,13 @@ class module : public detail::pimpl<module_impl>
*
* Copy content of existing module into an allocation associated
* with the specified hardware context.
* If module has multiple control codes, index is used to identify
* the control code that needs to be run.
*
* Throws if module is not compatible with hardware context
*/
XRT_API_EXPORT
module(const xrt::module& parent, const xrt::hw_context& hwctx, uint32_t ctrl_code_idx = 0);
module(const xrt::module& parent, const xrt::hw_context& hwctx, uint32_t ctrl_code_idx);

/**
* get_cfg_uuid() - Get the uuid of the hardware configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module::module(void* userptr, size_t sz, const xrt::uuid& uuid)
XRT_TOOLS_XBT_FUNC_EXIT(func);
}

module::module(const xrt::module& parent, const xrt::hw_context& hwctx, uint32_t /*ctrl_code_idx*/)
module::module(const xrt::module& parent, const xrt::hw_context& hwctx)
{
auto func = "xrt::module::module(const xrt::module&, const xrt::hw_context&)";
XRT_TOOLS_XBT_CALL_CTOR(dtbl.module.ctor_mod_ctx, this, parent, hwctx);
Expand Down

0 comments on commit f3576d4

Please sign in to comment.