Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ItemSharedInfoList.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/* ItemSharedInfoList.cc (C) 2000-2022 */
9/* */
10/* List of 'ItemSharedInfo'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/FatalErrorException.h"
15#include "arcane/utils/ITraceMng.h"
16
17#include "arcane/core/ISubDomain.h"
18#include "arcane/core/IMesh.h"
19#include "arcane/core/IMeshSubMeshTransition.h"
20#include "arcane/core/VariableTypes.h"
21
22#include "arcane/mesh/ItemSharedInfoList.h"
23#include "arcane/mesh/ItemFamily.h"
24
25#include <map>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane::mesh
31{
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36ItemSharedInfoWithType::
37ItemSharedInfoWithType(ItemSharedInfo* shared_info, ItemTypeInfo* item_type)
38: m_shared_info(shared_info)
39, m_type_id(item_type->typeId())
40{
41}
42
43ItemSharedInfoWithType::
44ItemSharedInfoWithType(ItemSharedInfo* shared_info, ItemTypeInfo* item_type, Int32ConstArrayView buffer)
45: m_shared_info(shared_info)
46, m_type_id(item_type->typeId())
47{
48 // The buffer size depends on the versions of Arcane.
49 // Before 3.2 (October 2021), the buffer size is 9 (non-AMR) or 13 (AMR)
50 // Between 3.2 and 3.6 (May 2022), the size is always 13
51 // Starting from 3.6, the size is 6.
52 //
53 // We are not trying to recover with versions
54 // of Arcane prior to 3.2, so we can assume that the size
55 // of the buffer is 13. These versions use the new connectivity
56 // and therefore the number of elements is always 0 (as well as the *allocated)
57 // except for m_nb_node.
58 //
59 // Starting from 3.6, the number of nodes is no longer used
60 // and is always 0. We can therefore delete these fields from ItemSharedInfo
61 // for the end of 2022 versions.
62
63 // TODO: Indicate that starting from version 3.7 we only support
64 // buf_size==6 with version number 0x0307
65 Int32 buf_size = buffer.size();
66 if (buf_size != 6)
67 ARCANE_FATAL("Invalid buf size '{0}'. This is probably because your checkpoint is from a version of Arcane which is too old (before 3.6)",
68 buf_size);
69 m_index = buffer[2];
70 m_nb_reference = buffer[3];
71}
72
73/*---------------------------------------------------------------------------*/
74/*---------------------------------------------------------------------------*/
75
76void ItemSharedInfoWithType::
77serializeWrite(Int32ArrayView buffer)
78{
79 buffer[0] = m_type_id; // Must always be the first
80
81 buffer[1] = 0x0307; // Version number (3.7).
82
83 buffer[2] = m_index;
84 buffer[3] = m_nb_reference;
85
86 buffer[4] = 0; // Reserved
87 buffer[5] = 0; // Reserved
88}
89
90/*---------------------------------------------------------------------------*/
91/*---------------------------------------------------------------------------*/
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
97{
98 public:
99
100 explicit ItemNumElements(Integer type)
101 : m_type(type)
102 {}
103
104 public:
105
106 static bool m_debug;
107
108 Integer m_type;
109
110 bool operator<(const ItemNumElements& b) const
111 {
112#ifdef ARCANE_CHECK
113 if (m_debug) {
114 cout << "Compare:\nTHIS=";
115 print(cout);
116 cout << "\nTO=";
117 b.print(cout);
118 cout << "\nVAL=" << compare(b);
119 cout << "\n";
120 }
121#endif
122 return compare(b);
123 }
124
125 private:
126
127 inline bool compare(const ItemNumElements& b) const
128 {
129 return m_type < b.m_type;
130 }
131
132 public:
133
134 void print(std::ostream& o) const
135 {
136 o << " Type=" << m_type;
137 }
138};
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143bool ItemSharedInfoList::ItemNumElements::m_debug = false;
144
145/*---------------------------------------------------------------------------*/
146/*---------------------------------------------------------------------------*/
147
148std::ostream& operator<<(std::ostream& o, const ItemSharedInfoList::ItemNumElements& v)
149{
150 v.print(o);
151 return o;
152}
153
154/*---------------------------------------------------------------------------*/
155/*---------------------------------------------------------------------------*/
156
158{
159 public:
160
161 Variables(IMesh* mesh, const String& name)
162 : m_infos_values(VariableBuildInfo(mesh, name))
163 {}
164
165 public:
166
167 VariableArray2Int32 m_infos_values;
168};
169
170/*---------------------------------------------------------------------------*/
171/*---------------------------------------------------------------------------*/
172
173/*---------------------------------------------------------------------------*/
174/*---------------------------------------------------------------------------*/
175
176ItemSharedInfoList::
177ItemSharedInfoList(ItemFamily* family, ItemSharedInfo* common_shared_info)
178: TraceAccessor(family->traceMng())
179, m_family(family)
180, m_common_item_shared_info(common_shared_info)
181, m_item_kind(family->itemKind())
182, m_item_shared_infos_buffer(new MultiBufferT<ItemSharedInfoWithType>(100))
183, m_infos_map(new ItemSharedInfoMap())
184{
185 {
186 String var_name(family->name());
187 var_name = var_name + "_SharedInfoList";
188 m_variables = new Variables(family->mesh(), var_name);
189 }
190}
191
192/*---------------------------------------------------------------------------*/
193/*---------------------------------------------------------------------------*/
194
197{
198 delete m_variables;
199 delete m_infos_map;
200 delete m_item_shared_infos_buffer;
201}
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
205
206void ItemSharedInfoList::
207prepareForDump()
208{
209 Integer n = m_item_shared_infos.size();
210 info(4) << "ItemSharedInfoList: write: " << m_family->name()
211 << " count=" << n << " changed=" << m_list_changed;
212 log() << "ItemSharedInfoList: write: " << m_family->name()
213 << " count=" << n << " changed=" << m_list_changed;
214
215 if (!m_list_changed)
216 return;
217 m_list_changed = false;
218 //Integer n = m_item_shared_infos.size();
219 Integer element_size = ItemSharedInfoWithType::serializeSize();
220 m_variables->m_infos_values.resize(n, element_size);
221 for (Integer i = 0; i < n; ++i) {
222 m_item_shared_infos[i]->serializeWrite(m_variables->m_infos_values[i]);
223 // if (i<20 && m_family->itemKind()==IK_Particle){
224 // ItemSharedInfo* isi = m_item_shared_infos[i];
225 // log() << "FAMILY" << m_family->name() << " ISI: i=" << i << " values=" << *isi;
226 // info() << "FAMILY" << m_family->name() << "ISI: i=" << i << " values=" << *isi;
227 // }
228 }
229}
230
231/*---------------------------------------------------------------------------*/
232/*---------------------------------------------------------------------------*/
233
234void ItemSharedInfoList::
235readFromDump()
236{
237 Integer element_size = ItemSharedInfoWithType::serializeSize();
238 Integer n = m_variables->m_infos_values.dim1Size();
239 info() << "ItemSharedInfoList: read: " << m_family->name() << " count=" << n;
240
241 if (n > 0) {
242 // The number of saved elements depends on the Arcane version and whether
243 // AMR is used.
244 Integer stored_size = m_variables->m_infos_values[0].size();
245 if (stored_size == ItemSharedInfoWithType::serializeSize()) {
246 }
247 else if (stored_size != element_size) {
248 // We can only read older versions (before 3.6)
249 // whose size is 13 (with AMR) or 9 (without AMR), which corresponds
250 // to Arcane versions from 2021.
251 if (stored_size != 13 && stored_size != 9)
252 ARCANE_FATAL("Incoherence of saved data (most probably due to a"
253 " difference of versions between the protection and the executable."
254 " stored_size={0} element_size={1} count={2}",
255 stored_size, element_size, n);
256 }
257 }
258
259 // All types will be added back to the list
260 m_item_shared_infos.clear();
261 m_infos_map->clear();
262
263 if (n == 0)
264 return;
265
266 for (Integer i = 0; i < n; ++i)
267 allocOne();
268
269 ItemTypeMng* itm = m_family->mesh()->itemTypeMng();
270 for (Integer i = 0; i < n; ++i) {
271 Int32ConstArrayView buffer(m_variables->m_infos_values[i]);
272 // The first element of the buffer always contains the entity type
273 ItemTypeInfo* it = itm->typeFromId(buffer[0]);
274 ItemSharedInfoWithType* isi = m_item_shared_infos[i];
275 *isi = ItemSharedInfoWithType(m_common_item_shared_info, it, buffer);
276
277 ItemNumElements ine(it->typeId());
278 std::pair<ItemSharedInfoMap::iterator, bool> old = m_infos_map->insert(std::make_pair(ine, isi));
279 if (!old.second) {
280 // Check that the added instance does not replace an already present instance,
281 // which is an internal error (faulty comparison operator)
282 dumpSharedInfos();
283 ItemNumElements::m_debug = true;
284 bool compare = m_infos_map->find(ine) != m_infos_map->end();
285 fatal() << "INTERNAL: ItemSharedInfoList::readfromDump(): SharedInfo already present family=" << m_family->name()
286 << "\nWanted:"
287 << " type=" << it->typeId()
288 << " compare=" << compare
289 << "\nNEW_INE=(" << ine << ")"
290 << "\nOLD_INE=(" << old.first->first << ")"
291 << "\nNEW_ISI=(" << *isi << ")"
292 << "\nOLD_ISI=(" << *old.first->second << ")";
293 }
294 }
295}
296
297/*---------------------------------------------------------------------------*/
298/*---------------------------------------------------------------------------*/
299
302{
303 bool has_error = false;
304
305 info() << "ItemSharedInfoList: check valid family=" << m_family->name()
306 << " count=" << m_nb_item_shared_info
307 << " list=" << m_item_shared_infos.size()
308 << " free=" << m_free_item_shared_infos.size()
309 << " changed=" << m_list_changed;
310
311 // Firstly, item->localId() must correspond to the index
312 // in the m_internal array
313 for (Integer i = 0, n = m_item_shared_infos.size(); i < n; ++i) {
314 ItemSharedInfoWithType* item = m_item_shared_infos[i];
315 if (item->index() != i) {
316 error() << "The index (" << item->index() << ") from the list 'ItemSharedInfo' "
317 << "of the family " << m_family->name() << " is not "
318 << "coherent with its internal value (" << i << ")";
319 has_error = true;
320 }
321 }
322 if (has_error)
323 ARCANE_FATAL("Internal error with the mesh structure");
324}
325
326/*---------------------------------------------------------------------------*/
327/*---------------------------------------------------------------------------*/
328
329ItemSharedInfoWithType* ItemSharedInfoList::
330findSharedInfo(ItemTypeInfo* type)
331{
332 ItemNumElements ine(type->typeId());
333 ItemSharedInfoMap::const_iterator i = m_infos_map->find(ine);
334 if (i != m_infos_map->end())
335 return i->second;
336 // Info not found. We build a new one
337 ItemSharedInfoWithType* isi = allocOne();
338 Integer old_index = isi->index();
339 *isi = ItemSharedInfoWithType(m_common_item_shared_info, type);
340 isi->setIndex(old_index);
341 std::pair<ItemSharedInfoMap::iterator, bool> old = m_infos_map->insert(std::make_pair(ine, isi));
342
343 //#ifdef ARCANE_CHECK
344 if (!old.second) {
345 // Check that the added instance does not replace an already present instance,
346 // which is an internal error (faulty comparison operator)
347 dumpSharedInfos();
348 ItemNumElements::m_debug = true;
349 bool compare = m_infos_map->find(ine) != m_infos_map->end();
350 fatal() << "INTERNAL: ItemSharedInfoList::findSharedInfo() SharedInfo already present\n"
351 << "\nWanted:"
352 << " type=" << type->typeId()
353 << " compare=" << compare
354 << "\nNEW_INE=(" << ine << ")"
355 << "\nOLD_INE=(" << old.first->first << ")"
356 << "\nNEW_ISI=(" << *isi << ")"
357 << "\nOLD_ISI=(" << *old.first->second << ")";
358 }
359 //#endif
360 return isi;
361}
362
363/*---------------------------------------------------------------------------*/
364/*---------------------------------------------------------------------------*/
365
366void ItemSharedInfoList::
367dumpSharedInfos()
368{
369 info() << "--- ItemSharedInfos: family=" << m_family->name();
370 for (ConstIterT<ItemSharedInfoMap> i(*m_infos_map); i(); ++i) {
371 info() << "INE: " << i->first;
372 info() << "ISI: " << *i->second;
373 }
374}
375
376/*---------------------------------------------------------------------------*/
377/*---------------------------------------------------------------------------*/
378
379} // End namespace Arcane::mesh
380
381/*---------------------------------------------------------------------------*/
382/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
constexpr Integer dim1Size() const
Number of elements in the first dimension.
Internal shared structure of a mesh entity.
Info on a mesh entity type.
Buffer for multiple allocation.
Definition MultiBuffer.h:45
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
TraceMessage fatal() const
Flow for a fatal error message.
TraceMessage log() const
Flow for a log message.
TraceMessage info() const
Flow for an information message.
TraceMessage error() const
Flow for an error message.
ITraceMng * traceMng() const
Trace manager.
Parameters necessary for building a variable.
virtual void resize(Integer new_size)
Reallocates the number of elements in the first dimension of the array.
IMesh * mesh() const override
Associated mesh.
String name() const override
Family name.
Definition ItemFamily.h:140
Integer m_nb_item_shared_info
Number of allocated objects.
void checkValid()
Checks if the internal structures of the instance are valid.
Temporary class to hold an ItemSharedInfo and an entity type.
VariableRefArray2T< Int32 > VariableArray2Int32
Two-dimensional array variable of 32-bit integer type.
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482