Skip to content

Commit

Permalink
VITIS-11024 enable hw_context support for xrt::graph objects (#8340)
Browse files Browse the repository at this point in the history
1) bo function calls sync / async added with hw ctx along with device specific
  2) profiling functions added with hw ctx along with device specific

Signed-off-by: Sravankumar allu <[email protected]>
Signed-off-by: Sravankumar allu <[email protected]>
Co-authored-by: Sravankumar allu <[email protected]>
  • Loading branch information
SravanKumarAllu-xilinx and Sravankumar allu authored Aug 19, 2024
1 parent 29767e0 commit 820a7bb
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 55 deletions.
68 changes: 30 additions & 38 deletions src/runtime_src/core/common/api/aie/xrt_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "core/common/message.h"
#include "core/common/system.h"
#include "core/common/shim/graph_handle.h"
#include "core/common/shim/profile_handle.h"

#include <limits>
#include <memory>
Expand Down Expand Up @@ -124,63 +125,49 @@ namespace xrt::aie {
class profiling_impl
{
private:
std::shared_ptr<xrt_core::device> device;
int profiling_hdl;
std::unique_ptr<xrt_core::profile_handle> m_profile_handle{nullptr};

public:
using handle = int;
static constexpr handle invalid_handle = -1;
static constexpr int invalid_handle = -1;

explicit profiling_impl(std::shared_ptr<xrt_core::device> dev)
: device(std::move(dev)),
profiling_hdl(invalid_handle)
profiling_impl(std::shared_ptr<xrt_core::device> device)
: m_profile_handle{device->open_profile_handle()}
{}

profiling_impl(const xrt::hw_context& hwctx)
{
auto hwctx_handle = static_cast<xrt_core::hwctx_handle*>(hwctx);
m_profile_handle = hwctx_handle->open_profile_handle();
}

~profiling_impl()
{
try {
if (profiling_hdl != invalid_handle)
device->stop_profiling(profiling_hdl);
}
catch(...) {
// do nothing
}
stop();
}

profiling_impl() = delete;
profiling_impl(const profiling_impl&) = delete;
profiling_impl(profiling_impl&&) = delete;
profiling_impl& operator=(const profiling_impl&) = delete;
profiling_impl& operator=(profiling_impl&&) = delete;


handle
start_profiling(int option, const std::string& port1_name, const std::string& port2_name, uint32_t value)
int
start(int option, const std::string& port1_name, const std::string& port2_name, uint32_t value)
{
profiling_hdl = device->start_profiling(option, port1_name.c_str(), port2_name.c_str(), value);
return profiling_hdl;
return m_profile_handle->start(option, port1_name.c_str(), port2_name.c_str(), value);
}

uint64_t
read_profiling()
read()
{
if (profiling_hdl == invalid_handle)
throw xrt_core::error(-EINVAL, "Not a valid profiling handle");

return device->read_profiling(profiling_hdl);

return m_profile_handle->read();
}

void
stop_profiling()
stop()
{
if (profiling_hdl == invalid_handle)
throw xrt_core::error(-EINVAL, "Not a valid profiling handle");

device->stop_profiling(profiling_hdl);
profiling_hdl = invalid_handle;
return m_profile_handle->stop();
}

};

} // xrt::aie
Expand Down Expand Up @@ -399,13 +386,18 @@ profiling(const xrt::device& device)
: detail::pimpl<profiling_impl>(create_profiling_event(device))
{}

profiling::
profiling(const xrt::hw_context& hwctx)
: detail::pimpl<profiling_impl>(std::make_shared<xrt::aie::profiling_impl>(hwctx))
{}

int
profiling::
start(xrt::aie::profiling::profiling_option option, const std::string& port1_name, const std::string& port2_name, uint32_t value) const
{
int opt = static_cast<int>(option);
return xdp::native::profiling_wrapper("xrt::aie::profiling::start", [this, opt, &port1_name, &port2_name, value] {
return get_handle()->start_profiling(opt, port1_name, port2_name, value);
return get_handle()->start(opt, port1_name, port2_name, value);
});
}

Expand All @@ -414,7 +406,7 @@ profiling::
read() const
{
return xdp::native::profiling_wrapper("xrt::aie::profiling::read", [this] {
return get_handle()->read_profiling();
return get_handle()->read();
});
}

