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(MPI_Comm comm)
29: m_mpi_communicator(comm)
30{
31 if (comm == MPI_COMM_NULL)
32 ARCCORE_FATAL("Invalid MPI Communicator : MPI_COMM_NULL");
33
34 MPI_Comm_rank(comm, &rank);
35 MPI_Comm_size(comm, &size);
36 m_message_passing_mng = MessagePassing::Mpi::StandaloneMpiMessagePassingMng::createRef(comm);
37};
38
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
42AlinaCommunicator::
43AlinaCommunicator(IMessagePassingMng* mpm_comm)
44{
45 MessagePassing::Communicator c = mpm_comm->communicator();
46 if (c.isValid())
47 m_mpi_communicator = static_cast<MPI_Comm>(c);
48 rank = mpm_comm->commRank();
49 size = mpm_comm->commSize();
50 m_message_passing_mng = makeRef(mpm_comm);
51};
52
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
56void AlinaCommunicator::
57check(bool cond, const String& message)
58{
59 int lc = (cond) ? 1 : 0;
60 int gc = mpAllReduce(m_message_passing_mng.get(), MessagePassing::eReduceType::ReduceMin, cond);
61
62 if (gc != 0)
63 return;
64 IMessagePassingMng* pm = m_message_passing_mng.get();
65 UniqueArray<int> c(size);
66 if (rank == 0)
67 c.resize(size);
68 ConstArrayView<int> in_view(1, &lc);
69 mpGather(pm, in_view, c, 0);
70 if (rank == 0) {
71 std::cerr << "Failed assumption: " << message << std::endl;
72 std::cerr << "Offending processes:";
73 for (int i = 0; i < size; ++i)
74 if (!c[i])
75 std::cerr << " " << i;
76 std::cerr << std::endl;
77 }
78 mpBarrier(pm);
79 ARCCORE_FATAL("CheckError in MessagePassingUtils: {0}", message);
80}
81
82/*---------------------------------------------------------------------------*/
83/*---------------------------------------------------------------------------*/
84
85MPI_Comm AlinaCommunicator::
86mpiCommunicator() const
87{
88 if (m_mpi_communicator == MPI_COMM_NULL)
89 ARCCORE_FATAL("Invalid MPI Communicator");
90 return m_mpi_communicator;
91}
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
96} // namespace Arcane::Alina
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
#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.
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.