Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
Parallel.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/* Parallel.cc (C) 2000-2025 */
9/* */
10/* Namespace for types managing parallelism. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15
16#include "arcane/utils/String.h"
17#include "arcane/utils/ArrayView.h"
18#include "arcane/utils/FatalErrorException.h"
19#include "arcane/utils/PlatformUtils.h"
20#include "arcane/utils/ITraceMng.h"
21#include "arcane/utils/Math.h"
22
23#include "arcane/core/IParallelMng.h"
24#include "arcane/core/IParallelMng.h"
25#include "arcane/core/IParallelTopology.h"
26#include "arcane/core/IParallelReplication.h"
27#include "arcane/core/SerializeBuffer.h"
28
29#include <iostream>
30#include <map>
31#include <set>
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
41
42/*---------------------------------------------------------------------------*/
43/*---------------------------------------------------------------------------*/
44
45namespace Arcane
46{
47
48/*---------------------------------------------------------------------------*/
49/*---------------------------------------------------------------------------*/
50
53{
54 return getMPICommunicator();
55}
56
57/*---------------------------------------------------------------------------*/
58/*---------------------------------------------------------------------------*/
59
60extern "C++" void MessagePassing::
61namedBarrier(IParallelMng* pm, const String& name)
62{
63 // Copies the first 1024 characters of name into an Int32 array
64 // and performs a 'max' reduction on this array. If the reduction is different
65 // from our value, it means that one of the ranks does not have the correct barrier.
67
68 const int nsize = 256;
69 Int32 sbuf[nsize];
70 Int32 max_sbuf[nsize];
71 Int32ArrayView buf(nsize, sbuf);
72 Int32ArrayView max_buf(nsize, max_sbuf);
73 ByteArrayView buf_as_bytes(nsize * sizeof(Int32), (Byte*)sbuf);
74 buf.fill(0);
76 ByteConstArrayView name_as_bytes(len, (const Byte*)name.localstr());
77 Integer name_len = math::min(buf_as_bytes.size(), name_as_bytes.size());
78 buf_as_bytes.copy(name_as_bytes.subView(0, name_len));
79 buf[nsize - 1] = 0;
80 max_buf.copy(buf);
81 pm->reduce(Parallel::ReduceMax, max_buf);
82
83 if (max_buf != buf) {
84 max_buf[nsize - 1] = 0;
85 String max_string((const char*)max_buf.unguardedBasePointer());
86 ARCANE_FATAL("Bad namedBarrier expected='{0}' found='{1}'",
87 name, max_string);
88 }
89}
90
91/*---------------------------------------------------------------------------*/
92/*---------------------------------------------------------------------------*/
93
94extern "C++" void MessagePassing::
96 Array<String>& common_strings)
97{
98 const Int32 nb_string = input_strings.size();
99
100 // Create a buffer to serialize the names of the variables we have
101 SerializeBuffer send_buf;
102 send_buf.setMode(ISerializer::ModeReserve);
103 send_buf.reserve(DT_Int32, 1);
104 for (Integer i = 0; i < nb_string; ++i) {
105 send_buf.reserve(input_strings[i]);
106 }
107
108 send_buf.allocateBuffer();
110 send_buf.putInt32(nb_string);
111 for (Integer i = 0; i < nb_string; ++i) {
112 send_buf.put(input_strings[i]);
113 }
114
115 // Retrieve information from other PEs.
116 SerializeBuffer recv_buf;
117 pm->allGather(&send_buf, &recv_buf);
118
119 std::map<String, Int32> string_occurences;
120
121 Int32 nb_rank = pm->commSize();
123 for (Integer i = 0; i < nb_rank; ++i) {
124 Int32 nb_string_rank = recv_buf.getInt32();
125 for (Integer z = 0; z < nb_string_rank; ++z) {
126 String x;
127 recv_buf.get(x);
128 auto vo = string_occurences.find(x);
129 if (vo == string_occurences.end())
130 string_occurences.insert(std::make_pair(x, 1));
131 else
132 vo->second = vo->second + 1;
133 }
134 }
135
136 // Iterates through the list of characteristic strings and ranges into \a out_strings
137 // those that are available on all ranks of \a pm
138 std::set<String> common_set;
139 {
140 auto end_iter = string_occurences.end();
141 for (Integer i = 0; i < nb_string; ++i) {
142 String str = input_strings[i];
143 auto i_str = string_occurences.find(str);
144 if (i_str == end_iter)
145 // Should not happen
146 continue;
147 if (i_str->second != nb_rank)
148 continue;
149 common_set.insert(str);
150 }
151 }
152
153 // Create the final list by iterating over \a common_set
154 // which is sorted alphabetically.
155 common_strings.clear();
156 for (const String& s : common_set)
157 common_strings.add(s);
158}
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
165{
168
169 Real mem_used = platform::getMemoryUsed();
170 Real mem_sum = 0.0;
171 Real mem_min = 0.0;
172 Real mem_max = 0.0;
173 Int32 mem_min_rank = 0;
174 Int32 mem_max_rank = 0;
175 pm->computeMinMaxSum(mem_used, mem_min, mem_max, mem_sum, mem_min_rank, mem_max_rank);
176 tm->info() << "Date: " << platform::getCurrentDateTime() << " MEM=" << (Int64)(mem_used / 1e6)
177 << " MAX_MEM=" << (Int64)(mem_max / 1e6)
178 << " MIN_MEM=" << (Int64)(mem_min / 1e6)
179 << " AVG_MEM=" << (Int64)(mem_sum / 1e6) / pm->commSize()
180 << " MIN_RANK=" << mem_min_rank << " MAX_RANK=" << mem_max_rank;
181}
182
183/*---------------------------------------------------------------------------*/
184/*---------------------------------------------------------------------------*/
185
186} // End namespace Arcane
187
188/*---------------------------------------------------------------------------*/
189/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro returning the pointer ptr if it is not null or throwing an exception if it is null.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
File containing declarations concerning the message passing model.
void copy(const U &copy_array)
Copies the array copy_array into the instance.
void fill(const T &o) noexcept
Fills the array with the value o.
constexpr pointer unguardedBasePointer() noexcept
Pointer to the beginning of the view.
constexpr Integer size() const noexcept
Returns the size of the array.
Base class for 1D data vectors.
void clear()
Removes the elements from the array.
void add(ConstReferenceType val)
Adds element val to the end of the array.
void put(Span< const Real > values) override
Add the array values.
void reserve(eBasicDataType dt, Int64 n) override
Reserves memory for n objects of type dt.
void putInt32(Int32 value) override
Add the integer value.
void allocateBuffer() override
Allocates the serializer memory.
void get(ArrayView< Real > values) override
Retrieve the array values.
Int32 getInt32() override
Retrieve an integer.
void setMode(eMode new_mode) override
Sets the current mode.
Constant view of an array of type T.
constexpr ConstArrayView< T > subView(Integer abegin, Integer asize) const noexcept
Sub-view (constant) starting from element abegin and containing asize elements.
constexpr Integer size() const noexcept
Number of elements in the array.
Interface of the parallelism manager for a subdomain.
virtual void computeMinMaxSum(char val, char &min_val, char &max_val, char &sum_val, Int32 &min_rank, Int32 &max_rank)=0
Calculates the sum, min, and max of a value in one operation.
virtual Int32 commSize() const =0
Number of instances in the communicator.
virtual void allGather(ConstArrayView< char > send_buf, ArrayView< char > recv_buf)=0
Performs an all-gather operation across all processors. This is a collective operation....
virtual ARCANE_DEPRECATED_120 void * mpiCommunicator()
Address of the MPI communicator associated with this manager.
Definition Parallel.cc:52
virtual void * getMPICommunicator()=0
Address of the MPI communicator associated with this manager.
virtual char reduce(eReduceType rt, char v)=0
Performs a reduction of type rt on the real v and returns the value.
virtual TraceMessage info()=0
Stream for an information message.
Implementation of a buffer for serialization.
Int64 length() const
Returns the length of the string.
Definition String.cc:340
const char * localstr() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:229
__host__ __device__ Real2 min(Real2 a, Real2 b)
Returns the minimum of two Real2.
Definition MathUtils.h:346
void namedBarrier(IParallelMng *pm, const String &name)
Performs a named barrier with name name.
Definition Parallel.cc:61
void dumpDateAndMemoryUsage(IParallelMng *pm, ITraceMng *tm)
Writes the date and memory consumed into tm.
Definition Parallel.cc:164
void filterCommonStrings(IParallelMng *pm, ConstArrayView< String > input_strings, Array< String > &common_strings)
Filters strings common to all ranks of pm.
Definition Parallel.cc:95
@ ReduceMax
Maximum of values.
double getMemoryUsed()
Memory used in bytes.
String getCurrentDateTime()
Current date and time in ISO 8601 format.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Integer arcaneCheckArraySize(unsigned long long size)
Checks that size can be converted into an 'Integer' to serve as the size of an array....
ArrayView< Byte > ByteArrayView
C equivalent of a 1D array of characters.
Definition UtilsTypes.h:447
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
ArrayView< Int32 > Int32ArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:453
double Real
Type representing a real number.
ConstArrayView< Byte > ByteConstArrayView
C equivalent of a 1D array of characters.
Definition UtilsTypes.h:476
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:43
@ DT_Int32
32-bit integer data type
Definition DataTypes.h:45
std::int32_t Int32
Signed integer type of 32 bits.