MADNESS 0.10.1
print.h
Go to the documentation of this file.
1/*
2 This file is part of MADNESS.
3
4 Copyright (C) 2007,2010 Oak Ridge National Laboratory
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 For more information please contact:
21
22 Robert J. Harrison
23 Oak Ridge National Laboratory
24 One Bethel Valley Road
25 P.O. Box 2008, MS-6367
26
27 email: harrisonrj@ornl.gov
28 tel: 865-241-3937
29 fax: 865-572-0680
30*/
31
32#ifndef MADNESS_WORLD_PRINT_H__INCLUDED
33#define MADNESS_WORLD_PRINT_H__INCLUDED
34
35/**
36 \file print.h
37 \brief Defines simple templates for printing to \c std::cout "a la Python".
38 \ingroup libraries
39*/
40
41#include <type_traits>
42#include <iostream>
43#include <filesystem>
44#include <fstream>
45#include <complex>
46#include <list>
47#include <vector>
50
51#ifdef BRAINDEAD
52// Cray XT nonsense
53#define ENDL "\n"
54
55#else
56
57#define ENDL std::endl
58
59#endif
60
61
62namespace madness {
63
64namespace operators {
65
66/// \addtogroup libraries
67/// @{
68
69/// Easy printing of complex numbers.
70
71/// \tparam T The "real" type of the complex number.
72/// \param[in,out] s The output stream.
73/// \param[in] c The complex number.
74/// \return The output stream (for chaining).
75template <typename T>
76std::ostream &operator<<(std::ostream &s, const std::complex<T> &c) {
77 s << c.real() << "+" << c.imag() << "j";
78 return s;
79}
80
81/// Easy printing of pairs.
82
83/// \tparam T Type 1 of the pair.
84/// \tparam U Type 2 of the pair.
85/// \param[in,out] s The output stream.
86/// \param[in] p The pair.
87/// \return The output stream (for chaining).
88template <typename T, typename U>
89std::ostream &operator<<(std::ostream &s, const std::pair<T, U> &p) {
90 s << "(" << p.first << "," << p.second << ")";
91 return s;
92}
93
94///// Easy printing of std::arrays.
95//
96///// \tparam U Type 2 of the pair.
97///// \param[in,out] s The output stream.
98///// \param[in] p The pair.
99///// \return The output stream (for chaining).
100//template <typename T, std::size_t NDIM>
101//std::ostream &operator<<(std::ostream &s, const std::array<T, NDIM> &p) {
102// s << "[";
103// for (int i=0; i<p.size(); ++i) s << p[i];
104// s << "]";
105// return s;
106//}
107
108/// Easy printing of lists.
109
110/// \tparam T Type stored in the list.
111/// \param[in,out] s The output stream.
112/// \param[in] c The list.
113/// \return The output stream (for chaining).
114template <typename T>
115std::ostream &operator<<(std::ostream &s, const std::list<T> &c) {
116 s << "[";
117 typename std::list<T>::const_iterator it = c.begin();
118 while (it != c.end()) {
119 s << *it;
120 ++it;
121 if (it != c.end())
122 s << ", ";
123 };
124 s << "]";
125 return s;
126}
127
128/// Easy printing of vectors.
129
130/// \tparam T Type stored in the vector.
131/// \param[in,out] s The output stream.
132/// \param[in] c The vector.
133/// \return The output stream (for chaining).
134template <typename T>
135std::ostream &operator<<(std::ostream &s, const std::vector<T> &c) {
136 s << "[";
137 typename std::vector<T>::const_iterator it = c.begin();
138 while (it != c.end()) {
139 s << *it;
140 ++it;
141 if (it != c.end())
142 s << ", ";
143 };
144 s << "]";
145 return s;
146}
147
148/// Easy printing of fixed dimension arrays.
149
150/// STL I/O already does char (thus the \c enable_if business).
151/// \tparam T Type of data in the array.
152/// \tparam N Size of the array.
153/// \param[in,out] s The output stream.
154/// \param[in] v The array.
155/// \return The output stream (for chaining).
156template <typename T, std::size_t N>
157typename std::enable_if<!std::is_same<T, char>::value, std::ostream &>::type
158operator<<(std::ostream &s, const T (&v)[N]) {
159 s << "[";
160 for (std::size_t i = 0; i < N; ++i) {
161 s << v[i];
162 if (i != (N - 1))
163 s << ",";
164 }
165 s << "]";
166 return s;
167}
168
169} // namespace operators
170
171 /// big section heading
172 void print_header1(const std::string& s);
173
174 /// medium section heading
175 void print_header2(const std::string& s);
176
177 /// small section heading
178 void print_header3(const std::string& s);
179
180 /// Print a string justified on the left to start at the given column with optional underlining.
181 void print_justified(const char* s, int column=0, bool underline=true);
182
183 /// Print a string centered at the given column with optional underlining.
184 void print_centered(const char* s, int column=40, bool underline=true);
185
186 ///
187 void printf_msg_energy_time(const std::string msg, const double energy, const double time);
188
189 // the "print" function and functions to help handle the variadic templates
190
191 /// Helper function for \c print. Base case.
192
193 /// This gets called recursively when there are no items left to print.
194 /// \param[in,out] out Output stream.
195 /// \return The output stream (for chaining).
196 inline std::ostream& print_helper(std::ostream& out) {
197 return out;
198 }
199
200 /// \brief Helper function for \c print. Prints the first item (\c t) and
201 /// recursively passes on the other items.
202
203 /// \tparam T Type of the item to print in this call.
204 /// \tparam Ts Argument pack type for the remaining items.
205 /// \param[in,out] out Output stream.
206 /// \param[in] t The item to print in this call.
207 /// \param[in] ts The remaining items in the argument pack (they get
208 /// recursively passed on).
209 /// \return The output stream (for chaining).
210 template <typename T, typename... Ts>
211 inline std::ostream& print_helper(std::ostream& out,
212 const T& t, const Ts&... ts) {
213 using madness::operators::operator<<;
214 out << ' ' << t;
215 return print_helper(out, ts...);
216 }
217
218 /// \brief Print items to \c std::cout (items separated by spaces) and
219 /// terminate with a new line
220
221 /// The first item is printed here so that it isn't preceded by a space.
222 /// \tparam T Type of the first item to be printed.
223 /// \tparam Ts Argument pack type for the items to be printed.
224 /// \param[in] t The first item to be printed.
225 /// \param[in] ts The remaining items to be printed in the argument pack.
226 /// \note \c ts are forwarding references, not \c const&: under C++23 ADL
227 /// on a \c std argument also finds \c std::print, and \c const& would lose
228 /// to its \c Args&& on that argument, making unqualified calls ambiguous.
229 /// \c t stays \c const& (it competes only with the \c format_string
230 /// parameter) so that a call with one argument resolves as before.
231 template<typename T, typename... Ts>
232 void print(const T& t, Ts&&... ts) {
233 using madness::operators::operator<<;
235 std::cout << t;
236 print_helper(std::cout, ts...) << ENDL;
237 }
238
239 /// \brief Print items to \c std::cerr (items separated by spaces) and
240 /// terminate with a new line
241
242 /// The first item is printed here so that it isn't preceded by a space.
243 /// \tparam T Type of the first item to be printed.
244 /// \tparam Ts Argument pack type for the items to be printed.
245 /// \param[in] t The first item to be printed.
246 /// \param[in] ts The remaining items to be printed in the argument pack.
247 template<typename T, typename... Ts>
248 void print_error(const T& t, Ts&&... ts) {
249 using madness::operators::operator<<;
251 std::cerr << t;
252 print_helper(std::cerr, ts...) << ENDL;
253 }
254
255
256 /// RAII class to redirect cout to a file or to /dev/null
257 struct io_redirect {
258 /// File writes <directory>/<filename>.<NNNNN>; Discard silences cout via /dev/null
259 enum class Mode { File, Discard };
260
261 std::streambuf* stream_buffer_cout;
262 static std::streambuf* stream_buffer_cout_default; ///< default stream buffer for cout, used to restore cout
263 std::ofstream ofile;
265 bool debug = false;
266
267 /// \param directory created if needed; empty means the current directory
268 io_redirect(Mode mode, const long task_number,
269 std::string directory, std::string filename, bool debug = false)
270 : mode(mode), debug(debug) {
271 stream_buffer_cout_default = std::cout.rdbuf();
272 if (mode == Mode::File) {
273 constexpr std::size_t bufsize = 512;
274 char cfilename[bufsize];
275 if (directory.empty()) {
276 std::snprintf(cfilename, bufsize, "%s.%5.5ld", filename.c_str(), task_number);
277 } else {
278 std::error_code ec;
279 std::filesystem::create_directories(directory, ec); // idempotent; races are benign
280 std::snprintf(cfilename, bufsize, "%s/%s.%5.5ld",
281 directory.c_str(), filename.c_str(), task_number);
282 }
283 ofile = std::ofstream(cfilename);
284 if (debug) std::cout << "redirecting to file " << cfilename << std::endl;
285 } else {
286 ofile = std::ofstream("/dev/null");
287 }
288 stream_buffer_cout = std::cout.rdbuf(ofile.rdbuf());
289 std::cout.sync_with_stdio(true);
290 }
291
292 /// a file in the current directory
293 io_redirect(const long task_number, std::string filename, bool debug = false)
294 : io_redirect(Mode::File, task_number, std::string(), std::move(filename), debug) {}
295
297 std::cout.rdbuf(stream_buffer_cout);
298 ofile.close();
299 std::cout.sync_with_stdio(true);
300 if (debug and mode == Mode::File) std::cout << "redirecting back to cout" << std::endl;
301 }
302 };
303
304 /// class to temporarily redirect output to cout
306 std::streambuf* stream_buffer_cout;
307
310 std::cout.sync_with_stdio(true);
311 }
312
314 std::cout.rdbuf(stream_buffer_cout);
315 std::cout.sync_with_stdio(true);
316 }
317 };
318
319}
320#endif // MADNESS_WORLD_PRINT_H__INCLUDED
Supplements to the std::array class, such as I/O operations, for convenience.
Mutex that is applied/released at start/end of a scope.
Definition worldmutex.h:258
double(* energy)()
Definition derivatives.cc:58
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
const std::size_t bufsize
Definition derivatives.cc:16
Mode
File writes <directory>/<filename>.<NNNNN>; Discard silences cout via /dev/null.
Definition print.h:259
io_redirect(const long task_number, std::string filename, bool debug=false)
a file in the current directory
Definition print.h:293
std::streambuf * stream_buffer_cout
Definition print.h:261
io_redirect_cout()
Definition print.h:308
static std::streambuf * stream_buffer_cout_default
default stream buffer for cout, used to restore cout
Definition print.h:262
std::streambuf * stream_buffer_cout
Definition print.h:306
Mode mode
Definition print.h:264
bool debug
Definition print.h:265
std::ofstream ofile
Definition print.h:263
~io_redirect()
Definition print.h:296
~io_redirect_cout()
Definition print.h:313
io_redirect(Mode mode, const long task_number, std::string directory, std::string filename, bool debug=false)
Definition print.h:268
static const double v
Definition hatom_sf_dirac.cc:20
Mutex printmutex
Definition worldmutex.cc:146
std::ostream & operator<<(std::ostream &s, const std::array< T, N > &a)
Output std::array to stream for human consumption.
Definition array_addons.h:59
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
void print_error(const T &t, Ts &&... ts)
Print items to std::cerr (items separated by spaces) and terminate with a new line.
Definition print.h:248
static const char * filename
Definition legendre.cc:96
void print_header2(const std::string &s)
medium section heading
Definition print.cc:54
std::ostream & print_helper(std::ostream &out)
Helper function for print. Base case.
Definition print.h:196
void print_justified(const char *s, int column, bool underline)
Print a string justified on the left to start at the given column with optional underlining.
Definition print.cc:75
void printf_msg_energy_time(const std::string msg, const double energy, const double time)
Definition print.cc:70
void print(const T &t, Ts &&... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:232
std::string type(const PairType &n)
Definition PNOParameters.h:18
void print_header1(const std::string &s)
big section heading
Definition print.cc:44
void print_centered(const char *s, int column, bool underline)
Print a string centered at the given column with optional underlining.
Definition print.cc:85
void print_header3(const std::string &s)
small section heading
Definition print.cc:63
Definition mraimpl.h:53
#define ENDL
Definition print.h:57
static const double c
Definition relops.cc:10
class to temporarily redirect output to cout
Definition print.h:305
RAII class to redirect cout to a file or to /dev/null.
Definition print.h:257
#define N
Definition testconv.cc:37
Implements Mutex, MutexFair, Spinlock, ConditionVariable.