35#ifdef DEAL_II_WITH_MPI
55#ifdef DEAL_II_WITH_MPI
56# define DEAL_II_MPI_CONST_CAST(expr) (expr)
66template <
int rank,
int dim,
typename Number>
68template <
int rank,
int dim,
typename Number>
70template <
typename Number>
91 const unsigned int my_partition_id,
92 const unsigned int n_partitions,
112 template <
typename T>
130 std::complex<double>,
131 std::complex<long double>,
160 std::vector<unsigned int>
185 std::vector<unsigned int>
188 const std::vector<unsigned int> &destinations);
212 const std::vector<unsigned int> &destinations);
462 template <
typename T>
470 template <
typename W,
typename G>
471 Future(W &&wait_operation, G &&get_and_cleanup_operation);
597 const
types::global_dof_index total_size);
599#ifdef DEAL_II_WITH_MPI
615 template <
class Iterator,
typename Number =
long double>
616 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
670 std::unique_ptr<MPI_Datatype, void (*)(MPI_Datatype *)>
692 template <
typename T>
705 template <
typename T,
typename U>
718 template <
typename T>
729 template <
int rank,
int dim,
typename Number>
739 template <
int rank,
int dim,
typename Number>
752 template <
typename Number>
777 template <
typename T>
790 template <
typename T,
typename U>
803 template <
typename T>
828 template <
typename T>
841 template <
typename T,
typename U>
854 template <
typename T>
883 template <
typename T>
901 template <
typename T,
typename U>
914 template <
typename T>
1031 std::vector<MinMaxAvg>
1188 template <
typename T>
1189 std::map<unsigned int, T>
1191 const std::map<unsigned int, T> &objects_to_send);
1206 template <
typename T>
1207 [[nodiscard]] std::vector<T>
1228 template <
typename T>
1229 [[nodiscard]] std::vector<T>
1231 const T &object_to_send,
1232 const unsigned int root_process = 0);
1248 template <
typename T>
1251 const std::vector<T> &objects_to_send,
1252 const unsigned int root_process = 0);
1292 template <
typename T>
1295 const T &object_to_send,
1296 const unsigned int root_process = 0);
1314 template <
typename T>
1317 const std::size_t count,
1318 const unsigned int root,
1333 template <
typename T>
1337 const std::function<T(
const T &,
const T &)> &combiner,
1338 const unsigned int root_process = 0);
1352 template <
typename T,
typename = std::enable_if_t<is_mpi_type<T> == true>>
1353 [[nodiscard]] std::pair<T, T>
1366 template <
typename T>
1370 const std::function<T(
const T &,
const T &)> &combiner);
1393 template <
typename T>
1397 const unsigned int target_rank,
1398 const unsigned int mpi_tag = 0);
1417 template <
typename T>
1420 const unsigned int source_rank,
1421 const unsigned int mpi_tag = 0);
1467 std::vector<unsigned int>
1482 std::pair<std::vector<unsigned int>, std::map<unsigned int, IndexSet>>
1494 template <
typename T>
1501 template <
typename T>
1516 namespace MPIDataTypes
1518#ifdef DEAL_II_WITH_MPI
1522 return MPI_CXX_BOOL;
1538 return MPI_SIGNED_CHAR;
1578 return MPI_LONG_LONG;
1586 return MPI_UNSIGNED_CHAR;
1594 return MPI_UNSIGNED_SHORT;
1602 return MPI_UNSIGNED;
1610 return MPI_UNSIGNED_LONG;
1618 return MPI_UNSIGNED_LONG_LONG;
1642 return MPI_LONG_DOUBLE;
1658 return MPI_DOUBLE_COMPLEX;
1666#ifdef DEAL_II_WITH_MPI
1684 template <
typename T>
1687 static_cast<std::remove_cv_t<std::remove_reference_t<T>
> *>(
nullptr));
1694 template <
typename T>
1703 template <
typename T>
1704 template <
typename W,
typename G>
1714 template <
typename T>
1715 Future<T>::~Future()
1725 if ((get_was_called ==
false) && get_and_cleanup_function)
1731 template <
typename T>
1735 if (is_done ==
false)
1744 template <
typename T>
1748 Assert(get_was_called ==
false,
1750 "You can't call get() more than once on a Future object."));
1751 get_was_called =
true;
1754 return get_and_cleanup_function();
1759 template <
typename T,
unsigned int N>
1761 sum(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&sums)[N])
1763 internal::all_reduce(MPI_SUM,
1771 template <
typename T,
unsigned int N>
1773 max(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&maxima)[N])
1775 internal::all_reduce(MPI_MAX,
1783 template <
typename T,
unsigned int N>
1785 min(
const T (&values)[N],
const MPI_Comm mpi_communicator, T (&minima)[N])
1787 internal::all_reduce(MPI_MIN,
1795 template <
typename T,
unsigned int N>
1801 static_assert(std::is_integral_v<T>,
1802 "The MPI_LOR operation only allows integral data types.");
1804 internal::all_reduce(MPI_LOR,
1815 bool return_value =
false;
1816 internal::all_reduce(MPI_LAND,
1820 return return_value;
1825 template <
typename T>
1826 std::map<unsigned int, T>
1828 const std::map<unsigned int, T> &objects_to_send)
1830# ifndef DEAL_II_WITH_MPI
1832 Assert(objects_to_send.size() < 2,
1833 ExcMessage(
"Cannot send to more than one processor."));
1834 Assert(objects_to_send.find(0) != objects_to_send.end() ||
1835 objects_to_send.empty(),
1836 ExcMessage(
"Can only send to myself or to nobody."));
1837 return objects_to_send;
1841 std::map<unsigned int, T> received_objects;
1843 std::vector<unsigned int> send_to;
1844 send_to.reserve(objects_to_send.size());
1845 for (
const auto &m : objects_to_send)
1847 received_objects[my_proc] = m.
second;
1849 send_to.emplace_back(m.first);
1851 const unsigned int n_expected_incoming_messages =
1855 static CollectiveMutex mutex;
1856 CollectiveMutex::ScopedLock lock(mutex,
comm);
1862 if (send_to.empty() && n_expected_incoming_messages == 0)
1863 return received_objects;
1866 internal::Tags::compute_point_to_point_communication_pattern;
1869 std::vector<std::vector<char>> buffers_to_send(send_to.size());
1870 std::vector<MPI_Request> buffer_send_requests(send_to.size());
1873 for (
const auto &rank_obj : objects_to_send)
1874 if (rank_obj.
first != my_proc)
1876 const auto &rank = rank_obj.first;
1877 buffers_to_send[i] =
1879 const int ierr = MPI_Isend(buffers_to_send[i].
data(),
1880 buffers_to_send[i].
size(),
1885 &buffer_send_requests[i]);
1893 std::vector<char> buffer;
1895 for (
unsigned int i = 0; i < n_expected_incoming_messages; ++i)
1899 int ierr = MPI_Probe(MPI_ANY_SOURCE, mpi_tag,
comm, &status);
1904 ierr = MPI_Get_count(&status, MPI_CHAR, &len);
1909 const unsigned int rank = status.MPI_SOURCE;
1912 ierr = MPI_Recv(buffer.data(),
1920 Assert(received_objects.find(rank) == received_objects.end(),
1922 "I should not receive again from this rank"));
1923 received_objects[rank] =
1924 Utilities::unpack<T>(buffer,
false);
1929 const int ierr = MPI_Waitall(send_to.size(),
1930 buffer_send_requests.data(),
1931 MPI_STATUSES_IGNORE);
1934 return received_objects;
1940 template <
typename T,
typename>
1944# ifndef DEAL_II_WITH_MPI
1971 return {prefix,
sum};
1978 template <
typename T>
1985# ifndef DEAL_II_WITH_MPI
1987 std::vector<T> v(1,
object);
1994 int n_local_data = buffer.size();
1997 std::vector<int> size_all_data(
n_procs, 0);
2000 int ierr = MPI_Allgather(
2001 &n_local_data, 1, MPI_INT, size_all_data.data(), 1, MPI_INT,
comm);
2006 std::vector<int> rdispls(
n_procs);
2008 for (
unsigned int i = 1; i <
n_procs; ++i)
2009 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
2012 std::vector<char> received_unrolled_buffer(rdispls.back() +
2013 size_all_data.back());
2015 ierr = MPI_Allgatherv(buffer.data(),
2018 received_unrolled_buffer.data(),
2019 size_all_data.data(),
2025 std::vector<T> received_objects(
n_procs);
2026 for (
unsigned int i = 0; i <
n_procs; ++i)
2028 std::vector<char> local_buffer(received_unrolled_buffer.begin() +
2030 received_unrolled_buffer.begin() +
2031 rdispls[i] + size_all_data[i]);
2032 received_objects[i] =
2033 Utilities::unpack<T>(local_buffer,
false);
2036 return received_objects;
2042 template <
typename T>
2045 const T &object_to_send,
2046 const unsigned int root_process)
2048# ifndef DEAL_II_WITH_MPI
2051 std::vector<T> v(1, object_to_send);
2059 std::vector<char> buffer =
2061 int n_local_data = buffer.size();
2065 std::vector<int> size_all_data;
2067 size_all_data.resize(
n_procs, 0);
2070 int ierr = MPI_Gather(&n_local_data,
2073 size_all_data.data(),
2082 std::vector<int> rdispls;
2086 for (
unsigned int i = 1; i <
n_procs; ++i)
2087 rdispls[i] = rdispls[i - 1] + size_all_data[i - 1];
2090 std::vector<char> received_unrolled_buffer;
2092 received_unrolled_buffer.resize(rdispls.back() + size_all_data.back());
2094 ierr = MPI_Gatherv(buffer.data(),
2097 received_unrolled_buffer.data(),
2098 size_all_data.data(),
2105 std::vector<T> received_objects;
2109 received_objects.resize(
n_procs);
2111 for (
unsigned int i = 0; i <
n_procs; ++i)
2112 received_objects[i] = Utilities::unpack<T>(
2113 received_unrolled_buffer.cbegin() + rdispls[i],
2114 received_unrolled_buffer.cbegin() + rdispls[i] + size_all_data[i],
2117 return received_objects;
2123 template <
typename T>
2126 const std::vector<T> &objects_to_send,
2127 const unsigned int root_process)
2129# ifndef DEAL_II_WITH_MPI
2135 return objects_to_send[0];
2142 (
my_rank != root_process && objects_to_send.empty()) ||
2143 objects_to_send.size() ==
n_procs,
2145 "The number of objects to be scattered must correspond to the number processes."));
2147 std::vector<char> send_buffer;
2148 std::vector<int> send_counts;
2149 std::vector<int> send_displacements;
2153 send_counts.resize(
n_procs, 0);
2154 send_displacements.resize(
n_procs + 1, 0);
2156 for (
unsigned int i = 0; i <
n_procs; ++i)
2158 const auto packed_data =
2160 send_buffer.insert(send_buffer.end(),
2161 packed_data.begin(),
2163 send_counts[i] = packed_data.size();
2166 for (
unsigned int i = 0; i <
n_procs; ++i)
2167 send_displacements[i + 1] = send_displacements[i] + send_counts[i];
2171 int ierr = MPI_Scatter(send_counts.data(),
2181 std::vector<char> recv_buffer(n_local_data);
2183 ierr = MPI_Scatterv(send_buffer.data(),
2185 send_displacements.data(),
2194 return Utilities::unpack<T>(recv_buffer,
false);
2199 template <
typename T>
2202 const std::size_t count,
2203 const unsigned int root,
2206 static_assert(is_mpi_type<T>,
2207 "This function is currently only implemented for "
2208 "MPI-supported data types.");
2210# ifndef DEAL_II_WITH_MPI
2221 const size_t max_send_count = std::numeric_limits<signed int>::max();
2223 size_t total_sent_count = 0;
2224 while (total_sent_count < count)
2226 const size_t current_count =
2227 std::min(count - total_sent_count, max_send_count);
2229 const int ierr = MPI_Bcast(buffer + total_sent_count,
2231 mpi_type_id_for_type<
decltype(*buffer)>,
2235 total_sent_count += current_count;
2242 template <
typename T>
2245 const T &object_to_send,
2246 const unsigned int root_process)
2248# ifndef DEAL_II_WITH_MPI
2251 return object_to_send;
2257 if constexpr (is_mpi_type<T>)
2259 T object = object_to_send;
2261 MPI_Bcast(&
object, 1, mpi_type_id_for_type<T>, root_process,
comm);
2268 std::vector<char> buffer;
2276 buffer_size = buffer.size();
2280 int ierr = MPI_Bcast(&buffer_size,
2282 mpi_type_id_for_type<
decltype(buffer_size)>,
2290 buffer.resize(buffer_size);
2295 return object_to_send;
2297 return Utilities::unpack<T>(buffer,
false);
2304 template <
typename T>
2306 isend(
const T &
object,
2308 const unsigned int target_rank,
2309 const unsigned int mpi_tag)
2311# ifndef DEAL_II_WITH_MPI
2317 return Future<void>([]() {}, []() {});
2332 std::shared_ptr<std::vector<char>> send_buffer =
2333 std::make_unique<std::vector<char>>(
2338 MPI_Request request;
2340 MPI_Isend(send_buffer->data(),
2341 send_buffer->size(),
2342 mpi_type_id_for_type<
decltype(*send_buffer->data())>,
2362 auto wait = [request]()
mutable {
2363 const int ierr = MPI_Wait(&request, MPI_STATUS_IGNORE);
2366 auto cleanup = [send_buffer = std::move(send_buffer)]() {
2367 send_buffer->clear();
2369 return Future<void>(wait, cleanup);
2375 template <
typename T>
2378 const unsigned int source_rank,
2379 const unsigned int mpi_tag)
2381# ifndef DEAL_II_WITH_MPI
2386 return Future<void>([]() {}, []() {
return T{}; });
2402 std::shared_ptr<MPI_Message> message = std::make_shared<MPI_Message>();
2403 std::shared_ptr<MPI_Status> status = std::make_shared<MPI_Status>();
2405 auto wait = [source_rank, mpi_tag, communicator, message, status]() {
2406 const int ierr = MPI_Mprobe(
2407 source_rank, mpi_tag, communicator, message.get(), status.get());
2413 auto get = [status, message]() {
2416 ierr = MPI_Get_count(status.get(), MPI_CHAR, &number_amount);
2419 std::vector<char> receive_buffer(number_amount);
2423 ierr = MPI_Mrecv(receive_buffer.data(),
2425 mpi_type_id_for_type<
decltype(*receive_buffer.data())>,
2431 return Utilities::unpack<T>(receive_buffer,
false);
2434 return Future<T>(wait, get);
2440# ifdef DEAL_II_WITH_MPI
2441 template <
class Iterator,
typename Number>
2442 std::pair<Number, typename numbers::NumberTraits<Number>::real_type>
2460 std::for_each(
begin,
end, [&mean, &sq_sum](
const Number &v) {
2464 return std::make_pair(mean,
* x_component_mask set(0, true)
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
* * if(update_pressure &update_flags) * compute_pressure(constitutive_request
* * * RotationFunction< dim, Number >::RotationFunction Number(dim)
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)
bool logical_and(const bool 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< const Number *, width > &ptrs, const unsigned int offset)