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 <fstream>
44#include <complex>
45#include <list>
46#include <vector>
49
50#ifdef BRAINDEAD
51// Cray XT nonsense
52#define ENDL "\n"
53
54#else
55
56#define ENDL std::endl
57
58#endif
59
60
61namespace madness {
62
63namespace operators {
64
65/// \addtogroup libraries
66/// @{
67
68/// Easy printing of complex numbers.
69
70/// \tparam T The "real" type of the complex number.
71/// \param[in,out] s The output stream.
72/// \param[in] c The complex number.
73/// \return The output stream (for chaining).
74template <typename T>
75std::ostream &operator<<(std::ostream &s, const std::complex<T> &c) {
76 s << c.real() << "+" << c.imag() << "j";
77 return s;
78}
79
80/// Easy printing of pairs.
81
82/// \tparam T Type 1 of the pair.
83/// \tparam U Type 2 of the pair.
84/// \param[in,out] s The output stream.
85/// \param[in] p The pair.
86/// \return The output stream (for chaining).
87template <typename T, typename U>
88std::ostream &operator<<(std::ostream &s, const std::pair<T, U> &p) {
89 s << "(" << p.first << "," << p.second << ")";
90 return s;
91}
92
93///// Easy printing of std::arrays.
94//
95///// \tparam U Type 2 of the pair.
96///// \param[in,out] s The output stream.
97///// \param[in] p The pair.
98///// \return The output stream (for chaining).
99//template <typename T, std::size_t NDIM>
100//std::ostream &operator<<(std::ostream &s, const std::array<T, NDIM> &p) {
101// s << "[";
102// for (int i=0; i<p.size(); ++i) s << p[i];
103// s << "]";
104// return s;
105//}
106
107/// Easy printing of lists.
108
109/// \tparam T Type stored in the list.
110/// \param[in,out] s The output stream.
111/// \param[in] c The list.
112/// \return The output stream (for chaining).
113template <typename T>
114std::ostream &operator<<(std::ostream &s, const std::list<T> &c) {
115 s << "[";
116 typename std::list<T>::const_iterator it = c.begin();
117 while (it != c.end()) {
118 s << *it;
119 ++it;
120 if (it != c.end())
121 s << ", ";
122 };
123 s << "]";
124 return s;
125}
126
127/// Easy printing of vectors.
128
129/// \tparam T Type stored in the vector.
130/// \param[in,out] s The output stream.
131/// \param[in] c The vector.
132/// \return The output stream (for chaining).
133template <typename T>
134std::ostream &operator<<(std::ostream &s, const std::vector<T> &c) {
135 s << "[";
136 typename std::vector<T>::const_iterator it = c.begin();
137 while (it != c.end()) {
138 s << *it;
139 ++it;
140 if (it != c.end())
141 s << ", ";
142 };
143 s << "]";
144 return s;
145}
146
147/// Easy printing of fixed dimension arrays.
148
149/// STL I/O already does char (thus the \c enable_if business).
150/// \tparam T Type of data in the array.
151/// \tparam N Size of the array.
152/// \param[in,out] s The output stream.
153/// \param[in] v The array.
154/// \return The output stream (for chaining).
155template <typename T, std::size_t N>
156typename std::enable_if<!std::is_same<T, char>::value, std::ostream &>::type
157operator<<(std::ostream &s, const T (&v)[N]) {
158 s << "[";
159 for (std::size_t i = 0; i < N; ++i) {
160 s << v[i];
161 if (i != (N - 1))
162 s << ",";
163 }
164 s << "]";
165 return s;
166}
167
168} // namespace operators
169
170 /// big section heading
171 void print_header1(const std::string& s);
172
173 /// medium section heading
174 void print_header2(const std::string& s);
175
176 /// small section heading
177 void print_header3(const std::string& s);
178
179 /// Print a string justified on the left to start at the given column with optional underlining.
180 void print_justified(const char* s, int column=0, bool underline=true);
181
182 /// Print a string centered at the given column with optional underlining.
183 void print_centered(const char* s, int column=40, bool underline=true);
184
185 ///
186 void printf_msg_energy_time(const std::string msg, const double energy, const double time);
187
188 // the "print" function and functions to help handle the variadic templates
189
190 /// Helper function for \c print. Base case.
191
192 /// This gets called recursively when there are no items left to print.
193 /// \param[in,out] out Output stream.
194 /// \return The output stream (for chaining).
195 inline std::ostream& print_helper(std::ostream& out) {
196 return out;
197 }
198
199 /// \brief Helper function for \c print. Prints the first item (\c t) and
200 /// recursively passes on the other items.
201
202 /// \tparam T Type of the item to print in this call.
203 /// \tparam Ts Argument pack type for the remaining items.
204 /// \param[in,out] out Output stream.
205 /// \param[in] t The item to print in this call.
206 /// \param[in] ts The remaining items in the argument pack (they get
207 /// recursively passed on).
208 /// \return The output stream (for chaining).
209 template <typename T, typename... Ts>
210 inline std::ostream& print_helper(std::ostream& out,
211 const T& t, const Ts&... ts) {
212 using madness::operators::operator<<;
213 out << ' ' << t;
214 return print_helper(out, ts...);
215 }
216
217 /// \brief Print items to \c std::cout (items separated by spaces) and
218 /// terminate with a new line
219
220 /// The first item is printed here so that it isn't preceded by a space.
221 /// \tparam T Type of the first item to be printed.
222 /// \tparam Ts Argument pack type for the items to be printed.
223 /// \param[in] t The first item to be printed.
224 /// \param[in] ts The remaining items to be printed in the argument pack.
225 template<typename T, typename... Ts>
226 void print(const T& t, const Ts&... ts) {
227 using madness::operators::operator<<;
229 std::cout << t;
230 print_helper(std::cout, ts...) << ENDL;
231 }
232
233 /// \brief Print items to \c std::cerr (items separated by spaces) and
234 /// terminate with a new line
235
236 /// The first item is printed here so that it isn't preceded by a space.
237 /// \tparam T Type of the first item to be printed.
238 /// \tparam Ts Argument pack type for the items to be printed.
239 /// \param[in] t The first item to be printed.
240 /// \param[in] ts The remaining items to be printed in the argument pack.
241 template<typename T, typename... Ts>
242 void print_error(const T& t, const Ts&... ts) {
243 using madness::operators::operator<<;
245 std::cerr << t;
246 print_helper(std::cerr, ts...) << ENDL;
247 }
248
249
250 /// RAII class to redirect cout to a file
251 struct io_redirect {
252 std::streambuf* stream_buffer_cout;
253 static std::streambuf* stream_buffer_cout_default; ///< default stream buffer for cout, used to restore cout
254 std::ofstream ofile;
255 bool debug = false;
256
257 io_redirect(const long task_number, std::string filename, bool debug = false) : debug(debug) {
258 stream_buffer_cout_default = std::cout.rdbuf();
259 constexpr std::size_t bufsize = 256;
260 char cfilename[bufsize];
261 std::snprintf(cfilename, bufsize, "%s.%5.5ld", filename.c_str(), task_number);
262 ofile = std::ofstream(cfilename);
263 if (debug) std::cout << "redirecting to file " << cfilename << std::endl;
264 stream_buffer_cout = std::cout.rdbuf(ofile.rdbuf());
265 std::cout.sync_with_stdio(true);
266 }
267
269 std::cout.rdbuf(stream_buffer_cout);
270 ofile.close();
271 std::cout.sync_with_stdio(true);
272 if (debug) std::cout << "redirecting back to cout" << std::endl;
273 }
274 };
275
276 /// class to temporarily redirect output to cout
278 std::streambuf* stream_buffer_cout;
279
282 std::cout.sync_with_stdio(true);
283 }
284
286 std::cout.rdbuf(stream_buffer_cout);
287 std::cout.sync_with_stdio(true);
288 }
289 };
290
291}
292#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
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:28
io_redirect(const long task_number, std::string filename, bool debug=false)
Definition print.h:257
std::streambuf * stream_buffer_cout
Definition print.h:252
io_redirect_cout()
Definition print.h:280
static std::streambuf * stream_buffer_cout_default
default stream buffer for cout, used to restore cout
Definition print.h:253
std::streambuf * stream_buffer_cout
Definition print.h:278
bool debug
Definition print.h:255
std::ofstream ofile
Definition print.h:254
~io_redirect()
Definition print.h:268
~io_redirect_cout()
Definition print.h:285
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:195
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:226
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
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:242
#define ENDL
Definition print.h:56
static const double c
Definition relops.cc:10
class to temporarily redirect output to cout
Definition print.h:277
RAII class to redirect cout to a file.
Definition print.h:251
#define N
Definition testconv.cc:37
Implements Mutex, MutexFair, Spinlock, ConditionVariable.