14#include "arcane/utils/ArcanePrecomp.h"
16#include "arcane/utils/NotImplementedException.h"
17#include "arcane/utils/TraceInfo.h"
18#include "arcane/utils/Array.h"
19#include "arcane/utils/MultiArray2.h"
20#include "arcane/utils/HashTableMap.h"
21#include "arcane/utils/Deleter.h"
23#include "arcane/core/IMesh.h"
24#include "arcane/core/IParallelMng.h"
25#include "arcane/core/IItemFamily.h"
26#include "arcane/core/Item.h"
27#include "arcane/core/VariableTypes.h"
28#include "arcane/core/IMeshPartitionConstraint.h"
30#include "arcane/mesh/MeshPartitionConstraintMng.h"
59 , m_is_debug(is_debug)
60 , m_true_first_uid(5000,
true)
61 , m_uids_owner(5000,
true)
82MeshPartitionConstraintMng::
93MeshPartitionConstraintMng::
94~MeshPartitionConstraintMng()
97 m_weak_constraints.each(
Deleter());
109 m_constraints.add(constraint);
118 m_constraints.remove(constraint);
127 Helper h(m_mesh, m_is_debug);
128 _computeAndApplyConstraints(h);
138 Helper h(m_mesh, m_is_debug);
139 _computeAndApplyConstraints(h);
140 h.computeList(tied_uids);
146void MeshPartitionConstraintMng::
147_computeAndApplyConstraints(Helper& h)
155 h.merge(linked_cells, linked_owners);
158void MeshPartitionConstraintMng::
159_computeAndApplyWeakConstraints(Helper& h)
164 IMeshPartitionConstraint* c = *i;
167 h.merge(linked_cells, linked_owners);
176 m_weak_constraints.
add(constraint);
182 m_weak_constraints.remove(constraint);
188 Helper h(m_mesh, m_is_debug);
189 _computeAndApplyWeakConstraints(h);
196 Helper h(m_mesh, m_is_debug);
197 _computeAndApplyWeakConstraints(h);
198 h.computeList(tied_uids);
204void MeshPartitionConstraintMng::Helper::
216 Integer nb_global = global_linked_items.
size() / 2;
217 info() <<
"NB_GLOBAL_LINKED_CELL=" << nb_global;
219 m_true_first_uid.clear();
220 m_uids_owner.clear();
224 for (
Integer i = 0; i < nb_global; ++i) {
225 Int64 first_uid = global_linked_items[(i * 2)];
226 Int64 second_uid = global_linked_items[(i * 2) + 1];
227 Int32 first_owner = global_linked_owners[i];
228 info() <<
"LinkedItem: first_uid=" << first_uid
229 <<
" second_uid=" << second_uid
230 <<
" first_owner=" << first_owner;
236 for (
Integer i = 0; i < nb_global; ++i) {
237 Int64 first_uid = global_linked_items[(i * 2)];
238 Int64 second_uid = global_linked_items[(i * 2) + 1];
240 Int32 first_owner = global_linked_owners[i];
241 m_uids_owner.
add(first_uid, first_owner);
244 ItemFirstUidData* first_data = m_true_first_uid.lookup(first_uid);
245 ItemFirstUidData* second_data = m_true_first_uid.lookup(second_uid);
246 Int64 first_data_uid = first_uid;
247 Int64 second_data_uid = second_uid;
249 first_data_uid = first_data->value();
251 second_data_uid = second_data->value();
254 if (second_data_uid > smallest_uid) {
256 info() <<
"Changed second current=" << second_data->value() <<
" to " << smallest_uid
257 <<
" (first=" << first_uid <<
",second=" << second_uid <<
")";
258 second_data->setValue(smallest_uid);
264 info() <<
"Changed second add current=" << second_uid <<
" to " << smallest_uid
265 <<
" (first=" << first_uid <<
",second=" << second_uid <<
")";
267 m_true_first_uid.add(second_uid, smallest_uid);
272 if (first_data_uid > smallest_uid) {
274 info() <<
"Changed first current=" << first_data->value() <<
" to " << smallest_uid
275 <<
" (first=" << first_uid <<
",second=" << second_uid <<
")";
276 first_data->setValue(smallest_uid);
282 info() <<
"Changed first add current=" << first_uid <<
" to " << smallest_uid
283 <<
" (first=" << first_uid <<
",second=" << second_uid <<
")";
285 m_true_first_uid.add(first_uid, smallest_uid);
291 }
while (nb_changed != 0 && nb_iter < 100);
293 fatal() <<
"Too many iterations";
296 for (
Integer i = 0; i < nb_global; ++i) {
297 Int64 first_uid = global_linked_items[(i * 2)];
298 Int64 second_uid = global_linked_items[(i * 2) + 1];
299 ItemFirstUidData* first_data = m_true_first_uid.lookup(first_uid);
301 m_true_first_uid.
add(first_uid, first_uid);
302 ItemFirstUidData* second_data = m_true_first_uid.lookup(second_uid);
304 m_true_first_uid.add(second_uid, first_uid);
311void MeshPartitionConstraintMng::Helper::
319 for (
auto i : m_true_first_uid.buckets())
320 for (ItemFirstUidData* nbid = i; nbid; nbid = nbid->next()) {
321 cells_uid.add(nbid->key());
322 cells_owner.add(m_uids_owner[nbid->value()]);
326 IItemFamily* cell_family = m_mesh->cellFamily();
327 cell_family->itemsUniqueIdToLocalId(cells_local_id, cells_uid,
false);
329 CellInfoListView cells_internal(cell_family);
330 for (
Integer i = 0; i < nb_cell; ++i) {
331 Int32 lid = cells_local_id[i];
332 if (lid != NULL_ITEM_LOCAL_ID) {
333 Cell cell = cells_internal[lid];
335 info() <<
"Change cell owner uid=" << cell.uniqueId() <<
" old=" << cell.owner()
336 <<
" new=" << cells_owner[i];
337 cells_new_owner[cell] = cells_owner[i];
342 cells_new_owner.synchronize();
348void MeshPartitionConstraintMng::Helper::
349computeList(MultiArray2<Int64>& tied_uids)
353 HashTableMapT<Int64, Integer> uids_tied_index(5000,
true);
355 for (
auto i : m_true_first_uid.buckets())
356 for (ItemFirstUidData* nbid = i; nbid; nbid = nbid->next()) {
357 Int64 tied_uid = nbid->value();
358 HashTableMapT<Int64, Integer>::Data* d = uids_tied_index.lookup(tied_uid);
360 ++nb_tieds[d->value()];
362 Integer index = nb_tieds.size();
363 uids_tied_index.add(tied_uid, index);
368 info() <<
"NB_TIED=" << nb_tieds.size();
369 tied_uids.resize(nb_tieds);
372 for (
auto i : m_true_first_uid.buckets())
373 for (ItemFirstUidData* nbid = i; nbid; nbid = nbid->next()) {
374 Int64 tied_uid = nbid->value();
375 Integer index = uids_tied_index[tied_uid];
376 Integer index2 = nb_tieds[index];
378 tied_uids.setAt(index, index2, nbid->key());
381 for (
Integer i = 0, n = tied_uids.dim1Size(); i < n; ++i) {
384 for (
Integer j = 0, js = uids.size(); j < js; ++j) {
Integer size() const
Number of elements in the vector.
void add(ConstReferenceType val)
Adds element val to the end of the array.
void reserve(Int64 new_capacity)
Reserves memory for new_capacity elements.
ArrayView< T > view() const
Mutable view of this array.
Utility class for destroying objects allocated by new.
Hash table for associative arrays.
Interface of a mesh partitioning constraint.
virtual void addLinkedCells(Int64Array &linked_cells, Int32Array &linked_owners)=0
Adds a set of constraints on the cells.
virtual IParallelMng * parallelMng()=0
Parallelism manager.
Interface of the parallelism manager for a subdomain.
virtual void allGatherVariable(ConstArrayView< char > send_buf, Array< char > &recv_buf)=0
Performs an all-gather operation across all processors.
ListEnumeratorT< T > Enumerator
Type of a constant iterator over the entire array.
Base class for multi-sized 2D arrays.
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
TraceMessageDbg debug(Trace::eDebugLevel=Trace::Medium) const
Flow for a debug message.
TraceMessage fatal() const
Flow for a fatal error message.
TraceMessage info() const
Flow for an information message.
ITraceMng * traceMng() const
Trace manager.
Helper for constraint management.
virtual void computeConstraintList(Int64MultiArray2 &tied_uids)
Computes constraints and returns a list of linked entities.
virtual void removeConstraint(IMeshPartitionConstraint *constraint)
Removes a constraint.
virtual void computeAndApplyWeakConstraints()
Computes and applies constraints.
virtual void addConstraint(IMeshPartitionConstraint *constraint)
Adds a constraint.
virtual void addWeakConstraint(IMeshPartitionConstraint *constraint)
Adds a weak constraint.
virtual void computeWeakConstraintList(Int64MultiArray2 &tied_uids)
Computes constraints and returns a list of linked entities.
virtual void computeAndApplyConstraints()
Computes and applies constraints.
virtual void removeWeakConstraint(IMeshPartitionConstraint *constraint)
Removes a constraint.
__host__ __device__ Real2 min(Real2 a, Real2 b)
Returns the minimum of two Real2.
ItemVariableScalarRefT< Int32 > VariableItemInt32
32-bit integer type quantity
Array< Int64 > Int64Array
Dynamic one-dimensional array of 64-bit integers.
UniqueArray< Int64 > Int64UniqueArray
Dynamic 1D array of 64-bit integers.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
MultiArray2< Int64 > Int64MultiArray2
2D variable size array of 64-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.
Array< Int32 > Int32Array
Dynamic one-dimensional array of 32-bit integers.
UniqueArray< Integer > IntegerUniqueArray
Dynamic 1D array of integers.
@ Cell
The mesh is AMR by cell.
std::int32_t Int32
Signed integer type of 32 bits.