Skip to content

Commit

Permalink
Refactor Level Zero Adapter to use new common logger
Browse files Browse the repository at this point in the history
This commit refactors the Level Zero adapter to adopt the new logger
introduced in d9cd223 (Integrate logger with library, 2023-02-03).
It marks the beginning of a series aimed at unifying logging across
all adapters.

Signed-off-by: Łukasz Plewa <lukasz.plewa@intel.com>
  • Loading branch information
lplewa committed Feb 8, 2024
1 parent 3be8f20 commit 5e108ea
Show file tree
Hide file tree
Showing 21 changed files with 383 additions and 211 deletions.
53 changes: 41 additions & 12 deletions source/adapters/level_zero/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@
#include "adapter.hpp"
#include "ur_level_zero.hpp"

class ur_legacy_sink : public logger::Sink {
public:
ur_legacy_sink(std::string logger_name = "", bool skip_prefix = true)
: Sink(std::move(logger_name), skip_prefix) {
this->ostream = &std::cerr;
}

virtual void print([[maybe_unused]] logger::Level level,
std::string msg) override {
fprintf(stderr, "%s", msg.c_str());
}

~ur_legacy_sink() = default;
};

ur_adapter_handle_t_::ur_adapter_handle_t_()
: logger(logger::get_logger("level_zero")) {
if (UrL0Debug & UR_L0_DEBUG_BASIC) {
logger.setLegacySink(std::make_unique<ur_legacy_sink>());
};
}

ur_adapter_handle_t_ Adapter{};

ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
Expand Down Expand Up @@ -60,7 +82,7 @@ ur_result_t adapterStateInit() {
}

if (getenv("SYCL_ENABLE_PCI") != nullptr) {
urPrint("WARNING: SYCL_ENABLE_PCI is deprecated and no longer needed.\n");
logger::warning("SYCL_ENABLE_PCI is deprecated and no longer needed.");
}

