Skip to content

Commit 1333d4a

Browse files
authored
Merge pull request #1342 from lplewa/cuda_log
Refactor cuda adapter to new logger
2 parents 55cf3ba + 281e3aa commit 1333d4a

File tree

4 files changed

+52
-34
lines changed

4 files changed

+52
-34
lines changed

source/adapters/cuda/adapter.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,42 @@
1111
#include <ur_api.h>
1212

1313
#include "common.hpp"
14+
#include "logger/ur_logger.hpp"
1415
#include "tracing.hpp"
1516

1617
struct ur_adapter_handle_t_ {
1718
std::atomic<uint32_t> RefCount = 0;
1819
std::mutex Mutex;
1920
struct cuda_tracing_context_t_ *TracingCtx = nullptr;
21+
logger::Logger &logger;
22+
ur_adapter_handle_t_();
2023
};
2124

25+
class ur_legacy_sink : public logger::Sink {
26+
public:
27+
ur_legacy_sink(std::string logger_name = "", bool skip_prefix = true)
28+
: Sink(std::move(logger_name), skip_prefix) {
29+
this->ostream = &std::cerr;
30+
}
31+
32+
virtual void print([[maybe_unused]] logger::Level level,
33+
const std::string &msg) override {
34+
std::cerr << msg << std::endl;
35+
}
36+
37+
~ur_legacy_sink() = default;
38+
};
39+
ur_adapter_handle_t_::ur_adapter_handle_t_()
40+
: logger(logger::get_logger("cuda")) {
41+
42+
if (std::getenv("UR_LOG_CUDA") != 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+
}
2250
ur_adapter_handle_t_ adapter{};
2351

2452
UR_APIEXPORT ur_result_t UR_APICALL

source/adapters/cuda/command_buffer.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <ur_api.h>
1313

1414
#include "context.hpp"
15+
#include "logger/ur_logger.hpp"
1516
#include <cuda.h>
1617
#include <memory>
1718

@@ -169,10 +170,10 @@ static inline const char *getUrResultString(ur_result_t Result) {
169170
#define UR_CALL(Call, Result) \
170171
{ \
171172
if (PrintTrace) \
172-
fprintf(stderr, "UR ---> %s\n", #Call); \
173+
logger::always("UR ---> {}", #Call); \
173174
Result = (Call); \
174175
if (PrintTrace) \
175-
fprintf(stderr, "UR <--- %s(%s)\n", #Call, getUrResultString(Result)); \
176+
logger::always("UR <--- {}({})", #Call, getUrResultString(Result)); \
176177
}
177178

178179
// Handle to a kernel command.

source/adapters/cuda/common.cpp

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "common.hpp"
12+
#include "logger/ur_logger.hpp"
1213

1314
#include <cuda.h>
1415

@@ -41,22 +42,18 @@ void checkErrorUR(CUresult Result, const char *Function, int Line,
4142
return;
4243
}
4344

44-
if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr &&
45-
std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) {
46-
const char *ErrorString = nullptr;
47-
const char *ErrorName = nullptr;
48-
cuGetErrorName(Result, &ErrorName);
49-
cuGetErrorString(Result, &ErrorString);
50-
std::stringstream SS;
51-
SS << "\nUR CUDA ERROR:"
52-
<< "\n\tValue: " << Result
53-
<< "\n\tName: " << ErrorName
54-
<< "\n\tDescription: " << ErrorString
55-
<< "\n\tFunction: " << Function << "\n\tSource Location: " << File
56-
<< ":" << Line << "\n"
57-
<< std::endl;
58-
std::cerr << SS.str();
59-
}
45+
const char *ErrorString = nullptr;
46+
const char *ErrorName = nullptr;
47+
cuGetErrorName(Result, &ErrorName);
48+
cuGetErrorString(Result, &ErrorString);
49+
std::stringstream SS;
50+
SS << "\nUR CUDA ERROR:"
51+
<< "\n\tValue: " << Result
52+
<< "\n\tName: " << ErrorName
53+
<< "\n\tDescription: " << ErrorString
54+
<< "\n\tFunction: " << Function << "\n\tSource Location: " << File
55+
<< ":" << Line << "\n";
56+
logger::error("{}", SS.str());
6057

6158
if (std::getenv("PI_CUDA_ABORT") != nullptr ||
6259
std::getenv("UR_CUDA_ABORT") != nullptr) {
@@ -72,16 +69,11 @@ void checkErrorUR(ur_result_t Result, const char *Function, int Line,
7269
return;
7370
}
7471

75-
if (std::getenv("SYCL_PI_SUPPRESS_ERROR_MESSAGE") == nullptr &&
76-
std::getenv("UR_SUPPRESS_ERROR_MESSAGE") == nullptr) {
77-
std::stringstream SS;
78-
SS << "\nUR ERROR:"
79-
<< "\n\tValue: " << Result
80-
<< "\n\tFunction: " << Function << "\n\tSource Location: " << File
81-
<< ":" << Line << "\n"
82-
<< std::endl;
83-
std::cerr << SS.str();
84-
}
72+
std::stringstream SS;
73+
SS << "\nUR ERROR:"
74+
<< "\n\tValue: " << Result << "\n\tFunction: " << Function
75+
<< "\n\tSource Location: " << File << ":" << Line << "\n";
76+
logger::error("{}", SS.str());
8577

8678
if (std::getenv("PI_CUDA_ABORT") != nullptr) {
8779
std::abort();
@@ -101,7 +93,7 @@ std::string getCudaVersionString() {
10193
}
10294

10395
void detail::ur::die(const char *Message) {
104-
std::cerr << "ur_die: " << Message << std::endl;
96+
logger::always("ur_die:{}", Message);
10597
std::terminate();
10698
}
10799

@@ -110,10 +102,6 @@ void detail::ur::assertion(bool Condition, const char *Message) {
110102
die(Message);
111103
}
112104

113-
void detail::ur::cuPrint(const char *Message) {
114-
std::cerr << "ur_print: " << Message << std::endl;
115-
}
116-
117105
// Global variables for ZER_EXT_RESULT_ADAPTER_SPECIFIC_ERROR
118106
thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS;
119107
thread_local char ErrorMessage[MaxMessageSize];

source/adapters/cuda/device.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "adapter.hpp"
1616
#include "context.hpp"
1717
#include "device.hpp"
18+
#include "logger/ur_logger.hpp"
1819
#include "platform.hpp"
1920
#include "ur_util.hpp"
2021

@@ -293,7 +294,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
293294
std::getenv("UR_CUDA_ENABLE_IMAGE_SUPPORT") != nullptr) {
294295
Enabled = true;
295296
} else {
296-
detail::ur::cuPrint(
297+
logger::always(
297298
"Images are not fully supported by the CUDA BE, their support is "
298299
"disabled by default. Their partial support can be activated by "
299300
"setting SYCL_PI_CUDA_ENABLE_IMAGE_SUPPORT environment variable at "

0 commit comments

Comments
 (0)