Skip to content

Commit

Permalink
Enable flow without xclbin using new elf
Browse files Browse the repository at this point in the history
Signed-off-by: rbramand <[email protected]>
  • Loading branch information
rbramand committed Oct 28, 2024
1 parent 9669aab commit ef27174
Show file tree
Hide file tree
Showing 14 changed files with 1,204 additions and 507 deletions.
7 changes: 7 additions & 0 deletions src/runtime_src/core/common/api/hw_context_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "core/common/config.h"
// This file defines implementation extensions to the XRT XCLBIN APIs.
#include "core/include/experimental/xrt_hw_context.h"
#include "core/include/experimental/xrt_module.h"

#include <cstdint>

Expand Down Expand Up @@ -43,6 +44,12 @@ XRT_CORE_COMMON_EXPORT
xrt::hw_context
create_hw_context_from_implementation(void* hwctx_impl);

xrt::xclbin::kernel
get_kernel(const xrt::hw_context& hwctx);

xrt::module
get_module(const xrt::hw_context& hwctx, const std::string& kname);

}} // hw_context_int, xrt_core

#endif
18 changes: 17 additions & 1 deletion src/runtime_src/core/common/api/module_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "core/include/experimental/xrt_bo.h"
#include "core/include/experimental/xrt_module.h"

#include "ert.h"

#include <string>

namespace xrt_core::module_int {
Expand All @@ -33,7 +35,8 @@ patch(const xrt::module&, const std::string& argnm, size_t index, const xrt::bo&
// of the control code buffer.
XRT_CORE_COMMON_EXPORT
void
patch(const xrt::module&, uint8_t*, size_t*, const std::vector<std::pair<std::string, uint64_t>>*);
patch(const xrt::module&, uint8_t*, size_t*, const std::vector<std::pair<std::string, uint64_t>>*,
uint32_t index = 0);

// Patch scalar into control code at given argument
XRT_CORE_COMMON_EXPORT
Expand All @@ -54,6 +57,19 @@ get_ert_opcode(const xrt::module& module);
void
dump_scratchpad_mem(const xrt::module& module);

xrt::module
get_module(const std::string& name);

std::string
get_kernel_signature(const xrt::module& module);

std::string
get_kernel_name(const xrt::module& module);

// Get partition size if ELF has info
uint32_t
get_partition_size(const xrt::module& module);

} // xrt_core::module_int

#endif
120 changes: 120 additions & 0 deletions src/runtime_src/core/common/api/xrt_hw_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
#define XRT_API_SOURCE // exporting xrt_hwcontext.h
#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 "hw_context_int.h"
#include "module_int.h"
#include "xclbin_int.h"

#include "core/common/device.h"
#include "core/common/trace.h"
Expand All @@ -30,6 +34,8 @@ class hw_context_impl : public std::enable_shared_from_this<hw_context_impl>

std::shared_ptr<xrt_core::device> m_core_device;
xrt::xclbin m_xclbin;
std::map<std::string, xrt::module> m_module_map; // map b/w kernel name and module
uint32_t m_partition_size = 0;
cfg_param_type m_cfg_param;
access_mode m_mode;
std::unique_ptr<xrt_core::hwctx_handle> m_hdl;
Expand All @@ -53,6 +59,25 @@ class hw_context_impl : public std::enable_shared_from_this<hw_context_impl>
, m_hdl{m_core_device->create_hw_context(xclbin_id, m_cfg_param, m_mode)}
{}

hw_context_impl(std::shared_ptr<xrt_core::device> device, cfg_param_type cfg_param, access_mode mode)
: m_core_device{std::move(device)}
, m_cfg_param{std::move(cfg_param)}
, m_mode{mode}
{}

hw_context_impl(std::shared_ptr<xrt_core::device> device, const xrt::elf& elf, cfg_param_type cfg_param, access_mode mode)
: m_core_device{std::move(device)}
, m_cfg_param{std::move(cfg_param)}
, m_mode{mode}
{
auto module = xrt::module(elf);
auto kernel_name = xrt_core::module_int::get_kernel_name(module);
m_module_map[kernel_name] = std::move(module);

m_partition_size = xrt_core::module_int::get_partition_size(m_module_map.begin()->second);
m_hdl = m_core_device->create_hw_context(m_partition_size, m_cfg_param, mode);
}

std::shared_ptr<hw_context_impl>
get_shared_ptr()
{
Expand Down Expand Up @@ -81,6 +106,33 @@ class hw_context_impl : public std::enable_shared_from_this<hw_context_impl>
hw_context_impl& operator=(const hw_context_impl&) = delete;
hw_context_impl& operator=(hw_context_impl&&) = delete;

void
add_config(const xrt::elf& elf)
{
auto module = xrt::module(elf);
auto kernel_name = xrt_core::module_int::get_kernel_name(module);
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);

m_partition_size = part_size;
m_hdl = m_core_device->create_hw_context(m_partition_size, m_cfg_param, m_mode);
return;
}

// add module to map if kernel name is different, else throw
if (m_partition_size != part_size)
throw std::runtime_error("can not add config to ctx with different configuration\n");

for (const auto& m : m_module_map) {
if (kernel_name == xrt_core::module_int::get_kernel_name(m.second))
throw std::runtime_error("config with kernel already exists, cannot add this config\n");
}
m_module_map[kernel_name] = std::move(module);
}

void
update_qos(const qos_type& qos)
{
Expand Down Expand Up @@ -129,6 +181,16 @@ class hw_context_impl : public std::enable_shared_from_this<hw_context_impl>
{
return m_usage_logger.get();
}

xrt::module
get_module(const std::string& kname) const
{
for (const auto& m : m_module_map) {
if (kname == xrt_core::module_int::get_kernel_name(m.second))
return m.second;
}
throw std::runtime_error("no module found with given kernel name in ctx");
}
};

} // xrt
Expand Down Expand Up @@ -166,6 +228,12 @@ create_hw_context_from_implementation(void* hwctx_impl)
return xrt::hw_context(impl_ptr->get_shared_ptr());
}

xrt::module
get_module(const xrt::hw_context& ctx, const std::string& kname)
{
return ctx.get_handle()->get_module(kname);
}

} // xrt_core::hw_context_int

////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -207,6 +275,41 @@ alloc_hwctx_from_mode(const xrt::device& device, const xrt::uuid& xclbin_id, xrt
return handle;
}

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;
}

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;
}

hw_context::
hw_context(const xrt::device& device, const xrt::uuid& xclbin_id, const xrt::hw_context::cfg_param_type& cfg_param)
: detail::pimpl<hw_context_impl>(alloc_hwctx_from_cfg(device, xclbin_id, cfg_param))
Expand All @@ -217,6 +320,23 @@ hw_context(const xrt::device& device, const xrt::uuid& xclbin_id, access_mode mo
: detail::pimpl<hw_context_impl>(alloc_hwctx_from_mode(device, xclbin_id, mode))
{}

hw_context::
hw_context(const xrt::device& device, const xrt::elf& elf, const cfg_param_type& cfg_param, access_mode mode)
: detail::pimpl<hw_context_impl>(alloc_hwctx_from_elf(device, elf, cfg_param, mode))
{}

hw_context::
hw_context(const xrt::device& device, const cfg_param_type& cfg_param, access_mode mode)
: detail::pimpl<hw_context_impl>(alloc_empty_hwctx(device, cfg_param, mode))
{}

void
hw_context::
add_config(const xrt::elf& elf)
{
get_handle()->add_config(elf);
}

void
hw_context::
update_qos(const qos_type& qos)
Expand Down
Loading

0 comments on commit ef27174

Please sign in to comment.