49#ifdef DEAL_II_WITH_MPI
50# define DEAL_II_MPI_CONST_CAST(expr) (expr)
60template <
int rank,
int dim,
typename Number>
62template <
int rank,
int dim,
typename Number>
64template <
typename Number>
85 const unsigned int my_partition_id,
86 const unsigned int n_partitions,
106 template <
typename T>
124 std::complex<double>,
125 std::complex<long double>,
154 std::vector<unsigned int>
179 std::vector<unsigned int>
182 const std::vector<unsigned int> &destinations);
206 const std::vector<unsigned int> &destinations);
456 template <
typename T>
464 template <
typename W,
typename G>
465 Future(W &&wait_operation, G &&get_and_cleanup_operation);
571#ifdef DEAL_II_WITH_MPI
574 const MPI_Group &group,
587 std::vector<IndexSet>
604#ifdef DEAL_II_WITH_MPI
620 template <
class Iterator,
typename Number =
long double>
621 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
675 std::unique_ptr<MPI_Datatype, void (*)(MPI_Datatype *)>
697 template <
typename T>
710 template <
typename T,
typename U>
723 template <
typename T>
734 template <
int rank,
int dim,
typename Number>
744 template <
int rank,
int dim,
typename Number>
757 template <
typename Number>
782 template <
typename T>
795 template <
typename T,
typename U>
808 template <
typename T>
833 template <
typename T>
846 template <
typename T,
typename U>
859 template <
typename T>
888 template <
typename T>
906 template <
typename T,
typename U>
919 template <
typename T>
1014 std::vector<MinMaxAvg>
1171 template <
typename T>
1172 std::map<unsigned int, T>
1174 const std::map<unsigned int, T> &objects_to_send);
1189 template <
typename T>
1208 template <
typename T>
1211 const T &object_to_send,
1212 const unsigned int root_process = 0);
1228 template <
typename T>
1231 const std::vector<T> &objects_to_send,
1232 const unsigned int root_process = 0);
1269 template <
typename T>
1270 std::enable_if_t<is_mpi_type<T> ==
false, T>
1272 const T &object_to_send,
1273 const unsigned int root_process = 0);
1297 template <
typename T>
1298 std::enable_if_t<is_mpi_type<T> ==
true, T>
1300 const T &object_to_send,
1301 const unsigned int root_process = 0);
1319 template <
typename T>
1323 const unsigned int root,
1338 template <
typename T>
1342 const std::function<T(
const T &,
const T &)> &combiner,
1343 const unsigned int root_process = 0);
1357 template <
typename T,
typename = std::enable_if_t<is_mpi_type<T> == true>>
1371 template <
typename T>
1375 const std::function<T(
const T &,
const T &)> &combiner);
1398 template <
typename T>
1402 const unsigned int target_rank,
1403 const unsigned int mpi_tag = 0);
1422 template <
typename T>
1425 const unsigned int source_rank,
1426 const unsigned int mpi_tag = 0);
1471 std::vector<unsigned int>
1473 const IndexSet &indices_to_look_up,
1483 template <
typename T>
1490 template <
typename T>
1505 namespace MPIDataTypes
1507#ifdef DEAL_II_WITH_MPI
1511 return MPI_CXX_BOOL;
1527 return MPI_SIGNED_CHAR;
1567 return MPI_LONG_LONG;
1575 return MPI_UNSIGNED_CHAR;
1583 return MPI_UNSIGNED_SHORT;
1591 return MPI_UNSIGNED;
1599 return MPI_UNSIGNED_LONG;
1607 return MPI_UNSIGNED_LONG_LONG;
1631 return MPI_LONG_DOUBLE;
1647 return MPI_DOUBLE_COMPLEX;
1655#ifdef DEAL_II_WITH_MPI
1673 template <
typename T>
1676 static_cast<std::remove_cv_t<std::remove_reference_t<T>
> *>(
nullptr));
1683 template <
typename T>
1692 template <
typename T>
1693 template <
typename W,
typename G>
1703 template <
typename T>
1704 Future<T>::~Future()
1714 if ((get_was_called ==
false) && get_and_cleanup_function)
1720 template <
typename T>
1724 if (is_done ==
false)
1733 template <
typename T>
1737 Assert(get_was_called ==
false,
1739 "You can't call get() more than once on a Future object."));
1740 get_was_called =
true;
1743 return get_and_cleanup_function();
1748 template <
typename T,
unsigned int N>
1750 sum(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&sums)[N])
1752 internal::all_reduce(MPI_SUM,
1760 template <
typename T,
unsigned int N>
1762 max(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&maxima)[N])
1764 internal::all_reduce(MPI_MAX,
1772 template <
typename T,
unsigned int N>
1774 min(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&minima)[N])
1776 internal::all_reduce(MPI_MIN,
1784 template <
typename T,
unsigned int N>
1790 static_assert(std::is_integral_v<T>,
1791 "The MPI_LOR operation only allows integral data types.");
1793 internal::all_reduce(MPI_LOR,
1801 template <
typename T>
1802 std::map<unsigned int, T>
1804 const std::map<unsigned int, T> &objects_to_send)
1806# ifndef DEAL_II_WITH_MPI
1808 Assert(objects_to_send.size() < 2,
1809 ExcMessage(
"Cannot send to more than one processor."));
1810 Assert(objects_to_send.find(0) != objects_to_send.end() ||
1811 objects_to_send.empty(),
1812 ExcMessage(
"Can only send to myself or to nobody."));
1813 return objects_to_send;
1817 std::map<unsigned int, T> received_objects;
1819 std::vector<unsigned int> send_to;
1820 send_to.reserve(objects_to_send.size());
1821 for (
const auto &m : objects_to_send)
1822 if (m.
first == my_proc)
1823 received_objects[my_proc] = m.
second;
1825 send_to.emplace_back(m.first);
1827 const unsigned int n_expected_incoming_messages =
1831 static CollectiveMutex mutex;
1832 CollectiveMutex::ScopedLock lock(mutex,
comm);
1838 if (send_to.empty() && n_expected_incoming_messages == 0)
1839 return received_objects;
1842 internal::Tags::compute_point_to_point_communication_pattern;
1845 std::vector<std::vector<char>> buffers_to_send(send_to.size());
1846 std::vector<MPI_Request> buffer_send_requests(send_to.size());
1849 for (
const auto &rank_obj : objects_to_send)
1850 if (rank_obj.
first != my_proc)
1852 const auto &rank = rank_obj.first;
1855 const int ierr = MPI_Isend(buffers_to_send[i].data(),
1856 buffers_to_send[i].size(),
1861 &buffer_send_requests[i]);
1869 std::vector<char> buffer;
1871 for (
unsigned int i = 0; i < n_expected_incoming_messages; ++i)
1875 int ierr = MPI_Probe(MPI_ANY_SOURCE, mpi_tag,
comm, &status);
1880 ierr = MPI_Get_count(&status, MPI_CHAR, &len);
1885 const unsigned int rank = status.MPI_SOURCE;
1888 ierr = MPI_Recv(buffer.data(),
1896 Assert(received_objects.find(rank) == received_objects.end(),
1898 "I should not receive again from this rank"));
1899 received_objects[rank] =
1906 const int ierr = MPI_Waitall(send_to.size(),
1907 buffer_send_requests.data(),
1908 MPI_STATUSES_IGNORE);
1911 return received_objects;
1917 template <
typename T,
typename>
1921# ifndef DEAL_II_WITH_MPI
1948 return {prefix,
sum};
1955 template <
typename T>
1962# ifndef DEAL_II_WITH_MPI
1964 std::vector<T> v(1,
object);
1971 int n_local_data = buffer.size();
1974 std::vector<int> size_all_data(n_procs, 0);
1977 int ierr = MPI_Allgather(
1978 &n_local_data, 1, MPI_INT, size_all_data.data(), 1, MPI_INT,
comm);
1983 std::vector<int> rdispls(n_procs);
1985 for (
unsigned int i = 1; i < n_procs; ++i)
1986 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
1989 std::vector<char> received_unrolled_buffer(rdispls.back() +
1990 size_all_data.back());
1992 ierr = MPI_Allgatherv(buffer.data(),
1995 received_unrolled_buffer.data(),
1996 size_all_data.data(),
2002 std::vector<T> received_objects(n_procs);
2003 for (
unsigned int i = 0; i < n_procs; ++i)
2005 std::vector<char> local_buffer(received_unrolled_buffer.begin() +
2007 received_unrolled_buffer.begin() +
2008 rdispls[i] + size_all_data[i]);
2012 return received_objects;
2018 template <
typename T>
2021 const T &object_to_send,
2022 const unsigned int root_process)
2024# ifndef DEAL_II_WITH_MPI
2027 std::vector<T> v(1, object_to_send);
2036 int n_local_data = buffer.size();
2040 std::vector<int> size_all_data;
2041 if (my_rank == root_process)
2042 size_all_data.resize(n_procs, 0);
2045 int ierr = MPI_Gather(&n_local_data,
2048 size_all_data.data(),
2057 std::vector<int> rdispls;
2058 if (my_rank == root_process)
2060 rdispls.resize(n_procs, 0);
2061 for (
unsigned int i = 1; i < n_procs; ++i)
2062 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
2065 std::vector<char> received_unrolled_buffer;
2066 if (my_rank == root_process)
2067 received_unrolled_buffer.resize(rdispls.back() + size_all_data.back());
2069 ierr = MPI_Gatherv(buffer.data(),
2072 received_unrolled_buffer.data(),
2073 size_all_data.data(),
2080 std::vector<T> received_objects;
2082 if (my_rank == root_process)
2084 received_objects.resize(n_procs);
2086 for (
unsigned int i = 0; i < n_procs; ++i)
2088 const std::vector<char> local_buffer(
2089 received_unrolled_buffer.begin() + rdispls[i],
2090 received_unrolled_buffer.begin() + rdispls[i] +
2095 return received_objects;
2101 template <
typename T>
2104 const std::vector<T> &objects_to_send,
2105 const unsigned int root_process)
2107# ifndef DEAL_II_WITH_MPI
2113 return objects_to_send[0];
2120 (my_rank != root_process && objects_to_send.empty()) ||
2121 objects_to_send.size() == n_procs,
2123 "The number of objects to be scattered must correspond to the number processes."));
2125 std::vector<char> send_buffer;
2126 std::vector<int> send_counts;
2127 std::vector<int> send_displacements;
2129 if (my_rank == root_process)
2131 send_counts.resize(n_procs, 0);
2132 send_displacements.resize(n_procs + 1, 0);
2134 for (
unsigned int i = 0; i < n_procs; ++i)
2137 send_buffer.insert(send_buffer.end(),
2138 packed_data.begin(),
2140 send_counts[i] = packed_data.size();
2143 for (
unsigned int i = 0; i < n_procs; ++i)
2144 send_displacements[i + 1] = send_displacements[i] + send_counts[i];
2148 int ierr = MPI_Scatter(send_counts.data(),
2158 std::vector<char> recv_buffer(n_local_data);
2160 ierr = MPI_Scatterv(send_buffer.data(),
2162 send_displacements.data(),
2176 template <
typename T>
2180 const unsigned int root,
2183# ifndef DEAL_II_WITH_MPI
2194 const size_t max_send_count = std::numeric_limits<signed int>::max();
2196 size_t total_sent_count = 0;
2197 while (total_sent_count < count)
2199 const size_t current_count =
2200 std::min(count - total_sent_count, max_send_count);
2202 const int ierr = MPI_Bcast(buffer + total_sent_count,
2204 mpi_type_id_for_type<
decltype(*buffer)>,
2208 total_sent_count += current_count;
2215 template <
typename T>
2216 std::enable_if_t<is_mpi_type<T> ==
false, T>
2218 const T &object_to_send,
2219 const unsigned int root_process)
2221# ifndef DEAL_II_WITH_MPI
2224 return object_to_send;
2230 std::vector<char> buffer;
2238 buffer_size = buffer.size();
2242 int ierr = MPI_Bcast(&buffer_size,
2244 mpi_type_id_for_type<
decltype(buffer_size)>,
2252 buffer.resize(buffer_size);
2257 return object_to_send;
2265 template <
typename T>
2266 std::enable_if_t<is_mpi_type<T> ==
true, T>
2268 const T &object_to_send,
2269 const unsigned int root_process)
2271# ifndef DEAL_II_WITH_MPI
2274 return object_to_send;
2277 T
object = object_to_send;
2279 MPI_Bcast(&
object, 1, mpi_type_id_for_type<T>, root_process,
comm);
2287 template <
typename T>
2289 isend(
const T &
object,
2291 const unsigned int target_rank,
2292 const unsigned int mpi_tag)
2294# ifndef DEAL_II_WITH_MPI
2300 return Future<void>([]() {}, []() {});
2315 std::shared_ptr<std::vector<char>> send_buffer =
2320 MPI_Request request;
2322 MPI_Isend(send_buffer->data(),
2323 send_buffer->size(),
2324 mpi_type_id_for_type<
decltype(*send_buffer->data())>,
2344 auto wait = [request]()
mutable {
2345 const int ierr = MPI_Wait(&request, MPI_STATUS_IGNORE);
2348 auto cleanup = [send_buffer = std::move(send_buffer)]() {
2349 send_buffer->clear();
2351 return Future<void>(wait, cleanup);
2357 template <
typename T>
2360 const unsigned int source_rank,
2361 const unsigned int mpi_tag)
2363# ifndef DEAL_II_WITH_MPI
2368 return Future<void>([]() {}, []() {
return T{}; });
2384 std::shared_ptr<MPI_Message> message = std::make_shared<MPI_Message>();
2385 std::shared_ptr<MPI_Status> status = std::make_shared<MPI_Status>();
2387 auto wait = [source_rank, mpi_tag, communicator, message, status]() {
2388 const int ierr = MPI_Mprobe(
2389 source_rank, mpi_tag, communicator, message.get(), status.get());
2395 auto get = [status, message]() {
2398 ierr = MPI_Get_count(status.get(), MPI_CHAR, &number_amount);
2401 std::vector<char> receive_buffer(number_amount);
2405 ierr = MPI_Mrecv(receive_buffer.data(),
2407 mpi_type_id_for_type<
decltype(*receive_buffer.data())>,
2416 return Future<T>(wait, get);
2422# ifdef DEAL_II_WITH_MPI
2423 template <
class Iterator,
typename Number>
2424 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
2435 const Number
sum = std::accumulate(begin, end, Number(0.));
2442 std::for_each(begin, end, [&mean, &sq_sum](
const Number &v) {
2446 return std::make_pair(mean,
2447 std::sqrt(sq_sum /
static_cast<Std
>(size - 1)));
void sum(const SparseMatrix< Number > &local, const MPI_Comm mpi_communicator, SparseMatrix< Number > &global)
SymmetricTensor< rank, dim, Number > sum(const SymmetricTensor< rank, dim, Number > &local, const MPI_Comm mpi_communicator)
Tensor< rank, dim, Number > sum(const Tensor< rank, dim, Number > &local, const MPI_Comm mpi_communicator)
ScopedLock(CollectiveMutex &mutex, const MPI_Comm comm)
void lock(const MPI_Comm comm)
void unlock(const MPI_Comm comm)
DuplicatedCommunicator(const DuplicatedCommunicator &)=delete
~DuplicatedCommunicator()
MPI_Comm operator*() const
DuplicatedCommunicator & operator=(const DuplicatedCommunicator &)=delete
DuplicatedCommunicator(const MPI_Comm communicator)
Future(const Future &)=delete
Future(Future &&) noexcept=default
std::function< void()> wait_function
std::function< T()> get_and_cleanup_function
Future(W &&wait_operation, G &&get_and_cleanup_operation)
~MPI_InitFinalize()=default
#define DEAL_II_DEPRECATED
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNeedsMPI()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertThrowMPI(error_code)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDivideByZero()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
MPI_Datatype mpi_type_id(const bool *)
std::vector< IndexSet > create_ascending_partitioning(const MPI_Comm comm, const types::global_dof_index locally_owned_size)
std::enable_if_t< is_mpi_type< T >==false, T > broadcast(const MPI_Comm comm, const T &object_to_send, const unsigned int root_process=0)
std::pair< T, T > partial_and_total_sum(const T &value, const MPI_Comm comm)
T sum(const T &t, const MPI_Comm mpi_communicator)
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)
Future< T > irecv(MPI_Comm communicator, const unsigned int source_rank, const unsigned int mpi_tag=0)
std::map< unsigned int, T > some_to_some(const MPI_Comm comm, const std::map< unsigned int, T > &objects_to_send)
std::pair< Number, typename numbers::NumberTraits< Number >::real_type > mean_and_standard_deviation(const Iterator begin, const Iterator end, const MPI_Comm comm)
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)
const MPI_Datatype mpi_type_id_for_type
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)
std::vector< T > gather(const MPI_Comm comm, const T &object_to_send, const unsigned int root_process=0)
MinMaxAvg min_max_avg(const double my_value, const MPI_Comm mpi_communicator)
constexpr bool is_mpi_type
T scatter(const MPI_Comm comm, const std::vector< T > &objects_to_send, const unsigned int root_process=0)
Future< void > isend(const T &object, MPI_Comm communicator, const unsigned int target_rank, const unsigned int mpi_tag=0)
size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
T unpack(const std::vector< char > &buffer, const bool allow_compression=true)
IndexSet create_evenly_distributed_partitioning(const unsigned int my_partition_id, const unsigned int n_partitions, const types::global_dof_index total_size)
static const unsigned int invalid_unsigned_int
const types::global_dof_index invalid_size_type
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
*braid_SplitCommworld & comm
static constexpr real_type abs_square(const number &x)
void gather(VectorizedArray< Number, width > &out, const std::array< Number *, width > &ptrs, const unsigned int offset)