SIRIUS 7.5.0
Electronic structure library and applications
pstdout.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2022 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file pstdout.hpp
21 *
22 * \brief Contains implementation of the parallel standard output.
23 */
24
25#ifndef __PSTDOUT_HPP__
26#define __PSTDOUT_HPP__
27
28#include "communicator.hpp"
29
30namespace sirius {
31
32namespace mpi {
33
34/// Parallel standard output.
35/** Proveides an ordered standard output from multiple MPI ranks.
36 * pstdout pout(comm);
37 * pout << "Hello from rank " << comm.rank() << std::end;
38 * // print from root rank (id=0) and flush the internal buffer
39 * std::cout << pout.flush(0);
40 */
41class pstdout : public std::stringstream
42{
43 private:
44 Communicator const& comm_;
45
46 public:
47 pstdout(Communicator const& comm__)
48 : comm_(comm__)
49 {
50 }
51
52 std::string flush(int root__)
53 {
54 std::stringstream s;
55
56 std::vector<int> counts(comm_.size());
57 int count = this->str().length();
58 comm_.allgather(&count, counts.data(), 1, comm_.rank());
59
60 int offset{0};
61 for (int i = 0; i < comm_.rank(); i++) {
62 offset += counts[i];
63 }
64
65 int sz = count;
66 /* total size of the output buffer */
67 comm_.allreduce(&sz, 1);
68
69 if (sz != 0) {
70 std::vector<char> outb(sz);
71 comm_.allgather(this->str().c_str(), &outb[0], count, offset);
72 s.write(outb.data(), sz);
73 }
74 /* reset the internal string */
75 this->str("");
76 if (comm_.rank() == root__) {
77 return s.str();
78 } else {
79 return std::string("");
80 }
81 }
82};
83
84}
85
86} // namespace sirius
87
88#endif
MPI communicator wrapper.
void allgather(T *buffer__, int const *recvcounts__, int const *displs__) const
In-place MPI_Allgatherv.
int size() const
Size of the communicator (number of ranks).
void allreduce(T *buffer__, int count__) const
Perform the in-place (the output buffer is used as the input buffer) all-to-all reduction.
int rank() const
Rank of MPI process inside communicator.
Parallel standard output.
Definition: pstdout.hpp:42
Contains declaration and implementation of mpi::Communicator class.
Namespace of the SIRIUS library.
Definition: sirius.f90:5