Skip to content

Commit db47fc0

Browse files
committed
Refactor hip adapter to new logger
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 <[email protected]>
1 parent 4d0183a commit db47fc0

File tree

2 files changed

+75
-53
lines changed

2 files changed

+75
-53
lines changed

source/adapters/hip/adapter.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,44 @@
1010

1111
#include "adapter.hpp"
1212
#include "common.hpp"
13+
#include "logger/ur_logger.hpp"
1314

1415
#include <atomic>
1516
#include <ur_api.h>
1617

1718
struct ur_adapter_handle_t_ {
1819
std::atomic<uint32_t> RefCount = 0;
20+
logger::Logger &logger;
21+
ur_adapter_handle_t_();
1922
};
2023

24+
class ur_legacy_sink : public logger::Sink {
25+
public:
26+
ur_legacy_sink(std::string logger_name = "", bool skip_prefix = true)
27+
: Sink(std::move(logger_name), skip_prefix) {
28+
this->ostream = &std::cerr;
29+
}
30+
31+
virtual void print([[maybe_unused]] logger::Level level,
32+
const std::string &msg) override {
33+
std::cerr << msg << std::endl;
34+
}
35+
36+
~ur_legacy_sink() = default;
37+
};
38+
39+
ur_adapter_handle_t_::ur_adapter_handle_t_()
40+
: logger(logger::get_logger("hip")) {
41+
42+
if (std::getenv("UR_LOG_HIP") != nullptr)
43+
return;
44+
45+
if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") != nullptr ||
46+
std::getenv("UR_SUPPRESS_ERROR_MESSAGE") != nullptr) {
47+
logger.setLegacySink(std::make_unique<ur_legacy_sink>());
48+
}
49+
}
50+
2151
ur_adapter_handle_t_ adapter{};
2252

2353
UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(

source/adapters/hip/common.cpp

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99
//===----------------------------------------------------------------------===//
1010
#include "common.hpp"
11+
#include "logger/ur_logger.hpp"
1112

1213
#include <sstream>
1314

@@ -54,35 +55,34 @@ void checkErrorUR(amd_comgr_status_t Result, const char *Function, int Line,
5455
return;
5556
}
5657

57-
if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr ||
58-
std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) {
59-
const char *ErrorString = nullptr;
60-
const char *ErrorName = nullptr;
61-
switch (Result) {
62-
case AMD_COMGR_STATUS_ERROR:
63-
ErrorName = "AMD_COMGR_STATUS_ERROR";
64-
ErrorString = "Generic error";
65-
break;
66-
case AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT:
67-
ErrorName = "AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT";
68-
ErrorString =
69-
"One of the actual arguments does not meet a precondition stated in "
70-
"the documentation of the corresponding formal argument.";
71-
break;
72-
case AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES:
73-
ErrorName = "AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES";
74-
ErrorString = "Failed to allocate the necessary resources";
75-
break;
76-
default:
77-
break;
78-
}
79-
std::cerr << "\nUR HIP ERROR:"
80-
<< "\n\tValue: " << Result
81-
<< "\n\tName: " << ErrorName
82-
<< "\n\tDescription: " << ErrorString
83-
<< "\n\tFunction: " << Function
84-
<< "\n\tSource Location: " << File << ":" << Line << "\n\n";
58+
const char *ErrorString = nullptr;
59+
const char *ErrorName = nullptr;
60+
switch (Result) {
61+
case AMD_COMGR_STATUS_ERROR:
62+
ErrorName = "AMD_COMGR_STATUS_ERROR";
63+
ErrorString = "Generic error";
64+
break;
65+
case AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT:
66+
ErrorName = "AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT";
67+
ErrorString =
68+
"One of the actual arguments does not meet a precondition stated in "
69+
"the documentation of the corresponding formal argument.";
70+
break;
71+
case AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES:
72+
ErrorName = "AMD_COMGR_STATUS_ERROR_OUT_OF_RESOURCES";
73+
ErrorString = "Failed to allocate the necessary resources";
74+
break;
75+
default:
76+
break;
8577
}
78+
std::stringstream SS;
79+
SS << "\nUR HIP ERROR:"
80+
<< "\n\tValue: " << Result
81+
<< "\n\tName: " << ErrorName
82+
<< "\n\tDescription: " << ErrorString
83+
<< "\n\tFunction: " << Function << "\n\tSource Location: " << File
84+
<< ":" << Line << "\n";
85+
logger::error("{}", SS.str());
8686

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

102-
if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr ||
103-
std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) {
104-
const char *ErrorString = nullptr;
105-
const char *ErrorName = nullptr;
106-
ErrorName = hipGetErrorName(Result);
107-
ErrorString = hipGetErrorString(Result);
108-
std::cerr << "\nUR HIP ERROR:"
109-
<< "\n\tValue: " << Result
110-
<< "\n\tName: " << ErrorName
111-
<< "\n\tDescription: " << ErrorString
112-
<< "\n\tFunction: " << Function
113-
<< "\n\tSource Location: " << File << ":" << Line << "\n\n";
114-
}
102+
const char *ErrorString = hipGetErrorString(Result);
103+
const char *ErrorName = hipGetErrorName(Result);
104+
105+
std::stringstream SS;
106+
SS << "\nUR HIP ERROR:"
107+
<< "\n\tValue: " << Result
108+
<< "\n\tName: " << ErrorName
109+
<< "\n\tDescription: " << ErrorString
110+
<< "\n\tFunction: " << Function << "\n\tSource Location: " << File
111+
<< ":" << Line << "\n";
112+
logger::error("{}", SS.str());
115113

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

130-
if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr ||
131-
std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) {
132-
std::cerr << "\nUR HIP ERROR:"
133-
<< "\n\tValue: " << Result
134-
<< "\n\tFunction: " << Function
135-
<< "\n\tSource Location: " << File << ":" << Line << "\n\n";
136-
}
128+
std::stringstream SS;
129+
SS << "\nUR HIP ERROR:"
130+
<< "\n\tValue: " << Result << "\n\tFunction: " << Function
131+
<< "\n\tSource Location: " << File << ":" << Line << "\n";
132+
logger::error("{}", SS.str());
137133

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

159155
void detail::ur::die(const char *pMessage) {
160-
std::cerr << "ur_die: " << pMessage << '\n';
156+
logger::always("ur_die: {}", pMessage);
161157
std::terminate();
162158
}
163159

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

169-
void detail::ur::hipPrint(const char *pMessage) {
170-
std::cerr << "ur_print: " << pMessage << '\n';
171-
}
172-
173165
// Global variables for UR_RESULT_ADAPTER_SPECIFIC_ERROR
174166
thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS;
175167
thread_local char ErrorMessage[MaxMessageSize];

0 commit comments

Comments
 (0)