Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
DataSynchronizeInfo.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/* DataSynchronizeInfo.cc (C) 2000-2024 */
9/* */
10/* Information for synchronizing data. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/impl/DataSynchronizeInfo.h"
15
16#include "arcane/utils/FatalErrorException.h"
17#include "arcane/utils/PlatformUtils.h"
18#include "arcane/utils/IMemoryRessourceMng.h"
19#include "arcane/utils/MemoryView.h"
20#include "arcane/utils/SmallArray.h"
21#include "arcane/utils/ITraceMng.h"
22#include "arcane/utils/TraceAccessor.h"
23#include "arcane/utils/ValueConvert.h"
24
25#include "arcane/core/VariableCollection.h"
26#include "arcane/core/ParallelMngUtils.h"
27#include "arcane/core/IParallelExchanger.h"
28#include "arcane/core/ISerializeMessage.h"
29#include "arcane/core/ISerializer.h"
30#include "arcane/core/IParallelMng.h"
31#include "arcane/core/IData.h"
32#include "arcane/core/internal/IParallelMngInternal.h"
33#include "arcane/core/internal/IDataInternal.h"
34
35#include "arcane/accelerator/core/Runner.h"
36
37#include "arcane/impl/IDataSynchronizeBuffer.h"
38
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
42namespace Arcane
43{
44
45/*---------------------------------------------------------------------------*/
46/*---------------------------------------------------------------------------*/
47
48// TODO: instead of using managed memory, it is preferable to have
49// a copy of the IDs on the device. This will prevent transfers
50// potentially if variable synchronization is mixed across accelerators and
51// on CPU.
52
53VariableSyncInfo::
54VariableSyncInfo()
55: m_share_ids(platform::getDefaultDataAllocator())
56, m_ghost_ids(platform::getDefaultDataAllocator())
57{
58}
59
60/*---------------------------------------------------------------------------*/
61/*---------------------------------------------------------------------------*/
62
63VariableSyncInfo::
64VariableSyncInfo(Int32ConstArrayView share_ids, Int32ConstArrayView ghost_ids,
65 Int32 rank)
66: VariableSyncInfo()
67{
68 m_target_rank = rank;
69 m_share_ids.copy(share_ids);
70 m_ghost_ids.copy(ghost_ids);
71}
72
73/*---------------------------------------------------------------------------*/
74/*---------------------------------------------------------------------------*/
75
76VariableSyncInfo::
77VariableSyncInfo(const VariableSyncInfo& rhs)
78: VariableSyncInfo()
79{
80 // NOTE: for the time being (April 2023) an explicit copy constructor
81 // is needed to specify the allocator
82 m_target_rank = rhs.m_target_rank;
83 m_share_ids.copy(rhs.m_share_ids);
84 m_ghost_ids.copy(rhs.m_ghost_ids);
85}
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
90void VariableSyncInfo::
91_changeIds(Array<Int32>& ids, Int32ConstArrayView old_to_new_ids)
92{
93 UniqueArray<Int32> orig_ids(ids);
94 ids.clear();
95
96 for (Integer z = 0, zs = orig_ids.size(); z < zs; ++z) {
97 Int32 old_id = orig_ids[z];
98 Int32 new_id = old_to_new_ids[old_id];
99 if (new_id != NULL_ITEM_LOCAL_ID)
100 ids.add(new_id);
101 }
102}
103
104/*---------------------------------------------------------------------------*/
105/*---------------------------------------------------------------------------*/
106
107void VariableSyncInfo::
108changeLocalIds(Int32ConstArrayView old_to_new_ids)
109{
110 _changeIds(m_share_ids, old_to_new_ids);
111 _changeIds(m_ghost_ids, old_to_new_ids);
112}
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
116
117/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
119
120void DataSynchronizeInfo::
121recompute()
122{
123 Integer nb_message = this->size();
124
125 DataSynchronizeBufferInfoList& receive_info = _receiveInfo();
126 DataSynchronizeBufferInfoList& send_info = _sendInfo();
127
128 receive_info.m_displacements_base.resize(nb_message);
129 send_info.m_displacements_base.resize(nb_message);
130
131 receive_info.m_total_nb_item = 0;
132 send_info.m_total_nb_item = 0;
133
134 {
135 Integer ghost_displacement = 0;
136 Integer share_displacement = 0;
137 Int32 index = 0;
138 for (const VariableSyncInfo& vsi : m_ranks_info) {
139 Int32 ghost_size = vsi.nbGhost();
140 receive_info.m_displacements_base[index] = ghost_displacement;
141 ghost_displacement += ghost_size;
142 Int32 share_size = vsi.nbShare();
143 send_info.m_displacements_base[index] = share_displacement;
144 share_displacement += share_size;
145 ++index;
146 }
147 receive_info.m_total_nb_item = ghost_displacement;
148 send_info.m_total_nb_item = share_displacement;
149 }
150}
151
152/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
154
155void DataSynchronizeInfo::
156changeLocalIds(Int32ConstArrayView old_to_new_ids)
157{
158 for (VariableSyncInfo& vsi : m_ranks_info) {
159 vsi.changeLocalIds(old_to_new_ids);
160 }
161 recompute();
162}
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167void DataSynchronizeInfo::
168add(const VariableSyncInfo& s)
169{
170 m_ranks_info.add(s);
171 m_communicating_ranks.add(s.targetRank());
172}
173
174/*---------------------------------------------------------------------------*/
175/*---------------------------------------------------------------------------*/
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
179
180ConstArrayView<Int32> DataSynchronizeBufferInfoList::
181localIds(Int32 index) const
182{
183 const VariableSyncInfo& s = m_sync_info->m_ranks_info[index];
184 return (m_is_share) ? s.shareIds() : s.ghostIds();
185}
186
187/*---------------------------------------------------------------------------*/
188/*---------------------------------------------------------------------------*/
189
190Int32 DataSynchronizeBufferInfoList::
191nbItem(Int32 index) const
192{
193 const VariableSyncInfo& s = m_sync_info->m_ranks_info[index];
194 return (m_is_share) ? s.nbShare() : s.nbGhost();
195}
196
197/*---------------------------------------------------------------------------*/
198/*---------------------------------------------------------------------------*/
199
200} // End namespace Arcane
201
202/*---------------------------------------------------------------------------*/
203/*---------------------------------------------------------------------------*/
void resize(Int64 s)
Changes the number of elements in the array to s.
Constant view of an array of type T.
bool m_is_share
If true, it is the send buffer, otherwise it is the receive buffer.
UniqueArray< Int64 > m_displacements_base
Offsets in the global buffer for each rank.
void recompute()
Notifies the instance that the values have changed.
Information about the list of shared/ghost entities for a given rank for a synchronization.
ConstArrayView< Int32 > ghostIds() const
localIds() of entities to receive from rank targetRank()
ConstArrayView< Int32 > shareIds() const
localIds() of entities to send to rank targetRank()
UniqueArray< Int32 > m_share_ids
localIds() of entities to send to processor m_rank
Int32 nbGhost() const
Number of ghost entities.
Int32 targetRank() const
Target processor rank.
Int32 nbShare() const
Number of shared entities.
UniqueArray< Int32 > m_ghost_ids
localIds() of entities to receive from processor m_rank
Namespace for platform-dependent functions.
IMemoryAllocator * getDefaultDataAllocator()
Default allocator for data.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482
std::int32_t Int32
Signed integer type of 32 bits.