// TODO: We can still safely recover if something goes wrong during the
Expand All @@ -83,7 +105,7 @@ ur_result_t adapterStateInit() {
return;
}
if (*Adapter.ZeResult != ZE_RESULT_SUCCESS) {
urPrint("zeInit: Level Zero initialization failure\n");
logger::error("zeInit: Level Zero initialization failure\n");
result = ze2urResult(*Adapter.ZeResult);
return;
}
Expand Down Expand Up @@ -144,10 +166,10 @@ ur_result_t adapterStateTeardown() {
// zeMemAllocShared = 0 \---> zeMemFree = 1
//
// clang-format on

fprintf(stderr, "Check balance of create/destroy calls\n");
fprintf(stderr,
"----------------------------------------------------------\n");
// TODO: use logger to print this messages
std::cerr << "Check balance of create/destroy calls\n";
std::cerr << "----------------------------------------------------------\n";
std::stringstream ss;
for (const auto &Row : CreateDestroySet) {
int diff = 0;
for (auto I = Row.begin(); I != Row.end();) {
Expand All @@ -158,23 +180,30 @@ ur_result_t adapterStateTeardown() {
bool Last = (++I == Row.end());

if (Last) {
fprintf(stderr, " \\--->");
ss << " \\--->";
diff -= ZeCount;
} else {
diff += ZeCount;
if (!First) {
fprintf(stderr, " | \n");
ss << " | ";
std::cerr << ss.str() << "\n";
ss.str("");
ss.clear();
}
}

fprintf(stderr, "%30s = %-5d", ZeName, ZeCount);
ss << std::setw(30) << std::right << ZeName;
ss << " = ";
ss << std::setw(5) << std::left << ZeCount;
}

if (diff) {
LeakFound = true;
fprintf(stderr, " ---> LEAK = %d", diff);
ss << " ---> LEAK = " << diff;
}
fprintf(stderr, "\n");

std::cerr << ss.str() << '\n';
ss.str("");
ss.clear();
}

ZeCallCount->clear();
Expand Down
5 changes: 5 additions & 0 deletions source/adapters/level_zero/adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
//===----------------------------------------------------------------------===//

#include "logger/ur_logger.hpp"
#include <atomic>
#include <mutex>
#include <optional>
Expand All @@ -16,12 +17,16 @@

using PlatformVec = std::vector<std::unique_ptr<ur_platform_handle_t_>>;

class ur_legacy_sink;

struct ur_adapter_handle_t_ {
ur_adapter_handle_t_();
std::atomic<uint32_t> RefCount = 0;
std::mutex Mutex;

std::optional<ze_result_t> ZeResult;
ZeCache<Result<PlatformVec>> PlatformCache;
logger::Logger &logger;
};

extern ur_adapter_handle_t_ Adapter;
63 changes: 34 additions & 29 deletions source/adapters/level_zero/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
//===----------------------------------------------------------------------===//
#include "command_buffer.hpp"
#include "logger/ur_logger.hpp"
#include "ur_level_zero.hpp"

/* Command-buffer Extension
Expand Down Expand Up @@ -180,16 +181,17 @@ ur_result_t calculateKernelWorkDimensions(
--GroupSize[I];
}
if (GlobalWorkSize[I] / GroupSize[I] > UINT32_MAX) {
urPrint("urCommandBufferAppendKernelLaunchExp: can't find a WG size "
"suitable for global work size > UINT32_MAX\n");
logger::debug(
"urCommandBufferAppendKernelLaunchExp: can't find a WG size "
"suitable for global work size > UINT32_MAX");
return UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE;
}
WG[I] = GroupSize[I];
}
urPrint(
"urCommandBufferAppendKernelLaunchExp: using computed WG size = {%d, "
"%d, %d}\n",
WG[0], WG[1], WG[2]);
logger::debug("urCommandBufferAppendKernelLaunchExp: using computed WG "
"size = {{{}, "
"{}, {}}}",
WG[0], WG[1], WG[2]);
}
}

Expand Down Expand Up @@ -217,30 +219,33 @@ ur_result_t calculateKernelWorkDimensions(
break;

default:
urPrint("urCommandBufferAppendKernelLaunchExp: unsupported work_dim\n");
logger::error("urCommandBufferAppendKernelLaunchExp: unsupported work_dim");
return UR_RESULT_ERROR_INVALID_VALUE;
}

// Error handling for non-uniform group size case
if (GlobalWorkSize[0] !=
size_t(ZeThreadGroupDimensions.groupCountX) * WG[0]) {
urPrint("urCommandBufferAppendKernelLaunchExp: invalid work_dim. The range "
"is not a "
"multiple of the group size in the 1st dimension\n");
logger::error(
"urCommandBufferAppendKernelLaunchExp: invalid work_dim. The range "
"is not a "
"multiple of the group size in the 1st dimension");
return UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE;
}
if (GlobalWorkSize[1] !=
size_t(ZeThreadGroupDimensions.groupCountY) * WG[1]) {
urPrint("urCommandBufferAppendKernelLaunchExp: invalid work_dim. The range "
"is not a "
"multiple of the group size in the 2nd dimension\n");
logger::error(
"urCommandBufferAppendKernelLaunchExp: invalid work_dim. The range "
"is not a "
"multiple of the group size in the 2nd dimension");
return UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE;
}
if (GlobalWorkSize[2] !=
size_t(ZeThreadGroupDimensions.groupCountZ) * WG[2]) {
urPrint("urCommandBufferAppendKernelLaunchExp: invalid work_dim. The range "
"is not a "
"multiple of the group size in the 3rd dimension\n");
logger::error(
"urCommandBufferAppendKernelLaunchExp: invalid work_dim. The range "
"is not a "
"multiple of the group size in the 3rd dimension");
return UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE;
}

Expand Down Expand Up @@ -304,9 +309,9 @@ static ur_result_t enqueueCommandBufferMemCopyHelper(
(CommandBuffer->ZeCommandList, Dst, Src, Size,
LaunchEvent->ZeEvent, ZeEventList.size(), ZeEventList.data()));

urPrint("calling zeCommandListAppendMemoryCopy() with"
" ZeEvent %#" PRIxPTR "\n",
ur_cast<std::uintptr_t>(LaunchEvent->ZeEvent));
logger::debug("calling zeCommandListAppendMemoryCopy() with"
" ZeEvent {}",
ur_cast<std::uintptr_t>(LaunchEvent->ZeEvent));

return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -370,9 +375,9 @@ static ur_result_t enqueueCommandBufferMemCopyRectHelper(
DstSlicePitch, Src, &ZeSrcRegion, SrcPitch, SrcSlicePitch,
LaunchEvent->ZeEvent, ZeEventList.size(), ZeEventList.data()));

urPrint("calling zeCommandListAppendMemoryCopyRegion() with"
" ZeEvent %#" PRIxPTR "\n",
ur_cast<std::uintptr_t>(LaunchEvent->ZeEvent));
logger::debug("calling zeCommandListAppendMemoryCopyRegion() with"
" ZeEvent {}",
ur_cast<std::uintptr_t>(LaunchEvent->ZeEvent));

return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -412,9 +417,9 @@ static ur_result_t enqueueCommandBufferFillHelper(
(CommandBuffer->ZeCommandList, Ptr, Pattern, PatternSize, Size,
LaunchEvent->ZeEvent, ZeEventList.size(), ZeEventList.data()));

urPrint("calling zeCommandListAppendMemoryFill() with"
" ZeEvent %#lx\n",
ur_cast<std::uintptr_t>(LaunchEvent->ZeEvent));
logger::debug("calling zeCommandListAppendMemoryFill() with"
" ZeEvent {}",
ur_cast<std::uintptr_t>(LaunchEvent->ZeEvent));

return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -514,7 +519,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
if (GlobalWorkOffset != NULL) {
if (!CommandBuffer->Context->getPlatform()
->ZeDriverGlobalOffsetExtensionFound) {
urPrint("No global offset extension found on this driver\n");
logger::debug("No global offset extension found on this driver");
return UR_RESULT_ERROR_INVALID_VALUE;
}

Expand Down Expand Up @@ -569,9 +574,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
&ZeThreadGroupDimensions, LaunchEvent->ZeEvent,
ZeEventList.size(), ZeEventList.data()));

urPrint("calling zeCommandListAppendLaunchKernel() with"
" ZeEvent %#" PRIxPTR "\n",
ur_cast<std::uintptr_t>(LaunchEvent->ZeEvent));
logger::debug("calling zeCommandListAppendLaunchKernel() with"
" ZeEvent {}",
ur_cast<std::uintptr_t>(LaunchEvent->ZeEvent));

return UR_RESULT_SUCCESS;
}
Expand Down
18 changes: 5 additions & 13 deletions source/adapters/level_zero/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//===----------------------------------------------------------------------===//

#include "common.hpp"
#include "logger/ur_logger.hpp"
#include "usm.hpp"

ur_result_t ze2urResult(ze_result_t ZeResult) {
Expand Down Expand Up @@ -63,15 +64,6 @@ ur_result_t ze2urResult(ze_result_t ZeResult) {
}
}

void urPrint(const char *Format, ...) {
if (UrL0Debug & UR_L0_DEBUG_BASIC) {
va_list Args;
va_start(Args, Format);
vfprintf(stderr, Format, Args);
va_end(Args);
}
}

usm::DisjointPoolAllConfigs DisjointPoolConfigInstance =
InitializeDisjointPoolConfig();

Expand All @@ -84,8 +76,8 @@ bool setEnvVar(const char *name, const char *value) {
int Res = setenv(name, value, 1);
#endif
if (Res != 0) {
urPrint("UR L0 Adapter was unable to set the environment variable: %s\n",
name);
logger::debug(
"UR L0 Adapter was unable to set the environment variable: {}", name);
return false;
}
return true;
Expand Down Expand Up @@ -147,7 +139,7 @@ inline void zeParseError(ze_result_t ZeError, const char *&ErrorString) {

ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *ZeName,
const char *ZeArgs, bool TraceError) {
urPrint("ZE ---> %s%s\n", ZeName, ZeArgs);
logger::debug("ZE ---> {}{}", ZeName, ZeArgs);

if (UrL0LeaksDebug) {
++(*ZeCallCount)[ZeName];
Expand All @@ -156,7 +148,7 @@ ze_result_t ZeCall::doCall(ze_result_t ZeResult, const char *ZeName,
if (ZeResult && TraceError) {
const char *ErrorString = "Unknown";
zeParseError(ZeResult, ErrorString);
urPrint("Error (%s) in %s\n", ErrorString, ZeName);
logger::error("Error ({}) in {}", ErrorString, ZeName);
}
return ZeResult;
}
Expand Down
14 changes: 4 additions & 10 deletions source/adapters/level_zero/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ static auto getUrResultString = [](ur_result_t Result) {
#define UR_CALL(Call) \
{ \
if (PrintTrace) \
fprintf(stderr, "UR ---> %s\n", #Call); \
logger::debug("UR ---> {}", #Call); \
ur_result_t Result = (Call); \
if (PrintTrace) \
fprintf(stderr, "UR <--- %s(%s)\n", #Call, getUrResultString(Result)); \
logger::debug("UR <--- {}({})", #Call, getUrResultString(Result)); \
if (Result != UR_RESULT_SUCCESS) \
return Result; \
}
Expand Down Expand Up @@ -258,9 +258,6 @@ class ZeCall {
// setting environment variables.
bool setEnvVar(const char *name, const char *value);

// Prints to stderr if UR_L0_DEBUG allows it
void urPrint(const char *Format, ...);

// Helper for one-liner validation
#define UR_ASSERT(condition, error) \
if (!(condition)) \
Expand Down Expand Up @@ -291,10 +288,10 @@ template <class T> struct ZesStruct : public T {
#define UR_CALL(Call) \
{ \
if (PrintTrace) \
fprintf(stderr, "UR ---> %s\n", #Call); \
logger::debug("UR ---> {}", #Call); \
ur_result_t Result = (Call); \
if (PrintTrace) \
fprintf(stderr, "UR <--- %s(%s)\n", #Call, getUrResultString(Result)); \
logger::debug("UR <--- {}({})", #Call, getUrResultString(Result)); \
if (Result != UR_RESULT_SUCCESS) \
return Result; \
}
Expand All @@ -303,9 +300,6 @@ template <class T> struct ZesStruct : public T {
// setting environment variables.
bool setEnvVar(const char *name, const char *value);

// Prints to stderr if UR_L0_DEBUG allows it
void urPrint(const char *Format, ...);

// Helper for one-liner validation
#define UR_ASSERT(condition, error) \
if (!(condition)) \
Expand Down
7 changes: 5 additions & 2 deletions source/adapters/level_zero/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <string.h>

#include "context.hpp"
#include "logger/ur_logger.hpp"
#include "ur_level_zero.hpp"

UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(
Expand Down Expand Up @@ -174,7 +175,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextSetExtendedDeleter(
std::ignore = Context;
std::ignore = Deleter;
std::ignore = UserData;
urPrint("[UR][L0] %s function not implemented!\n", __FUNCTION__);
logger::error(logger::LegacyMessage("[UR][L0] {} function not implemented!"),
"{} function not implemented!", __FUNCTION__);
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}

Expand Down Expand Up @@ -508,7 +510,8 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
if (ProfilingEnabled)
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
urPrint("ze_event_pool_desc_t flags set to: %d\n", ZeEventPoolDesc.flags);
logger::debug("ze_event_pool_desc_t flags set to: {}",
ZeEventPoolDesc.flags);

std::vector<ze_device_handle_t> ZeDevices;
std::for_each(
Expand Down
Loading

0 comments on commit 5e108ea

Please sign in to comment.