19#include <deal.II/base/mpi.templates.h>
28#include <boost/serialization/utility.hpp>
43 const unsigned int my_partition_id,
44 const unsigned int n_partitions,
47 static_assert(std::is_same_v<types::global_dof_index, IndexSet::size_type>,
48 "IndexSet::size_type must match types::global_dof_index for "
49 "using this function");
50 const unsigned int remain = total_size % n_partitions;
55 min_size * my_partition_id +
std::min(my_partition_id, remain);
57 min_size * (my_partition_id + 1) +
std::min(my_partition_id + 1, remain);
78 std::vector<MinMaxAvg>
82 std::vector<MinMaxAvg> results(my_values.size());
90#ifdef DEAL_II_WITH_MPI
97 const int ierr = MPI_Comm_size(mpi_communicator, &n_jobs);
112 const int ierr = MPI_Comm_rank(mpi_communicator, &rank);
122 std::vector<unsigned int>
127 return std::vector<unsigned int>{0};
132 std::vector<unsigned int> ranks(size);
133 const int ierr = MPI_Allgather(
134 &rank, 1, MPI_UNSIGNED, ranks.data(), 1, MPI_UNSIGNED, comm_small);
146 const int ierr = MPI_Comm_dup(mpi_communicator, &new_communicator);
148 return new_communicator;
157 const int ierr = MPI_Comm_free(&mpi_communicator);
165 const MPI_Group &group,
169 const int ierr = MPI_Comm_create_group(
comm, group, tag, new_comm);
176 std::vector<IndexSet>
182 std::is_same_v<types::global_dof_index, IndexSet::size_type>,
183 "IndexSet::size_type must match types::global_dof_index for "
184 "using this function");
186 const std::vector<IndexSet::size_type> sizes =
188 const auto total_size =
191 std::vector<IndexSet> res(n_proc,
IndexSet(total_size));
194 for (
unsigned int i = 0; i < n_proc; ++i)
196 res[i].add_range(begin, begin + sizes[i]);
197 begin = begin + sizes[i];
220 std::unique_ptr<MPI_Datatype, void (*)(MPI_Datatype *)>
226 ierr = MPI_Type_commit(&result);
231 ierr = MPI_Type_size_x(result, &size64);
246 auto deleter = [](MPI_Datatype *p) {
249 const int ierr = MPI_Type_free(p);
257 return std::unique_ptr<MPI_Datatype, void (*)(MPI_Datatype *)>(
258 new MPI_Datatype(result), deleter);
263 std::vector<unsigned int>
266 const std::vector<unsigned int> &destinations)
273 for (
const unsigned int destination : destinations)
286 const bool my_destinations_are_unique = [destinations]() {
287 if (destinations.empty())
291 std::vector<unsigned int> my_destinations = destinations;
292 std::sort(my_destinations.begin(), my_destinations.end());
293 return (std::adjacent_find(my_destinations.begin(),
294 my_destinations.end()) ==
295 my_destinations.end());
306 destinations, {}, {}, {}, mpi_comm);
320 std::vector<unsigned int> dest_vector(n_procs);
321 for (
const auto &el : destinations)
327 unsigned int n_recv_from;
328 const int ierr = MPI_Reduce_scatter_block(
329 dest_vector.data(), &n_recv_from, 1, MPI_UNSIGNED, MPI_SUM, mpi_comm);
334 std::vector<MPI_Request> send_requests(destinations.size());
335 for (
const auto &el : destinations)
344 send_requests.data() + (&el - destinations.data()));
352 std::vector<unsigned int> origins(n_recv_from);
353 for (
auto &el : origins)
355 const int ierr = MPI_Recv(&el,
365 if (destinations.size() > 0)
367 const int ierr = MPI_Waitall(destinations.size(),
368 send_requests.data(),
369 MPI_STATUSES_IGNORE);
381 const std::vector<unsigned int> &destinations)
385 const bool my_destinations_are_unique = [destinations]() {
386 std::vector<unsigned int> my_destinations = destinations;
387 const unsigned int n_destinations = my_destinations.size();
388 std::sort(my_destinations.begin(), my_destinations.end());
389 my_destinations.erase(std::unique(my_destinations.begin(),
390 my_destinations.end()),
391 my_destinations.end());
392 return (my_destinations.size() == n_destinations);
402 destinations, {}, {}, {}, mpi_comm)
407 const unsigned int n_procs =
410 for (
const unsigned int destination : destinations)
416 "There is no point in communicating with ourselves."));
420 std::vector<unsigned int> dest_vector(n_procs);
421 for (
const auto &el : destinations)
426 unsigned int n_recv_from = 0;
428 const int ierr = MPI_Reduce_scatter_block(dest_vector.data(),
447 max_reduce(
const void *in_lhs_,
452 const MinMaxAvg *in_lhs =
static_cast<const MinMaxAvg *
>(in_lhs_);
453 MinMaxAvg *inout_rhs =
static_cast<MinMaxAvg *
>(inout_rhs_);
455 for (
int i = 0; i < *len; ++i)
457 inout_rhs[i].sum += in_lhs[i].sum;
458 if (inout_rhs[i].
min > in_lhs[i].
min)
460 inout_rhs[i].min = in_lhs[i].min;
461 inout_rhs[i].min_index = in_lhs[i].min_index;
463 else if (inout_rhs[i].
min == in_lhs[i].
min)
466 if (inout_rhs[i].min_index > in_lhs[i].min_index)
467 inout_rhs[i].min_index = in_lhs[i].min_index;
470 if (inout_rhs[i].
max < in_lhs[i].
max)
472 inout_rhs[i].max = in_lhs[i].max;
473 inout_rhs[i].max_index = in_lhs[i].max_index;
475 else if (inout_rhs[i].
max == in_lhs[i].
max)
478 if (inout_rhs[i].max_index > in_lhs[i].max_index)
479 inout_rhs[i].max_index = in_lhs[i].max_index;
496 for (
unsigned int i = 0; i < my_values.
size(); ++i)
498 result[i].sum = my_values[i];
499 result[i].avg = my_values[i];
500 result[i].min = my_values[i];
501 result[i].max = my_values[i];
502 result[i].min_index = 0;
503 result[i].max_index = 0;
513 static MPI_Datatype type = []() {
516 int lengths[] = {3, 2, 1};
518 MPI_Aint displacements[] = {0,
522 MPI_Datatype
types[] = {MPI_DOUBLE, MPI_INT, MPI_DOUBLE};
525 MPI_Type_create_struct(3, lengths, displacements,
types, &type);
528 ierr = MPI_Type_commit(&type);
534 int ierr = MPI_Type_free(&type);
546 static MPI_Op op = []() {
550 MPI_Op_create(
reinterpret_cast<MPI_User_function *
>(&max_reduce),
551 static_cast<int>(
true),
558 int ierr = MPI_Op_free(&op);
573 std::numeric_limits<double>::max(),
574 std::numeric_limits<double>::lowest(),
579 for (
auto &i : result)
582 const unsigned int my_id =
584 const unsigned int numproc =
587 std::vector<MinMaxAvg> in(my_values.
size());
589 for (
unsigned int i = 0; i < my_values.
size(); ++i)
591 in[i].sum = in[i].min = in[i].max = my_values[i];
592 in[i].min_index = in[i].max_index = my_id;
595 int ierr = MPI_Allreduce(
596 in.data(), result.
data(), my_values.
size(), type, op, mpi_communicator);
599 for (
auto &r : result)
600 r.avg = r.sum / numproc;
622 std::vector<unsigned int>
625 return std::vector<unsigned int>{0};
630 std::vector<IndexSet>
651 return mpi_communicator;
669 for (
unsigned int i = 0; i < my_values.
size(); ++i)
671 result[i].sum = my_values[i];
672 result[i].avg = my_values[i];
673 result[i].min = my_values[i];
674 result[i].max = my_values[i];
675 result[i].min_index = 0;
676 result[i].max_index = 0;
685 const unsigned int max_num_threads)
699#ifdef DEAL_II_WITH_MPI
700 int MPI_has_been_started = 0;
701 const int ierr = MPI_Initialized(&MPI_has_been_started);
704 return (MPI_has_been_started > 0);
712 std::vector<unsigned int>
718 ExcMessage(
"IndexSets have to have the same sizes."));
722 ExcMessage(
"IndexSets have to have the same size on all processes."));
724 std::vector<unsigned int> owning_ranks(indices_to_look_up.
n_elements());
732 owned_indices, indices_to_look_up,
comm, owning_ranks);
740 std::pair<types::global_dof_index, types::global_dof_index>>,
741 std::vector<unsigned int>>
743 consensus_algorithm.
run(process,
comm);
752 namespace CollectiveMutexImplementation
761#ifdef DEAL_II_WITH_MPI
762# if __cpp_lib_uncaught_exceptions >= 201411
764 if (std::uncaught_exceptions() != 0)
766 if (std::uncaught_exception() ==
true)
770 <<
"---------------------------------------------------------\n"
771 <<
"An exception was thrown inside a section of the program\n"
772 <<
"guarded by a CollectiveMutex.\n"
773 <<
"Because a CollectiveMutex guards critical communication\n"
774 <<
"handling the exception would likely\n"
775 <<
"deadlock because only the current process is aware of the\n"
776 <<
"exception. To prevent this deadlock, the program will be\n"
778 <<
"---------------------------------------------------------"
781 MPI_Abort(MPI_COMM_WORLD, 1);
792 , request(MPI_REQUEST_NULL)
808 "Error: MPI::CollectiveMutex is still locked while being destroyed!"));
823 "Error: MPI::CollectiveMutex needs to be unlocked before lock()"));
825#ifdef DEAL_II_WITH_MPI
833 const int ierr = MPI_Barrier(
comm);
839 const int ierr = MPI_Wait(&
request, MPI_STATUS_IGNORE);
864 "Error: MPI::CollectiveMutex needs to be locked before unlock()"));
866#ifdef DEAL_II_WITH_MPI
877 const int ierr = MPI_Barrier(
comm);
894 const std::function<
bool(
const bool &,
const bool &)> &,
897 template std::vector<bool>
898 reduce(
const std::vector<bool> &,
900 const std::function<std::vector<bool>(
const std::vector<bool> &,
901 const std::vector<bool> &)> &,
907 const std::function<
bool(
const bool &,
const bool &)> &);
909 template std::vector<bool>
911 const std::vector<bool> &,
913 const std::function<std::vector<bool>(
const std::vector<bool> &,
914 const std::vector<bool> &)> &);
919 internal::all_reduce<bool>(
const MPI_Op &,
935 template std::vector<unsigned int>
940 template std::set<unsigned int>
value_type * data() const noexcept
size_type n_elements() const
void add_range(const size_type begin, const size_type end)
static void unregister_request(MPI_Request &request)
static void register_request(MPI_Request &request)
void lock(const MPI_Comm comm)
void unlock(const MPI_Comm comm)
virtual std::vector< unsigned int > run(const std::vector< unsigned int > &targets, const std::function< RequestType(const unsigned int)> &create_request, const std::function< AnswerType(const unsigned int, const RequestType &)> &answer_request, const std::function< void(const unsigned int, const AnswerType &)> &process_answer, const MPI_Comm comm) override
MPI_InitFinalize(int &argc, char **&argv, const unsigned int max_num_threads=numbers::invalid_unsigned_int)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertThrowMPI(error_code)
#define AssertNothrow(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
IndexSet complete_index_set(const IndexSet::size_type N)
std::vector< unsigned int > nbx(const std::vector< unsigned int > &targets, const std::function< RequestType(const unsigned int)> &create_request, const std::function< AnswerType(const unsigned int, const RequestType &)> &answer_request, const std::function< void(const unsigned int, const AnswerType &)> &process_answer, const MPI_Comm comm)
int Type_contiguous_c(MPI_Count count, MPI_Datatype oldtype, MPI_Datatype *newtype)
std::vector< IndexSet > create_ascending_partitioning(const MPI_Comm comm, const types::global_dof_index locally_owned_size)
T logical_or(const T &t, const MPI_Comm mpi_communicator)
std::unique_ptr< MPI_Datatype, void(*)(MPI_Datatype *)> create_mpi_data_type_n_bytes(const std::size_t n_bytes)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
T max(const T &t, const MPI_Comm mpi_communicator)
T min(const T &t, const MPI_Comm mpi_communicator)
std::vector< T > all_gather(const MPI_Comm comm, const T &object_to_send)
std::vector< unsigned int > compute_index_owner(const IndexSet &owned_indices, const IndexSet &indices_to_look_up, const MPI_Comm comm)
std::vector< T > compute_set_union(const std::vector< T > &vec, const MPI_Comm comm)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
T all_reduce(const T &local_value, const MPI_Comm comm, const std::function< T(const T &, const T &)> &combiner)
IndexSet create_evenly_distributed_partitioning(const MPI_Comm comm, const types::global_dof_index total_size)
std::vector< unsigned int > compute_point_to_point_communication_pattern(const MPI_Comm mpi_comm, const std::vector< unsigned int > &destinations)
int create_group(const MPI_Comm comm, const MPI_Group &group, const int tag, MPI_Comm *new_comm)
MPI_Comm duplicate_communicator(const MPI_Comm mpi_communicator)
T reduce(const T &local_value, const MPI_Comm comm, const std::function< T(const T &, const T &)> &combiner, const unsigned int root_process=0)
void free_communicator(MPI_Comm mpi_communicator)
std::vector< unsigned int > mpi_processes_within_communicator(const MPI_Comm comm_large, const MPI_Comm comm_small)
unsigned int compute_n_point_to_point_communications(const MPI_Comm mpi_comm, const std::vector< unsigned int > &destinations)
MinMaxAvg min_max_avg(const double my_value, const MPI_Comm mpi_communicator)
IndexSet create_evenly_distributed_partitioning(const unsigned int my_partition_id, const unsigned int n_partitions, const types::global_dof_index total_size)
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
*braid_SplitCommworld & comm
boost::signals2::signal< void()> at_mpi_finalize