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);
551 const
types::global_dof_index locally_owned_size);
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);
1407 std::vector<unsigned int>
1409 const IndexSet &indices_to_look_up,
1419 template <
typename T>
1426 template <
typename T>
1441 namespace MPIDataTypes
1443#ifdef DEAL_II_WITH_MPI
1447 return MPI_CXX_BOOL;
1463 return MPI_SIGNED_CHAR;
1503 return MPI_LONG_LONG;
1511 return MPI_UNSIGNED_CHAR;
1519 return MPI_UNSIGNED_SHORT;
1527 return MPI_UNSIGNED;
1535 return MPI_UNSIGNED_LONG;
1543 return MPI_UNSIGNED_LONG_LONG;
1567 return MPI_LONG_DOUBLE;
1583 return MPI_DOUBLE_COMPLEX;
1591#ifdef DEAL_II_WITH_MPI
1609 template <
typename T>
1612 static_cast<std::remove_cv_t<std::remove_reference_t<T>
> *>(
nullptr));
1619 template <
typename T>
1628 template <
typename T>
1629 template <
typename W,
typename G>
1639 template <
typename T>
1640 Future<T>::~Future()
1650 if ((get_was_called ==
false) && get_and_cleanup_function)
1656 template <
typename T>
1660 if (is_done ==
false)
1669 template <
typename T>
1673 Assert(get_was_called ==
false,
1675 "You can't call get() more than once on a Future object."));
1676 get_was_called =
true;
1679 return get_and_cleanup_function();
1684 template <
typename T,
unsigned int N>
1686 sum(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&sums)[N])
1688 internal::all_reduce(MPI_SUM,
1696 template <
typename T,
unsigned int N>
1698 max(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&maxima)[N])
1700 internal::all_reduce(MPI_MAX,
1708 template <
typename T,
unsigned int N>
1710 min(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&minima)[N])
1712 internal::all_reduce(MPI_MIN,
1720 template <
typename T,
unsigned int N>
1726 static_assert(std::is_integral_v<T>,
1727 "The MPI_LOR operation only allows integral data types.");
1729 internal::all_reduce(MPI_LOR,
1737 template <
typename T>
1738 std::map<unsigned int, T>
1740 const std::map<unsigned int, T> &objects_to_send)
1742# ifndef DEAL_II_WITH_MPI
1744 Assert(objects_to_send.size() < 2,
1745 ExcMessage(
"Cannot send to more than one processor."));
1746 Assert(objects_to_send.find(0) != objects_to_send.end() ||
1747 objects_to_send.empty(),
1748 ExcMessage(
"Can only send to myself or to nobody."));
1749 return objects_to_send;
1753 std::map<unsigned int, T> received_objects;
1755 std::vector<unsigned int> send_to;
1756 send_to.reserve(objects_to_send.size());
1757 for (
const auto &m : objects_to_send)
1758 if (m.
first == my_proc)
1759 received_objects[my_proc] = m.
second;
1761 send_to.emplace_back(m.first);
1763 const unsigned int n_expected_incoming_messages =
1767 static CollectiveMutex mutex;
1768 CollectiveMutex::ScopedLock lock(mutex,
comm);
1774 if (send_to.empty() && n_expected_incoming_messages == 0)
1775 return received_objects;
1778 internal::Tags::compute_point_to_point_communication_pattern;
1781 std::vector<std::vector<char>> buffers_to_send(send_to.size());
1782 std::vector<MPI_Request> buffer_send_requests(send_to.size());
1785 for (
const auto &rank_obj : objects_to_send)
1786 if (rank_obj.
first != my_proc)
1788 const auto &rank = rank_obj.first;
1791 const int ierr = MPI_Isend(buffers_to_send[i].data(),
1792 buffers_to_send[i].size(),
1797 &buffer_send_requests[i]);
1805 std::vector<char> buffer;
1807 for (
unsigned int i = 0; i < n_expected_incoming_messages; ++i)
1811 int ierr = MPI_Probe(MPI_ANY_SOURCE, mpi_tag,
comm, &status);
1816 ierr = MPI_Get_count(&status, MPI_CHAR, &len);
1821 const unsigned int rank = status.MPI_SOURCE;
1824 ierr = MPI_Recv(buffer.data(),
1832 Assert(received_objects.find(rank) == received_objects.end(),
1834 "I should not receive again from this rank"));
1835 received_objects[rank] =
1836 Utilities::unpack<T>(buffer,
1842 const int ierr = MPI_Waitall(send_to.size(),
1843 buffer_send_requests.data(),
1844 MPI_STATUSES_IGNORE);
1847 return received_objects;
1853 template <
typename T,
typename>
1857# ifndef DEAL_II_WITH_MPI
1884 return {prefix,
sum};
1891 template <
typename T>
1898# ifndef DEAL_II_WITH_MPI
1900 std::vector<T> v(1,
object);
1907 int n_local_data = buffer.size();
1910 std::vector<int> size_all_data(n_procs, 0);
1913 int ierr = MPI_Allgather(
1914 &n_local_data, 1, MPI_INT, size_all_data.data(), 1, MPI_INT,
comm);
1919 std::vector<int> rdispls(n_procs);
1921 for (
unsigned int i = 1; i < n_procs; ++i)
1922 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
1925 std::vector<char> received_unrolled_buffer(rdispls.back() +
1926 size_all_data.back());
1928 ierr = MPI_Allgatherv(buffer.data(),
1931 received_unrolled_buffer.data(),
1932 size_all_data.data(),
1938 std::vector<T> received_objects(n_procs);
1939 for (
unsigned int i = 0; i < n_procs; ++i)
1941 std::vector<char> local_buffer(received_unrolled_buffer.begin() +
1943 received_unrolled_buffer.begin() +
1944 rdispls[i] + size_all_data[i]);
1945 received_objects[i] = Utilities::unpack<T>(local_buffer);
1948 return received_objects;
1954 template <
typename T>
1957 const T &object_to_send,
1958 const unsigned int root_process)
1960# ifndef DEAL_II_WITH_MPI
1963 std::vector<T> v(1, object_to_send);
1972 int n_local_data = buffer.size();
1976 std::vector<int> size_all_data;
1977 if (my_rank == root_process)
1978 size_all_data.resize(n_procs, 0);
1981 int ierr = MPI_Gather(&n_local_data,
1984 size_all_data.data(),
1993 std::vector<int> rdispls;
1994 if (my_rank == root_process)
1996 rdispls.resize(n_procs, 0);
1997 for (
unsigned int i = 1; i < n_procs; ++i)
1998 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
2001 std::vector<char> received_unrolled_buffer;
2002 if (my_rank == root_process)
2003 received_unrolled_buffer.resize(rdispls.back() + size_all_data.back());
2005 ierr = MPI_Gatherv(buffer.data(),
2008 received_unrolled_buffer.data(),
2009 size_all_data.data(),
2016 std::vector<T> received_objects;
2018 if (my_rank == root_process)
2020 received_objects.resize(n_procs);
2022 for (
unsigned int i = 0; i < n_procs; ++i)
2024 const std::vector<char> local_buffer(
2025 received_unrolled_buffer.begin() + rdispls[i],
2026 received_unrolled_buffer.begin() + rdispls[i] +
2028 received_objects[i] = Utilities::unpack<T>(local_buffer);
2031 return received_objects;
2037 template <
typename T>
2040 const std::vector<T> &objects_to_send,
2041 const unsigned int root_process)
2043# ifndef DEAL_II_WITH_MPI
2049 return objects_to_send[0];
2056 (my_rank != root_process && objects_to_send.empty()) ||
2057 objects_to_send.size() == n_procs,
2059 "The number of objects to be scattered must correspond to the number processes."));
2061 std::vector<char> send_buffer;
2062 std::vector<int> send_counts;
2063 std::vector<int> send_displacements;
2065 if (my_rank == root_process)
2067 send_counts.resize(n_procs, 0);
2068 send_displacements.resize(n_procs + 1, 0);
2070 for (
unsigned int i = 0; i < n_procs; ++i)
2073 send_buffer.insert(send_buffer.end(),
2074 packed_data.begin(),
2076 send_counts[i] = packed_data.size();
2079 for (
unsigned int i = 0; i < n_procs; ++i)
2080 send_displacements[i + 1] = send_displacements[i] + send_counts[i];
2084 int ierr = MPI_Scatter(send_counts.data(),
2094 std::vector<char> recv_buffer(n_local_data);
2096 ierr = MPI_Scatterv(send_buffer.data(),
2098 send_displacements.data(),
2107 return Utilities::unpack<T>(recv_buffer);
2112 template <
typename T>
2116 const unsigned int root,
2119# ifndef DEAL_II_WITH_MPI
2130 const size_t max_send_count = std::numeric_limits<signed int>::max();
2132 size_t total_sent_count = 0;
2133 while (total_sent_count < count)
2135 const size_t current_count =
2136 std::min(count - total_sent_count, max_send_count);
2138 const int ierr = MPI_Bcast(buffer + total_sent_count,
2140 mpi_type_id_for_type<
decltype(*buffer)>,
2144 total_sent_count += current_count;
2151 template <
typename T>
2154 const T &object_to_send,
2155 const unsigned int root_process)
2157# ifndef DEAL_II_WITH_MPI
2160 return object_to_send;
2166 if constexpr (is_mpi_type<T>)
2168 T
object = object_to_send;
2170 MPI_Bcast(&
object, 1, mpi_type_id_for_type<T>, root_process,
comm);
2177 std::vector<char> buffer;
2185 buffer_size = buffer.size();
2189 int ierr = MPI_Bcast(&buffer_size,
2191 mpi_type_id_for_type<
decltype(buffer_size)>,
2199 buffer.resize(buffer_size);
2204 return object_to_send;
2206 return Utilities::unpack<T>(buffer,
false);
2213 template <
typename T>
2215 isend(
const T &
object,
2217 const unsigned int target_rank,
2218 const unsigned int mpi_tag)
2220# ifndef DEAL_II_WITH_MPI
2226 return Future<void>([]() {}, []() {});
2241 std::shared_ptr<std::vector<char>> send_buffer =
2246 MPI_Request request;
2248 MPI_Isend(send_buffer->data(),
2249 send_buffer->size(),
2250 mpi_type_id_for_type<
decltype(*send_buffer->data())>,
2270 auto wait = [request]()
mutable {
2271 const int ierr = MPI_Wait(&request, MPI_STATUS_IGNORE);
2274 auto cleanup = [send_buffer = std::move(send_buffer)]() {
2275 send_buffer->clear();
2277 return Future<void>(wait, cleanup);
2283 template <
typename T>
2286 const unsigned int source_rank,
2287 const unsigned int mpi_tag)
2289# ifndef DEAL_II_WITH_MPI
2294 return Future<void>([]() {}, []() {
return T{}; });
2310 std::shared_ptr<MPI_Message> message = std::make_shared<MPI_Message>();
2311 std::shared_ptr<MPI_Status> status = std::make_shared<MPI_Status>();
2313 auto wait = [source_rank, mpi_tag, communicator, message, status]() {
2314 const int ierr = MPI_Mprobe(
2315 source_rank, mpi_tag, communicator, message.get(), status.get());
2321 auto get = [status, message]() {
2324 ierr = MPI_Get_count(status.get(), MPI_CHAR, &number_amount);
2327 std::vector<char> receive_buffer(number_amount);
2331 ierr = MPI_Mrecv(receive_buffer.data(),
2333 mpi_type_id_for_type<
decltype(*receive_buffer.data())>,
2339 return Utilities::unpack<T>(receive_buffer,
false);
2342 return Future<T>(wait, get);
2348# ifdef DEAL_II_WITH_MPI
2349 template <
class Iterator,
typename Number>
2350 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
2361 const Number
sum = std::accumulate(begin, end, Number(0.));
2368 std::for_each(begin, end, [&mean, &sq_sum](
const Number &v) {
2372 return std::make_pair(mean,
2373 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_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::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 > &)
*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)