Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Communicator.h
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/* Communicator.h (C) 2000-2025 */
9/* */
10/* Communicator for message exchange. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_MESSAGEPASSING_COMMUNICATOR_H
13#define ARCCORE_MESSAGEPASSING_COMMUNICATOR_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arccore/base/Ref.h"
19
20#include <cstddef>
21#include <iosfwd>
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32/*!
33 * \brief Communicator for message exchange.
34 *
35 * This class is an abstraction of the communicator found
36 * in the MPI standard under the type 'MPI_Comm'.
37 *
38 * This class allows generically storing a communicator without
39 * knowing its exact type (for example MPI_Comm with the MPI standard). We use
40 * a union for this purpose.
41 *
42 * Before using an instance of this class, the null communicator must be set
43 * by calling the static method setNullCommunicator() with the null communicator
44 * value for the implementation used.
45 */
46class ARCCORE_MESSAGEPASSING_EXPORT Communicator
47{
48 union _Communicator
49 {
50 int i;
51 long l;
52 std::size_t st;
53 void* v;
54 const void* cv;
55 };
56
57 enum Type
58 {
59 T_Int,
60 T_Long,
61 T_SizeT,
62 T_Ptr,
63 T_Null
64 };
65
66 public:
67
68 Communicator()
69 {
70 m_type = T_Null;
71 m_communicator = null_communicator;
72 }
73
74 public:
75
76 explicit Communicator(void* acommunicator)
77 {
78 m_type = T_Ptr;
79 m_communicator.v = acommunicator;
80 }
81
82 explicit Communicator(const void* acommunicator)
83 {
84 m_type = T_Ptr;
85 m_communicator.cv = acommunicator;
86 }
87
88 explicit Communicator(int acommunicator)
89 {
90 m_type = T_Int;
91 m_communicator.i = acommunicator;
92 }
93
94 explicit Communicator(long acommunicator)
95 {
96 m_type = T_Long;
97 m_communicator.l = acommunicator;
98 }
99
100 explicit Communicator(std::size_t acommunicator)
101 {
102 m_type = T_SizeT;
103 m_communicator.st = acommunicator;
104 }
105
106 public:
107
108 template <typename T>
109 operator const T*() const { return (const T*)m_communicator.cv; }
110 template <typename T>
111 operator T*() const { return (T*)m_communicator.v; }
112 operator int() const { return m_communicator.i; }
113 operator long() const { return m_communicator.l; }
114 operator size_t() const { return m_communicator.st; }
115 void* communicatorAddress() { return &m_communicator; }
116
117 public:
118
119 /*!
120 * \brief Indicates if the communicator is valid.
121 *
122 * A communicator is valid if it is different from the null communicator.
123 */
124 bool isValid() const
125 {
126 if (m_type == T_Null)
127 return false;
128 // If the request type is different from the null request type,
129 // then the request is considered valid.
130 if (m_type != null_communicator_type)
131 return true;
132 if (m_type == T_Int)
133 return m_communicator.i != null_communicator.i;
134 if (m_type == T_Long)
135 return m_communicator.l != null_communicator.l;
136 if (m_type == T_SizeT)
137 return m_communicator.st != null_communicator.st;
138 if (m_type == T_Ptr)
139 return m_communicator.cv != null_communicator.cv;
140 return false;
141 }
142
143 static void setNullCommunicator(Communicator r)
144 {
145 null_communicator = r.m_communicator;
146 null_communicator_type = r.m_type;
147 }
148
149 void reset()
150 {
151 m_communicator = null_communicator;
152 m_type = T_Null;
153 }
154
155 void print(std::ostream& o) const;
156
157 friend inline std::ostream& operator<<(std::ostream& o, const Communicator& pcommunicator)
158 {
159 pcommunicator.print(o);
160 return o;
161 }
162
163 //! \internal
164 Int32 _type() const { return m_type; }
165
166 private:
167
168 int m_type = T_Null;
169 _Communicator m_communicator;
170 static _Communicator null_communicator;
171 static int null_communicator_type;
172};
173
174/*---------------------------------------------------------------------------*/
175/*---------------------------------------------------------------------------*/
176
177} // namespace Arcane::MessagePassing
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
181
182#endif
General declarations for the 'message_passing' component.
Management of references to a C++ class.
Communicator for message exchange.
bool isValid() const
Indicates if the communicator is valid.
Declarations of types and methods used by message exchange mechanisms.