Skip to content

Commit

Permalink
Refactor hip adapter to new logger
Browse files Browse the repository at this point in the history
This commit refactors the hip adapter to adopt the new logger
introduced in d9cd223 (Integrate logger with library, 2023-02-03).

Signed-off-by: Łukasz Plewa <lukasz.plewa@intel.com>
  • Loading branch information
lplewa committed Mar 19, 2024
1 parent 4d0183a commit db47fc0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 53 deletions.
30 changes: 30 additions & 0 deletions source/adapters/hip/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,44 @@

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

#include <atomic>
#include <ur_api.h>

struct ur_adapter_handle_t_ {
std::atomic<uint32_t> RefCount = 0;
logger::Logger &logger;
ur_adapter_handle_t_();
};

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,
const std::string &msg) override {
std::cerr << msg << std::endl;
}

~ur_legacy_sink() = default;
};

ur_adapter_handle_t_::ur_adapter_handle_t_()
: logger(logger::get_logger("hip")) {

if (std::getenv("UR_LOG_HIP") != nullptr)
return;

if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") != nullptr ||
std::getenv("UR_SUPPRESS_ERROR_MESSAGE") != nullptr) {
logger.setLegacySink(std::make_unique<ur_legacy_sink>());
}
}

ur_adapter_handle_t_ adapter{};

UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(
Expand Down
98 changes: 45 additions & 53 deletions source/adapters/hip/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
//===----------------------------------------------------------------------===//
#include "common.hpp"
#include "logger/ur_logger.hpp"

#include <sstream>

Expand Down Expand Up @@ -54,35 +55,34 @@ void checkErrorUR(amd_comgr_status_t Result, const char *Function, int Line,
return;
}

if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr ||
std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) {
const char *ErrorString = nullptr;
const char *ErrorName = nullptr;
switch (Result) {
case AMD_COMGR_STATUS_ERROR:
ErrorName = "AMD_COMGR_STATUS_ERROR";
ErrorString = "Generic error";
break;
case AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT:
ErrorName = "AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT";
ErrorString =
"One of the actual arguments does not meet a precondition stated in "
"the documentation of the corresponding formal argument.";
break;
case AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES:
ErrorName = "AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES";
ErrorString = "Failed to allocate the necessary resources";
break;
default:
break;
}
std::cerr << "\nUR HIP ERROR:"
<< "\n\tValue: " << Result
<< "\n\tName: " << ErrorName
<< "\n\tDescription: " << ErrorString
<< "\n\tFunction: " << Function
<< "\n\tSource Location: " << File << ":" << Line << "\n\n";
const char *ErrorString = nullptr;
const char *ErrorName = nullptr;
switch (Result) {
case AMD_COMGR_STATUS_ERROR:
ErrorName = "AMD_COMGR_STATUS_ERROR";
ErrorString = "Generic error";
break;
case AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT:
ErrorName = "AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT";
ErrorString =
"One of the actual arguments does not meet a precondition stated in "
"the documentation of the corresponding formal argument.";
break;
case AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES:
ErrorName = "AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES";
ErrorString = "Failed to allocate the necessary resources";
break;
default:
break;
}
std::stringstream SS;
SS << "\nUR HIP ERROR:"
<< "\n\tValue: " << Result
<< "\n\tName: " << ErrorName
<< "\n\tDescription: " << ErrorString
<< "\n\tFunction: " << Function << "\n\tSource Location: " << File
<< ":" << Line << "\n";
logger::error("{}", SS.str());

if (std::getenv("PI_HIP_ABORT") != nullptr ||
std::getenv("UR_HIP_ABORT") != nullptr) {
Expand All @@ -99,19 +99,17 @@ void checkErrorUR(hipError_t Result, const char *Function, int Line,
return;
}

if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr ||
std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) {
const char *ErrorString = nullptr;
const char *ErrorName = nullptr;
ErrorName = hipGetErrorName(Result);
ErrorString = hipGetErrorString(Result);
std::cerr << "\nUR HIP ERROR:"
<< "\n\tValue: " << Result
<< "\n\tName: " << ErrorName
<< "\n\tDescription: " << ErrorString
<< "\n\tFunction: " << Function
<< "\n\tSource Location: " << File << ":" << Line << "\n\n";
}
const char *ErrorString = hipGetErrorString(Result);
const char *ErrorName = hipGetErrorName(Result);

std::stringstream SS;
SS << "\nUR HIP ERROR:"
<< "\n\tValue: " << Result
<< "\n\tName: " << ErrorName
<< "\n\tDescription: " << ErrorString
<< "\n\tFunction: " << Function << "\n\tSource Location: " << File
<< ":" << Line << "\n";
logger::error("{}", SS.str());

if (std::getenv("PI_HIP_ABORT") != nullptr ||
std::getenv("UR_HIP_ABORT") != nullptr) {
Expand All @@ -127,13 +125,11 @@ void checkErrorUR(ur_result_t Result, const char *Function, int Line,
return;
}

if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr ||
std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) {
std::cerr << "\nUR HIP ERROR:"
<< "\n\tValue: " << Result
<< "\n\tFunction: " << Function
<< "\n\tSource Location: " << File << ":" << Line << "\n\n";
}
std::stringstream SS;
SS << "\nUR HIP ERROR:"
<< "\n\tValue: " << Result << "\n\tFunction: " << Function
<< "\n\tSource Location: " << File << ":" << Line << "\n";
logger::error("{}", SS.str());

if (std::getenv("PI_HIP_ABORT") != nullptr ||
std::getenv("UR_HIP_ABORT") != nullptr) {
Expand All @@ -157,7 +153,7 @@ hipError_t getHipVersionString(std::string &Version) {
}

void detail::ur::die(const char *pMessage) {
std::cerr << "ur_die: " << pMessage << '\n';
logger::always("ur_die: {}", pMessage);
std::terminate();
}

Expand All @@ -166,10 +162,6 @@ void detail::ur::assertion(bool Condition, const char *pMessage) {
die(pMessage);
}

void detail::ur::hipPrint(const char *pMessage) {
std::cerr << "ur_print: " << pMessage << '\n';
}

// Global variables for UR_RESULT_ADAPTER_SPECIFIC_ERROR
thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS;
thread_local char ErrorMessage[MaxMessageSize];
Expand Down

0 comments on commit db47fc0

Please sign in to comment.