ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
print.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2#ifndef TTG_PRINT_H
3#define TTG_PRINT_H
4
5#include <iostream>
6#include <mutex>
7#include <complex>
8#include <vector>
9#include <array>
10#include <utility>
11
12namespace ttg {
13 constexpr char print_separator = ' ';
14 constexpr char print_seq_separator = ',';
15 constexpr char print_seq_begin = '{';
16 constexpr char print_seq_end = '}';
17
18 namespace iostream {
19
22
24
29 template <typename T>
30 std::ostream &operator<<(std::ostream &s, const std::complex<T> &c) {
31 s << c.real() << (c.imag() >= 0 ? "+" : "") << c.imag() << "j";
32 return s;
33 }
34
36
42 template <typename T, typename U>
43 std::ostream &operator<<(std::ostream &s, const std::pair<T, U> &p) {
44 s << print_seq_begin << p.first << print_seq_separator << p.second << print_seq_end;
45 return s;
46 }
47
49
54 template <typename T>
55 std::ostream &operator<<(std::ostream &s, const std::vector<T> &c) {
56 s << print_seq_begin;
57 typename std::vector<T>::const_iterator it = c.begin();
58 while (it != c.end()) {
59 s << *it;
60 ++it;
61 if (it != c.end())
63 };
64 s << print_seq_end;
65 return s;
66 }
67
69
76 template <typename T, std::size_t N>
77 typename std::enable_if<!std::is_same<T, char>::value, std::ostream &>::type
78 operator<<(std::ostream &s, const std::array<T,N>& v) {
79 s << print_seq_begin;
80 for (std::size_t i = 0; i < N; ++i) {
81 s << v[i];
82 if (i != (N - 1))
84 }
85 s << print_seq_end;
86 return s;
87 }
88
90
97 template <typename T, std::size_t N>
98 typename std::enable_if<!std::is_same<T, char>::value, std::ostream &>::type
99 operator<<(std::ostream &s, const T (&v)[N]) {
100 s << print_seq_begin;
101 for (std::size_t i = 0; i < N; ++i) {
102 s << v[i];
103 if (i != (N - 1))
105 }
106 s << print_seq_end;
107 return s;
108 }
109
110 } // namespace operators
111
112 namespace detail {
113 inline std::ostream &print_helper(std::ostream &out) { return out; }
114 template <typename T, typename... Ts>
115 inline std::ostream &print_helper(std::ostream &out, const T &t, const Ts &... ts) {
116 using ttg::iostream::operator<<;
117 out << print_separator << t;
118 return print_helper(out, ts...);
119 }
120 //
121 enum class StdOstreamTag { Cout, Cerr };
122 template <StdOstreamTag>
123 inline std::mutex &print_mutex_accessor() {
124 static std::mutex mutex;
125 return mutex;
126 }
127 } // namespace detail
128
130 template <typename T, typename... Ts>
131 void print(const T &t, const Ts &... ts) {
132 std::lock_guard<std::mutex> lock(detail::print_mutex_accessor<detail::StdOstreamTag::Cout>());
133 std::cout << t;
134 detail::print_helper(std::cout, ts...) << std::endl;
135 }
136
138 template <typename T, typename... Ts>
139 void print_error(const T &t, const Ts &... ts) {
140 std::lock_guard<std::mutex> lock(detail::print_mutex_accessor<detail::StdOstreamTag::Cout>());
141 std::cerr << t;
142 detail::print_helper(std::cerr, ts...) << std::endl;
143 }
144
146 template <typename T, typename... Ts>
147 void log(const T &t, const Ts &... ts) {
148 std::lock_guard<std::mutex> lock(detail::print_mutex_accessor<detail::StdOstreamTag::Cout>());
149 std::clog << t;
150 detail::print_helper(std::clog, ts...) << std::endl;
151 }
152} // namespace ttg
153
154#endif // TTG_PRINT_H
std::ostream & print_helper(std::ostream &out)
Definition print.h:113
std::mutex & print_mutex_accessor()
Definition print.h:123
std::ostream & operator<<(std::ostream &s, const std::complex< T > &c)
default printing of std::complex
Definition print.h:30
top-level TTG namespace contains runtime-neutral functionality
Definition keymap.h:9
constexpr char print_seq_separator
Definition print.h:14
void print(const T &t, const Ts &... ts)
atomically prints to std::cout a sequence of items (separated by ttg::print_separator) followed by st...
Definition print.h:131
constexpr char print_separator
Definition print.h:13
void log(const T &t, const Ts &... ts)
atomically prints to std::clog a sequence of items (separated by ttg::print_separator) followed by st...
Definition print.h:147
void print_error(const T &t, const Ts &... ts)
atomically prints to std::cerr a sequence of items (separated by ttg::print_separator) followed by st...
Definition print.h:139
constexpr char print_seq_end
Definition print.h:16
constexpr char print_seq_begin
Definition print.h:15