Skip to content

Commit e5be948

Browse files
committed
Add full_print function, use struct for option + linter
- full_print function to always print full array ignoring options - use struct to set the options in order to use designated initializers - use scoped_lock instead of lock_guard to follow linter recommendation Signed-off-by: Paul Gannay <paul.gannay@cea.fr>
1 parent 5cd2ab3 commit e5be948

File tree

3 files changed

+174
-53
lines changed

3 files changed

+174
-53
lines changed

src/ddc/print.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,31 @@ void print_dim_name(
6767

6868
} // namespace detail
6969

70-
bool set_print_options(std::size_t edgeitems, std::size_t threshold)
70+
PrinterOptions set_print_options(PrinterOptions const options)
7171
{
72-
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::getInstance();
72+
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::get_instance();
73+
74+
PrinterOptions old_options = printer.m_options;
7375

7476
// Ensure options are not modified while an other thread is printing
75-
std::lock_guard lock(printer.m_global_lock);
77+
std::scoped_lock const lock(printer.m_global_lock);
7678

7779
// Ensure that m_edgeitems < (m_threshold / 2) stays true.
78-
if (edgeitems < threshold / 2) {
79-
printer.m_edgeitems = edgeitems;
80-
printer.m_threshold = threshold;
81-
return true;
80+
if (options.edgeitems < options.threshold / 2) {
81+
printer.m_options = options;
8282
} else {
83-
std::cerr << "DDC Printer: invalid values " << edgeitems << " for edgeitems and "
84-
<< threshold << " for threshold have been ignored\n"
83+
std::cerr << "DDC Printer: invalid values " << options.edgeitems << " for edgeitems and "
84+
<< options.threshold << " for threshold have been ignored\n"
8585
<< "threshold needs to be at least twice as big as edgeitems\n";
86-
return false;
8786
}
87+
88+
return old_options;
89+
}
90+
91+
PrinterOptions get_print_options()
92+
{
93+
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::get_instance();
94+
return printer.m_options;
8895
}
8996

9097
} // namespace ddc

src/ddc/print.hpp

Lines changed: 86 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@
2020
#include "discrete_vector.hpp"
2121

