Skip to content

Commit

Permalink
Added Skeleton for AIE Profiling Plugin on Windows Devices (Xilinx#7530)
Browse files Browse the repository at this point in the history
  • Loading branch information
nishraptor authored May 10, 2023
1 parent 0bb56ce commit 073c963
Show file tree
Hide file tree
Showing 10 changed files with 605 additions and 434 deletions.
1 change: 1 addition & 0 deletions src/runtime_src/xdp/profile/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ if (XDP_MINIMAL_BUILD STREQUAL "yes")

add_subdirectory(native)
add_subdirectory(user)
add_subdirectory(aie_profile)

else()

Expand Down
17 changes: 15 additions & 2 deletions src/runtime_src/xdp/profile/plugin/aie_profile/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
# on both Edge and x86 platforms that support AIE.
# ====================================================================

if (${XRT_NATIVE_BUILD} STREQUAL "yes")
if (XDP_MINIMAL_BUILD STREQUAL "yes")
set(IMPL_DIR "${PROFILE_DIR}/plugin/aie_profile/win")
elseif (${XRT_NATIVE_BUILD} STREQUAL "yes")
set(IMPL_DIR "${PROFILE_DIR}/plugin/aie_profile/x86")
elseif (DEFINED XRT_AIE_BUILD AND ${XRT_NATIVE_BUILD} STREQUAL "no")
set(IMPL_DIR "${PROFILE_DIR}/plugin/aie_profile/edge")
Expand All @@ -22,8 +24,19 @@ file(GLOB AIE_PROFILE_PLUGIN_FILES
"${IMPL_DIR}/*.cpp"
)

if (${XRT_NATIVE_BUILD} STREQUAL "yes")
if (XDP_MINIMAL_BUILD STREQUAL "yes")
add_library(xdp_aie_profile_plugin MODULE ${AIE_PROFILE_PLUGIN_FILES})
add_dependencies(xdp_aie_profile_plugin xdp_core xrt_coreutil)
target_link_libraries(xdp_aie_profile_plugin PRIVATE xdp_core xrt_coreutil)

set_target_properties(xdp_aie_profile_plugin PROPERTIES VERSION ${XRT_VERSION_STRING} SOVERSION ${XRT_SOVERSION})

install (TARGETS xdp_aie_profile_plugin
LIBRARY DESTINATION ${XDP_PLUGIN_INSTALL_DIR}
)


elseif (${XRT_NATIVE_BUILD} STREQUAL "yes")
add_library(xdp_aie_profile_plugin MODULE ${AIE_PROFILE_PLUGIN_FILES})
add_dependencies(xdp_aie_profile_plugin xdp_core xrt_coreutil)
target_link_libraries(xdp_aie_profile_plugin PRIVATE xdp_core xrt_coreutil)
Expand Down
488 changes: 247 additions & 241 deletions src/runtime_src/xdp/profile/plugin/aie_profile/aie_profile_metadata.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#define XDP_SOURCE

#include "xdp/profile/plugin/aie_profile/aie_profile_plugin.h"

#include <boost/algorithm/string.hpp>
#include <ctime>
#include <iomanip>
Expand All @@ -31,24 +33,25 @@
#include "xdp/profile/device/device_intf.h"
#include "xdp/profile/device/hal_device/xdp_hal_device.h"
#include "xdp/profile/device/utility.h"
#include "xdp/profile/plugin/aie_profile/aie_profile_plugin.h"
#include "xdp/profile/plugin/vp_base/info.h"
#include "xdp/profile/writer/aie_profile/aie_writer.h"

#ifdef XRT_X86_BUILD
#include "x86/aie_profile.h"
#ifdef _WIN32
#include "win/aie_profile.h"
// #include "shim.h"
#elif defined(XRT_X86_BUILD)
#include "x86/aie_profile.h"
#else
#include "edge/aie_profile.h"
#include "core/edge/user/shim.h"
#include "core/edge/user/shim.h"
#include "edge/aie_profile.h"
#endif

namespace xdp {
using severity_level = xrt_core::message::severity_level;

bool AieProfilePlugin::live = false;

AieProfilePlugin::AieProfilePlugin()
: XDPPlugin()
AieProfilePlugin::AieProfilePlugin() : XDPPlugin()
{
AieProfilePlugin::live = true;

Expand Down Expand Up @@ -85,17 +88,21 @@ namespace xdp {
if (itr != handleToAIEData.end())
return itr->second.deviceID;

#ifdef _WIN32
return 0;
#else
char pathBuf[PATH_LENGTH];
memset(pathBuf, 0, PATH_LENGTH);

xclGetDebugIPlayoutPath(handle, pathBuf, PATH_LENGTH);
std::string sysfspath(pathBuf);
uint64_t deviceID = db->addDevice(sysfspath); // Get the unique device Id
uint64_t deviceID = db->addDevice(sysfspath); // Get the unique device Id
return deviceID;
#endif
}

void AieProfilePlugin::updateAIEDevice(void* handle)
{

// Don't update if no profiling is requested
if (!xrt_core::config::get_aie_profile())
return;
Expand All @@ -108,66 +115,77 @@ namespace xdp {
// Update the static database with information from xclbin
(db->getStaticInfo()).updateDevice(deviceID, handle);
{
#ifdef _WIN32
(db->getStaticInfo()).setDeviceName(deviceID, "win_device");
#else
struct xclDeviceInfo2 info;
if(xclGetDeviceInfo2(handle, &info) == 0) {
if (xclGetDeviceInfo2(handle, &info) == 0) {
(db->getStaticInfo()).setDeviceName(deviceID, std::string(info.mName));
}
#endif
}

// delete old data
if (handleToAIEData.find(handle) != handleToAIEData.end())
handleToAIEData.erase(handle);
handleToAIEData.erase(handle);
auto& AIEData = handleToAIEData[handle];

AIEData.deviceID = deviceID;
AIEData.metadata = std::make_shared<AieProfileMetadata>(deviceID, handle);

#ifdef XRT_X86_BUILD
AIEData.implementation = std::make_unique<AieProfile_x86Impl>(db, AIEData.metadata);
#else
AIEData.implementation = std::make_unique<AieProfile_EdgeImpl>(db, AIEData.metadata);
#endif
#ifdef _WIN32
AIEData.implementation = std::make_unique<AieProfile_WinImpl>(db, AIEData.metadata);
#elif defined(XRT_X86_BUILD)
AIEData.implementation = std::make_unique<AieProfile_x86Impl>(db, AIEData.metadata);
#else
AIEData.implementation = std::make_unique<AieProfile_EdgeImpl>(db, AIEData.metadata);
#endif
auto& implementation = AIEData.implementation;

// Ensure we only read/configure once per xclbin
if (!(db->getStaticInfo()).isAIECounterRead(deviceID)) {

//Sets up and calls the PS kernel on x86 implementation
//Sets up and the hardware on the edge implementation
// Sets up and calls the PS kernel on x86 implementation
// Sets up and the hardware on the edge implementation
implementation->updateDevice();

(db->getStaticInfo()).setIsAIECounterRead(deviceID, true);
}

// Open the writer for this device
auto time = std::time(nullptr);

#ifdef _WIN32
std::tm tm{};
localtime_s(&tm, &time);
std::string deviceName = "win_device";
#else
auto tm = *std::localtime(&time);
struct xclDeviceInfo2 info;
xclGetDeviceInfo2(handle, &info);
std::string deviceName = std::string(info.mName);

// Get current time
auto time = std::time(nullptr);
auto tm = *std::localtime(&time);
#endif

std::ostringstream timeOss;
timeOss << std::put_time(&tm, "_%Y_%m_%d_%H%M%S");
std::string timestamp = timeOss.str();

std::string outputFile = "aie_profile_" + deviceName + timestamp + ".csv";

VPWriter* writer = new AIEProfilingWriter(outputFile.c_str(),
deviceName.c_str(), mIndex);

VPWriter* writer = new AIEProfilingWriter(outputFile.c_str(), deviceName.c_str(), mIndex);
writers.push_back(writer);
db->getStaticInfo().addOpenedFile(writer->getcurrentFileName(), "AIE_PROFILE");

// Start the AIE profiling thread
AIEData.threadCtrlBool = true;
AIEData.threadCtrlBool = true;
auto device_thread = std::thread(&AieProfilePlugin::pollAIECounters, this, mIndex, handle);
// auto device_thread = std::thread(&AieProfileImpl::pollAIECounters, implementation.get(), mIndex, handle);
// auto device_thread = std::thread(&AieProfileImpl::pollAIECounters,
// implementation.get(), mIndex, handle);
AIEData.thread = std::move(device_thread);

++mIndex;
}

void AieProfilePlugin::pollAIECounters(uint32_t index, void* handle)
void AieProfilePlugin::pollAIECounters(uint32_t index, void* handle)
{
auto it = handleToAIEData.find(handle);
if (it == handleToAIEData.end())
Expand All @@ -177,9 +195,9 @@ namespace xdp {

while (should_continue) {
handleToAIEData[handle].implementation->poll(index, handle);
std::this_thread::sleep_for(std::chrono::microseconds(handleToAIEData[handle].metadata->getPollingIntervalVal()));
std::this_thread::sleep_for(std::chrono::microseconds(handleToAIEData[handle].metadata->getPollingIntervalVal()));
}
//Final Polling Operation
// Final Polling Operation
handleToAIEData[handle].implementation->poll(index, handle);
}

Expand All @@ -198,13 +216,11 @@ namespace xdp {
void AieProfilePlugin::endPoll()
{
// Ask all threads to end
for (auto& p : handleToAIEData)
p.second.threadCtrlBool = false;
for (auto& p : handleToAIEData) p.second.threadCtrlBool = false;

for (auto& p : handleToAIEData)
p.second.thread.join();
for (auto& p : handleToAIEData) p.second.thread.join();

handleToAIEData.clear();
}

} // end namespace xdp
} // end namespace xdp
91 changes: 91 additions & 0 deletions src/runtime_src/xdp/profile/plugin/aie_profile/win/aie_profile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright (C) 2022-2023 Advanced Micro Devices, Inc. - All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

#define XDP_SOURCE

#include "aie_profile.h"

#include <boost/algorithm/string.hpp>
#include <cmath>
#include <cstring>
#include <memory>

#include "core/common/message.h"
#include "core/common/time.h"
#include "core/common/xrt_profiling.h"
#include "core/include/xrt/xrt_kernel.h"
#include "xdp/profile/database/database.h"
#include "xdp/profile/database/static_info/aie_constructs.h"
#include "xdp/profile/database/static_info/pl_constructs.h"

namespace xdp {

AieProfile_WinImpl::AieProfile_WinImpl(VPDatabase* database, std::shared_ptr<AieProfileMetadata> metadata)
: AieProfileImpl(database, metadata)
{
// auto spdevice = xrt_core::get_userpf_device(metadata->getHandle());
// device = xrt::device(spdevice);

// auto uuid = device.get_xclbin_uuid();

// if (metadata->getHardwareGen() == 1)
// aie_profile_kernel = xrt::kernel(device, uuid.get(),
// "aie_profile_config");
// else
// aie_profile_kernel = xrt::kernel(device, uuid.get(),
// "aie2_profile_config");
}

void AieProfile_WinImpl::updateDevice()
{
setMetricsSettings(metadata->getDeviceID(), metadata->getHandle());
}

void setupAIEProfiler(uint8_t col, uint8_t row, uint32_t start, uint32_t end)
{
std::cout << col << " " << row << " " << start << " " << end << std::endl;
// aiegraph_handle_.clearTransactionBuffer();
// buffers_.clear();
// inputs_.clear();
// outputs_.clear();
// bufferidx_to_xrt_subbo_.clear();
// buffer_to_xrt_bo_.clear();

// std::vector<uint32_t>pc_list;
// pc_list.push_back(start);
// pc_list.push_back(end);
}

bool AieProfile_WinImpl::setMetricsSettings(uint64_t deviceId, void* handle)
{
std::cout << deviceId << handle << std::endl;
return true;
}

void AieProfile_WinImpl::poll(uint32_t index, void* handle)
{
std::cout << index << handle << std::endl;
// For now, we are waiting for a way to retreive polling information from
// the AIE.
return;
}

void AieProfile_WinImpl::freeResources()
{
// TODO - if there are any resources we need to free after the application
// is complete, it must be done here.
}
} // namespace xdp
60 changes: 60 additions & 0 deletions src/runtime_src/xdp/profile/plugin/aie_profile/win/aie_profile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (C) 2022-2023 Advanced Micro Devices, Inc. - All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

#ifndef AIE_PROFILE_H
#define AIE_PROFILE_H

#include <cstdint>

#include "core/include/xrt/xrt_kernel.h"
// #include "aiegraph.hpp"

#include "xdp/profile/plugin/aie_profile/aie_profile_impl.h"

namespace xdp {

class AieProfile_WinImpl : public AieProfileImpl {
public:
AieProfile_WinImpl(VPDatabase* database, std::shared_ptr<AieProfileMetadata> metadata);
~AieProfile_WinImpl() = default;

void updateDevice();
void poll(uint32_t index, void* handle);
void freeResources();
bool setMetricsSettings(uint64_t deviceId, void* handle);

private:
xrt::device device;
// xrt::kernel aie_profile_kernel;

// //inpu specific private members
// std::string name;

// AIEGraphHandle aiegraph_handle;
// XrtContext &xrt_context_
// std::vector<bd_bundle> bd_bundles;
// xrt::bo bo_fm;
// xrt::bo bo_instr;
// std::vector<xrt::bo> bufferidx_to_xrt_subbo;

// std::unordered_set<void*> inputs;
// std::unordered_set<void*> outputs;
// std::unordered_set<void*> buffers;
};

} // namespace xdp

#endif
Loading

0 comments on commit 073c963

Please sign in to comment.