Skip to content

Commit d6af758

Browse files
authored
Merge pull request #329 from veselypeta/petr/format-tests
[ur] Enable better formating for tests
2 parents 7799dec + 604544f commit d6af758

File tree

104 files changed

+10216
-7723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+10216
-7723
lines changed

.clang-format

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ BasedOnStyle: LLVM
44
IndentWidth: 4
55
InsertBraces: true
66
ReflowComments: false
7-
ColumnLimit: 0
87
...

examples/collector/collector.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@
2525
#include "ur_api.h"
2626
#include "xpti/xpti_trace_framework.h"
2727

28-
constexpr uint16_t TRACE_FN_BEGIN = static_cast<uint16_t>(xpti::trace_point_type_t::function_with_args_begin);
29-
constexpr uint16_t TRACE_FN_END = static_cast<uint16_t>(xpti::trace_point_type_t::function_with_args_end);
28+
constexpr uint16_t TRACE_FN_BEGIN =
29+
static_cast<uint16_t>(xpti::trace_point_type_t::function_with_args_begin);
30+
constexpr uint16_t TRACE_FN_END =
31+
static_cast<uint16_t>(xpti::trace_point_type_t::function_with_args_end);
3032
constexpr std::string_view UR_STREAM_NAME = "ur";
3133

