Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
PartitionConverter.h
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/* PartitionConverter.h (C) 2010 */
9/* */
10/* Use of ArrayConverter to convert weights from floats to integers by */
11/* "scaling" them correctly. */
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_STD_PARTITIONCONVERTER_H
14#define ARCANE_STD_PARTITIONCONVERTER_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/ArrayConverter.h"
19#include "arcane/utils/ITraceMng.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
35template <typename TypeA, typename TypeB>
36class PartitionConverter
37{
38 public:
39
40 // Max is hard-coded to work on integers.
41 PartitionConverter(IParallelMng* pm = NULL, Real max = (2 << 30), bool check = false)
42 : m_pm(pm)
43 , m_maxAllowed(max)
44 , m_sum(0)
45 , m_zoomfactor(0)
46 , m_ready(false)
47 , m_check(check)
48 {
49 m_sum.fill(0.0);
50 m_zoomfactor.fill(1.0);
51 }
52
53 //< This converter knows how to deal with multi-weights.
54 PartitionConverter(IParallelMng* pm, Real max, ConstArrayView<TypeA> input,
55 Integer ncon = 1, bool check = false)
56 : m_pm(pm)
57 , m_maxAllowed(max)
58 , m_ready(false)
59 , m_check(check)
60 {
61 reset(ncon);
62 computeContrib(input);
63 }
64
65 //< Allow to use the same converter in different contexts.
66 void reset(Integer ncon = 1, bool check = false)
67 {
68 m_check = check;
69 m_sum.resize(ncon + 1);
70 m_max.resize(ncon);
71 m_max.fill(0.0);
72 m_zoomfactor.resize(ncon);
73 m_sum.fill(0.0);
74 m_ready = false;
75 }
76
77 //< Check if input can be evenly distributed with imb balance tolerance
78 template <typename DataReal>
79 bool isBalancable(ConstArrayView<TypeA> input, ArrayView<DataReal> imb, int partnum)
80 {
81 bool cond = true;
82 if (!m_check || imb.size() < m_max.size())
83 return true;
84
85 if (!m_ready) {
86 computeContrib(input);
87 }
88
89 for (int i = 0; i < m_max.size(); ++i) {
90 while (m_max[i] > imb[i] * m_sum[i] / partnum) {
91 imb[i] += 0.1f; // Increase imbalance by 10%
92 cond = false;
93 }
94 }
95 return cond;
96 }
97
98 //< Scan array to compute correct scaling.
99 void computeContrib(ConstArrayView<TypeA> input, Real multiplier = 1.0)
100 {
101 Integer ncon = m_zoomfactor.size();
102 for (Integer i = 0, is = input.size(); i < is; ++i) {
103 m_sum[i % ncon] += (Real)input[i] * multiplier;
104 }
105 m_sum[ncon] += input.size();
106 m_pm->reduce(Parallel::ReduceSum, m_sum);
107
108 if (m_check) { // Check if partition can respect imbalance constraint
109 for (Integer i = 0, is = input.size(); i < is; ++i) {
110 m_max[i % ncon] = math::max((Real)input[i] * multiplier, m_max[i % ncon]);
111 }
112 m_pm->reduce(Parallel::ReduceMax, m_max);
113 }
114 m_zoomfactor[0] = (m_maxAllowed - m_sum[ncon]) / (m_sum[0] + 1);
115 /* for (Integer i = 1 ; i < ncon ; ++i) { // Allow null weight for the other constraints */
116 /* m_zoomfactor[i] = (m_maxAllowed)/(m_sum[i]+1); */
117 /* } */
118
119 for (Integer i = 0; i < ncon; ++i) { // Allow null weight for the other constraints
120 m_zoomfactor[i] = (m_maxAllowed - m_sum[ncon]) / (m_sum[i] + 1);
121 }
122
123 for (Integer i = 0; i < ncon; ++i) { // Do not scale if not necessary !
124 m_zoomfactor[i] = math::min(1.0, m_zoomfactor[i]);
125 }
126 m_ready = true;
127 }
128
129 //< Real convertion is here !
130 void convertFromAToB(ConstArrayView<TypeA> input, ArrayView<TypeB> output)
131 {
132 if (!m_ready)
133 computeContrib(input);
134 Integer ncon = m_zoomfactor.size();
135 for (Integer i = 0, is = input.size(); i < is; ++i)
136 output[i] = (TypeB)((Real)input[i] * m_zoomfactor[i % ncon]) + 1;
137 // First constraint have to be != 0
138 /* for( Integer i=0, is=input.size(); i<is; i+=ncon) */
139 /* output[i] += 1; */
140 }
141
142 //< Not implemented as not needed by partitioners.
143 void convertFromBToA(ConstArrayView<TypeB> input, ArrayView<TypeA> output)
144 {
145 ARCANE_UNUSED(input);
146 ARCANE_UNUSED(output);
147 }
148
149 private:
150
151 IParallelMng* m_pm;
152 Real m_maxAllowed;
153 SharedArray<Real> m_max; //< Initial max for each component
154 SharedArray<Real> m_sum; //< Initial sum for each component
155 SharedArray<Real> m_zoomfactor;
156 bool m_ready;
157 bool m_check;
158};
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163} // namespace Arcane
164
165/*---------------------------------------------------------------------------*/
166/*---------------------------------------------------------------------------*/
167
168#endif
Modifiable view of an array of type T.
constexpr Integer size() const noexcept
Returns the size of the array.
Constant view of an array of type T.
constexpr Integer size() const noexcept
Number of elements in the array.
Interface of the parallelism manager for a subdomain.
1D vector of data with reference semantics.
__host__ __device__ Real2 min(Real2 a, Real2 b)
Returns the minimum of two Real2.
Definition MathUtils.h:346
T max(const T &a, const T &b, const T &c)
Returns the maximum of three elements.
Definition MathUtils.h:407
@ ReduceMax
Maximum of values.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.