2222
namespace ddc {
23+
24+
struct PrinterOptions
25+
{
26+
std::size_t threshold {10};
27+
std::size_t edgeitems {3};
28+
29+
bool operator==(PrinterOptions const& rhs) const
30+
{
31+
return threshold == rhs.threshold && edgeitems == rhs.edgeitems;
32+
}
33+
};
34+
2335
namespace detail {
2436
/**
2537
* This class is a singleton, as it contains global printing option
@@ -33,18 +45,17 @@ struct ChunkPrinter
3345
*/
3446
std::recursive_mutex m_global_lock;
3547

36-
std::size_t m_threshold;
37-
std::size_t m_edgeitems;
48+
PrinterOptions m_options;
3849

3950
// Copy of the stream format, used to compute how much space each element of the mdspan will take when printed
4051
std::stringstream m_ss;
4152

42-
private:
43-
ChunkPrinter() : m_global_lock(), m_threshold(10), m_edgeitems(3) {}
44-
4553
ChunkPrinter(ChunkPrinter&) = delete;
4654
ChunkPrinter(ChunkPrinter&&) = delete;
4755

56+
private:
57+
ChunkPrinter() {}
58+
4859
/**
4960
* Print the spaces needed to align value to os
5061
*/
@@ -134,7 +145,7 @@ struct ChunkPrinter
134145
}
135146

136147
public:
137-
static ChunkPrinter& getInstance()
148+
static ChunkPrinter& get_instance()
138149
{
139150
static ChunkPrinter instance;
140151
return instance;
@@ -170,7 +181,7 @@ struct ChunkPrinter
170181
auto extent = span.extent(I0);
171182
if constexpr (sizeof...(Is) > 0) {
172183
os << '[';
173-
if (extent < m_threshold) {
184+
if (extent < m_options.threshold) {
174185
recursive_display(
175186
os,
176187
span,
@@ -186,7 +197,7 @@ struct ChunkPrinter
186197
level,
187198
largest_element,
188199
0,
189-
m_edgeitems,
200+
m_options.edgeitems,
190201
std::make_index_sequence<sizeof...(Is)>());
191202
for (int ndims = 0; ndims < sizeof...(Is); ++ndims) {
192203
os << '\n';
@@ -202,19 +213,25 @@ struct ChunkPrinter
202213
span,
203214
level,
204215
largest_element,
205-
extent - m_edgeitems,
216+
extent - m_options.edgeitems,
206217
extent,
207218
std::make_index_sequence<sizeof...(Is)>());
208219
}
209220
os << "]";
210221
} else {
211222
os << "[";
212-
if (extent < m_threshold) {
223+
if (extent < m_options.threshold) {
213224
base_case_display(os, span, largest_element, 0, extent, extent);
214225
} else {
215-
base_case_display(os, span, largest_element, 0, m_edgeitems, extent);
226+
base_case_display(os, span, largest_element, 0, m_options.edgeitems, extent);
216227
os << "... ";
217-
base_case_display(os, span, largest_element, extent - m_edgeitems, extent, extent);
228+
base_case_display(
229+
os,
230+
span,
231+
largest_element,
232+
extent - m_options.edgeitems,
233+
extent,
234+
extent);
218235
}
219236
os << "]";
220237
}
@@ -247,7 +264,7 @@ struct ChunkPrinter
247264
std::size_t ret = 0;
248265
auto extent = span.extent(I0);
249266
if constexpr (sizeof...(Is) > 0) {
250-
if (extent < m_threshold) {
267+
if (extent < m_options.threshold) {
251268
for (std::size_t i0 = 0; i0 < extent; ++i0) {
252269
ret = std::max(
253270
ret,
@@ -256,14 +273,14 @@ struct ChunkPrinter
256273
std::make_index_sequence<sizeof...(Is)>()));
257274
}
258275
} else {
259-
for (std::size_t i0 = 0; i0 < m_edgeitems; ++i0) {
276+
for (std::size_t i0 = 0; i0 < m_options.edgeitems; ++i0) {
260277
ret = std::max(
261278
ret,
262279
find_largest_displayed_element(
263280
Kokkos::submdspan(span, i0, ((void)Is, Kokkos::full_extent)...),
264281
std::make_index_sequence<sizeof...(Is)>()));
265282
}
266-
for (std::size_t i0 = extent - m_edgeitems; i0 < extent; ++i0) {
283+
for (std::size_t i0 = extent - m_options.edgeitems; i0 < extent; ++i0) {
267284
ret = std::max(
268285
ret,
269286
find_largest_displayed_element(
@@ -272,15 +289,15 @@ struct ChunkPrinter
272289
}
273290
}
274291
} else {
275-
if (extent < m_threshold) {
292+
if (extent < m_options.threshold) {
276293
for (std::size_t i0 = 0; i0 < extent; ++i0) {
277294
ret = std::max(ret, get_element_width(span[i0]));
278295
}
279296
} else {
280-
for (std::size_t i0 = 0; i0 < m_edgeitems; ++i0) {
297+
for (std::size_t i0 = 0; i0 < m_options.edgeitems; ++i0) {
281298
ret = std::max(ret, get_element_width(span[i0]));
282299
}
283-
for (std::size_t i0 = extent - m_edgeitems; i0 < extent; ++i0) {
300+
for (std::size_t i0 = extent - m_options.edgeitems; i0 < extent; ++i0) {
284301
ret = std::max(ret, get_element_width(span[i0]));
285302
}
286303
}
@@ -319,6 +336,22 @@ void print_dim_name(std::ostream& os, DiscreteVector<Dims...> const& dd)
319336

320337
} // namespace detail
321338

339+
/**
340+
* Try to set the options for the printer, returns the old format (or the
341+
* current format if the option passed are invalid).
342+
* option is invalid if m_edgeitems >= (m_threshold / 2), in this case the
343+
* format isn't changed.
344+
*/
345+
PrinterOptions set_print_options(PrinterOptions const options = PrinterOptions());
346+
347+
/**
348+
* Return the currently used format options
349+
*/
350+
PrinterOptions get_print_options();
351+
352+
/**
353+
* Print the content of a ChunkSpan
354+
*/
322355
template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
323356
std::ostream& print_content(
324357
std::ostream& os,
@@ -334,8 +367,8 @@ std::ostream& print_content(
334367

335368

336369

337-
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::getInstance();
338-
std::lock_guard lock(printer.m_global_lock);
370+
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::get_instance();
371+
std::scoped_lock const lock(printer.m_global_lock);
339372

340373
printer.saveformat(os);
341374

@@ -353,13 +386,16 @@ std::ostream& print_content(
353386
return os;
354387
}
355388

389+
/**
390+
* Print the metadata of a ChunkSpan (size, dimension name, ...)
391+
*/
356392
template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
357393
std::ostream& print_type_info(
358394
std::ostream& os,
359395
ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace> const& chunk_span)
360396
{
361-
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::getInstance();
362-
std::lock_guard lock(printer.m_global_lock);
397+
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::get_instance();
398+
std::scoped_lock const lock(printer.m_global_lock);
363399

364400
ddc::detail::print_dim_name(os, chunk_span.extents());
365401
os << '\n';
@@ -369,17 +405,43 @@ std::ostream& print_type_info(
369405
return os;
370406
}
371407

408+
/**
409+
* Print metadata and content of a ChunkSpan
410+
*/
372411
template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
373412
std::ostream& print(
374413
std::ostream& os,
375414
ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace> const& chunk_span)
376415
{
377-
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::getInstance();
378-
std::lock_guard lock(printer.m_global_lock);
416+
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::get_instance();
417+
std::scoped_lock const lock(printer.m_global_lock);
418+
419+
print_type_info(os, chunk_span);
420+
print_content(os, chunk_span);
421+
422+
return os;
423+
}
424+
425+
/**
426+
* Print metadata and content of a ChunkSpan
427+
* Always print without elision, regardless of options set
428+
*/
429+
template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
430+
std::ostream& print_full(
431+
std::ostream& os,
432+
ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace> const& chunk_span)
433+
{
434+
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::get_instance();
435+
std::scoped_lock const lock(printer.m_global_lock);
436+
437+
PrinterOptions old_options
438+
= set_print_options({.threshold = std::numeric_limits<size_t>::max(), .edgeitems = 1});
379439

380440
print_type_info(os, chunk_span);
381441
print_content(os, chunk_span);
382442

443+
set_print_options(old_options);
444+
383445
return os;
384446
}
385447

@@ -391,10 +453,4 @@ std::ostream& operator<<(
391453
return print(os, chunk_span);
392454
}
393455

394-
/**
395-
* Try to set the options for the printer, returns true if the option could be
396-
* set and false if parameters were invalids
397-
*/
398-
bool set_print_options(std::size_t edgeitems = 3, std::size_t threshold = 10);
399-
400456
} // namespace ddc

0 commit comments

Comments
 (0)