Skip to content

Commit 47aa1aa

Browse files
committed
#2397: formatting: use the new format_as to significantly reduce complexity
1 parent 34ae400 commit 47aa1aa

File tree

13 files changed

+103
-269
lines changed

13 files changed

+103
-269
lines changed

cmake_config.h.in

+2
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@
9797
#define INCLUDE_FMT_CORE <fmt/core.h>
9898
#define INCLUDE_FMT_FORMAT <fmt/format.h>
9999
#define INCLUDE_FMT_OSTREAM <fmt/ostream.h>
100+
#define INCLUDE_FMT_BASE <fmt/base.h>
100101

101102
#define VT_FMT_NAMESPACE_BEGIN namespace fmt {
102103
#define VT_FMT_NAMESPACE_END }
103104
#else
104105
#define INCLUDE_FMT_CORE <fmt-vt/core.h>
105106
#define INCLUDE_FMT_FORMAT <fmt-vt/format.h>
106107
#define INCLUDE_FMT_OSTREAM <fmt-vt/ostream.h>
108+
#define INCLUDE_FMT_BASE <fmt-vt/base.h>
107109

108110
#define VT_FMT_NAMESPACE_BEGIN namespace fmt { inline namespace vt {
109111
#define VT_FMT_NAMESPACE_END } }

src/vt/collective/reduce/reduce_scope.h

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848

4949
#include <unordered_map>
5050
#include <variant>
51+
#include <string>
52+
53+
#include INCLUDE_FMT_FORMAT
5154

