Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
PartitionConverter.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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/* Utilisation de ArrayConverter pour convertir les poids en flottants */
11/* en poids en entier en les "scalant" correctement. */
12/*---------------------------------------------------------------------------*/
13
14#ifndef ARCANE_STD_PARTITIONCONVERTER_H
15#define ARCANE_STD_PARTITIONCONVERTER_H
16/*---------------------------------------------------------------------------*/
17/*---------------------------------------------------------------------------*/
18#include "arcane/utils/ArrayConverter.h"
19#include "arcane/utils/ITraceMng.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24ARCANE_BEGIN_NAMESPACE
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32/*---------------------------------------------------------------------------*/
38template<typename TypeA,typename TypeB>
40{
41 public:
42
43 // Max is hard-coded to work on integers.
44 PartitionConverter(IParallelMng* pm=NULL, Real max=(2<<30), bool check=false)
45 : m_pm(pm), m_maxAllowed(max), m_sum(0), m_zoomfactor(0), m_ready(false), m_check(check)
46 {
47 m_sum.fill(0.0);
48 m_zoomfactor.fill(1.0);
49 }
50
51 //< This converter knows how to deal with multi-weights.
53 Integer ncon=1, bool check=false)
54 : m_pm(pm), m_maxAllowed(max), m_ready(false), m_check(check)
55 {
56 reset(ncon);
57 computeContrib(input);
58 }
59
60 //< Allow to use the same converter in different contexts.
61 void reset(Integer ncon=1, bool check=false)
62 {
63 m_check = check;
64 m_sum.resize(ncon+1);
65 m_max.resize(ncon);
66 m_max.fill(0.0);
67 m_zoomfactor.resize(ncon);
68 m_sum.fill(0.0);
69 m_ready=false;
70 }
71
72 //< Check if input can be evenly distributed with imb balance tolerance
73 template<typename DataReal>
75 bool cond = true;
76 if (!m_check || imb.size() < m_max.size())
77 return true;
78
79 if (!m_ready) {
80 computeContrib(input);
81 }
82
83 for (int i = 0 ; i < m_max.size() ; ++i) {
84 while (m_max[i] > imb[i]*m_sum[i]/partnum) {
85 imb[i] += 0.1f; // Increase imbalance by 10%
86 cond = false;
87 }
88 }
89 return cond;
90 }
91
92 //< Scan array to compute correct scaling.
93 void computeContrib(ConstArrayView<TypeA> input, Real multiplier=1.0)
94 {
95 Integer ncon = m_zoomfactor.size();
96 for( Integer i=0, is=input.size(); i<is; ++i ) {
97 m_sum[i%ncon] += (Real)input[i]*multiplier;
98 }
99 m_sum[ncon] += input.size();
100 m_pm->reduce(Parallel::ReduceSum, m_sum);
101
102 if (m_check) { // Check if partition can respect imbalance constraint
103 for( Integer i=0, is=input.size(); i<is; ++i ) {
104 m_max[i%ncon] = math::max((Real)input[i]*multiplier, m_max[i%ncon]);
105 }
106 m_pm->reduce(Parallel::ReduceMax, m_max);
107 }
108 m_zoomfactor[0] = (m_maxAllowed-m_sum[ncon])/(m_sum[0]+1);
109 /* for (Integer i = 1 ; i < ncon ; ++i) { // Allow null weight for the other constraints */
110 /* m_zoomfactor[i] = (m_maxAllowed)/(m_sum[i]+1); */
111 /* } */
112
113 for (Integer i = 0 ; i < ncon ; ++i) { // Allow null weight for the other constraints
114 m_zoomfactor[i] = (m_maxAllowed-m_sum[ncon])/(m_sum[i]+1);
115 }
116
117 for (Integer i = 0 ; i < ncon ; ++i) { // Do not scale if not necessary !
118 m_zoomfactor[i] = math::min(1.0, m_zoomfactor[i]);
119 }
120 m_ready = true;
121 }
122
123 //< Real convertion is here !
124 void convertFromAToB(ConstArrayView<TypeA> input,ArrayView<TypeB> output)
125 {
126 if (!m_ready)
127 computeContrib(input);
128 Integer ncon = m_zoomfactor.size();
129 for( Integer i=0, is=input.size(); i<is; ++i )
130 output[i] = (TypeB)((Real)input[i]*m_zoomfactor[i%ncon])+1;
131 // First constraint have to be != 0
132 /* for( Integer i=0, is=input.size(); i<is; i+=ncon) */
133 /* output[i] += 1; */
134 }
135
136 //< Not implemented as not needed by partitioners.
137 void convertFromBToA(ConstArrayView<TypeB> input,ArrayView<TypeA> output)
138 {
139 ARCANE_UNUSED(input);
140 ARCANE_UNUSED(output);
141 }
142
143 private:
144 IParallelMng* m_pm;
145 Real m_maxAllowed;
146 SharedArray<Real> m_max; //< Initial max for each component
147 SharedArray<Real> m_sum; //< Initial sum for each component
148 SharedArray<Real> m_zoomfactor;
149 bool m_ready;
150 bool m_check;
151};
152
153/*---------------------------------------------------------------------------*/
154/*---------------------------------------------------------------------------*/
155
156ARCANE_END_NAMESPACE
157
158/*---------------------------------------------------------------------------*/
159/*---------------------------------------------------------------------------*/
160
161
162#endif //ARCANE_STD_PARTITIONCONVERTER_H
Interface du gestionnaire de parallélisme pour un sous-domaine.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Conversion d'un tableau de flottants vers un tableau d'entiers/longs. \abstract Cette classe gere le ...