IOSS  2.0
Ioss_ParallelUtils.h
Go to the documentation of this file.
1 // Copyright(C) 1999-2020 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6 
7 #ifndef IOSS_Ioss_ParallelUtils_h
8 #define IOSS_Ioss_ParallelUtils_h
9 
10 #include <Ioss_CodeTypes.h> // for Int64Vector, IntVector
11 #include <Ioss_Utils.h>
12 #include <cassert>
13 #include <cstddef> // for size_t
14 #include <string> // for string
15 #include <vector> // for vector
16 
17 #ifdef SEACAS_HAVE_MPI
18 #include <Ioss_SerializeIO.h>
19 #endif
20 
21 namespace Ioss {
22 
24  {
25  public:
26  explicit ParallelUtils(MPI_Comm the_communicator);
27  ~ParallelUtils() = default;
28 
29  // Assignment operator
30  // Copy constructor
31 
32  enum MinMax { DO_MAX, DO_MIN, DO_SUM };
33 
34  /*!
35  * Returns 'true' if 'name' is defined in the environment.
36  * The value of the environment variable is returned in 'value'.
37  * getenv system call is only done on processor 0.
38  * If '!sync_parallel', then don't push to other processors.
39  */
40  bool get_environment(const std::string &name, std::string &value, bool sync_parallel) const;
41 
42  /*!
43  * See if any external properties specified via the
44  * IOSS_PROPERTIES environment variable. If any found, add to
45  * `properties`.
46  */
48 
49  /*!
50  * Returns 'true' if 'name' is defined in the environment.
51  * The value of the environment variable is converted to an
52  * integer via the atoi library call and returned in 'value'.
53  * No checking is done to ensure that the environment variable
54  * points to a valid integer.
55  * getenv system call is only done on processor 0.
56  * If '!sync_parallel', then don't push to other processors.
57  */
58  bool get_environment(const std::string &name, int &value, bool sync_parallel) const;
59 
60  /*!
61  * Returns 'true' if 'name' is defined in the environment no
62  * matter what the value. Returns false otherwise.
63  * getenv system call is only done on processor 0.
64  * If '!sync_parallel', then don't push to other processors.
65  */
66  bool get_environment(const std::string &name, bool sync_parallel) const;
67 
68  std::string decode_filename(const std::string &filename, bool is_parallel) const;
69 
70  MPI_Comm communicator() const { return communicator_; }
71  int parallel_size() const;
72  int parallel_rank() const;
73 
74  void barrier() const;
75 
76  /*!
77  * Global OR of attribute strings, the processors which have no
78  * knowledge of the value should initialize to '0' and the
79  * processors with knowledge set the appropriate values.
80  */
81  void attribute_reduction(int length, char buffer[]) const;
82 
83  /*!
84  * Generate a "globally unique id" which is unique over all entities
85  * of a specific type over all processors.
86  * Used by some applications for uniquely identifying an entity.
87  * If `rank` == -1, then use parallel_rank; otherwise use rank
88  */
89  int64_t generate_guid(size_t id, int rank = -1) const;
90 
91  /*! Return min, max, average memory used by any process */
92  void memory_stats(int64_t &min, int64_t &max, int64_t &avg) const;
93 
94  /*! Return high-water-mark min, max, average memory used by any process */
95  /* May be inaccurate unless system maintains this information */
96  void hwm_memory_stats(int64_t &min, int64_t &max, int64_t &avg) const;
97 
98  /*! Vector 'local_counts' contains the number of objects
99  * local to this processor. On exit, global_counts
100  * contains the total number of objects on all processors.
101  * Assumes that ordering is the same on all processors
102  */
103  void global_count(const IntVector &local_counts, IntVector &global_counts) const;
104  void global_count(const Int64Vector &local_counts, Int64Vector &global_counts) const;
105 
106  template <typename T> T global_minmax(T local_minmax, MinMax which) const;
107 
108  template <typename T>
109  void global_array_minmax(std::vector<T> &local_minmax, MinMax which) const;
110 
111  template <typename T> void gather(T my_value, std::vector<T> &result) const;
112  template <typename T> void all_gather(T my_value, std::vector<T> &result) const;
113  template <typename T> void gather(std::vector<T> &my_values, std::vector<T> &result) const;
114  template <typename T> void all_gather(std::vector<T> &my_values, std::vector<T> &result) const;
115  template <typename T>
116  int gather(int vals_count, int size_per_val, std::vector<T> &my_values,
117  std::vector<T> &result) const;
118 
119  void progress(const std::string &output) const;
120 
121  private:
123  };
124 
125 #ifdef SEACAS_HAVE_MPI
126  inline MPI_Datatype mpi_type(double /*dummy*/) { return MPI_DOUBLE; }
127  inline MPI_Datatype mpi_type(float /*dummy*/) { return MPI_FLOAT; }
128  inline MPI_Datatype mpi_type(int /*dummy*/) { return MPI_INT; }
129  inline MPI_Datatype mpi_type(char /*dummy*/) { return MPI_CHAR; }
130  inline MPI_Datatype mpi_type(long int /*dummy*/) { return MPI_LONG_LONG_INT; }
131  inline MPI_Datatype mpi_type(long long int /*dummy*/) { return MPI_LONG_LONG_INT; }
132  inline MPI_Datatype mpi_type(unsigned int /*dummy*/) { return MPI_UNSIGNED; }
133  inline MPI_Datatype mpi_type(unsigned long int /*dummy*/) { return MPI_UNSIGNED_LONG; }
134 
135  template <typename T>
136  int MY_Alltoallv64(const std::vector<T> &sendbuf, const std::vector<int64_t> &sendcounts,
137  const std::vector<int64_t> &senddisp, std::vector<T> &recvbuf,
138  const std::vector<int64_t> &recvcounts, const std::vector<int64_t> &recvdisp,
139  MPI_Comm comm)
140  {
141  int processor_count = 0;
142  int my_processor = 0;
143  MPI_Comm_size(comm, &processor_count);
144  MPI_Comm_rank(comm, &my_processor);
145 
146  // Verify that all 'counts' can fit in an integer. Symmetric
147  // communication, so recvcounts are sendcounts on another processor.
148  for (int i = 0; i < processor_count; i++) {
149  int snd_cnt = static_cast<int>(sendcounts[i]);
150  if (static_cast<int64_t>(snd_cnt) != sendcounts[i]) {
151  std::ostringstream errmsg;
152  errmsg << "ERROR: The number of items that must be communicated via MPI calls from\n"
153  << " processor " << my_processor << " to processor " << i << " is "
154  << sendcounts[i]
155  << "\n which exceeds the storage capacity of the integers "
156  "used by MPI functions.\n";
157  IOSS_ERROR(errmsg);
158  }
159  }
160 
161  size_t pow_2 = Ioss::Utils::power_2(processor_count);
162 
163  for (size_t i = 1; i < pow_2; i++) {
164  MPI_Status status{};
165 
166  int tag = 24713;
167  size_t exchange_proc = i ^ my_processor;
168  if (exchange_proc < static_cast<size_t>(processor_count)) {
169  int snd_cnt = static_cast<int>(
170  sendcounts[exchange_proc]); // Converts from int64_t to int as needed by mpi
171  int rcv_cnt = static_cast<int>(recvcounts[exchange_proc]);
172  if (static_cast<size_t>(my_processor) < exchange_proc) {
173  MPI_Send((void *)&sendbuf[senddisp[exchange_proc]], snd_cnt, mpi_type(T(0)),
174  exchange_proc, tag, comm);
175  MPI_Recv(&recvbuf[recvdisp[exchange_proc]], rcv_cnt, mpi_type(T(0)), exchange_proc, tag,
176  comm, &status);
177  }
178  else {
179  MPI_Recv(&recvbuf[recvdisp[exchange_proc]], rcv_cnt, mpi_type(T(0)), exchange_proc, tag,
180  comm, &status);
181  MPI_Send((void *)&sendbuf[senddisp[exchange_proc]], snd_cnt, mpi_type(T(0)),
182  exchange_proc, tag, comm);
183  }
184  }
185  }
186 
187  // Take care of this processor's data movement...
188  std::copy(&sendbuf[senddisp[my_processor]],
189  &sendbuf[senddisp[my_processor] + sendcounts[my_processor]],
190  &recvbuf[recvdisp[my_processor]]);
191  return 0;
192  }
193 
194  template <typename T>
195  int MY_Alltoallv(const std::vector<T> &sendbuf, const std::vector<int64_t> &sendcnts,
196  const std::vector<int64_t> &senddisp, std::vector<T> &recvbuf,
197  const std::vector<int64_t> &recvcnts, const std::vector<int64_t> &recvdisp,
198  MPI_Comm comm)
199  {
200 // Wrapper to handle case where send/recv counts and displacements are 64-bit integers.
201 // Two cases:
202 // 1) They are of type 64-bit integers, but only storing data in the 32-bit integer range.
203 // -- if (sendcnts[#proc-1] + senddisp[#proc-1] < 2^31, then we are ok
204 // 2) They are of type 64-bit integers, and storing data in the 64-bit integer range.
205 // -- call special alltoallv which does point-to-point sends
206 #if 1
207  int processor_count = 0;
208  MPI_Comm_size(comm, &processor_count);
209  size_t max_comm = sendcnts[processor_count - 1] + senddisp[processor_count - 1];
210  size_t one = 1;
211  if (max_comm < one << 31) {
212  // count and displacement data in range, need to copy to integer vector.
213  std::vector<int> send_cnt(sendcnts.begin(), sendcnts.end());
214  std::vector<int> send_dis(senddisp.begin(), senddisp.end());
215  std::vector<int> recv_cnt(recvcnts.begin(), recvcnts.end());
216  std::vector<int> recv_dis(recvdisp.begin(), recvdisp.end());
217  return MPI_Alltoallv((void *)sendbuf.data(), send_cnt.data(), send_dis.data(), mpi_type(T(0)),
218  (void *)recvbuf.data(), recv_cnt.data(), recv_dis.data(), mpi_type(T(0)),
219  comm);
220  }
221  else {
222 #endif
223  // Same as if each processor sent a message to every other process with:
224  // MPI_Send(sendbuf+senddisp[i]*sizeof(sendtype),sendcnts[i], sendtype, i, tag, comm);
225  // And received a message from each processor with a call to:
226  // MPI_Recv(recvbuf+recvdisp[i]*sizeof(recvtype),recvcnts[i], recvtype, i, tag, comm);
227  return MY_Alltoallv64(sendbuf, sendcnts, senddisp, recvbuf, recvcnts, recvdisp, comm);
228 #if 1
229  }
230 #endif
231  }
232 
233  template <typename T>
234  int MY_Alltoallv(const std::vector<T> &sendbuf, const std::vector<int> &sendcnts,
235  const std::vector<int> &senddisp, std::vector<T> &recvbuf,
236  const std::vector<int> &recvcnts, const std::vector<int> &recvdisp,
237  MPI_Comm comm)
238  {
239  return MPI_Alltoallv((void *)sendbuf.data(), const_cast<int *>(sendcnts.data()),
240  const_cast<int *>(senddisp.data()), mpi_type(T(0)), recvbuf.data(),
241  const_cast<int *>(recvcnts.data()), const_cast<int *>(recvdisp.data()),
242  mpi_type(T(0)), comm);
243  }
244 #endif
245 
246  template <typename T>
247  void ParallelUtils::global_array_minmax(std::vector<T> &local_minmax, MinMax which) const
248  {
249  PAR_UNUSED(local_minmax);
250  PAR_UNUSED(which);
251 #ifdef SEACAS_HAVE_MPI
252  if (parallel_size() > 1 && !local_minmax.empty()) {
254  std::ostringstream errmsg;
255  errmsg << "Attempting mpi while in barrier owned by " << Ioss::SerializeIO::getOwner();
256  IOSS_ERROR(errmsg);
257  }
258 
259  std::vector<T> maxout(local_minmax.size());
260  MPI_Op oper = MPI_MAX;
261  if (which == Ioss::ParallelUtils::DO_MAX) {
262  oper = MPI_MAX;
263  }
264  else if (which == Ioss::ParallelUtils::DO_MIN) {
265  oper = MPI_MIN;
266  }
267  else if (which == Ioss::ParallelUtils::DO_SUM) {
268  oper = MPI_SUM;
269  }
270 
271  const int success =
272  MPI_Allreduce((void *)(local_minmax.data()), maxout.data(),
273  static_cast<int>(local_minmax.size()), mpi_type(T()), oper, communicator_);
274  if (success != MPI_SUCCESS) {
275  std::ostringstream errmsg;
276  errmsg << "Ioss::ParallelUtils::global_array_minmax - MPI_Allreduce failed";
277  IOSS_ERROR(errmsg);
278  }
279  // Now copy back into passed in array...
280  for (size_t i = 0; i < local_minmax.size(); i++) {
281  local_minmax[i] = maxout[i];
282  }
283  }
284 #endif
285  }
286 
287 } // namespace Ioss
288 #endif
Ioss::ParallelUtils::hwm_memory_stats
void hwm_memory_stats(int64_t &min, int64_t &max, int64_t &avg) const
Definition: Ioss_ParallelUtils.C:242
Ioss::IntVector
std::vector< int > IntVector
Definition: Ioss_CodeTypes.h:25
Ioss::ParallelUtils::attribute_reduction
void attribute_reduction(int length, char buffer[]) const
Definition: Ioss_ParallelUtils.C:276
Ioss::ParallelUtils::global_array_minmax
void global_array_minmax(std::vector< T > &local_minmax, MinMax which) const
Definition: Ioss_ParallelUtils.h:247
Ioss::ParallelUtils::decode_filename
std::string decode_filename(const std::string &filename, bool is_parallel) const
Definition: Ioss_ParallelUtils.C:190
Ioss::ParallelUtils::DO_MIN
@ DO_MIN
Definition: Ioss_ParallelUtils.h:32
Ioss::ParallelUtils::parallel_rank
int parallel_rank() const
Definition: Ioss_ParallelUtils.C:217
Ioss
The main namespace for the Ioss library.
Definition: Ioad_DatabaseIO.C:40
Ioss::ParallelUtils::DO_SUM
@ DO_SUM
Definition: Ioss_ParallelUtils.h:32
Ioss::ParallelUtils::add_environment_properties
void add_environment_properties(Ioss::PropertyManager &properties)
Definition: Ioss_ParallelUtils.C:50
Ioss::ParallelUtils::~ParallelUtils
~ParallelUtils()=default
Ioss::ParallelUtils::global_minmax
T global_minmax(T local_minmax, MinMax which) const
Definition: Ioss_ParallelUtils.C:381
Ioss::SerializeIO::inBarrier
static bool inBarrier()
Definition: Ioss_SerializeIO.h:82
Ioss::PropertyManager
A collection of Ioss::Property objects.
Definition: Ioss_PropertyManager.h:24
anonymous_namespace{Iocgns_DecompositionData.C}::rank
int rank
Definition: Iocgns_DecompositionData.C:30
Ioss::SerializeIO::isEnabled
static bool isEnabled()
Definition: Ioss_SerializeIO.h:80
Ioss::ParallelUtils::memory_stats
void memory_stats(int64_t &min, int64_t &max, int64_t &avg) const
Definition: Ioss_ParallelUtils.C:228
IOSS_ERROR
#define IOSS_ERROR(errmsg)
Definition: Ioss_Utils.h:32
Ioss::ParallelUtils::generate_guid
int64_t generate_guid(size_t id, int rank=-1) const
Definition: Ioss_ParallelUtils.C:259
Ioss::Int64Vector
std::vector< int64_t > Int64Vector
Definition: Ioss_CodeTypes.h:26
Ioss_Utils.h
Ioss::Utils::power_2
static int power_2(int count)
Definition: Ioss_Utils.h:222
Ioss::ParallelUtils::DO_MAX
@ DO_MAX
Definition: Ioss_ParallelUtils.h:32
Ioss_SerializeIO.h
Ioss::ParallelUtils::all_gather
void all_gather(T my_value, std::vector< T > &result) const
Definition: Ioss_ParallelUtils.C:450
Ioss::ParallelUtils::parallel_size
int parallel_size() const
Definition: Ioss_ParallelUtils.C:206
Ioss::ParallelUtils::global_count
void global_count(const IntVector &local_counts, IntVector &global_counts) const
Definition: Ioss_ParallelUtils.C:305
Ioss::ParallelUtils::get_environment
bool get_environment(const std::string &name, std::string &value, bool sync_parallel) const
Definition: Ioss_ParallelUtils.C:98
Ioss::ParallelUtils::gather
void gather(T my_value, std::vector< T > &result) const
Definition: Ioss_ParallelUtils.C:427
Ioss::ParallelUtils::communicator
MPI_Comm communicator() const
Definition: Ioss_ParallelUtils.h:70
Ioss::ParallelUtils
Definition: Ioss_ParallelUtils.h:24
Ioss::ParallelUtils::barrier
void barrier() const
Definition: Ioss_ParallelUtils.C:298
anonymous_namespace{io_info.C}::name
std::string name(const Ioss::GroupingEntity *entity)
Definition: io_info.C:65
MPI_Comm
int MPI_Comm
Definition: Ioss_CodeTypes.h:66
Ioss::ParallelUtils::communicator_
MPI_Comm communicator_
Definition: Ioss_ParallelUtils.h:122
Ioss::ParallelUtils::ParallelUtils
ParallelUtils(MPI_Comm the_communicator)
Definition: Ioss_ParallelUtils.C:48
Ioss_CodeTypes.h
Ioss::ParallelUtils::MinMax
MinMax
Definition: Ioss_ParallelUtils.h:32
PAR_UNUSED
#define PAR_UNUSED(x)
Definition: Ioss_CodeTypes.h:56
Ioss::ParallelUtils::progress
void progress(const std::string &output) const
Definition: Ioss_ParallelUtils.C:494
Ioss::SerializeIO::getOwner
static int getOwner()
Definition: Ioss_SerializeIO.h:68