5255
namespace vt { namespace collective { namespace reduce { namespace detail {
5356

src/vt/elm/elm_id.h

+9-46
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
#include "vt/configs/types/types_type.h"
4848
#include "vt/configs/types/types_sentinels.h"
4949

50+
#include "vt/cmake_config.h"
51+
#include INCLUDE_FMT_FORMAT
52+
5053
namespace vt { namespace elm {
5154

5255
/// The underlying element ID type
@@ -81,6 +84,12 @@ struct ElementIDStruct {
8184
bool isLocatedOnThisNode() const;
8285
};
8386

87+
inline auto format_as(ElementIDStruct e) {
88+
auto fmt_str = "({},{},{},{})";
89+
return fmt::format(
90+
fmt_str, e.id, e.getHomeNode(), e.curr_node, e.isMigratable()
91+
);
92+
}
8493

8594
}} /* end namespace vt::elm */
8695

@@ -95,50 +104,4 @@ struct hash<vt::elm::ElementIDStruct> {
95104

96105
} /* end namespace std */
97106

98-
#include "vt/cmake_config.h"
99-
#include INCLUDE_FMT_FORMAT
100-
101-
VT_FMT_NAMESPACE_BEGIN
102-
103-
/// Custom fmt formatter/print for \c vt::elm::ElementIDStruct
104-
template <>
105-
struct formatter<::vt::elm::ElementIDStruct> {
106-
/// Presentation format:
107-
/// - 'x' - hex (default)
108-
/// - 'd' - decimal
109-
/// - 'b' - binary
110-
char presentation = 'x';
111-
112-
/// Parses format specifications of the form ['x' | 'd' | 'b'].
113-
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
114-
// Parse the presentation format and store it in the formatter:
115-
auto it = ctx.begin(), end = ctx.end();
116-
if (it != end && (*it == 'x' || *it == 'd' || *it == 'b')) {
117-
presentation = *it++;
118-
}
119-
120-
// Check if reached the end of the range:
121-
if (it != end && *it != '}') {
122-
throw format_error("invalid format");
123-
}
124-
125-
// Return an iterator past the end of the parsed range:
126-
return it;
127-
}
128-
129-
/// Formats the epoch using the parsed format specification (presentation)
130-
/// stored in this formatter.
131-
template <typename FormatContext>
132-
auto format(::vt::elm::ElementIDStruct const& e, FormatContext& ctx) const {
133-
std::string id_format =
134-
presentation == 'b' ? "{:b}" : (presentation == 'd' ? "{:d}" : "{:x}");
135-
auto fmt_str = "(" + id_format + ",{},{},{})";
136-
return format_to(
137-
ctx.out(), fmt_str, e.id, e.getHomeNode(), e.curr_node, e.isMigratable()
138-
);
139-
}
140-
};
141-
142-
VT_FMT_NAMESPACE_END
143-
144107
#endif /*INCLUDED_VT_ELM_ELM_ID_H*/

src/vt/epoch/epoch_type.h

+5-45
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
#include "vt/utils/strong/strong_type.h"
4848
#include "vt/epoch/epoch_impl_type.h"
4949

50+
#include "vt/cmake_config.h"
51+
#include INCLUDE_FMT_BASE
52+
5053
namespace vt { namespace epoch {
5154

5255
struct EpochType : Strong<
@@ -94,6 +97,8 @@ constexpr inline EpochType makeEpochZero() {
9497
return EpochType{static_cast<EpochType::ImplType>(0ull)};
9598
}
9699

100+
inline auto format_as(EpochType e) { return *e; }
101+
97102
}} /* end namespace vt::epoch */
98103

99104
namespace std {
@@ -108,51 +113,6 @@ struct hash<vt::epoch::EpochType> {
108113

109114
} /* end namespace std */
110115

111-
#include "vt/cmake_config.h"
112-
#include INCLUDE_FMT_FORMAT
113-
114-
VT_FMT_NAMESPACE_BEGIN
115-
116-
/// Custom fmt formatter/print for \c EpochType
117-
template <>
118-
struct formatter<::vt::epoch::EpochType> {
119-
/// Presentation format:
120-
/// - 'x' - hex (default)
121-
/// - 'd' - decimal
122-
/// - 'b' - binary
123-
char presentation = 'x';
124-
125-
/// Parses format specifications of the form ['x' | 'd' | 'b'].
126-
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
127-
// Parse the presentation format and store it in the formatter:
128-
auto it = ctx.begin(), end = ctx.end();
129-
if (it != end && (*it == 'x' || *it == 'd' || *it == 'b')) {
130-
presentation = *it++;
131-
}
132-
133-
// Check if reached the end of the range:
134-
if (it != end && *it != '}') {
135-
throw format_error("invalid format");
136-
}
137-
138-
// Return an iterator past the end of the parsed range:
139-
return it;
140-
}
141-
142-
/// Formats the epoch using the parsed format specification (presentation)
143-
/// stored in this formatter.
144-
template <typename FormatContext>
145-
auto format(::vt::epoch::EpochType const& e, FormatContext& ctx) const {
146-
return format_to(
147-
ctx.out(),
148-
presentation == 'b' ? "{:b}" : (presentation == 'd' ? "{:d}" : "{:x}"),
149-
*e
150-
);
151-
}
152-
};
153-
154-
VT_FMT_NAMESPACE_END
155-
156116
namespace vt {
157117

158118
/// The strong epoch type for holding a epoch for termination detection

src/vt/epoch/epoch_window.h

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#include "vt/termination/interval/strong_integral_set.h"
4848
#include "vt/utils/adt/ranged_counter.h"
4949

50+
#include <memory>
51+
5052
namespace vt { namespace epoch {
5153

5254
/**

src/vt/rdma/rdma_common.h

+21-32
Original file line numberDiff line numberDiff line change
@@ -115,40 +115,29 @@ using RDMA_ElmMapType = std::function<RDMA_BlockElmRangeType(RDMA_ElmType,RDMA_E
115115
static constexpr Type uninitialized_rdma_type = Type::Uninitialized;
116116
static constexpr ByteType rdma_default_byte_size = sizeof(char);
117117

118-
}} //end namespace vt::rdma
119-
120-
VT_FMT_NAMESPACE_BEGIN
121-
122-
template <>
123-
struct formatter<::vt::rdma::Type> {
124-
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
125-
126-
template <typename FormatContext>
127-
auto format(::vt::rdma::Type t, FormatContext& ctx) const {
128-
std::string_view name = "Unknown";
129-
switch (t) {
130-
case ::vt::rdma::Type::Get:
131-
name = "Get";
132-
break;
133-
case ::vt::rdma::Type::Put:
134-
name = "Put";
135-
break;
136-
case ::vt::rdma::Type::GetOrPut:
137-
name = "GetOrPut";
138-
break;
139-
case ::vt::rdma::Type::Uninitialized:
140-
name = "Uninitialized";
141-
break;
142-
default:
143-
name = fmt::format(
144-
"{}", static_cast<std::underlying_type_t<::vt::rdma::Type>>(t));
145-
}
146-
147-
return fmt::format_to(ctx.out(), name);
118+
inline auto format_as(Type t) {
119+
std::string_view name = "Unknown";
120+
switch (t) {
121+
case ::vt::rdma::Type::Get:
122+
name = "Get";
123+
break;
124+
case ::vt::rdma::Type::Put:
125+
name = "Put";
126+
break;
127+
case ::vt::rdma::Type::GetOrPut:
128+
name = "GetOrPut";
129+
break;
130+
case ::vt::rdma::Type::Uninitialized:
131+
name = "Uninitialized";
132+
break;
133+
default:
134+
name = "Unknown";
135+
break;
148136
}
149-
};
137+
return name;
138+
}
150139

151-
VT_FMT_NAMESPACE_END
140+
}} //end namespace vt::rdma
152141

153142
#define PRINT_CHANNEL_TYPE(rdma_op_type) ( \
154143
rdma_op_type == vt::rdma::Type::Get ? "rdma::Get" : ( \

src/vt/termination/interval/interval.h

+5-16
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ struct IntervalCompare {
218218
}
219219
};
220220

221+
template <typename DomainT, DomainT sentinel>
222+
auto format_as(Interval<DomainT, sentinel> i) {
223+
return fmt::format("Interval[{}, {}]", i.lower(), i.upper());
224+
}
225+
221226
}}} /* end namespace vt::term::interval */
222227

223228
namespace vt {
@@ -237,20 +242,4 @@ struct Interval;
237242

238243
}}}
239244

240-
VT_FMT_NAMESPACE_BEGIN
241-
242-
template <typename DomainT, DomainT sentinel>
243-
struct formatter<::vt::term::interval::Interval<DomainT, sentinel>>
244-
: formatter<std::string> {
245-
template <typename FormatContext>
246-
auto format(
247-
const ::vt::term::interval::Interval<DomainT, sentinel>& interval,
248-
FormatContext& ctx) const {
249-
return format_to(
250-
ctx.out(), "Interval[{}, {}]", interval.lower(), interval.upper());
251-
}
252-
};
253-
254-
VT_FMT_NAMESPACE_END
255-
256245
#endif /*INCLUDED_VT_TERMINATION_INTERVAL_INTERVAL_H*/

src/vt/timing/timing.h

-26
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@
4444
#if !defined INCLUDED_VT_TIMING_TIMING_H
4545
#define INCLUDED_VT_TIMING_TIMING_H
4646

47-
#include <string>
48-
49-
#include <EngFormat-Cpp/eng_format.hpp>
50-
#include "vt/cmake_config.h"
51-
#include INCLUDE_FMT_CORE
52-
5347
#include "vt/timing/timing_type.h"
5448

5549
namespace vt { namespace timing {
@@ -63,24 +57,4 @@ TimeType getCurrentTime();
6357

6458
}} /* end namespace vt::timing */
6559

66-
VT_FMT_NAMESPACE_BEGIN
67-
68-
template<>
69-
struct formatter<::vt::TimeTypeWrapper> {
70-
template<typename ParseContext>
71-
constexpr auto parse(ParseContext& ctx) {
72-
return ctx.begin();
73-
}
74-
75-
template<typename FormatContext>
76-
auto format(::vt::TimeTypeWrapper const& t, FormatContext& ctx) const {
77-
return fmt::format_to(
78-
ctx.out(), "{}",
79-
to_engineering_string(t.seconds(), 5, eng_exponential, "s")
80-
);
81-
}
82-
};
83-
84-
VT_FMT_NAMESPACE_END
85-
8660
#endif /*INCLUDED_VT_TIMING_TIMING_H*/

src/vt/timing/timing_type.h

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
#include <limits>
4848
#include <algorithm>
4949
#include <cmath>
50+
#include <string>
51+
52+
#include <EngFormat-Cpp/eng_format.hpp>
53+
54+
#include "vt/cmake_config.h"
55+
#include INCLUDE_FMT_BASE
5056

5157
namespace vt {
5258

@@ -164,6 +170,10 @@ struct TimeTypeWrapper {
164170
TimeTypeInternal time_;
165171
};
166172

173+
inline auto format_as(TimeTypeWrapper t) {
174+
return to_engineering_string(t.seconds(), 5, eng_exponential, "s");
175+
}
176+
167177
using TimeType = TimeTypeWrapper;
168178

169179
} /* end namespace vt */

src/vt/topos/index/dense/dense_array.h

+5-16
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,12 @@ static_assert(
170170
"DenseIndexArray must follow the index concept"
171171
);
172172

173-
}} // end namespace vt::index
174-
175-
VT_FMT_NAMESPACE_BEGIN
176-
177-
template <typename IndexType, ::vt::index::NumDimensionsType ndim>
178-
struct formatter<::vt::index::DenseIndexArray<IndexType, ndim>> {
179-
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
173+
template <typename T, NumDimensionsType nd>
174+
auto format_as(DenseIndexArray<T, nd> d) {
175+
return d.toString();
176+
}
180177

181-
template <typename FormatContext>
182-
auto format(
183-
const ::vt::index::DenseIndexArray<IndexType, ndim>& idx,
184-
FormatContext& ctx) const {
185-
return fmt::format_to(ctx.out(), idx.toString());
186-
}
187-
};
188-
189-
VT_FMT_NAMESPACE_END
178+
}} // end namespace vt::index
190179

191180
#include "vt/topos/index/dense/dense_array.impl.h"
192181

0 commit comments

Comments
 (0)