7 #ifndef IOSS_Ioss_ParallelUtils_h
8 #define IOSS_Ioss_ParallelUtils_h
17 #ifdef SEACAS_HAVE_MPI
40 bool get_environment(
const std::string &
name, std::string &value,
bool sync_parallel)
const;
68 std::string
decode_filename(
const std::string &filename,
bool is_parallel)
const;
92 void memory_stats(int64_t &min, int64_t &max, int64_t &avg)
const;
108 template <
typename T>
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;
119 void progress(
const std::string &output)
const;
125 #ifdef SEACAS_HAVE_MPI
126 inline MPI_Datatype mpi_type(
double ) {
return MPI_DOUBLE; }
127 inline MPI_Datatype mpi_type(
float ) {
return MPI_FLOAT; }
128 inline MPI_Datatype mpi_type(
int ) {
return MPI_INT; }
129 inline MPI_Datatype mpi_type(
char ) {
return MPI_CHAR; }
130 inline MPI_Datatype mpi_type(
long int ) {
return MPI_LONG_LONG_INT; }
131 inline MPI_Datatype mpi_type(
long long int ) {
return MPI_LONG_LONG_INT; }
132 inline MPI_Datatype mpi_type(
unsigned int ) {
return MPI_UNSIGNED; }
133 inline MPI_Datatype mpi_type(
unsigned long int ) {
return MPI_UNSIGNED_LONG; }
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,
141 int processor_count = 0;
142 int my_processor = 0;
143 MPI_Comm_size(comm, &processor_count);
144 MPI_Comm_rank(comm, &my_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 "
155 <<
"\n which exceeds the storage capacity of the integers "
156 "used by MPI functions.\n";
163 for (
size_t i = 1; i < pow_2; i++) {
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]);
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,
179 MPI_Recv(&recvbuf[recvdisp[exchange_proc]], rcv_cnt, mpi_type(T(0)), exchange_proc, tag,
181 MPI_Send((
void *)&sendbuf[senddisp[exchange_proc]], snd_cnt, mpi_type(T(0)),
182 exchange_proc, tag, comm);
188 std::copy(&sendbuf[senddisp[my_processor]],
189 &sendbuf[senddisp[my_processor] + sendcounts[my_processor]],
190 &recvbuf[recvdisp[my_processor]]);
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,
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];
211 if (max_comm < one << 31) {
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)),
227 return MY_Alltoallv64(sendbuf, sendcnts, senddisp, recvbuf, recvcnts, recvdisp, comm);
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,
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);
246 template <
typename T>
251 #ifdef SEACAS_HAVE_MPI
254 std::ostringstream errmsg;
259 std::vector<T> maxout(local_minmax.size());
260 MPI_Op oper = MPI_MAX;
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";
280 for (
size_t i = 0; i < local_minmax.size(); i++) {
281 local_minmax[i] = maxout[i];