14#include "arcane/utils/FatalErrorException.h"
15#include "arcane/utils/PlatformUtils.h"
16#include "arcane/utils/NotImplementedException.h"
17#include "arcane/utils/StringBuilder.h"
19#include "arcane/core/IMesh.h"
20#include "arcane/core/IItemFamily.h"
22#include "arcane/core/IParallelMng.h"
23#include "arcane/core/ItemPrinter.h"
24#include "arcane/core/VariableTypes.h"
25#include "arcane/core/Timer.h"
26#include "arcane/core/IItemInternalSortFunction.h"
27#include "arcane/core/ItemFamilyCompactInfos.h"
28#include "arcane/core/IMeshCompacter.h"
29#include "arcane/core/MeshPartInfo.h"
30#include "arcane/core/ItemFamilyItemListChangedEventArgs.h"
32#include "arcane/mesh/DynamicMeshKindInfos.h"
33#include "arcane/mesh/ItemFamily.h"
58, m_use_new_finalize(true)
59, m_is_first_finalize(true)
60, m_has_unique_id_map(true)
78void DynamicMeshKindInfos::
94 m_use_new_finalize =
false;
97 m_is_first_finalize =
true;
115 <<
" internal size= " << internal_size
118 for(
Integer i=0; i<internal_size; ++i ){
120 info() <<
"Item " << i <<
" lib=" << item->
localId()
128 Integer nb_added_items = m_added_items.size();
129 Integer nb_removed_items = m_removed_items.size();
130 bool test_mode = m_use_new_finalize;
134 <<
" internal size " << internal_size
135 <<
" nb_add=" << nb_added_items
136 <<
" nb_remove=" << nb_removed_items;
144 if (!pm->
isParallel() && nb_added_items != 0) {
146 for (
Integer i = 0; i < nb_added_items; ++i)
153 impl.beginTransaction();
154 if (nb_added_items == 0 && nb_removed_items != 0 && test_mode) {
155 impl.removeItems(m_removed_items,
true);
157 else if (nb_added_items != 0 && nb_removed_items == 0 && test_mode) {
158 impl.addItems(m_added_items,
true);
161 impl.removeAddItems(m_removed_items, m_added_items,
true);
163 impl.endTransaction();
166 impl.checkNeedUpdate();
168#if defined(ARCANE_DEBUG_MESH)
170 m_all_group.checkValid();
176#if defined(ARCANE_DEBUG_MESH)
181 m_removed_items.clear();
182 m_added_items.clear();
188void DynamicMeshKindInfos::
191 if (!m_has_unique_id_map)
196 Int64 unique_id = ids[i];
202 Int64 unique_id = ids[i];
210 Int64 unique_id = ids[i];
212 if ((ids[i] == NULL_ITEM_LOCAL_ID) && do_fatal) {
214 error() <<
"DynamicMeshKindInfos::itemsUniqueIdToLocalId() can't find "
216 << unique_id <<
" in the subdomain.";
231void DynamicMeshKindInfos::
235 if (!m_has_unique_id_map)
239 for (
Integer i = 0, s = unique_ids.size(); i < s; ++i) {
240 Int64 unique_id = unique_ids[i];
241 local_ids[i] = (unique_id == NULL_ITEM_UNIQUE_ID) ? NULL_ITEM_LOCAL_ID :
m_items_map.findLocalId(unique_id);
245 for (
Integer i = 0, s = unique_ids.size(); i < s; ++i) {
246 Int64 unique_id = unique_ids[i];
247 local_ids[i] =
m_items_map.tryFindLocalId(unique_id);
253 for (
Integer i = 0, s = unique_ids.size(); i < s; ++i) {
254 Int64 unique_id = unique_ids[i];
255 local_ids[i] =
m_items_map.tryFindLocalId(unique_id);
256 if ((local_ids[i] == NULL_ITEM_LOCAL_ID) && do_fatal && unique_id != NULL_ITEM_UNIQUE_ID) {
258 error() <<
"DynamicMeshKindInfos::itemsUniqueIdToLocalId() can't find "
260 << unique_id <<
" in the subdomain.";
275void DynamicMeshKindInfos::
277 ConstArrayView<ItemUniqueId> unique_ids,
bool do_fatal)
const
279 if (!m_has_unique_id_map)
283 for (
Integer i = 0, s = unique_ids.size(); i < s; ++i) {
284 Int64 unique_id = unique_ids[i];
289 for (
Integer i = 0, s = unique_ids.size(); i < s; ++i) {
290 Int64 unique_id = unique_ids[i];
291 local_ids[i] =
m_items_map.tryFindLocalId(unique_id);
297 for (
Integer i = 0, s = unique_ids.size(); i < s; ++i) {
298 Int64 unique_id = unique_ids[i];
299 local_ids[i] =
m_items_map.tryFindLocalId(unique_id);
300 if ((local_ids[i] == NULL_ITEM_LOCAL_ID) && do_fatal) {
302 error() <<
"DynamicMeshKindInfos::itemsUniqueIdToLocalId() can't find "
304 << unique_id <<
" in the subdomain.";
334 _updateItemSharedInfoInternalView();
339 m_free_internals_in_multi_buffer.clear();
358 for (
Integer i = 0; i < nb_internal; ++i) {
360 if (item->
localId() != i && nb_error < 10) {
361 error() <<
"The local id (" << item->
localId() <<
") of the entity "
363 <<
"consistent with its internal value (" << i <<
")";
370 if (item->
owner() == NULL_SUB_DOMAIN_ID && nb_error < 10) {
372 <<
":uid=" << item->
uniqueId() <<
" belongs to no subdomain";
376 if (item->
owner() != sid && item->
isOwn() && nb_error < 10) {
378 <<
":uid=" << item->
uniqueId() <<
" incoherence between isOwn() and owner()"
379 <<
" " << item->
owner() <<
' ' << sid;
384 if (nb_killed_item != nb_free) {
386 <<
": incoherence between killed and free entities"
387 <<
" free=" << nb_free
388 <<
" killed=" << nb_killed_item
389 <<
" internal=" << nb_internal
395 ARCANE_FATAL(
"Internal error in the mesh structure mesh={0} part={1}",
403class ItemParticleCompareWithSuppression
435 return item1.m_unique_id < item2.m_unique_id;
482 bool do_sort = compact_infos.compacter()->
isSorted();
488 info(4) <<
m_kind_name <<
" beginCompactItems() Family Internal Size=" << nb_internal
491 <<
" do_sort=" << do_sort;
493 if (nb_internal == 0)
500 old_to_new_local_ids.
fill(NULL_ITEM_LOCAL_ID);
502 new_to_old_local_ids.
fill(NULL_ITEM_LOCAL_ID);
510 const bool print_infos =
false;
516 for (
Integer i = 0, is = items.
size(); i < is; ++i) {
517 info() <<
"Before Sort: " << i <<
" uid=" << items[i]->uniqueId()
518 <<
" lid=" << items[i]->localId()
519 <<
" destroyed?=" << items[i]->isSuppressed();
528 <<
" sort_func_name=" << sort_func->
name();
531 for (
Integer i = 0, n = items.
size(); i < n; ++i) {
532 info() <<
"After Sort: " << i <<
" uid=" << items[i]->uniqueId()
533 <<
" lid=" << items[i]->localId()
534 <<
" destroyed?=" << items[i]->isSuppressed();
539#ifdef ARCANE_TEST_ADD_MESH
540 typedef std::set<ItemInternal*, ItemCompare> ItemSet;
545 ItemSet::const_iterator b = items_set.begin();
546 ItemSet::const_iterator e = items_set.end();
547 for (
Integer z = 0; b != e; ++b, ++z)
553 Integer current_local_id = items[i]->localId();
556 old_to_new_local_ids[current_local_id] = i;
557 new_to_old_local_ids[i] = current_local_id;
561 const bool use_old =
true;
563 info(4) <<
"USE_OLD_METHOD";
567 Integer current_local_id = existing_items[i];
568 old_to_new_local_ids[current_local_id] = i;
569 new_to_old_local_ids[i] = current_local_id;
577 for (
Integer i = 0, n = total_nb; i < n; ++i) {
579 if (old_to_new_local_ids[i] == NULL_ITEM_ID) {
581 old_to_new_local_ids[i] = old_index;
589 if (old_index != total_nb) {
590 ARCANE_FATAL(
"Family '{0}' bad indices: expected={1} found={2} (nb_internal={3})",
591 itemFamily()->name(), old_index, total_nb, nb_internal);
604 old_to_new_local_ids[i] = free_index;
609 old_to_new_local_ids[i] = new_index;
610 new_to_old_local_ids[new_index] = i;
618 ARCANE_FATAL(
"Family '{0}' bad indices: expected={1} found={2}",
619 itemFamily()->name(),
m_nb_item, new_index);
623 compact_infos.setOldToNewLocalIds(std::move(old_to_new_local_ids));
624 compact_infos.setNewToOldLocalIds(std::move(new_to_old_local_ids));
630void DynamicMeshKindInfos::
642 <<
" nb_internal=" << nb_internal
645 <<
" nb_free" << m_free_internals_in_multi_buffer.size();
647 if (nb_internal == 0)
661 info() <<
"DumpItemsBefore:";
675 new_uids[i] = old_item->
uniqueId().asInt64();
676 new_owners[i] = old_item->
owner();
677 new_flags[i] = old_item->
flags();
678 new_typeids[i] = old_item->
typeId();
679 new_items[i] = *old_item;
689 ii->setFlags(new_flags[i]);
690 ii->setOwner(new_owners[i], sid);
691 ii->setUniqueId(new_uids[i]);
692 ItemSharedInfo* isi = ItemInternalCompatibility::_getSharedInfo(ii);
693 ii->_setSharedInfo(isi, ItemTypeId(new_typeids[i]));
697 if (ii->isSuppressed()) {
699 error() <<
"Entity deleted from the list of created entities "
700 << ItemPrinter(ii) <<
" index=" << i;
715 m_free_internals_in_multi_buffer.add(removed_item);
720 _updateItemSharedInfoInternalView();
724 info() <<
"DumpItemsAfter:";
729 ARCANE_FATAL(
"Error in compacting nb_error={0}", nb_error);
731 debug() <<
"Compression: old=" << nb_internal <<
" new=" <<
m_nb_item
736 compact_infos.clear();
770 std::sort(std::begin(ids), std::end(ids), _Inverse());
779void DynamicMeshKindInfos::
780printFreeInternals(
Integer max_print)
783 max_print =
math::min(nb_free, max_print);
784 for (
Integer i = 0; i < max_print; ++i) {
785 Integer pos = (nb_free - 1) - i;
788 info() <<
"I=" << i <<
" pos=" << pos <<
" index=" << index
789 <<
" local_id=" << iitem->
localId();
802 if (m_has_unique_id_map) {
803 for (
Integer i = 0; i < nb_item; ++i) {
804 Int32 lid = local_ids[i];
807 _checkActiveItem(item);
809 _setSuppressed(item);
810 if (uids[lid] != NULL_ITEM_UNIQUE_ID)
815 for (
Integer i = 0; i < nb_item; ++i) {
816 Int32 lid = local_ids[i];
819 _checkActiveItem(item);
821 _setSuppressed(item);
826 Integer nb_removed = m_removed_items.size();
827 m_removed_items.resize(nb_removed + nb_item);
828 memcpy(m_removed_items.data() + nb_removed, local_ids.
data(),
sizeof(
Int32) * nb_item);
837void DynamicMeshKindInfos::
838setHasUniqueIdMap(
bool v)
842 m_has_unique_id_map = v;
848void DynamicMeshKindInfos::
852 ARCANE_FATAL(
"Attempting to remove an entity already deleted item={0}",
859void DynamicMeshKindInfos::
864 for (
Integer i = 0; i < nb_internal; ++i) {
869 info() <<
"Item: INDEX=" << i
870 <<
" LID=" << item->localId()
871 <<
" UID=" << item->uniqueId()
872 <<
" KILLED=" << is_suppressed;
874 info() <<
"EndOfDump: "
875 <<
" nb_internal=" << nb_internal
877 <<
" computed_nb_item= " << (nb_internal - computed_nb_item);
883void DynamicMeshKindInfos::
884_badSameUniqueId(
Int64 unique_id)
const
893void DynamicMeshKindInfos::
894_badUniqueIdMap()
const
896 if (m_has_unique_id_map)
905void DynamicMeshKindInfos::
909 m_common_item_shared_info = item_family->commonItemSharedInfo();
915void DynamicMeshKindInfos::
916_updateItemSharedInfoInternalView()
918 if (m_common_item_shared_info)
919 m_common_item_shared_info->m_items_internal =
m_internals.constView();
925EventObservableView<const ItemFamilyItemListChangedEventArgs&> DynamicMeshKindInfos::
926itemListChangedEvent()
928 return EventObservableView<const ItemFamilyItemListChangedEventArgs&>(m_item_list_change_event);
934void DynamicMeshKindInfos::
935_notifyRemove2(ItemInternal* item)
937 ItemFamilyItemListChangedEventArgs args(
m_item_family, item->localId(), item->uniqueId());
938 m_item_list_change_event.notify(args);
939 args.setIsAdd(
false);
945void DynamicMeshKindInfos::
946_notifyAdd2(ItemInternal* item,
Int64 uid)
948 ItemFamilyItemListChangedEventArgs args(
m_item_family, item->localId(), uid);
950 m_item_list_change_event.notify(args);
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Integer size() const
Number of elements in the vector.
Modifiable view of an array of type T.
constexpr Integer size() const noexcept
Returns the size of the array.
void fill(ConstReferenceType value)
Fills the array with the value value.
const T * data() const
Access to the root of the array without any protection.
constexpr const_pointer data() const noexcept
Pointer to the allocated memory.
constexpr Integer size() const noexcept
Number of elements in the array.
Interface of an entity sorting function.
virtual void sortItems(ItemInternalMutableArrayView items)=0
Sorts the entities in the array items.
virtual const String & name() const =0
Name of the sorting function.
virtual bool isSorted() const =0
Indicates whether it wishes to sort the entities in addition to compacting them.
virtual const MeshPartInfo & meshPartInfo() const =0
Mesh part information.
Interface of the parallelism manager for a subdomain.
virtual Int32 commRank() const =0
Rank of this instance in the communicator.
virtual bool isParallel() const =0
Returns true if the execution is parallel.
Interface of a timer manager.
bool isSuppressed() const
True if the entity is suppressed.
ItemUniqueId uniqueId() const
Unique number of the entity.
Int32 owner() const
Number of the owning subdomain of the entity.
Int32 flags() const
Flags of the entity.
Int16 typeId() const
Type of the entity.
bool isOwn() const
True if the entity belongs to the subdomain.
Int32 localId() const
Local number (in the subdomain) of the entity.
Information to manage the compaction of entities of a family.
Int32ConstArrayView newToOldLocalIds() const
Conversion between new and old local IDs.
Int32ConstArrayView oldToNewLocalIds() const
Conversion between old and new local IDs.
Brief: Implementation of a mesh entity group.
void setIsAllItems()
Indicates to this group that it is the group of all entities in the family.
void setLocalToSubDomain(bool v)
Sets the boolean indicating if the group is local to the subdomain.
ItemGroupImpl * internal() const
Returns the group implementation.
Internal structure of a mesh entity.
Utility class for printing information about an entity.
ItemUniqueId uniqueId() const
Unique identifier across all domains.
Buffer for multiple allocation.
Cell cell() const
Cell to which the particle belongs. You must call setCell() before calling this function....
Unicode character string constructor.
Unicode character string.
Sentinel for the timer. The sentinel associated with a timer allows it to be triggered upon its const...
@ TimerReal
Timer using real time.
Real lastActivationTime() const
Returns the time (in seconds) spent during the last activation of the timer.
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
TraceMessageDbg debug(Trace::eDebugLevel=Trace::Medium) const
Flow for a debug message.
TraceMessage info() const
Flow for an information message.
TraceMessage error() const
Flow for an error message.
ITraceMng * traceMng() const
Trace manager.
TraceMessage pwarning() const
1D data vector with value semantics (STL style).
Integer m_nb_item
Group of all entities.
String m_kind_name
Name of the entity kind (Node, Cell, ...).
Integer maxUsedLocalId() const
Largest local ID used.
MultiBufferT< ItemInternal > * m_item_internals_buffer
Buffer to store an instance of ItemInternal.
void finalizeMeshChanged()
Reallocates and recalculates structures after mesh modification.
void removeMany(Int32ConstArrayView local_ids)
Removes a list of entities.
void prepareForDump()
Prepares variables for dumps.
void beginCompactItems(ItemFamilyCompactInfos &compact_infos)
Calculates the new local IDs of the entities so that they are consecutive.
ARCANE_DEPRECATED_240 Int32ConstArrayView oldToNewLocalIds() const
Conversion between old and new local IDs.
UniqueArray< ItemInternal * > m_internals
ItemInternals of the entities.
void readFromDump()
Restores info from dumps.
DynamicMeshKindInfos(IMesh *mesh, eItemKind kind, const String &kind_name)
Creates an instance for a given mesh and kind.
ARCANE_DEPRECATED_240 Int32ConstArrayView newToOldLocalIds() const
Conversion between new and old local IDs.
ItemFamilyCompactInfos * m_compact_infos
Temporary while oldToNewLocalIds() and newToOldLocalIds() exist.
void checkValid()
Checks if the internal structures of the instance are valid.
~DynamicMeshKindInfos()
Frees resources.
String m_all_group_name
Name of the group containing all entities.
ItemInternalMap m_items_map
Hash table for uniqueId() -> ItemInternal* conversion.
Int32UniqueArray m_free_internals
List of free ItemInternals from m_item_internals_buffer.
void clear()
Removes all entities.
IMesh * m_mesh
Associated mesh.
eItemKind m_kind
Corresponding kind.
ItemFamily * m_item_family
Associated mesh family.
ItemGroup createGroup(const String &name, Int32ConstArrayView elements, bool do_override=false) override
Creates an entity group named name containing the entities local_ids.
Int32 tryFindLocalId(Int64 key) const
Returns the localId() associated with key if found, or none otherwise.
Int32 findLocalId(Int64 uid) const
Returns the local number of the unique ID entity uid.
void _changeLocalIds(ArrayView< ItemInternal * > items_internal, ConstArrayView< Int32 > old_to_new_local_ids)
Changes the values of localId().
__host__ __device__ Real2 min(Real2 a, Real2 b)
Returns the minimum of two Real2.
ArrayView< Int64 > Int64ArrayView
C equivalent of a 1D array of 64-bit integers.
bool arcaneIsCheck()
True if running in check mode.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
ConstArrayView< Int64 > Int64ConstArrayView
C equivalent of a 1D array of 64-bit integers.
UniqueArray< Int32 > Int32UniqueArray
Dynamic 1D array of 32-bit integers.
ArrayView< Int32 > Int32ArrayView
C equivalent of a 1D array of 32-bit integers.
eItemKind
Mesh entity type.
std::int32_t Int32
Signed integer type of 32 bits.