Arcane  4.2.3.0
Developer documentation
Loading...
Searching...
No Matches
MessagePassingUtils.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MessagePassingUtils.cc (C) 2000-2026 */
9/* */
10/* Various utilities to handle message passing. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/alina/MessagePassingUtils.h"
15
16#include <mpi.h>
17
18/*---------------------------------------------------------------------------*/
19/*---------------------------------------------------------------------------*/
20
21namespace Arcane::Alina
22{
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27AlinaCommunicator::
28AlinaCommunicator(IMessagePassingMng* mpm_comm)
29{
30 m_message_passing_mng = makeRef(mpm_comm);
31 _init();
32};
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37AlinaCommunicator::
38AlinaCommunicator(const Ref<IMessagePassingMng>& mpm_comm)
39{
40 m_message_passing_mng = mpm_comm;
41 _init();
42};
43
44/*---------------------------------------------------------------------------*/
45/*---------------------------------------------------------------------------*/
46
47void AlinaCommunicator::
48_init()
49{
50 IMessagePassingMng* mpm_comm = m_message_passing_mng.get();
51
52 MessagePassing::Communicator c = mpm_comm->communicator();
53 if (c.isValid())
54 m_mpi_communicator = static_cast<MPI_Comm>(c);
55 rank = mpm_comm->commRank();
56 size = mpm_comm->commSize();
57}
58
59/*---------------------------------------------------------------------------*/
60/*---------------------------------------------------------------------------*/
61
62AlinaCommunicator AlinaCommunicator::
63createFromMPI(MPI_Comm comm)
64{
66 return AlinaCommunicator(mpm);
67}
68
69/*---------------------------------------------------------------------------*/
70/*---------------------------------------------------------------------------*/
71
73check(bool cond, const String& message)
74{
75 int lc = (cond) ? 1 : 0;
76 int gc = mpAllReduce(m_message_passing_mng.get(), MessagePassing::eReduceType::ReduceMin, cond);
77
78 if (gc != 0)
79 return;
80 IMessagePassingMng* pm = m_message_passing_mng.get();
81 UniqueArray<int> c(size);
82 if (rank == 0)
83 c.resize(size);
84 ConstArrayView<int> in_view(1, &lc);
85 mpGather(pm, in_view, c, 0);
86 if (rank == 0) {
87 std::cerr << "Failed assumption: " << message << std::endl;
88 std::cerr << "Offending processes:";
89 for (int i = 0; i < size; ++i)
90 if (!c[i])
91 std::cerr << " " << i;
92 std::cerr << std::endl;
93 }
94 mpBarrier(pm);
95 ARCCORE_FATAL("CheckError in MessagePassingUtils: {0}", message);
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101MPI_Comm AlinaCommunicator::
102mpiCommunicator() const
103{
104 if (m_mpi_communicator == MPI_COMM_NULL)
105 ARCCORE_FATAL("Invalid MPI Communicator");
106 return m_mpi_communicator;
107}
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
111
112} // namespace Arcane::Alina
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
void resize(Int64 s)
Changes the number of elements in the array to s.
Constant view of an array of type T.
Interface of the message passing manager.
static Ref< IMessagePassingMng > createRef(MPI_Comm comm, bool clean_comm=false)
Creates a manager associated with the communicator comm.
1D data vector with value semantics (STL style).
C void mpGather(IMessagePassingMng *pm, Span< const char > send_buf, Span< char > recv_buf, Int32 rank)
C char mpAllReduce(IMessagePassingMng *pm, eReduceType rt, char v)
void mpBarrier(IMessagePassingMng *pm)
Performs a barrier.
Definition Messages.cc:259
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
Convenience wrapper around MPI_Comm.
void check(bool cond, const String &message)
Communicator-wise condition checking.