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);
563 const
types::global_dof_index total_size);
565#ifdef DEAL_II_WITH_MPI
581 template <
class Iterator,
typename Number =
long double>
582 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
636 std::unique_ptr<MPI_Datatype, void (*)(MPI_Datatype *)>
658 template <
typename T>
671 template <
typename T,
typename U>
684 template <
typename T>
695 template <
int rank,
int dim,
typename Number>
705 template <
int rank,
int dim,
typename Number>
718 template <
typename Number>
743 template <
typename T>
756 template <
typename T,
typename U>
769 template <
typename T>
794 template <
typename T>
807 template <
typename T,
typename U>
820 template <
typename T>
849 template <
typename T>
867 template <
typename T,
typename U>
880 template <
typename T>
975 std::vector<MinMaxAvg>
1132 template <
typename T>
1133 std::map<unsigned int, T>
1135 const std::map<unsigned int, T> &objects_to_send);
1150 template <
typename T>
1169 template <
typename T>
1172 const T &object_to_send,
1173 const unsigned int root_process = 0);
1189 template <
typename T>
1192 const std::vector<T> &objects_to_send,
1193 const unsigned int root_process = 0);
1233 template <
typename T>
1236 const T &object_to_send,
1237 const unsigned int root_process = 0);
1255 template <
typename T>
1259 const unsigned int root,
1274 template <
typename T>
1278 const std::function<T(
const T &,
const T &)> &combiner,
1279 const unsigned int root_process = 0);
1293 template <
typename T,
typename = std::enable_if_t<is_mpi_type<T> == true>>
1307 template <
typename T>
1311 const std::function<T(
const T &,
const T &)> &combiner);
1334 template <
typename T>
1338 const unsigned int target_rank,
1339 const unsigned int mpi_tag = 0);
1358 template <
typename T>
1361 const unsigned int source_rank,
1362 const unsigned int mpi_tag = 0);
1408 std::vector<unsigned int>
1423 std::pair<std::vector<unsigned int>, std::map<unsigned int, IndexSet>>
1435 template <
typename T>
1442 template <
typename T>
1457 namespace MPIDataTypes
1459#ifdef DEAL_II_WITH_MPI
1463 return MPI_CXX_BOOL;
1479 return MPI_SIGNED_CHAR;
1519 return MPI_LONG_LONG;
1527 return MPI_UNSIGNED_CHAR;
1535 return MPI_UNSIGNED_SHORT;
1543 return MPI_UNSIGNED;
1551 return MPI_UNSIGNED_LONG;
1559 return MPI_UNSIGNED_LONG_LONG;
1583 return MPI_LONG_DOUBLE;
1599 return MPI_DOUBLE_COMPLEX;
1607#ifdef DEAL_II_WITH_MPI
1625 template <
typename T>
1628 static_cast<std::remove_cv_t<std::remove_reference_t<T>
> *>(
nullptr));
1635 template <
typename T>
1644 template <
typename T>
1645 template <
typename W,
typename G>
1655 template <
typename T>
1656 Future<T>::~Future()
1666 if ((get_was_called ==
false) && get_and_cleanup_function)
1672 template <
typename T>
1676 if (is_done ==
false)
1685 template <
typename T>
1689 Assert(get_was_called ==
false,
1691 "You can't call get() more than once on a Future object."));
1692 get_was_called =
true;
1695 return get_and_cleanup_function();
1700 template <
typename T,
unsigned int N>
1702 sum(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&sums)[N])
1704 internal::all_reduce(MPI_SUM,
1712 template <
typename T,
unsigned int N>
1714 max(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&maxima)[N])
1716 internal::all_reduce(MPI_MAX,
1724 template <
typename T,
unsigned int N>
1726 min(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&minima)[N])
1728 internal::all_reduce(MPI_MIN,
1736 template <
typename T,
unsigned int N>
1742 static_assert(std::is_integral_v<T>,
1743 "The MPI_LOR operation only allows integral data types.");
1745 internal::all_reduce(MPI_LOR,
1753 template <
typename T>
1754 std::map<unsigned int, T>
1756 const std::map<unsigned int, T> &objects_to_send)
1758# ifndef DEAL_II_WITH_MPI
1760 Assert(objects_to_send.size() < 2,
1761 ExcMessage(
"Cannot send to more than one processor."));
1762 Assert(objects_to_send.find(0) != objects_to_send.end() ||
1763 objects_to_send.empty(),
1764 ExcMessage(
"Can only send to myself or to nobody."));
1765 return objects_to_send;
1769 std::map<unsigned int, T> received_objects;
1771 std::vector<unsigned int> send_to;
1772 send_to.reserve(objects_to_send.size());
1773 for (
const auto &m : objects_to_send)
1774 if (m.
first == my_proc)
1775 received_objects[my_proc] = m.
second;
1777 send_to.emplace_back(m.first);
1779 const unsigned int n_expected_incoming_messages =
1783 static CollectiveMutex mutex;
1784 CollectiveMutex::ScopedLock lock(mutex,
comm);
1790 if (send_to.empty() && n_expected_incoming_messages == 0)
1791 return received_objects;
1794 internal::Tags::compute_point_to_point_communication_pattern;
1797 std::vector<std::vector<char>> buffers_to_send(send_to.size());
1798 std::vector<MPI_Request> buffer_send_requests(send_to.size());
1801 for (
const auto &rank_obj : objects_to_send)
1802 if (rank_obj.
first != my_proc)
1804 const auto &rank = rank_obj.first;
1807 const int ierr = MPI_Isend(buffers_to_send[i].
data(),
1808 buffers_to_send[i].
size(),
1813 &buffer_send_requests[i]);
1821 std::vector<char> buffer;
1823 for (
unsigned int i = 0; i < n_expected_incoming_messages; ++i)
1827 int ierr = MPI_Probe(MPI_ANY_SOURCE, mpi_tag,
comm, &status);
1832 ierr = MPI_Get_count(&status, MPI_CHAR, &len);
1837 const unsigned int rank = status.MPI_SOURCE;
1840 ierr = MPI_Recv(buffer.data(),
1848 Assert(received_objects.find(rank) == received_objects.end(),
1850 "I should not receive again from this rank"));
1851 received_objects[rank] =
1852 Utilities::unpack<T>(buffer,
1858 const int ierr = MPI_Waitall(send_to.size(),
1859 buffer_send_requests.data(),
1860 MPI_STATUSES_IGNORE);
1863 return received_objects;
1869 template <
typename T,
typename>
1873# ifndef DEAL_II_WITH_MPI
1900 return {prefix,
sum};
1907 template <
typename T>
1914# ifndef DEAL_II_WITH_MPI
1916 std::vector<T> v(1,
object);
1923 int n_local_data = buffer.size();
1926 std::vector<int> size_all_data(
n_procs, 0);
1929 int ierr = MPI_Allgather(
1930 &n_local_data, 1, MPI_INT, size_all_data.data(), 1, MPI_INT,
comm);
1935 std::vector<int> rdispls(
n_procs);
1937 for (
unsigned int i = 1; i <
n_procs; ++i)
1938 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
1941 std::vector<char> received_unrolled_buffer(rdispls.back() +
1942 size_all_data.back());
1944 ierr = MPI_Allgatherv(buffer.data(),
1947 received_unrolled_buffer.data(),
1948 size_all_data.data(),
1954 std::vector<T> received_objects(
n_procs);
1955 for (
unsigned int i = 0; i <
n_procs; ++i)
1957 std::vector<char> local_buffer(received_unrolled_buffer.begin() +
1959 received_unrolled_buffer.begin() +
1960 rdispls[i] + size_all_data[i]);
1961 received_objects[i] = Utilities::unpack<T>(local_buffer);
1964 return received_objects;
1970 template <
typename T>
1973 const T &object_to_send,
1974 const unsigned int root_process)
1976# ifndef DEAL_II_WITH_MPI
1979 std::vector<T> v(1, object_to_send);
1988 int n_local_data = buffer.size();
1992 std::vector<int> size_all_data;
1994 size_all_data.resize(
n_procs, 0);
1997 int ierr = MPI_Gather(&n_local_data,
2000 size_all_data.data(),
2009 std::vector<int> rdispls;
2013 for (
unsigned int i = 1; i <
n_procs; ++i)
2014 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
2017 std::vector<char> received_unrolled_buffer;
2019 received_unrolled_buffer.resize(rdispls.back() + size_all_data.back());
2021 ierr = MPI_Gatherv(buffer.data(),
2024 received_unrolled_buffer.data(),
2025 size_all_data.data(),
2032 std::vector<T> received_objects;
2036 received_objects.resize(
n_procs);
2038 for (
unsigned int i = 0; i <
n_procs; ++i)
2040 const std::vector<char> local_buffer(
2041 received_unrolled_buffer.begin() + rdispls[i],
2042 received_unrolled_buffer.begin() + rdispls[i] +
2044 received_objects[i] = Utilities::unpack<T>(local_buffer);
2047 return received_objects;
2053 template <
typename T>
2056 const std::vector<T> &objects_to_send,
2057 const unsigned int root_process)
2059# ifndef DEAL_II_WITH_MPI
2065 return objects_to_send[0];
2072 (
my_rank != root_process && objects_to_send.empty()) ||
2073 objects_to_send.size() ==
n_procs,
2075 "The number of objects to be scattered must correspond to the number processes."));
2077 std::vector<char> send_buffer;
2078 std::vector<int> send_counts;
2079 std::vector<int> send_displacements;
2083 send_counts.resize(
n_procs, 0);
2084 send_displacements.resize(
n_procs + 1, 0);
2086 for (
unsigned int i = 0; i <
n_procs; ++i)
2089 send_buffer.insert(send_buffer.end(),
2090 packed_data.begin(),
2092 send_counts[i] = packed_data.size();
2095 for (
unsigned int i = 0; i <
n_procs; ++i)
2096 send_displacements[i + 1] = send_displacements[i] + send_counts[i];
2100 int ierr = MPI_Scatter(send_counts.data(),
2110 std::vector<char> recv_buffer(n_local_data);
2112 ierr = MPI_Scatterv(send_buffer.data(),
2114 send_displacements.data(),
2123 return Utilities::unpack<T>(recv_buffer);
2128 template <
typename T>
2132 const unsigned int root,
2135# ifndef DEAL_II_WITH_MPI
2146 const size_t max_send_count = std::numeric_limits<signed int>::max();
2148 size_t total_sent_count = 0;
2149 while (total_sent_count < count)
2151 const size_t current_count =
2152 std::min(count - total_sent_count, max_send_count);
2154 const int ierr = MPI_Bcast(buffer + total_sent_count,
2156 mpi_type_id_for_type<
decltype(*buffer)>,
2160 total_sent_count += current_count;
2167 template <
typename T>
2170 const T &object_to_send,
2171 const unsigned int root_process)
2173# ifndef DEAL_II_WITH_MPI
2176 return object_to_send;
2182 if constexpr (is_mpi_type<T>)
2184 T
object = object_to_send;
2186 MPI_Bcast(&
object, 1, mpi_type_id_for_type<T>, root_process,
comm);
2193 std::vector<char> buffer;
2201 buffer_size = buffer.size();
2205 int ierr = MPI_Bcast(&buffer_size,
2207 mpi_type_id_for_type<
decltype(buffer_size)>,
2215 buffer.resize(buffer_size);
2220 return object_to_send;
2222 return Utilities::unpack<T>(buffer,
false);
2229 template <
typename T>
2231 isend(
const T &
object,
2233 const unsigned int target_rank,
2234 const unsigned int mpi_tag)
2236# ifndef DEAL_II_WITH_MPI
2242 return Future<void>([]() {}, []() {});
2257 std::shared_ptr<std::vector<char>> send_buffer =
2262 MPI_Request request;
2264 MPI_Isend(send_buffer->data(),
2265 send_buffer->size(),
2266 mpi_type_id_for_type<
decltype(*send_buffer->data())>,
2286 auto wait = [request]()
mutable {
2287 const int ierr = MPI_Wait(&request, MPI_STATUS_IGNORE);
2290 auto cleanup = [send_buffer = std::move(send_buffer)]() {
2291 send_buffer->clear();
2293 return Future<void>(wait, cleanup);
2299 template <
typename T>
2302 const unsigned int source_rank,
2303 const unsigned int mpi_tag)
2305# ifndef DEAL_II_WITH_MPI
2310 return Future<void>([]() {}, []() {
return T{}; });
2326 std::shared_ptr<MPI_Message> message = std::make_shared<MPI_Message>();
2327 std::shared_ptr<MPI_Status> status = std::make_shared<MPI_Status>();
2329 auto wait = [source_rank, mpi_tag, communicator, message, status]() {
2330 const int ierr = MPI_Mprobe(
2331 source_rank, mpi_tag, communicator, message.get(), status.get());
2337 auto get = [status, message]() {
2340 ierr = MPI_Get_count(status.get(), MPI_CHAR, &number_amount);
2343 std::vector<char> receive_buffer(number_amount);
2347 ierr = MPI_Mrecv(receive_buffer.data(),
2349 mpi_type_id_for_type<
decltype(*receive_buffer.data())>,
2355 return Utilities::unpack<T>(receive_buffer,
false);
2358 return Future<T>(wait, get);
2364# ifdef DEAL_II_WITH_MPI
2365 template <
class Iterator,
typename Number>
2366 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
2377 const Number
sum = std::accumulate(begin, end, Number(0.));
2384 std::for_each(begin, end, [&mean, &sq_sum](
const Number &v) {
2388 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_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)
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)
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)
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 > &)
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)