37#ifdef DEAL_II_WITH_MPI
57#ifdef DEAL_II_WITH_MPI
58# define DEAL_II_MPI_CONST_CAST(expr) (expr)
68template <
int rank,
int dim,
typename Number>
70template <
int rank,
int dim,
typename Number>
72template <
typename Number>
93 const unsigned int my_partition_id,
94 const unsigned int n_partitions,
114 template <
typename T>
132 std::complex<double>,
133 std::complex<long double>,
162 std::vector<unsigned int>
187 std::vector<unsigned int>
190 const std::vector<unsigned int> &destinations);
214 const std::vector<unsigned int> &destinations);
464 template <
typename T>
472 template <
typename W,
typename G>
473 Future(W &&wait_operation, G &&get_and_cleanup_operation);
599 const
types::global_dof_index total_size);
601#ifdef DEAL_II_WITH_MPI
617 template <
class Iterator,
typename Number =
long double>
618 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
672 std::unique_ptr<MPI_Datatype, void (*)(MPI_Datatype *)>
694 template <
typename T>
707 template <
typename T,
typename U>
720 template <
typename T>
731 template <
int rank,
int dim,
typename Number>
741 template <
int rank,
int dim,
typename Number>
754 template <
typename Number>
779 template <
typename T>
792 template <
typename T,
typename U>
805 template <
typename T>
830 template <
typename T>
843 template <
typename T,
typename U>
856 template <
typename T>
885 template <
typename T>
903 template <
typename T,
typename U>
916 template <
typename T>
1011 std::vector<MinMaxAvg>
1168 template <
typename T>
1169 std::map<unsigned int, T>
1171 const std::map<unsigned int, T> &objects_to_send);
1186 template <
typename T>
1187 [[nodiscard]] std::vector<T>
1205 template <
typename T>
1206 [[nodiscard]] std::vector<T>
1208 const T &object_to_send,
1209 const unsigned int root_process = 0);
1225 template <
typename T>
1228 const std::vector<T> &objects_to_send,
1229 const unsigned int root_process = 0);
1269 template <
typename T>
1272 const T &object_to_send,
1273 const unsigned int root_process = 0);
1291 template <
typename T>
1294 const std::size_t count,
1295 const unsigned int root,
1310 template <
typename T>
1314 const std::function<T(
const T &,
const T &)> &combiner,
1315 const unsigned int root_process = 0);
1329 template <
typename T,
typename = std::enable_if_t<is_mpi_type<T> == true>>
1330 [[nodiscard]] std::pair<T, T>
1343 template <
typename T>
1347 const std::function<T(
const T &,
const T &)> &combiner);
1370 template <
typename T>
1374 const unsigned int target_rank,
1375 const unsigned int mpi_tag = 0);
1394 template <
typename T>
1397 const unsigned int source_rank,
1398 const unsigned int mpi_tag = 0);
1444 std::vector<unsigned int>
1459 std::pair<std::vector<unsigned int>, std::map<unsigned int, IndexSet>>
1471 template <
typename T>
1478 template <
typename T>
1493 namespace MPIDataTypes
1495#ifdef DEAL_II_WITH_MPI
1499 return MPI_CXX_BOOL;
1515 return MPI_SIGNED_CHAR;
1555 return MPI_LONG_LONG;
1563 return MPI_UNSIGNED_CHAR;
1571 return MPI_UNSIGNED_SHORT;
1579 return MPI_UNSIGNED;
1587 return MPI_UNSIGNED_LONG;
1595 return MPI_UNSIGNED_LONG_LONG;
1619 return MPI_LONG_DOUBLE;
1635 return MPI_DOUBLE_COMPLEX;
1643#ifdef DEAL_II_WITH_MPI
1661 template <
typename T>
1664 static_cast<std::remove_cv_t<std::remove_reference_t<T>
> *>(
nullptr));
1671 template <
typename T>
1680 template <
typename T>
1681 template <
typename W,
typename G>
1691 template <
typename T>
1692 Future<T>::~Future()
1702 if ((get_was_called ==
false) && get_and_cleanup_function)
1708 template <
typename T>
1712 if (is_done ==
false)
1721 template <
typename T>
1725 Assert(get_was_called ==
false,
1727 "You can't call get() more than once on a Future object."));
1728 get_was_called =
true;
1731 return get_and_cleanup_function();
1736 template <
typename T,
unsigned int N>
1738 sum(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&sums)[N])
1740 internal::all_reduce(MPI_SUM,
1748 template <
typename T,
unsigned int N>
1750 max(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&maxima)[N])
1752 internal::all_reduce(MPI_MAX,
1760 template <
typename T,
unsigned int N>
1762 min(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&minima)[N])
1764 internal::all_reduce(MPI_MIN,
1772 template <
typename T,
unsigned int N>
1778 static_assert(std::is_integral_v<T>,
1779 "The MPI_LOR operation only allows integral data types.");
1781 internal::all_reduce(MPI_LOR,
1789 template <
typename T>
1790 std::map<unsigned int, T>
1792 const std::map<unsigned int, T> &objects_to_send)
1794# ifndef DEAL_II_WITH_MPI
1796 Assert(objects_to_send.size() < 2,
1797 ExcMessage(
"Cannot send to more than one processor."));
1798 Assert(objects_to_send.find(0) != objects_to_send.end() ||
1799 objects_to_send.empty(),
1800 ExcMessage(
"Can only send to myself or to nobody."));
1801 return objects_to_send;
1805 std::map<unsigned int, T> received_objects;
1807 std::vector<unsigned int> send_to;
1808 send_to.reserve(objects_to_send.size());
1809 for (
const auto &m : objects_to_send)
1810 if (m.
first == my_proc)
1811 received_objects[my_proc] = m.
second;
1813 send_to.emplace_back(m.first);
1815 const unsigned int n_expected_incoming_messages =
1819 static CollectiveMutex mutex;
1820 CollectiveMutex::ScopedLock lock(mutex,
comm);
1826 if (send_to.empty() && n_expected_incoming_messages == 0)
1827 return received_objects;
1830 internal::Tags::compute_point_to_point_communication_pattern;
1833 std::vector<std::vector<char>> buffers_to_send(send_to.size());
1834 std::vector<MPI_Request> buffer_send_requests(send_to.size());
1837 for (
const auto &rank_obj : objects_to_send)
1838 if (rank_obj.
first != my_proc)
1840 const auto &rank = rank_obj.first;
1843 const int ierr = MPI_Isend(buffers_to_send[i].
data(),
1844 buffers_to_send[i].
size(),
1849 &buffer_send_requests[i]);
1857 std::vector<char> buffer;
1859 for (
unsigned int i = 0; i < n_expected_incoming_messages; ++i)
1863 int ierr = MPI_Probe(MPI_ANY_SOURCE, mpi_tag,
comm, &status);
1868 ierr = MPI_Get_count(&status, MPI_CHAR, &len);
1873 const unsigned int rank = status.MPI_SOURCE;
1876 ierr = MPI_Recv(buffer.data(),
1884 Assert(received_objects.find(rank) == received_objects.end(),
1886 "I should not receive again from this rank"));
1887 received_objects[rank] =
1888 Utilities::unpack<T>(buffer,
1894 const int ierr = MPI_Waitall(send_to.size(),
1895 buffer_send_requests.data(),
1896 MPI_STATUSES_IGNORE);
1899 return received_objects;
1905 template <
typename T,
typename>
1909# ifndef DEAL_II_WITH_MPI
1936 return {prefix,
sum};
1943 template <
typename T>
1950# ifndef DEAL_II_WITH_MPI
1952 std::vector<T> v(1,
object);
1959 int n_local_data = buffer.size();
1962 std::vector<int> size_all_data(
n_procs, 0);
1965 int ierr = MPI_Allgather(
1966 &n_local_data, 1, MPI_INT, size_all_data.data(), 1, MPI_INT,
comm);
1971 std::vector<int> rdispls(
n_procs);
1973 for (
unsigned int i = 1; i <
n_procs; ++i)
1974 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
1977 std::vector<char> received_unrolled_buffer(rdispls.back() +
1978 size_all_data.back());
1980 ierr = MPI_Allgatherv(buffer.data(),
1983 received_unrolled_buffer.data(),
1984 size_all_data.data(),
1990 std::vector<T> received_objects(
n_procs);
1991 for (
unsigned int i = 0; i <
n_procs; ++i)
1993 std::vector<char> local_buffer(received_unrolled_buffer.begin() +
1995 received_unrolled_buffer.begin() +
1996 rdispls[i] + size_all_data[i]);
1997 received_objects[i] = Utilities::unpack<T>(local_buffer);
2000 return received_objects;
2006 template <
typename T>
2009 const T &object_to_send,
2010 const unsigned int root_process)
2012# ifndef DEAL_II_WITH_MPI
2015 std::vector<T> v(1, object_to_send);
2024 int n_local_data = buffer.size();
2028 std::vector<int> size_all_data;
2030 size_all_data.resize(
n_procs, 0);
2033 int ierr = MPI_Gather(&n_local_data,
2036 size_all_data.data(),
2045 std::vector<int> rdispls;
2049 for (
unsigned int i = 1; i <
n_procs; ++i)
2050 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
2053 std::vector<char> received_unrolled_buffer;
2055 received_unrolled_buffer.resize(rdispls.back() + size_all_data.back());
2057 ierr = MPI_Gatherv(buffer.data(),
2060 received_unrolled_buffer.data(),
2061 size_all_data.data(),
2068 std::vector<T> received_objects;
2072 received_objects.resize(
n_procs);
2074 for (
unsigned int i = 0; i <
n_procs; ++i)
2076 const std::vector<char> local_buffer(
2077 received_unrolled_buffer.begin() + rdispls[i],
2078 received_unrolled_buffer.begin() + rdispls[i] +
2080 received_objects[i] = Utilities::unpack<T>(local_buffer);
2083 return received_objects;
2089 template <
typename T>
2092 const std::vector<T> &objects_to_send,
2093 const unsigned int root_process)
2095# ifndef DEAL_II_WITH_MPI
2101 return objects_to_send[0];
2108 (
my_rank != root_process && objects_to_send.empty()) ||
2109 objects_to_send.size() ==
n_procs,
2111 "The number of objects to be scattered must correspond to the number processes."));
2113 std::vector<char> send_buffer;
2114 std::vector<int> send_counts;
2115 std::vector<int> send_displacements;
2119 send_counts.resize(
n_procs, 0);
2120 send_displacements.resize(
n_procs + 1, 0);
2122 for (
unsigned int i = 0; i <
n_procs; ++i)
2125 send_buffer.insert(send_buffer.end(),
2126 packed_data.begin(),
2128 send_counts[i] = packed_data.size();
2131 for (
unsigned int i = 0; i <
n_procs; ++i)
2132 send_displacements[i + 1] = send_displacements[i] + send_counts[i];
2136 int ierr = MPI_Scatter(send_counts.data(),
2146 std::vector<char> recv_buffer(n_local_data);
2148 ierr = MPI_Scatterv(send_buffer.data(),
2150 send_displacements.data(),
2159 return Utilities::unpack<T>(recv_buffer);
2164 template <
typename T>
2167 const std::size_t count,
2168 const unsigned int root,
2171# ifndef DEAL_II_WITH_MPI
2182 const size_t max_send_count = std::numeric_limits<signed int>::max();
2184 size_t total_sent_count = 0;
2185 while (total_sent_count < count)
2187 const size_t current_count =
2188 std::min(count - total_sent_count, max_send_count);
2190 const int ierr = MPI_Bcast(buffer + total_sent_count,
2192 mpi_type_id_for_type<
decltype(*buffer)>,
2196 total_sent_count += current_count;
2203 template <
typename T>
2206 const T &object_to_send,
2207 const unsigned int root_process)
2209# ifndef DEAL_II_WITH_MPI
2212 return object_to_send;
2218 if constexpr (is_mpi_type<T>)
2220 T object = object_to_send;
2222 MPI_Bcast(&
object, 1, mpi_type_id_for_type<T>, root_process,
comm);
2229 std::vector<char> buffer;
2237 buffer_size = buffer.size();
2241 int ierr = MPI_Bcast(&buffer_size,
2243 mpi_type_id_for_type<
decltype(buffer_size)>,
2251 buffer.resize(buffer_size);
2256 return object_to_send;
2258 return Utilities::unpack<T>(buffer,
false);
2265 template <
typename T>
2267 isend(
const T &
object,
2269 const unsigned int target_rank,
2270 const unsigned int mpi_tag)
2272# ifndef DEAL_II_WITH_MPI
2278 return Future<void>([]() {}, []() {});
2293 std::shared_ptr<std::vector<char>> send_buffer =
2298 MPI_Request request;
2300 MPI_Isend(send_buffer->data(),
2301 send_buffer->size(),
2302 mpi_type_id_for_type<
decltype(*send_buffer->data())>,
2322 auto wait = [request]()
mutable {
2323 const int ierr = MPI_Wait(&request, MPI_STATUS_IGNORE);
2326 auto cleanup = [send_buffer = std::move(send_buffer)]() {
2327 send_buffer->clear();
2329 return Future<void>(wait, cleanup);
2335 template <
typename T>
2338 const unsigned int source_rank,
2339 const unsigned int mpi_tag)
2341# ifndef DEAL_II_WITH_MPI
2346 return Future<void>([]() {}, []() {
return T{}; });
2362 std::shared_ptr<MPI_Message> message = std::make_shared<MPI_Message>();
2363 std::shared_ptr<MPI_Status> status = std::make_shared<MPI_Status>();
2365 auto wait = [source_rank, mpi_tag, communicator, message, status]() {
2366 const int ierr = MPI_Mprobe(
2367 source_rank, mpi_tag, communicator, message.get(), status.get());
2373 auto get = [status, message]() {
2376 ierr = MPI_Get_count(status.get(), MPI_CHAR, &number_amount);
2379 std::vector<char> receive_buffer(number_amount);
2383 ierr = MPI_Mrecv(receive_buffer.data(),
2385 mpi_type_id_for_type<
decltype(*receive_buffer.data())>,
2391 return Utilities::unpack<T>(receive_buffer,
false);
2394 return Future<T>(wait, get);
2400# ifdef DEAL_II_WITH_MPI
2401 template <
class Iterator,
typename Number>
2402 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
2413 const Number
sum = std::accumulate(begin, end, Number(0.));
2420 std::for_each(begin, end, [&mean, &sq_sum](
const Number &v) {
2424 return std::make_pair(mean,
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_NAMESPACE_OPEN
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
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)
const unsigned int my_rank
const IndexSet & indices_to_look_up
std::vector< index_type > data
const IndexSet & owned_indices
types::global_dof_index locally_owned_size
const unsigned int n_procs
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::pair< std::vector< unsigned int >, std::map< unsigned int, IndexSet > > compute_index_owner_and_requesters(const IndexSet &owned_indices, const IndexSet &indices_to_look_up, const MPI_Comm &comm)
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)
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)
T broadcast(const MPI_Comm comm, const T &object_to_send, const unsigned int root_process=0)
std::size_t pack(const T &object, std::vector< char > &dest_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)
constexpr types::global_dof_index invalid_size_type
constexpr unsigned int invalid_unsigned_int
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
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)