Block-Structured AMR Software Framework
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 : rank(ParallelContext::IOProcessorNumberSub())
46 , comm(ParallelContext::CommunicatorSub())
47 , os(os_)
48 { ss.precision(os.precision()); }
49
54 Print (int rank_, std::ostream& os_ = amrex::OutStream())
55 : rank(rank_)
56 , comm(ParallelContext::CommunicatorSub())
57 , os(os_)
58 { ss.precision(os.precision()); }
59
64 Print (int rank_, MPI_Comm comm_, std::ostream& os_ = amrex::OutStream())
65 : rank(rank_)
66 , comm(comm_)
67 , os(os_)
68 { ss.precision(os.precision()); }
69
70 ~Print () {
72 std::ostream * my_os = ParallelContext::OFSPtrSub();
73 if (my_os) {
74 my_os->flush();
75 (*my_os) << ss.str();
76 my_os->flush();
77 }
78 os.flush();
79 os << ss.str();
80 os.flush();
81 }
82 }
83
84 Print (Print const&) = delete;
85 Print (Print &&) = delete;
86 Print& operator= (Print const&) = delete;
87 Print& operator= (Print &&) = delete;
88
90 ss.precision(p);
91 return *this;
92 }
93
94 template <typename T>
95 Print& operator<< (const T& x) {
96 ss << x;
97 return *this;
98 }
99
100 Print& operator<< ( std::basic_ostream<char, std::char_traits<char> >&
101 (*func)(std::basic_ostream<char, std::char_traits<char> >&))
102 {
103 ss << func;
104 return *this;
105 }
106
107 private:
108 int rank;
110 std::ostream &os;
111 std::ostringstream ss;
112 };
113
116 : public Print
117 {
118 public:
120 explicit AllPrint (std::ostream& os_ = amrex::OutStream())
121 : Print(Print::AllProcs, os_)
122 {}
123
124 };
125
128 {
129 public:
130
131 static constexpr int AllProcs = -1;
132
133 explicit PrintToFile (std::string file_name_)
134 : file_name(std::move(file_name_))
135 , rank(ParallelContext::IOProcessorNumberSub())
136 , comm(ParallelContext::CommunicatorSub())
137 { Initialize(); }
138
139 PrintToFile (std::string file_name_, int rank_ )
140 : file_name(std::move(file_name_))
141 , rank(rank_)
142 , comm(ParallelContext::CommunicatorSub())
143 { Initialize(); }
144
145 PrintToFile (std::string file_name_, int rank_, MPI_Comm comm_)
146 : file_name(std::move(file_name_))
147 , rank(rank_)
148 , comm(comm_)
149 { Initialize(); }
150
153 ofs.flush();
154 ofs << ss.str();
155 ofs.flush();
156 }
157 }
158
159 PrintToFile (PrintToFile const&) = delete;
163
165 ss.precision(p);
166 return *this;
167 }
168
169 template <typename T>
171 ss << x;
172 return *this;
173 }
174
175 PrintToFile& operator<< ( std::basic_ostream<char, std::char_traits<char> >&
176 (*func)(std::basic_ostream<char, std::char_traits<char> >&))
177 {
178 ss << func;
179 return *this;
180 }
181
182 private:
183
184 void Initialize() {
185 int my_proc = ParallelContext::MyProcSub();
186 if (rank == AllProcs || rank == my_proc) {
187 int my_proc_global = ParallelDescriptor::MyProc();
188 std::string proc_file_name = file_name + "." + std::to_string(my_proc_global);
189#ifdef AMREX_USE_OMP
190 proc_file_name += "." + std::to_string(omp_get_thread_num());
191#endif
192 ofs.open(proc_file_name, std::ios_base::app);
193 if (!ofs.is_open()) {
194 amrex::Error("Could not open file for appending in amrex::Print()");
195 }
196 ss.precision(ofs.precision());
197 }
198 }
199
200 std::string file_name;
201 int rank;
203 std::ofstream ofs;
204 std::ostringstream ss;
205 };
206
209 : public PrintToFile
210 {
211 public:
213 explicit AllPrintToFile (std::string file_name_)
214 : PrintToFile(std::move(file_name_), Print::AllProcs)
215 {}
216
217 };
218}
219
220#endif
#define AMREX_D_TERM(a, b, c)
Definition AMReX_SPACE.H:129
int MPI_Comm
Definition AMReX_ccse-mpi.H:47
Print on all processors of the default communicator.
Definition AMReX_Print.H:210
AllPrintToFile(std::string file_name_)
Example: AllPrint() << " x = " << x << ' ';.
Definition AMReX_Print.H:213
Print on all processors of the default communicator.
Definition AMReX_Print.H:117
AllPrint(std::ostream &os_=amrex::OutStream())
Example: AllPrint() << " x = " << x << ' ';.
Definition AMReX_Print.H:120
This class prints to a file with a given base name.
Definition AMReX_Print.H:128
static constexpr int AllProcs
Definition AMReX_Print.H:131
PrintToFile(std::string file_name_, int rank_)
Definition AMReX_Print.H:139
std::ofstream ofs
Definition AMReX_Print.H:203
MPI_Comm comm
Definition AMReX_Print.H:202
void Initialize()
Definition AMReX_Print.H:184
PrintToFile & operator=(PrintToFile const &)=delete
int rank
Definition AMReX_Print.H:201
PrintToFile(std::string file_name_, int rank_, MPI_Comm comm_)
Definition AMReX_Print.H:145
PrintToFile(std::string file_name_)
Definition AMReX_Print.H:133
PrintToFile(PrintToFile const &)=delete
PrintToFile & operator<<(const T &x)
Definition AMReX_Print.H:170
PrintToFile(PrintToFile &&)=delete
std::string file_name
Definition AMReX_Print.H:200
~PrintToFile()
Definition AMReX_Print.H:151
PrintToFile & SetPrecision(int p)
Definition AMReX_Print.H:164
std::ostringstream ss
Definition AMReX_Print.H:204
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:95
Print & operator=(Print const &)=delete
Print(Print &&)=delete
Print(Print const &)=delete
~Print()
Definition AMReX_Print.H:70
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:64
MPI_Comm comm
Definition AMReX_Print.H:109
int rank
Definition AMReX_Print.H:108
std::ostream & os
Definition AMReX_Print.H:110
Print & SetPrecision(int p)
Definition AMReX_Print.H:89
std::ostringstream ss
Definition AMReX_Print.H:111
static constexpr int AllProcs
Definition AMReX_Print.H:38
Print(int rank_, std::ostream &os_=amrex::OutStream())
Print on all processors of the default communicator Example: Print(Print::AllProcs) << " x = " << x <...
Definition AMReX_Print.H:54
int MyProcSub() noexcept
my sub-rank in current frame
Definition AMReX_ParallelContext.H:76
std::ofstream * OFSPtrSub() noexcept
Pointer to ofstream.
Definition AMReX_ParallelContext.H:82
int MyProc() noexcept
return the rank number local to the current Parallel Context
Definition AMReX_ParallelDescriptor.H:125
Definition AMReX_Amr.cpp:49
void Error(const std::string &msg)
Print out message to cerr and exit via amrex::Abort().
Definition AMReX.cpp:224
std::ostream & OutStream()
Definition AMReX.cpp:899
std::ostream & operator<<(std::ostream &os, AmrMesh const &amr_mesh)
Definition AMReX_AmrMesh.cpp:1236
std::array< T, N > Array
Definition AMReX_Array.H:24