Expand All @@ -423,7 +415,7 @@ profiling::
stop() const
{
xdp::native::profiling_wrapper("xrt::aie::profiling::stop", [this] {
return get_handle()->stop_profiling();
return get_handle()->stop();
});
}

Expand Down Expand Up @@ -868,7 +860,7 @@ xrtAIEStartProfiling(xrtDeviceHandle handle, int option, const char *port1Name,
throw xrt_core::error(-EINVAL, "Not a valid profiling option");
const std::string port1 = port1Name ? port1Name : "";
const std::string port2 = port2Name ? port2Name : "";
auto hdl = event->start_profiling(option, port1, port2, value);
auto hdl = event->start(option, port1, port2, value);
if (hdl != xrt::aie::profiling_impl::invalid_handle) {
profiling_cache[hdl] = event;
return hdl;
Expand Down Expand Up @@ -901,7 +893,7 @@ xrtAIEReadProfiling(xrtDeviceHandle /*handle*/, int pHandle)
try {
auto it = profiling_cache.find(pHandle);
if (it != profiling_cache.end())
return it->second->read_profiling();
return it->second->read();
else
throw xrt_core::error(-EINVAL, "No such profiling handle");
}
Expand Down Expand Up @@ -931,7 +923,7 @@ xrtAIEStopProfiling(xrtDeviceHandle /*handle*/, int pHandle)
try {
auto it = profiling_cache.find(pHandle);
if (it != profiling_cache.end()) {
it->second->stop_profiling();
it->second->stop();
profiling_cache.erase(pHandle);
}
else
Expand Down
7 changes: 5 additions & 2 deletions src/runtime_src/core/common/api/xrt_bo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ class bo_impl
void
sync(xrt::bo& bo, const std::string& port, xclBOSyncDirection dir, size_t sz, size_t offset)
{
device->sync_aie_bo(bo, port.c_str(), dir, sz, offset);
//call sync & async functions with "Buffer handle" instead of with device, so that it could be handled with both device & hwctx
handle->sync_aie_bo(bo, port.c_str(), dir, sz, offset);
}

xrt::bo::async_handle
Expand Down Expand Up @@ -611,7 +612,9 @@ xrt::bo::async_handle
bo_impl::
async(xrt::bo& bo, const std::string& port, xclBOSyncDirection dir, size_t sz, size_t offset)
{
device->sync_aie_bo_nb(bo, port.c_str(), dir, sz, offset);
//call sync & async functions with "Buffer handle" instead of with device, so that it could be handled with both device & hwctx
handle->sync_aie_bo_nb(bo, port.c_str(), dir, sz, offset);

auto a_bo_impl = std::make_shared<xrt::aie::bo::async_handle_impl>(bo, 0, port);

return xrt::bo::async_handle{a_bo_impl};
Expand Down
7 changes: 7 additions & 0 deletions src/runtime_src/core/common/ishim.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "core/include/shim_int.h"
#include "core/include/xdp/counters.h"
#include "core/common/shim/graph_handle.h"
#include "core/common/shim/profile_handle.h"

#include "xrt/xrt_aie.h"
#include "xrt/xrt_bo.h"
Expand Down Expand Up @@ -204,6 +205,12 @@ struct ishim
{
throw not_supported_error{__func__};
}

virtual std::unique_ptr<profile_handle>
open_profile_handle()
{
throw not_supported_error{__func__};
}
////////////////////////////////////////////////////////////////

virtual void
Expand Down
16 changes: 16 additions & 0 deletions src/runtime_src/core/common/shim/buffer_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "core/common/shim/shared_handle.h"
#include "xrt.h"
#include "xrt/xrt_bo.h"
#include "core/common/error.h"

#include <cstdint>
#include <cstddef>
Expand All @@ -20,6 +22,8 @@ namespace xrt_core {
class buffer_handle
{
public:

using bo_direction = xclBOSyncDirection;
// map_type - determines how a buffer is mapped
enum class map_type { read, write };

Expand Down Expand Up @@ -87,6 +91,18 @@ class buffer_handle
bind_at(size_t /*pos*/, const buffer_handle* /*bh*/, size_t /*offset*/, size_t /*size*/)
{
}

virtual void
sync_aie_bo(xrt::bo&, const char *, bo_direction, size_t, size_t)
{
throw xrt_core::error(std::errc::not_supported, __func__);
}

virtual void
sync_aie_bo_nb(xrt::bo&, const char *, bo_direction, size_t, size_t)
{
throw xrt_core::error(std::errc::not_supported, __func__);
}
};

} // xrt_core
Expand Down
7 changes: 7 additions & 0 deletions src/runtime_src/core/common/shim/hwctx_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "core/common/shim/hwqueue_handle.h"
#include "core/common/shim/shared_handle.h"
#include "core/common/shim/graph_handle.h"
#include "profile_handle.h"

#include "xrt/xrt_hw_context.h"
#include "xrt/xrt_graph.h"
Expand Down Expand Up @@ -99,6 +100,12 @@ class hwctx_handle
{
throw xrt_core::error(std::errc::not_supported, __func__);
}

virtual std::unique_ptr<xrt_core::profile_handle>
open_profile_handle()
{
throw xrt_core::error(std::errc::not_supported, __func__);
}
};

} // xrt_core
Expand Down
35 changes: 35 additions & 0 deletions src/runtime_src/core/common/shim/profile_handle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2023-2024 Advanced Micro Devices, Inc. All rights reserved.
#ifndef XRT_CORE_PROFILE_HANDLE_H
#define XRT_CORE_PROFILE_HANDLE_H

#include <stdint.h>
#include "core/common/error.h"

namespace xrt_core {

class profile_handle
{
public:
virtual int
start(int, const char*, const char*, uint32_t)
{
throw xrt_core::error(std::errc::not_supported, __func__);
}

virtual uint64_t
read()
{
throw xrt_core::error(std::errc::not_supported, __func__);
}

virtual void
stop()
{
throw xrt_core::error(std::errc::not_supported, __func__);
}

}; //profile_handle

} //namespace xrt_core
#endif
58 changes: 58 additions & 0 deletions src/runtime_src/core/edge/user/aie/profile_object.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
#include "profile_object.h"
#include "core/edge/user/shim.h"

static inline
std::string value_or_empty(const char* s)
{
return s == nullptr ? "" : s;
}

namespace zynqaie {

profile_object::
profile_object(ZYNQ::shim* shim, Aie* aie_array)
: m_shim{shim},
m_aie_array{aie_array},
m_profile_id{invalid_profile_id}
{}

int
profile_object::
start(int option, const char* port1Name, const char* port2Name, uint32_t value)
{
auto device = xrt_core::get_userpf_device(m_shim);

if (!m_aie_array->is_context_set()) {
m_aie_array->open_context(device.get(), xrt::aie::access_mode::primary);
}
m_profile_id = m_aie_array->start_profiling(option, value_or_empty(port1Name), value_or_empty(port2Name), value);
return m_profile_id;
}

uint64_t
profile_object::
read()
{
auto device = xrt_core::get_userpf_device(m_shim);

if (!m_aie_array->is_context_set()) {
m_aie_array->open_context(device.get(), xrt::aie::access_mode::primary);
}
return m_aie_array->read_profiling(m_profile_id);
}

void
profile_object::
stop()
{
auto device = xrt_core::get_userpf_device(m_shim);

if (!m_aie_array->is_context_set()) {
m_aie_array->open_context(device.get(), xrt::aie::access_mode::primary);
}
m_aie_array->stop_profiling(m_profile_id);
}

} //namespace zynqaie
41 changes: 41 additions & 0 deletions src/runtime_src/core/edge/user/aie/profile_object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
#ifndef _ZYNQ_PROFILE_OBJECT_H_
#define _ZYNQ_PROFILE_OBJECT_H_

#include "core/common/shim/profile_handle.h"
#include <string>

namespace ZYNQ {

class shim;

}

namespace zynqaie {

class Aie;

class profile_object : public xrt_core::profile_handle
{
public:
static constexpr int invalid_profile_id = -1;
ZYNQ::shim* m_shim{nullptr};
Aie* m_aie_array{nullptr};
int m_profile_id;

profile_object(ZYNQ::shim* shim, Aie* aie_array);

int
start(int option, const char* port1Name, const char* port2Name, uint32_t value) override;

uint64_t
read() override;

void
stop() override;

}; //profile_object

} // namespace zynqaie
#endif
Loading

0 comments on commit 820a7bb

Please sign in to comment.