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);
571 const
types::global_dof_index total_size);
573#ifdef DEAL_II_WITH_MPI
589 template <
class Iterator,
typename Number =
long double>
590 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
644 std::unique_ptr<MPI_Datatype, void (*)(MPI_Datatype *)>
666 template <
typename T>
679 template <
typename T,
typename U>
692 template <
typename T>
703 template <
int rank,
int dim,
typename Number>
713 template <
int rank,
int dim,
typename Number>
726 template <
typename Number>
751 template <
typename T>
764 template <
typename T,
typename U>
777 template <
typename T>
802 template <
typename T>
815 template <
typename T,
typename U>
828 template <
typename T>
857 template <
typename T>
875 template <
typename T,
typename U>
888 template <
typename T>
983 std::vector<MinMaxAvg>
1140 template <
typename T>
1141 std::map<unsigned int, T>
1143 const std::map<unsigned int, T> &objects_to_send);
1158 template <
typename T>
1177 template <
typename T>
1180 const T &object_to_send,
1181 const unsigned int root_process = 0);
1197 template <
typename T>
1200 const std::vector<T> &objects_to_send,
1201 const unsigned int root_process = 0);
1241 template <
typename T>
1244 const T &object_to_send,
1245 const unsigned int root_process = 0);
1263 template <
typename T>
1266 const std::size_t count,
1267 const unsigned int root,
1282 template <
typename T>
1286 const std::function<T(
const T &,
const T &)> &combiner,
1287 const unsigned int root_process = 0);
1301 template <
typename T,
typename = std::enable_if_t<is_mpi_type<T> == true>>
1315 template <
typename T>
1319 const std::function<T(
const T &,
const T &)> &combiner);
1342 template <
typename T>
1346 const unsigned int target_rank,
1347 const unsigned int mpi_tag = 0);
1366 template <
typename T>
1369 const unsigned int source_rank,
1370 const unsigned int mpi_tag = 0);
1416 std::vector<unsigned int>
1431 std::pair<std::vector<unsigned int>, std::map<unsigned int, IndexSet>>
1443 template <
typename T>
1450 template <
typename T>
1465 namespace MPIDataTypes
1467#ifdef DEAL_II_WITH_MPI
1471 return MPI_CXX_BOOL;
1487 return MPI_SIGNED_CHAR;
1527 return MPI_LONG_LONG;
1535 return MPI_UNSIGNED_CHAR;
1543 return MPI_UNSIGNED_SHORT;
1551 return MPI_UNSIGNED;
1559 return MPI_UNSIGNED_LONG;
1567 return MPI_UNSIGNED_LONG_LONG;
1591 return MPI_LONG_DOUBLE;
1607 return MPI_DOUBLE_COMPLEX;
1465 namespace MPIDataTypes {
…}
1615#ifdef DEAL_II_WITH_MPI
1633 template <
typename T>
1636 static_cast<std::remove_cv_t<std::remove_reference_t<T>
> *>(
nullptr));
1643 template <
typename T>
1652 template <
typename T>
1653 template <
typename W,
typename G>
1663 template <
typename T>
1664 Future<T>::~Future()
1674 if ((get_was_called ==
false) && get_and_cleanup_function)
1680 template <
typename T>
1684 if (is_done ==
false)
1693 template <
typename T>
1697 Assert(get_was_called ==
false,
1699 "You can't call get() more than once on a Future object."));
1700 get_was_called =
true;
1703 return get_and_cleanup_function();
1708 template <
typename T,
unsigned int N>
1710 sum(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&sums)[N])
1712 internal::all_reduce(MPI_SUM,
1720 template <
typename T,
unsigned int N>
1722 max(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&maxima)[N])
1724 internal::all_reduce(MPI_MAX,
1732 template <
typename T,
unsigned int N>
1734 min(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&minima)[N])
1736 internal::all_reduce(MPI_MIN,
1744 template <
typename T,
unsigned int N>
1750 static_assert(std::is_integral_v<T>,
1751 "The MPI_LOR operation only allows integral data types.");
1753 internal::all_reduce(MPI_LOR,
1761 template <
typename T>
1762 std::map<unsigned int, T>
1764 const std::map<unsigned int, T> &objects_to_send)
1766# ifndef DEAL_II_WITH_MPI
1768 Assert(objects_to_send.size() < 2,
1769 ExcMessage(
"Cannot send to more than one processor."));
1770 Assert(objects_to_send.find(0) != objects_to_send.end() ||
1771 objects_to_send.empty(),
1772 ExcMessage(
"Can only send to myself or to nobody."));
1773 return objects_to_send;
1777 std::map<unsigned int, T> received_objects;
1779 std::vector<unsigned int> send_to;
1780 send_to.reserve(objects_to_send.size());
1781 for (
const auto &m : objects_to_send)
1782 if (m.
first == my_proc)
1783 received_objects[my_proc] = m.
second;
1785 send_to.emplace_back(m.first);
1787 const unsigned int n_expected_incoming_messages =
1791 static CollectiveMutex mutex;
1792 CollectiveMutex::ScopedLock lock(mutex,
comm);
1798 if (send_to.empty() && n_expected_incoming_messages == 0)
1799 return received_objects;
1802 internal::Tags::compute_point_to_point_communication_pattern;
1805 std::vector<std::vector<char>> buffers_to_send(send_to.size());
1806 std::vector<MPI_Request> buffer_send_requests(send_to.size());
1809 for (
const auto &rank_obj : objects_to_send)
1810 if (rank_obj.
first != my_proc)
1812 const auto &rank = rank_obj.first;
1815 const int ierr = MPI_Isend(buffers_to_send[i].
data(),
1816 buffers_to_send[i].
size(),
1821 &buffer_send_requests[i]);
1829 std::vector<char> buffer;
1831 for (
unsigned int i = 0; i < n_expected_incoming_messages; ++i)
1835 int ierr = MPI_Probe(MPI_ANY_SOURCE, mpi_tag,
comm, &status);
1840 ierr = MPI_Get_count(&status, MPI_CHAR, &len);
1845 const unsigned int rank = status.MPI_SOURCE;
1848 ierr = MPI_Recv(buffer.data(),
1856 Assert(received_objects.find(rank) == received_objects.end(),
1858 "I should not receive again from this rank"));
1859 received_objects[rank] =
1860 Utilities::unpack<T>(buffer,
1866 const int ierr = MPI_Waitall(send_to.size(),
1867 buffer_send_requests.data(),
1868 MPI_STATUSES_IGNORE);
1871 return received_objects;
1877 template <
typename T,
typename>
1881# ifndef DEAL_II_WITH_MPI
1908 return {prefix,
sum};
1915 template <
typename T>
1922# ifndef DEAL_II_WITH_MPI
1924 std::vector<T> v(1,
object);
1931 int n_local_data = buffer.size();
1934 std::vector<int> size_all_data(
n_procs, 0);
1937 int ierr = MPI_Allgather(
1938 &n_local_data, 1, MPI_INT, size_all_data.data(), 1, MPI_INT,
comm);
1943 std::vector<int> rdispls(
n_procs);
1945 for (
unsigned int i = 1; i <
n_procs; ++i)
1946 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
1949 std::vector<char> received_unrolled_buffer(rdispls.back() +
1950 size_all_data.back());
1952 ierr = MPI_Allgatherv(buffer.data(),
1955 received_unrolled_buffer.data(),
1956 size_all_data.data(),
1962 std::vector<T> received_objects(
n_procs);
1963 for (
unsigned int i = 0; i <
n_procs; ++i)
1965 std::vector<char> local_buffer(received_unrolled_buffer.begin() +
1967 received_unrolled_buffer.begin() +
1968 rdispls[i] + size_all_data[i]);
1969 received_objects[i] = Utilities::unpack<T>(local_buffer);
1972 return received_objects;
1978 template <
typename T>
1981 const T &object_to_send,
1982 const unsigned int root_process)
1984# ifndef DEAL_II_WITH_MPI
1987 std::vector<T> v(1, object_to_send);
1996 int n_local_data = buffer.size();
2000 std::vector<int> size_all_data;
2002 size_all_data.resize(
n_procs, 0);
2005 int ierr = MPI_Gather(&n_local_data,
2008 size_all_data.data(),
2017 std::vector<int> rdispls;
2021 for (
unsigned int i = 1; i <
n_procs; ++i)
2022 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
2025 std::vector<char> received_unrolled_buffer;
2027 received_unrolled_buffer.resize(rdispls.back() + size_all_data.back());
2029 ierr = MPI_Gatherv(buffer.data(),
2032 received_unrolled_buffer.data(),
2033 size_all_data.data(),
2040 std::vector<T> received_objects;
2044 received_objects.resize(
n_procs);
2046 for (
unsigned int i = 0; i <
n_procs; ++i)
2048 const std::vector<char> local_buffer(
2049 received_unrolled_buffer.begin() + rdispls[i],
2050 received_unrolled_buffer.begin() + rdispls[i] +
2052 received_objects[i] = Utilities::unpack<T>(local_buffer);
2055 return received_objects;
2061 template <
typename T>
2064 const std::vector<T> &objects_to_send,
2065 const unsigned int root_process)
2067# ifndef DEAL_II_WITH_MPI
2073 return objects_to_send[0];
2080 (
my_rank != root_process && objects_to_send.empty()) ||
2081 objects_to_send.size() ==
n_procs,
2083 "The number of objects to be scattered must correspond to the number processes."));
2085 std::vector<char> send_buffer;
2086 std::vector<int> send_counts;
2087 std::vector<int> send_displacements;
2091 send_counts.resize(
n_procs, 0);
2092 send_displacements.resize(
n_procs + 1, 0);
2094 for (
unsigned int i = 0; i <
n_procs; ++i)
2097 send_buffer.insert(send_buffer.end(),
2098 packed_data.begin(),
2100 send_counts[i] = packed_data.size();
2103 for (
unsigned int i = 0; i <
n_procs; ++i)
2104 send_displacements[i + 1] = send_displacements[i] + send_counts[i];
2108 int ierr = MPI_Scatter(send_counts.data(),
2118 std::vector<char> recv_buffer(n_local_data);
2120 ierr = MPI_Scatterv(send_buffer.data(),
2122 send_displacements.data(),
2131 return Utilities::unpack<T>(recv_buffer);
2136 template <
typename T>
2139 const std::size_t count,
2140 const unsigned int root,
2143# ifndef DEAL_II_WITH_MPI
2154 const size_t max_send_count = std::numeric_limits<signed int>::max();
2156 size_t total_sent_count = 0;
2157 while (total_sent_count < count)
2159 const size_t current_count =
2160 std::min(count - total_sent_count, max_send_count);
2162 const int ierr = MPI_Bcast(buffer + total_sent_count,
2164 mpi_type_id_for_type<
decltype(*buffer)>,
2168 total_sent_count += current_count;
2175 template <
typename T>
2178 const T &object_to_send,
2179 const unsigned int root_process)
2181# ifndef DEAL_II_WITH_MPI
2184 return object_to_send;
2190 if constexpr (is_mpi_type<T>)
2192 T object = object_to_send;
2194 MPI_Bcast(&
object, 1, mpi_type_id_for_type<T>, root_process,
comm);
2201 std::vector<char> buffer;
2209 buffer_size = buffer.size();
2213 int ierr = MPI_Bcast(&buffer_size,
2215 mpi_type_id_for_type<
decltype(buffer_size)>,
2223 buffer.resize(buffer_size);
2228 return object_to_send;
2230 return Utilities::unpack<T>(buffer,
false);
2237 template <
typename T>
2239 isend(
const T &
object,
2241 const unsigned int target_rank,
2242 const unsigned int mpi_tag)
2244# ifndef DEAL_II_WITH_MPI
2250 return Future<void>([]() {}, []() {});
2265 std::shared_ptr<std::vector<char>> send_buffer =
2270 MPI_Request request;
2272 MPI_Isend(send_buffer->data(),
2273 send_buffer->size(),
2274 mpi_type_id_for_type<
decltype(*send_buffer->data())>,
2294 auto wait = [request]()
mutable {
2295 const int ierr = MPI_Wait(&request, MPI_STATUS_IGNORE);
2298 auto cleanup = [send_buffer = std::move(send_buffer)]() {
2299 send_buffer->clear();
2301 return Future<void>(wait, cleanup);
2307 template <
typename T>
2310 const unsigned int source_rank,
2311 const unsigned int mpi_tag)
2313# ifndef DEAL_II_WITH_MPI
2318 return Future<void>([]() {}, []() {
return T{}; });
2334 std::shared_ptr<MPI_Message> message = std::make_shared<MPI_Message>();
2335 std::shared_ptr<MPI_Status> status = std::make_shared<MPI_Status>();
2337 auto wait = [source_rank, mpi_tag, communicator, message, status]() {
2338 const int ierr = MPI_Mprobe(
2339 source_rank, mpi_tag, communicator, message.get(), status.get());
2345 auto get = [status, message]() {
2348 ierr = MPI_Get_count(status.get(), MPI_CHAR, &number_amount);
2351 std::vector<char> receive_buffer(number_amount);
2355 ierr = MPI_Mrecv(receive_buffer.data(),
2357 mpi_type_id_for_type<
decltype(*receive_buffer.data())>,
2363 return Utilities::unpack<T>(receive_buffer,
false);
2366 return Future<T>(wait, get);
2372# ifdef DEAL_II_WITH_MPI
2373 template <
class Iterator,
typename Number>
2374 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
2385 const Number
sum = std::accumulate(begin, end, Number(0.));
2392 std::for_each(begin, end, [&mean, &sq_sum](
const Number &v) {
2396 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)