DDC 0.12.0
Loading...
Searching...
No Matches
print.cpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#include <cstdlib>
6#include <iostream>
7#include <memory>
8#include <mutex>
9#include <ostream>
10
11#include <Kokkos_Macros.hpp>
12
14#include "print.hpp"
15
16#if defined(KOKKOS_COMPILER_GNU) || defined(KOKKOS_COMPILER_CLANG)
17# include <cxxabi.h>
18#endif
19
20namespace ddc {
21
22// Workaround for nvcc, it should be `= default` but this creates a missing symbol at link stage.
23bool PrinterOptions::operator==(PrinterOptions const& rhs) const noexcept
24{
25 return threshold == rhs.threshold && edgeitems == rhs.edgeitems;
26}
27
28namespace detail {
29
30ChunkPrinter::~ChunkPrinter() = default;
31
32ChunkPrinter::ChunkPrinter() = default;
33
34void ChunkPrinter::saveformat(std::ostream& os)
35{
36 m_ss.copyfmt(os);
37}
38
39std::ostream& ChunkPrinter::align(std::ostream& os, int const level)
40{
41 for (int i = 0; i <= level; ++i) {
42 os << ' ';
43 }
44 return os;
45}
46
47ChunkPrinter& ChunkPrinter::get_instance()
48{
49 static ChunkPrinter instance;
50 return instance;
51}
52
53void print_demangled_type_name(std::ostream& os, char const* const mangled_name)
54{
55#if defined(KOKKOS_COMPILER_GNU) || defined(KOKKOS_COMPILER_CLANG)
56 int status;
57
58 std::unique_ptr<char, decltype(std::free)*> const
59 demangled_name(abi::__cxa_demangle(mangled_name, nullptr, nullptr, &status), std::free);
60 if (status != 0) {
61 std::cerr << "Error demangling dimension name: " << status << '\n' << std::flush;
62 os << mangled_name;
63 } else {
64 os << demangled_name.get();
65 }
66
67#else
68 os << mangled_name;
69#endif
70}
71
72void print_single_dim_name(
73 std::ostream& os,
74 char const* const dim,
75 DiscreteVectorElement const size)
76{
77 print_demangled_type_name(os, dim);
78 os << '(' << size << ')';
79}
80
81void print_dim_name(
82 std::ostream& os,
83 char const* const* const dims,
84 DiscreteVectorElement const* const sizes,
85 std::size_t const n)
86{
87 if (n == 0) {
88 os << "Scalar";
89 } else {
90 print_single_dim_name(os, dims[0], sizes[0]);
91 for (std::size_t i = 1; i < n; ++i) {
92 os << "×";
93 print_single_dim_name(os, dims[i], sizes[i]);
94 }
95 }
96}
97
98} // namespace detail
99
100PrinterOptions set_print_options(PrinterOptions const options)
101{
102 ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::get_instance();
103
104 PrinterOptions old_options = printer.m_options;
105
106 // Ensure options are not modified while an other thread is printing
107 std::scoped_lock const lock(printer.m_global_lock);
108
109 // Ensure that m_edgeitems < (m_threshold / 2) stays true.
110 if (options.edgeitems < options.threshold / 2) {
111 printer.m_options = options;
112 } else {
113 std::cerr << "DDC Printer: invalid values " << options.edgeitems << " for edgeitems and "
114 << options.threshold << " for threshold have been ignored\n"
115 << "threshold needs to be at least twice as big as edgeitems\n";
116 }
117
118 return old_options;
119}
120
122{
123 ddc::detail::ChunkPrinter const& printer = ddc::detail::ChunkPrinter::get_instance();
124 return printer.m_options;
125}
126
127} // namespace ddc
The top-level namespace of DDC.
PrinterOptions get_print_options()
Return the currently used format options.
Definition print.cpp:121
std::size_t threshold
Definition print.hpp:26
bool operator==(PrinterOptions const &rhs) const noexcept
Definition print.cpp:23
std::size_t edgeitems
Definition print.hpp:27