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 template<typename T, typename... Ts>
227 void print(const T& t, const Ts&... ts) {
228 using madness::operators::operator<<;
230 std::cout << t;
231 print_helper(std::cout, ts...) << ENDL;
232 }
233
234 /// \brief Print items to \c std::cerr (items separated by spaces) and
235 /// terminate with a new line
236
237 /// The first item is printed here so that it isn't preceded by a space.
238 /// \tparam T Type of the first item to be printed.
239 /// \tparam Ts Argument pack type for the items to be printed.
240 /// \param[in] t The first item to be printed.
241 /// \param[in] ts The remaining items to be printed in the argument pack.
242 template<typename T, typename... Ts>
243 void print_error(const T& t, const Ts&... ts) {
244 using madness::operators::operator<<;
246 std::cerr << t;
247 print_helper(std::cerr, ts...) << ENDL;
248 }
249
250
251 /// RAII class to redirect cout to a file or to /dev/null
252 struct io_redirect {
253 /// File writes <directory>/<filename>.<NNNNN>; Discard silences cout via /dev/null
254 enum class Mode { File, Discard };
255
256 std::streambuf* stream_buffer_cout;
257 static std::streambuf* stream_buffer_cout_default; ///< default stream buffer for cout, used to restore cout
258 std::ofstream ofile;
260 bool debug = false;
261
262 /// \param directory created if needed; empty means the current directory
264 std::string directory, std::string filename, bool debug = false)
265 : mode(mode), debug(debug) {
266 stream_buffer_cout_default = std::cout.rdbuf();
267 if (mode == Mode::File) {
268 constexpr std::size_t bufsize = 512;
269 char cfilename[bufsize];
270 if (directory.empty()) {
271 std::snprintf(cfilename, bufsize, "%s.%5.5ld", filename.c_str(), task_number);
272 } else {
273 std::error_code ec;
274 std::filesystem::create_directories(directory, ec); // idempotent; races are benign
275 std::snprintf(cfilename, bufsize, "%s/%s.%5.5ld",
276 directory.c_str(), filename.c_str(), task_number);
277 }
278 ofile = std::ofstream(cfilename);
279 if (debug) std::cout << "redirecting to file " << cfilename << std::endl;
280 } else {
281 ofile = std::ofstream("/dev/null");
282 }
283 stream_buffer_cout = std::cout.rdbuf(ofile.rdbuf());
284 std::cout.sync_with_stdio(true);
285 }
286
287 /// a file in the current directory
288 io_redirect(const long task_number, std::string filename, bool debug = false)
289 : io_redirect(Mode::File, task_number, std::string(), std::move(filename), debug) {}
290
292 std::cout.rdbuf(stream_buffer_cout);
293 ofile.close();
294 std::cout.sync_with_stdio(true);
295 if (debug and mode == Mode::File) std::cout << "redirecting back to cout" << std::endl;
296 }
297 };
298
299 /// class to temporarily redirect output to cout
301 std::streambuf* stream_buffer_cout;
302
305 std::cout.sync_with_stdio(true);
306 }
307
309 std::cout.rdbuf(stream_buffer_cout);
310 std::cout.sync_with_stdio(true);
311 }
312 };
313
314}
315#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:239
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:254
io_redirect(const long task_number, std::string filename, bool debug=false)
a file in the current directory
Definition print.h:288
std::streambuf * stream_buffer_cout
Definition print.h:256
io_redirect_cout()
Definition print.h:303
static std::streambuf * stream_buffer_cout_default
default stream buffer for cout, used to restore cout
Definition print.h:257
std::streambuf * stream_buffer_cout
Definition print.h:301
Mode mode
Definition print.h:259
bool debug
Definition print.h:260
std::ofstream ofile
Definition print.h:258
~io_redirect()
Definition print.h:291
~io_redirect_cout()
Definition print.h:308
io_redirect(Mode mode, const long task_number, std::string directory, std::string filename, bool debug=false)
Definition print.h:263
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 DFParameters.h:10
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(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:227
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
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
static XNonlinearSolver< std::vector< Function< T, NDIM > >, T, vector_function_allocator< T, NDIM > > nonlinear_vector_solver(World &world, const long nvec)
Definition nonlinsol.h:371
void print_error(const T &t, const Ts &... ts)
Print items to std::cerr (items separated by spaces) and terminate with a new line.
Definition print.h:243
Definition mraimpl.h:51
#define ENDL
Definition print.h:57
static const double c
Definition relops.cc:10
class to temporarily redirect output to cout
Definition print.h:300
RAII class to redirect cout to a file or to /dev/null.
Definition print.h:252
#define N
Definition testconv.cc:37
Implements Mutex, MutexFair, Spinlock, ConditionVariable.