3234
/**
3335
* @brief Formats the function parameters and arguments for urInit
3436
*/
35-
std::ostream &operator<<(std::ostream &os, const struct ur_init_params_t *params) {
37+
std::ostream &operator<<(std::ostream &os,
38+
const struct ur_init_params_t *params) {
3639
os << ".device_flags = ";
3740
if (*params->pdevice_flags & UR_DEVICE_INIT_FLAG_GPU) {
3841
os << "UR_DEVICE_INIT_FLAG_GPU";
@@ -48,14 +51,15 @@ std::ostream &operator<<(std::ostream &os, const struct ur_init_params_t *params
4851
* This example only implements a handler for one function, `urInit`, but it's
4952
* trivial to expand it to support more.
5053
*/
51-
static std::unordered_map<std::string_view,
52-
std::function<void(const xpti::function_with_args_t *, std::ostream &)>>
53-
handlers =
54-
{
55-
{"urInit", [](const xpti::function_with_args_t *fn_args, std::ostream &os) {
56-
auto params = static_cast<const struct ur_init_params_t *>(fn_args->args_data);
57-
os << params;
58-
}}};
54+
static std::unordered_map<
55+
std::string_view,
56+
std::function<void(const xpti::function_with_args_t *, std::ostream &)>>
57+
handlers = {{"urInit", [](const xpti::function_with_args_t *fn_args,
58+
std::ostream &os) {
59+
auto params = static_cast<const struct ur_init_params_t *>(
60+
fn_args->args_data);
61+
os << params;
62+
}}};
5963

6064
/**
6165
* @brief Tracing callback invoked by the dispatcher on every event.
@@ -74,7 +78,8 @@ XPTI_CALLBACK_API void trace_cb(uint16_t trace_type,
7478
auto *args = static_cast<const xpti::function_with_args_t *>(user_data);
7579
std::ostringstream out;
7680
if (trace_type == TRACE_FN_BEGIN) {
77-
out << "function_with_args_begin(" << instance << ") - " << args->function_name << "(";
81+
out << "function_with_args_begin(" << instance << ") - "
82+
<< args->function_name << "(";
7883
auto it = handlers.find(args->function_name);
7984
if (it == handlers.end()) {
8085
out << "unimplemented";
@@ -84,7 +89,9 @@ XPTI_CALLBACK_API void trace_cb(uint16_t trace_type,
8489
out << ");";
8590
} else if (trace_type == TRACE_FN_END) {
8691
auto result = static_cast<const ur_result_t *>(args->ret_data);
87-
out << "function_with_args_end(" << instance << ") - " << args->function_name << "(...) -> ur_result_t(" << *result << ");";
92+
out << "function_with_args_end(" << instance << ") - "
93+
<< args->function_name << "(...) -> ur_result_t(" << *result
94+
<< ");";
8895
} else {
8996
out << "unsupported trace type";
9097
}
@@ -105,12 +112,18 @@ XPTI_CALLBACK_API void xptiTraceInit(unsigned int major_version,
105112
const char *version_str,
106113
const char *stream_name) {
107114
if (!stream_name || std::string_view(stream_name) != UR_STREAM_NAME) {
108-
std::cout << "Invalid stream name: " << stream_name << ". Expected " << UR_STREAM_NAME << ". Aborting." << std::endl;
115+
std::cout << "Invalid stream name: " << stream_name << ". Expected "
116+
<< UR_STREAM_NAME << ". Aborting." << std::endl;
109117
return;
110118
}
111119

112-
if (UR_MAKE_VERSION(major_version, minor_version) != UR_API_VERSION_CURRENT) {
113-
std::cout << "Invalid stream version: " << major_version << "." << minor_version << ". Expected " << UR_MAJOR_VERSION(UR_API_VERSION_CURRENT) << "." << UR_MINOR_VERSION(UR_API_VERSION_CURRENT) << ". Aborting." << std::endl;
120+
if (UR_MAKE_VERSION(major_version, minor_version) !=
121+
UR_API_VERSION_CURRENT) {
122+
std::cout << "Invalid stream version: " << major_version << "."
123+
<< minor_version << ". Expected "
124+
<< UR_MAJOR_VERSION(UR_API_VERSION_CURRENT) << "."
125+
<< UR_MINOR_VERSION(UR_API_VERSION_CURRENT) << ". Aborting."
126+
<< std::endl;
114127
return;
115128
}
116129

@@ -130,6 +143,5 @@ XPTI_CALLBACK_API void xptiTraceInit(unsigned int major_version,
130143
*
131144
* Can be used to cleanup state or resources.
132145
*/
133-
XPTI_CALLBACK_API void xptiTraceFinish(const char *stream_name) {
134-
/* noop */
146+
XPTI_CALLBACK_API void xptiTraceFinish(const char *stream_name) { /* noop */
135147
}

examples/hello_world/hello_world.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,64 @@ int main(int argc, char *argv[]) {
2929

3030
status = urPlatformGet(1, nullptr, &platformCount);
3131
if (status != UR_RESULT_SUCCESS) {
32-
std::cout << "urPlatformGet failed with return code: " << status << std::endl;
32+
std::cout << "urPlatformGet failed with return code: " << status
33+
<< std::endl;
3334
goto out;
3435
}
3536

3637
platforms.resize(platformCount);
3738
status = urPlatformGet(platformCount, platforms.data(), nullptr);
3839
if (status != UR_RESULT_SUCCESS) {
39-
std::cout << "urPlatformGet failed with return code: " << status << std::endl;
40+
std::cout << "urPlatformGet failed with return code: " << status
41+
<< std::endl;
4042
goto out;
4143
}
4244

4345
for (auto p : platforms) {
4446
ur_api_version_t api_version = {};
4547
status = urPlatformGetApiVersion(p, &api_version);
4648
if (status != UR_RESULT_SUCCESS) {
47-
std::cout << "urPlatformGetApiVersion failed with return code: " << status << std::endl;
49+
std::cout << "urPlatformGetApiVersion failed with return code: "
50+
<< status << std::endl;
4851
goto out;
4952
}
50-
std::cout << "API version: " << UR_MAJOR_VERSION(api_version) << "." << UR_MINOR_VERSION(api_version) << std::endl;
53+
std::cout << "API version: " << UR_MAJOR_VERSION(api_version) << "."
54+
<< UR_MINOR_VERSION(api_version) << std::endl;
5155

5256
uint32_t deviceCount = 0;
5357
status = urDeviceGet(p, UR_DEVICE_TYPE_GPU, 0, nullptr, &deviceCount);
5458
if (status != UR_RESULT_SUCCESS) {
55-
std::cout << "urDeviceGet failed with return code: " << status << std::endl;
59+
std::cout << "urDeviceGet failed with return code: " << status
60+
<< std::endl;
5661
goto out;
5762
}
5863

5964
std::vector<ur_device_handle_t> devices(deviceCount);
60-
status = urDeviceGet(p, UR_DEVICE_TYPE_GPU, deviceCount, devices.data(), nullptr);
65+
status = urDeviceGet(p, UR_DEVICE_TYPE_GPU, deviceCount, devices.data(),
66+
nullptr);
6167
if (status != UR_RESULT_SUCCESS) {
62-
std::cout << "urDeviceGet failed with return code: " << status << std::endl;
68+
std::cout << "urDeviceGet failed with return code: " << status
69+
<< std::endl;
6370
goto out;
6471
}
6572
for (auto d : devices) {
6673
ur_device_type_t device_type;
67-
status = urDeviceGetInfo(d, UR_DEVICE_INFO_TYPE, sizeof(ur_device_type_t), static_cast<void *>(&device_type), nullptr);
74+
status = urDeviceGetInfo(
75+
d, UR_DEVICE_INFO_TYPE, sizeof(ur_device_type_t),
76+
static_cast<void *>(&device_type), nullptr);
6877
if (status != UR_RESULT_SUCCESS) {
69-
std::cout << "urDeviceGetInfo failed with return code: " << status << std::endl;
78+
std::cout << "urDeviceGetInfo failed with return code: "
79+
<< status << std::endl;
7080
goto out;
7181
}
7282
static const size_t DEVICE_NAME_MAX_LEN = 1024;
7383
char device_name[DEVICE_NAME_MAX_LEN] = {0};
74-
status = urDeviceGetInfo(d, UR_DEVICE_INFO_NAME, DEVICE_NAME_MAX_LEN - 1, static_cast<void *>(&device_name), nullptr);
84+
status =
85+
urDeviceGetInfo(d, UR_DEVICE_INFO_NAME, DEVICE_NAME_MAX_LEN - 1,
86+
static_cast<void *>(&device_name), nullptr);
7587
if (status != UR_RESULT_SUCCESS) {
76-
std::cout << "urDeviceGetInfo failed with return code: " << status << std::endl;
88+
std::cout << "urDeviceGetInfo failed with return code: "
89+
<< status << std::endl;
7790
goto out;
7891
}
7992
if (device_type == UR_DEVICE_TYPE_GPU) {

include/.clang-format

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: LLVM
4+
IndentWidth: 4
5+
InsertBraces: true
6+
ReflowComments: false
7+
ColumnLimit: 0
8+
...

source/common/logger/ur_level.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99

1010
namespace logger {
1111

12-
enum class Level { DEBUG,
13-
INFO,
14-
WARN,
15-
ERR,
16-
QUIET };
12+
enum class Level { DEBUG, INFO, WARN, ERR, QUIET };
1713

1814
inline constexpr auto level_to_str(Level level) {
1915
switch (level) {

source/common/logger/ur_logger.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ inline Logger &get_logger(std::string name = "common") {
1919
return logger;
2020
}
2121

22-
inline void init(std::string name) {
23-
get_logger(name);
24-
}
22+
inline void init(std::string name) { get_logger(name); }
2523

2624
template <typename... Args>
2725
inline void debug(const char *format, Args &&...args) {
@@ -45,7 +43,9 @@ inline void error(const char *format, Args &&...args) {
4543

4644
inline void setLevel(logger::Level level) { get_logger().setLevel(level); }
4745

48-
inline void setFlushLevel(logger::Level level) { get_logger().setFlushLevel(level); }
46+
inline void setFlushLevel(logger::Level level) {
47+
get_logger().setFlushLevel(level);
48+
}
4949

5050
/// @brief Create an instance of the logger with parameters obtained from the respective
5151
/// environment variable or with default configuration if the env var is empty,
@@ -102,10 +102,13 @@ inline Logger create_logger(std::string logger_name) {
102102
values = kv->second;
103103
}
104104

105-
sink = values.size() == 2 ? sink_from_str(logger_name, values[0], values[1])
106-
: sink_from_str(logger_name, values[0]);
105+
sink = values.size() == 2
106+
? sink_from_str(logger_name, values[0], values[1])
107+
: sink_from_str(logger_name, values[0]);
107108
} catch (const std::invalid_argument &e) {
108-
std::cerr << "Error when creating a logger instance from environment variable" << e.what();
109+
std::cerr
110+
<< "Error when creating a logger instance from environment variable"
111+
<< e.what();
109112
return Logger(std::make_unique<logger::StderrSink>(logger_name));
110113
}
111114
sink->setFlushLevel(flush_level);

source/common/logger/ur_logger_details.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ class Logger {
4343
this->sink->setFlushLevel(level);
4444
}
4545

46-
template <typename... Args>
47-
void debug(const char *format, Args &&...args) {
46+
template <typename... Args> void debug(const char *format, Args &&...args) {
4847
log(logger::Level::DEBUG, format, std::forward<Args>(args)...);
4948
}
5049

51-
template <typename... Args>
52-
void info(const char *format, Args &&...args) {
50+
template <typename... Args> void info(const char *format, Args &&...args) {
5351
log(logger::Level::INFO, format, std::forward<Args>(args)...);
5452
}
5553

@@ -58,8 +56,7 @@ class Logger {
5856
log(logger::Level::WARN, format, std::forward<Args>(args)...);
5957
}
6058

61-
template <typename... Args>
62-
void error(const char *format, Args &&...args) {
59+
template <typename... Args> void error(const char *format, Args &&...args) {
6360
log(logger::Level::ERR, format, std::forward<Args>(args)...);
6461
}
6562

source/common/logger/ur_sinks.hpp

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class Sink {
3030
std::ostream *ostream;
3131
logger::Level flush_level;
3232

33-
Sink(std::string logger_name) : logger_name(logger_name) { flush_level = logger::Level::ERR; }
33+
Sink(std::string logger_name) : logger_name(logger_name) {
34+
flush_level = logger::Level::ERR;
35+
}
3436

3537
private:
3638
std::string logger_name;
@@ -45,20 +47,21 @@ class Sink {
4547
if (*(++fmt) == '{') {
4648
*ostream << *fmt++;
4749
} else {
48-
throw std::runtime_error("No arguments provided and braces not escaped!");
50+
throw std::runtime_error(
51+
"No arguments provided and braces not escaped!");
4952
}
5053
} else if (*fmt == '}') {
5154
if (*(++fmt) == '}') {
5255
*ostream << *fmt++;
5356
} else {
54-
throw std::runtime_error("Closing curly brace not escaped!");
57+
throw std::runtime_error(
58+
"Closing curly brace not escaped!");
5559
}
5660
}
5761
}
5862
}
5963

60-
template <typename Arg>
61-
void format(const char *fmt, Arg &&arg) {
64+
template <typename Arg> void format(const char *fmt, Arg &&arg) {
6265
while (*fmt != '\0') {
6366
while (*fmt != '{' && *fmt != '}' && *fmt != '\0') {
6467
*ostream << *fmt++;
@@ -77,7 +80,8 @@ class Sink {
7780
if (*(++fmt) == '}') {
7881
*ostream << *fmt++;
7982
} else {
80-
throw std::runtime_error("Closing curly brace not escaped!");
83+
throw std::runtime_error(
84+
"Closing curly brace not escaped!");
8185
}
8286
}
8387
}
@@ -104,7 +108,8 @@ class Sink {
104108
if (*(++fmt) == '}') {
105109
*ostream << *fmt++;
106110
} else {
107-
throw std::runtime_error("Closing curly brace not escaped!");
111+
throw std::runtime_error(
112+
"Closing curly brace not escaped!");
108113
}
109114
}
110115
}
@@ -115,9 +120,12 @@ class Sink {
115120

116121
class StdoutSink : public Sink {
117122
public:
118-
StdoutSink(std::string logger_name) : Sink(logger_name) { this->ostream = &std::cout; }
123+
StdoutSink(std::string logger_name) : Sink(logger_name) {
124+
this->ostream = &std::cout;
125+
}
119126

120-
StdoutSink(std::string logger_name, Level flush_lvl) : StdoutSink(logger_name) {
127+
StdoutSink(std::string logger_name, Level flush_lvl)
128+
: StdoutSink(logger_name) {
121129
this->flush_level = flush_lvl;
122130
}
123131

@@ -126,9 +134,12 @@ class StdoutSink : public Sink {
126134

127135
class StderrSink : public Sink {
128136
public:
129-
StderrSink(std::string logger_name) : Sink(logger_name) { this->ostream = &std::cerr; }
137+
StderrSink(std::string logger_name) : Sink(logger_name) {
138+
this->ostream = &std::cerr;
139+
}
130140

131-
StderrSink(std::string logger_name, Level flush_lvl) : StderrSink(logger_name) {
141+
StderrSink(std::string logger_name, Level flush_lvl)
142+
: StderrSink(logger_name) {
132143
this->flush_level = flush_lvl;
133144
}
134145

@@ -137,7 +148,8 @@ class StderrSink : public Sink {
137148

138149
class FileSink : public Sink {
139150
public:
140-
FileSink(std::string logger_name, std::string file_path) : Sink(logger_name) {
151+
FileSink(std::string logger_name, std::string file_path)
152+
: Sink(logger_name) {
141153
ofstream = std::ofstream(file_path, std::ofstream::out);
142154
if (ofstream.rdstate() != std::ofstream::goodbit) {
143155
throw std::invalid_argument(
@@ -147,21 +159,25 @@ class FileSink : public Sink {
147159
this->ostream = &ofstream;
148160
}
149161

150-
FileSink(std::string logger_name, std::string file_path, Level flush_lvl) : FileSink(logger_name, file_path) {
162+
FileSink(std::string logger_name, std::string file_path, Level flush_lvl)
163+
: FileSink(logger_name, file_path) {
151164
this->flush_level = flush_lvl;
152165
}
153166

154167
private:
155168
std::ofstream ofstream;
156169
};
157170

158-
inline std::unique_ptr<Sink> sink_from_str(std::string logger_name, std::string name, std::string file_path = "") {
171+
inline std::unique_ptr<Sink> sink_from_str(std::string logger_name,
172+
std::string name,
173+
std::string file_path = "") {
159174
if (name == "stdout") {
160175
return std::make_unique<logger::StdoutSink>(logger_name);
161176
} else if (name == "stderr") {
162177
return std::make_unique<logger::StderrSink>(logger_name);
163178
} else if (name == "file" && !file_path.empty()) {
164-
return std::make_unique<logger::FileSink>(logger_name, file_path.c_str());
179+
return std::make_unique<logger::FileSink>(logger_name,
180+
file_path.c_str());
165181
}
166182

167183
throw std::invalid_argument(

0 commit comments

Comments
 (0)