Skip to content

Commit eb5ab7a

Browse files
authored
Merge pull request #136 from grnydawn/ykim/omega/logging
fixed bugs in Omega logging
2 parents 2b346b2 + 7e723b7 commit eb5ab7a

File tree

7 files changed

+202
-121
lines changed

7 files changed

+202
-121
lines changed

components/omega/OmegaBuild.cmake

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ set(CASEROOT "${OMEGA_BUILD_DIR}/e3smcase")
2727
macro(common)
2828

2929
option(OMEGA_DEBUG "Turn on error message throwing (default OFF)." OFF)
30-
option(OMEGA_LOG_UNBUFFERED "Turn on unbuffered logging (default OFF)." OFF)
30+
option(OMEGA_LOG_FLUSH "Turn on unbuffered logging (default OFF)." OFF)
3131

3232
if(NOT DEFINED OMEGA_CXX_FLAGS)
3333
set(OMEGA_CXX_FLAGS "")
@@ -521,6 +521,7 @@ macro(update_variables)
521521
endif()
522522

523523
if(OMEGA_DEBUG)
524+
set(OMEGA_LOG_FLUSH ON)
524525
add_definitions(-DOMEGA_DEBUG -DOMEGA_LOG_LEVEL=1)
525526
else()
526527
string(TOUPPER "${OMEGA_LOG_LEVEL}" _LOG_LEVEL)
@@ -543,8 +544,8 @@ macro(update_variables)
543544
endif()
544545
endif()
545546

546-
if(OMEGA_LOG_UNBUFFERED)
547-
add_definitions(-DOMEGA_LOG_UNBUFFERED)
547+
if(OMEGA_LOG_FLUSH)
548+
add_definitions(-DOMEGA_LOG_FLUSH)
548549
endif()
549550

550551
if(OMEGA_LOG_TASKS)

