Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_Print.H
Go to the documentation of this file.
1#ifndef AMREX_PRINT_H_
2#define AMREX_PRINT_H_
3#include <AMReX_Config.H>
4
5#include <AMReX.H>
8#include <AMReX_ANSIEscCode.H>
9
10#include <sstream>
11#include <fstream>
12#include <iomanip>
13#include <utility>
14
15namespace amrex
16{
17 template <typename T>
18 std::ostream& operator<< (std::ostream& os, Array<T,AMREX_SPACEDIM> const& a)
19 {
20 os << AMREX_D_TERM( '(' << a[0] , <<
21 ',' << a[1] , <<
22 ',' << a[2]) << ')';
23 return os;
24 }
25
26 template <typename T, typename S>
27 std::ostream& operator<<(std::ostream& os, const std::pair<T, S>& v)
28 {
29 os << "(" << v.first << ", " << v.second << ")";
30 return os;
31 }
32
34 class Print
35 {
36 public:
37
38 static constexpr int AllProcs = -1;
39
44 explicit Print (std::ostream& os_ = amrex::OutStream())
45 : to_print(ParallelContext::IOProcessorSub())
46 , os(os_)
47 { ss.precision(os.precision()); }
48
53 Print (int rank_, std::ostream& os_ = amrex::OutStream())
54 : to_print(rank_ == AllProcs || rank_ == ParallelContext::MyProcSub())
55 , os(os_)
56 { ss.precision(os.precision()); }
57
62 Print (int rank_, MPI_Comm comm_, std::ostream& os_ = amrex::OutStream())
63 : to_print((rank_ == AllProcs) || (
64#ifdef AMREX_USE_MPI
65 comm_ != MPI_COMM_NULL &&
66#endif
67 rank_ == ParallelDescriptor::MyProc(comm_)))
68 , os(os_)
69 {
70 ss.precision(os.precision());
71 }
72
73 ~Print () {
74 if (to_print) {
75 os.flush();
76 os << ss.str();
77 os.flush();
78 }
79 }
80
81 Print (Print const&) = delete;
82 Print (Print &&) = delete;
83 Print& operator= (Print const&) = delete;
84 Print& operator= (Print &&) = delete;
85
87 ss.precision(p);
88 return *this;
89 }
90
91 template <typename T>
92 Print& operator<< (const T& x) {
93 ss << x;
94 return *this;
95 }
96
97 Print& operator<< ( std::basic_ostream<char, std::char_traits<char> >&
98 (*func)(std::basic_ostream<char, std::char_traits<char> >&))
99 {
100 ss << func;
101 return *this;
102 }
103
104 private:
105 bool to_print;
106 std::ostream &os;
107 std::ostringstream ss;
108 };
109
112 : public Print
113 {
114 public:
116 explicit AllPrint (std::ostream& os_ = amrex::OutStream())
117 : Print(Print::AllProcs, os_)
118 {}
119
120 };
121
124 {
125 public:
126
127 static constexpr int AllProcs = -1;
128
129 explicit PrintToFile (std::string file_name_)
130 : file_name(std::move(file_name_))
131 , to_print(ParallelContext::IOProcessorSub())
132 { Initialize(); }
133
134 PrintToFile (std::string file_name_, int rank_ )
135 : file_name(std::move(file_name_))
136 , to_print(rank_ == AllProcs || rank_ == ParallelContext::MyProcSub())
137 { Initialize(); }
138
139 PrintToFile (std::string file_name_, int rank_, MPI_Comm comm_)
140 : file_name(std::move(file_name_))
141 , to_print((rank_ == AllProcs) || (
142#ifdef AMREX_USE_MPI
143 comm_ != MPI_COMM_NULL &&
144#endif
145 rank_ == ParallelDescriptor::MyProc(comm_)))
146 { Initialize(); }
147
149 if (to_print) {
150 ofs.flush();
151 ofs << ss.str();
152 ofs.flush();
153 }
154 }
155
156 PrintToFile (PrintToFile const&) = delete;
160
162 ss.precision(p);
163 return *this;
164 }
165
166 template <typename T>
168 ss << x;
169 return *this;
170 }
171
172 PrintToFile& operator<< ( std::basic_ostream<char, std::char_traits<char> >&
173 (*func)(std::basic_ostream<char, std::char_traits<char> >&))
174 {
175 ss << func;
176 return *this;
177 }
178
179 private:
180
181 void Initialize() {
182 if (to_print) {
183 int my_proc_global = ParallelDescriptor::MyProc();
184 std::string proc_file_name = file_name + "." + std::to_string(my_proc_global);
185#ifdef AMREX_USE_OMP
186 proc_file_name += "." + std::to_string(omp_get_thread_num());
187#endif
188 ofs.open(proc_file_name, std::ios_base::app);
189 if (!ofs.is_open()) {
190 amrex::Error("Could not open file for appending in amrex::Print()");
191 }
192 ss.precision(ofs.precision());
193 }
194 }
195
196 std::string file_name;
197 bool to_print;
198 std::ofstream ofs;
199 std::ostringstream ss;
200 };
201
204 : public PrintToFile
205 {
206 public:
208 explicit AllPrintToFile (std::string file_name_)
209 : PrintToFile(std::move(file_name_), Print::AllProcs)
210 {}
211
212 };
213}
214
215#endif
#define AMREX_D_TERM(a, b, c)
Definition AMReX_SPACE.H:172
Print on all processors of the default communicator.
Definition AMReX_Print.H:205
AllPrintToFile(std::string file_name_)
Example: AllPrint() << " x = " << x << ' ';.
Definition AMReX_Print.H:208
Print on all processors of the default communicator.
Definition AMReX_Print.H:113
AllPrint(std::ostream &os_=amrex::OutStream())
Example: AllPrint() << " x = " << x << ' ';.
Definition AMReX_Print.H:116
This class prints to a file with a given base name.
Definition AMReX_Print.H:124
static constexpr int AllProcs
Definition AMReX_Print.H:127
PrintToFile(std::string file_name_, int rank_)
Definition AMReX_Print.H:134
PrintToFile & operator=(PrintToFile const &)=delete
PrintToFile(std::string file_name_, int rank_, MPI_Comm comm_)
Definition AMReX_Print.H:139
PrintToFile(std::string file_name_)
Definition AMReX_Print.H:129
PrintToFile(PrintToFile const &)=delete
PrintToFile & operator<<(const T &x)
Definition AMReX_Print.H:167
PrintToFile(PrintToFile &&)=delete
~PrintToFile()
Definition AMReX_Print.H:148
PrintToFile & SetPrecision(int p)
Definition AMReX_Print.H:161
This class provides the user with a few print options.
Definition AMReX_Print.H:35
Print(std::ostream &os_=amrex::OutStream())
Print on I/O Processor of the default communicator Example: Print() << " x = " << x << ' ';.
Definition AMReX_Print.H:44
Print & operator<<(const T &x)
Definition AMReX_Print.H:92
Print & operator=(Print const &)=delete
Print(Print &&)=delete
Print(Print const &)=delete
~Print()
Definition AMReX_Print.H:73
Print(int rank_, MPI_Comm comm_, std::ostream &os_=amrex::OutStream())
Print on process rank_ of communicator comm_ Example: Print(rank_, comm_) << " x = " << x << ' ';.
Definition AMReX_Print.H:62
Print & SetPrecision(int p)
Definition AMReX_Print.H:86
static constexpr int AllProcs
Definition AMReX_Print.H:38
Print(int rank_, std::ostream &os_=amrex::OutStream())
Print on given rank of the default communicator Example: Print(2) << " x = " << x << ' '; // Print on...
Definition AMReX_Print.H:53
std::array< T, N > Array
Definition AMReX_Array.H:26
int MyProc() noexcept
Definition AMReX_ParallelDescriptor.H:128
int MPI_Comm
Definition AMReX_ccse-mpi.H:51
static constexpr int MPI_COMM_NULL
Definition AMReX_ccse-mpi.H:59
Definition AMReX_Amr.cpp:49
std::ostream & operator<<(std::ostream &os, AmrMesh const &amr_mesh)
Stream helper; forwards to the friend declared inside AmrMesh.
Definition AMReX_AmrMesh.cpp:1238
void Error(const std::string &msg)
Print out message to cerr and exit via amrex::Abort().
Definition AMReX.cpp:234
std::ostream & OutStream()
Definition AMReX.cpp:956