components/omega/doc/devGuide/CMakeBuild.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ OMEGA_HIP_FLAGS: HIP compiler flags
6666
OMEGA_MEMORY_LAYOUT: Kokkos memory layout ("LEFT" or "RIGHT"). "RIGHT" is a default value.
6767
OMEGA_TILE_LENGTH: a length of one "side" of a Kokkos tile. 64 is a default value.
6868
OMEGA_LOG_LEVEL: a default logging level. "OMEGA_LOG_INFO" is a default value.
69-
OMEGA_LOG_UNBUFFERED: turn on the unbuffered logging. "OFF" is a default value.
69+
OMEGA_LOG_FLUSH: turn on the unbuffered logging. "OFF" is a default value.
7070
OMEGA_LOG_TASKS: set the tasks that generate log file. "0" is a default value.
7171
```
7272

components/omega/src/infra/LogFormatters.h

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,40 @@
1212
#include "DataTypes.h"
1313
#include <spdlog/spdlog.h>
1414

15-
// TODO:
16-
// 1. Use template to create formatter for various array types
17-
// 2. Consider using some of the following for formatting
18-
// View.rank()
19-
// View.rank_dynamic()
20-
// View.stride_(0, 1,2,3...)()
21-
// View.span()
22-
// View.size()
23-
// View.span_is_contiguous()
24-
// View.use_count()
25-
// View.label()
26-
// View.is_allocated()
27-
// ExecSpace.name()
28-
// ExecSpace.print_configuration(ostr);
29-
// ExecSpace.print_configuration(ostr, detail);
30-
// MemSpace.name()
31-
32-
template <>
33-
struct fmt::formatter<OMEGA::HostArray1DReal> : fmt::formatter<std::string> {
34-
auto format(OMEGA::HostArray1DReal my,
35-
format_context &ctx) -> decltype(ctx.out()) {
3615
#ifdef OMEGA_DEBUG
37-
return fmt::format_to(
38-
ctx.out(), "[data type of '{}' is HostArray1DReal.]", my.label());
16+
#define GENERATE_FORMATTER(D, T) \
17+
template <> \
18+
struct fmt::formatter<OMEGA::Array##D##T> : fmt::formatter<std::string> { \
19+
auto format(OMEGA::Array##D##T my, \
20+
format_context &ctx) -> decltype(ctx.out()) { \
21+
return fmt::format_to(ctx.out(), "{}({}D:{})", my.label(), my.rank(), \
22+
my.size()); \
23+
} \
24+
};
3925
#else
40-
return fmt::format_to(ctx.out(), "[data type of '' is HostArray1DReal.]");
26+
#define GENERATE_FORMATTER(D, T) \
27+
template <> \
28+
struct fmt::formatter<OMEGA::Array##D##T> : fmt::formatter<std::string> { \
29+
auto format(OMEGA::Array##D##T my, \
30+
format_context &ctx) -> decltype(ctx.out()) { \
31+
return fmt::format_to(ctx.out(), "{}", my.label()); \
32+
} \
33+
};
4134
#endif
42-
}
43-
};
4435

45-
template <>
46-
struct fmt::formatter<OMEGA::HostArray2DReal> : fmt::formatter<std::string> {
47-
auto format(OMEGA::HostArray2DReal my,
48-
format_context &ctx) -> decltype(ctx.out()) {
49-
#ifdef OMEGA_DEBUG
50-
return fmt::format_to(
51-
ctx.out(), "[data type of '{}' is HostArray2DReal.]", my.label());
52-
#else
53-
return fmt::format_to(ctx.out(), "[data type of '' is HostArray2DReal.]");
54-
#endif
55-
}
56-
};
36+
#define GENERATE_FORMATTER_DIM(D) \
37+
GENERATE_FORMATTER(D, I4) \
38+
GENERATE_FORMATTER(D, I8) \
39+
GENERATE_FORMATTER(D, R4) \
40+
GENERATE_FORMATTER(D, R8)
41+
42+
GENERATE_FORMATTER_DIM(1D)
43+
GENERATE_FORMATTER_DIM(2D)
44+
GENERATE_FORMATTER_DIM(3D)
45+
GENERATE_FORMATTER_DIM(4D)
46+
GENERATE_FORMATTER_DIM(5D)
47+
48+
#undef GENERATE_FORMATTER_DIM
49+
#undef GENERATE_FORMATTER
5750

5851
#endif // OMEGA_LOG_FORMATTERS_H

components/omega/src/infra/Logging.cpp

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
/// This implements Omega logging initialization.
77
//
88
//===----------------------------------------------------------------------===//
9-
10-
#define _OMEGA_STRINGIFY(x) #x
11-
#define _OMEGA_TOSTRING(x) _OMEGA_STRINGIFY(x)
12-
139
#include "Logging.h"
1410
#include "MachEnv.h"
1511
#include <iostream>
1612
#include <spdlog/sinks/basic_file_sink.h>
1713

14+
#define _OMEGA_STRINGIFY(x) #x
15+
#define _OMEGA_TOSTRING(x) _OMEGA_STRINGIFY(x)
16+
1817
namespace OMEGA {
1918

2019
// Function to pack log message
@@ -30,15 +29,17 @@ std::string _PackLogMsg(const char *file, int line, const std::string &msg) {
3029
return "[" + path + ":" + std::to_string(line) + "] " + msg;
3130
}
3231

33-
std::vector<int> splitTasks(const std::string &str) {
32+
std::vector<int> splitTasks(const std::string &str, const OMEGA::I4 NumTasks) {
3433
std::vector<int> Tasks;
3534
std::stringstream ss(str);
3635
std::string Task;
3736
int Start, Stop;
3837
char Dash;
3938

4039
if (str == "ALL") {
41-
Tasks.push_back(-1);
40+
for (int i = 0; i < NumTasks; ++i) {
41+
Tasks.push_back(i);
42+
}
4243
return Tasks;
4344
}
4445

@@ -59,25 +60,58 @@ std::vector<int> splitTasks(const std::string &str) {
5960
}
6061
}
6162

63+
if (std::find(Tasks.begin(), Tasks.end(), -1) != Tasks.end()) {
64+
Tasks.clear();
65+
for (int i = 0; i < NumTasks; ++i) {
66+
Tasks.push_back(i);
67+
}
68+
}
69+
6270
return Tasks;
6371
}
6472

65-
int initLogging(std::shared_ptr<spdlog::logger> Logger) {
73+
// return code: 1->enabled, 0->disabled, negative values->errors
74+
int initLogging(const OMEGA::MachEnv *DefEnv,
75+
std::shared_ptr<spdlog::logger> Logger) {
76+
77+
int RetVal = 0;
6678

67-
int RetVal = 1;
79+
OMEGA::I4 TaskId = DefEnv->getMyTask();
80+
OMEGA::I4 NumTasks = DefEnv->getNumTasks();
6881

69-
spdlog::set_default_logger(Logger);
82+
std::vector<int> Tasks =
83+
splitTasks(_OMEGA_TOSTRING(OMEGA_LOG_TASKS), NumTasks);
84+
85+
if (Tasks.size() > 0 &&
86+
(std::find(Tasks.begin(), Tasks.end(), TaskId) != Tasks.end())) {
87+
88+
spdlog::set_default_logger(Logger);
89+
90+
spdlog::set_pattern("[%n %l] %v");
91+
spdlog::set_level(
92+
static_cast<spdlog::level::level_enum>(SPDLOG_ACTIVE_LEVEL));
93+
spdlog::flush_on(spdlog::level::warn);
94+
95+
RetVal = 1; // log enabled
96+
97+
} else {
98+
spdlog::set_level(spdlog::level::off);
99+
RetVal = 0; // log disabled
100+
}
70101

71102
return RetVal;
72103
}
73104

105+
// return code: 1->enabled, 0->disabled, negative values->errors
74106
int initLogging(const OMEGA::MachEnv *DefEnv, std::string const &LogFilePath) {
75107

76-
int RetVal;
108+
int RetVal = 0;
77109

78-
OMEGA::I4 TaskId = DefEnv->getMyTask();
110+
OMEGA::I4 TaskId = DefEnv->getMyTask();
111+
OMEGA::I4 NumTasks = DefEnv->getNumTasks();
79112

80-
std::vector<int> Tasks = splitTasks(_OMEGA_TOSTRING(OMEGA_LOG_TASKS));
113+
std::vector<int> Tasks =
114+
splitTasks(_OMEGA_TOSTRING(OMEGA_LOG_TASKS), NumTasks);
81115

82116
if (Tasks.size() > 0 &&
83117
(std::find(Tasks.begin(), Tasks.end(), TaskId) != Tasks.end())) {
@@ -89,27 +123,28 @@ int initLogging(const OMEGA::MachEnv *DefEnv, std::string const &LogFilePath) {
89123
auto NewLogFilePath = LogFilePath.substr(0, dotPos) + "_" +
90124
std::to_string(TaskId) +
91125
LogFilePath.substr(dotPos);
92-
initLogging(spdlog::basic_logger_mt("*", NewLogFilePath));
93-
126+
spdlog::set_default_logger(
127+
spdlog::basic_logger_mt("*", NewLogFilePath));
94128
} else {
95-
initLogging(spdlog::basic_logger_mt("*", LogFilePath));
129+
spdlog::set_default_logger(
130+
spdlog::basic_logger_mt("*", LogFilePath));
96131
}
97132

98133
spdlog::set_pattern("[%n %l] %v");
99134
spdlog::set_level(
100135
static_cast<spdlog::level::level_enum>(SPDLOG_ACTIVE_LEVEL));
101136
spdlog::flush_on(spdlog::level::warn);
102137

103-
RetVal = 1;
138+
RetVal = 1; // log enalbed
104139

105140
} catch (spdlog::spdlog_ex const &Ex) {
106141
std::cout << "Log init failed: " << Ex.what() << std::endl;
107-
RetVal = -1;
142+
RetVal = -1; // error occured
108143
}
109144

110145
} else {
111146
spdlog::set_level(spdlog::level::off);
112-
RetVal = 0;
147+
RetVal = 0; // log disabled
113148
}
114149

115150
return RetVal;

components/omega/src/infra/Logging.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
#define SPDLOG_ACTIVE_LEVEL 2
1818
#endif
1919

20-
#include "LogFormatters.h"
20+
#include "DataTypes.h"
2121
#include <spdlog/spdlog.h>
2222
#include <string>
2323

24-
#if defined(OMEGA_LOG_UNBUFFERED)
25-
#define _LOG_FLUSH spdlog::logger::flush()
24+
#include "LogFormatters.h"
25+
26+
#if defined(OMEGA_LOG_FLUSH)
27+
#define _LOG_FLUSH \
28+
spdlog::apply_all([](std::shared_ptr<spdlog::logger> l) { l->flush(); })
2629
#else
2730
#define _LOG_FLUSH (void)0
2831
#endif
@@ -77,10 +80,14 @@ class MachEnv;
7780

7881
const std::string OmegaDefaultLogfile = "omega.log";
7982

80-
int initLogging(std::shared_ptr<spdlog::logger> Logger);
83+
int initLogging(const OMEGA::MachEnv *DefEnv,
84+
std::shared_ptr<spdlog::logger> Logger);
85+
8186
int initLogging(const OMEGA::MachEnv *DefEnv,
8287
std::string const &LogFilePath = OmegaDefaultLogfile);
88+
8389
std::string _PackLogMsg(const char *file, int line, const std::string &msg);
90+
8491
} // namespace OMEGA
8592

8693
#endif // OMEGA_LOG_H

0 commit comments

Comments
 (0)