Skip to content

Commit 1b70d5d

Browse files
Merge pull request #1186 from arcaneframework/dev/gg-minor-optimisation-in-constituent-update
Minor optimisation in incremental material update
2 parents e71f4b8 + bbb49f9 commit 1b70d5d

6 files changed

+57
-76
lines changed

arcane/src/arcane/materials/AllEnvData.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ void AllEnvData::
406406
recomputeIncremental()
407407
{
408408
forceRecompute(false);
409-
_checkConnectivityCoherency();
409+
if (arcaneIsCheck())
410+
_checkConnectivityCoherency();
410411
}
411412

412413
/*---------------------------------------------------------------------------*/

arcane/src/arcane/materials/IncrementalComponentModifier.cc

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ apply(MaterialModifierOperation* operation)
7070
{
7171
bool is_add = operation->isAdd();
7272
IMeshMaterial* mat = operation->material();
73-
Int32ConstArrayView ids = operation->ids();
73+
ConstArrayView<Int32> orig_ids = operation->ids();
74+
ConstArrayView<Int32> ids = orig_ids;
7475

7576
auto* true_mat = ARCANE_CHECK_POINTER(dynamic_cast<MeshMaterial*>(mat));
7677

@@ -82,8 +83,6 @@ apply(MaterialModifierOperation* operation)
8283

8384
ConstituentConnectivityList* connectivity = m_all_env_data->componentConnectivityList();
8485

85-
Int32UniqueArray cells_changed_in_env;
86-
8786
if (nb_mat != 1) {
8887

8988
// S'il est possible d'avoir plusieurs matériaux par milieu, il faut gérer
@@ -94,12 +93,18 @@ apply(MaterialModifierOperation* operation)
9493
// - en cas de suppression, le milieu évolue dans la maille s'il y avait
9594
// 1 seul matériau avant. Dans ce cas le milieu est supprimé de la maille.
9695

97-
Int32UniqueArray cells_unchanged_in_env;
96+
UniqueArray<Int32>& cells_changed_in_env = m_work_info.cells_changed_in_env;
97+
UniqueArray<Int32>& cells_unchanged_in_env = m_work_info.cells_unchanged_in_env;
98+
const Int32 nb_id = ids.size();
99+
cells_unchanged_in_env.clear();
100+
cells_unchanged_in_env.reserve(nb_id);
101+
cells_changed_in_env.clear();
102+
cells_changed_in_env.reserve(nb_id);
98103
const Int32 ref_nb_mat = is_add ? 0 : 1;
99104
const Int16 env_id = true_env->componentId();
100105
info(4) << "Using optimisation updateMaterialDirect is_add?=" << is_add;
101106

102-
for (Integer i = 0, n = ids.size(); i < n; ++i) {
107+
for (Integer i = 0; i < nb_id; ++i) {
103108
Int32 lid = ids[i];
104109
Int32 current_cell_nb_mat = connectivity->cellNbMaterial(CellLocalId(lid), env_id);
105110
if (current_cell_nb_mat != ref_nb_mat) {
@@ -153,9 +158,9 @@ apply(MaterialModifierOperation* operation)
153158
// d'ajout) ou les mailles partielles en mailles pures (en cas de
154159
// suppression).
155160
info(4) << "Transform PartialPure for material name=" << true_mat->name();
156-
_switchComponentItemsForMaterials(true_mat);
161+
_switchCellsForMaterials(true_mat, orig_ids);
157162
info(4) << "Transform PartialPure for environment name=" << env->name();
158-
_switchComponentItemsForEnvironments(env);
163+
_switchCellsForEnvironments(env, orig_ids);
159164

160165
// Si je suis mono-mat, alors mat->cells()<=>env->cells() et il ne faut
161166
// mettre à jour que l'un des deux groupes.
@@ -190,7 +195,8 @@ apply(MaterialModifierOperation* operation)
190195
* (suppression d'un matériau)
191196
*/
192197
void IncrementalComponentModifier::
193-
_switchComponentItemsForMaterials(const MeshMaterial* modified_mat)
198+
_switchCellsForMaterials(const MeshMaterial* modified_mat,
199+
ConstArrayView<Int32> ids)
194200
{
195201
const bool is_add = m_work_info.isAdd();
196202

@@ -203,16 +209,13 @@ _switchComponentItemsForMaterials(const MeshMaterial* modified_mat)
203209
m_work_info.pure_local_ids.clear();
204210
m_work_info.partial_indexes.clear();
205211

206-
const MeshEnvironment* env = mat->trueEnvironment();
207-
if (env != true_env)
208-
ARCANE_FATAL("BAD ENV");
209212
MeshMaterialVariableIndexer* indexer = mat->variableIndexer();
210213

211214
info(4) << "TransformCells (V3) is_add?=" << is_add << " indexer=" << indexer->name();
212215

213-
_computeCellsToTransform(mat);
214-
216+
_computeCellsToTransformForMaterial(mat, ids);
215217
indexer->transformCellsV2(m_work_info);
218+
m_work_info.resetTransformedCells(ids);
216219

217220
info(4) << "NB_MAT_TRANSFORM=" << m_work_info.pure_local_ids.size() << " name=" << mat->name();
218221

@@ -238,7 +241,8 @@ _switchComponentItemsForMaterials(const MeshMaterial* modified_mat)
238241
* en pure (dans le cas de suppression d'un matériau)
239242
*/
240243
void IncrementalComponentModifier::
241-
_switchComponentItemsForEnvironments(const IMeshEnvironment* modified_env)
244+
_switchCellsForEnvironments(const IMeshEnvironment* modified_env,
245+
ConstArrayView<Int32> ids)
242246
{
243247
const bool is_add = m_work_info.isAdd();
244248

@@ -260,8 +264,9 @@ _switchComponentItemsForEnvironments(const IMeshEnvironment* modified_env)
260264

261265
info(4) << "TransformCells (V2) is_add?=" << is_add << " indexer=" << indexer->name();
262266

263-
_computeCellsToTransform();
267+
_computeCellsToTransformForEnvironments(ids);
264268
indexer->transformCellsV2(m_work_info);
269+
m_work_info.resetTransformedCells(ids);
265270

266271
info(4) << "NB_ENV_TRANSFORM=" << m_work_info.pure_local_ids.size()
267272
<< " name=" << env->name();
@@ -279,7 +284,7 @@ _switchComponentItemsForEnvironments(const IMeshEnvironment* modified_env)
279284
* \brief Calcule les mailles à transformer pour le matériau \at mat.
280285
*/
281286
void IncrementalComponentModifier::
282-
_computeCellsToTransform(const MeshMaterial* mat)
287+
_computeCellsToTransformForMaterial(const MeshMaterial* mat, ConstArrayView<Int32> ids)
283288
{
284289
const MeshEnvironment* env = mat->trueEnvironment();
285290
const Int16 env_id = env->componentId();
@@ -289,23 +294,25 @@ _computeCellsToTransform(const MeshMaterial* mat)
289294
ConstituentConnectivityList* connectivity = m_all_env_data->componentConnectivityList();
290295
ConstArrayView<Int16> cells_nb_env = connectivity->cellsNbEnvironment();
291296

292-
ENUMERATE_ (Cell, icell, all_cells) {
297+
for (Int32 local_id : ids) {
293298
bool do_transform = false;
299+
CellLocalId cell_id(local_id);
294300
// En cas d'ajout on passe de pure à partiel s'il y a plusieurs milieux ou
295301
// plusieurs matériaux dans le milieu.
296302
// En cas de supression, on passe de partiel à pure si on est le seul matériau
297303
// et le seul milieu.
304+
const Int16 nb_env = cells_nb_env[local_id];
298305
if (is_add) {
299-
do_transform = cells_nb_env[icell.itemLocalId()] > 1;
306+
do_transform = (nb_env > 1);
300307
if (!do_transform)
301-
do_transform = connectivity->cellNbMaterial(icell, env_id) > 1;
308+
do_transform = connectivity->cellNbMaterial(cell_id, env_id) > 1;
302309
}
303310
else {
304-
do_transform = cells_nb_env[icell.itemLocalId()] == 1;
311+
do_transform = (nb_env == 1);
305312
if (do_transform)
306-
do_transform = connectivity->cellNbMaterial(icell, env_id) == 1;
313+
do_transform = connectivity->cellNbMaterial(cell_id, env_id) == 1;
307314
}
308-
m_work_info.setTransformedCell(icell, do_transform);
315+
m_work_info.setTransformedCell(cell_id, do_transform);
309316
}
310317
}
311318

@@ -316,22 +323,22 @@ _computeCellsToTransform(const MeshMaterial* mat)
316323
* d'un milieu.
317324
*/
318325
void IncrementalComponentModifier::
319-
_computeCellsToTransform()
326+
_computeCellsToTransformForEnvironments(ConstArrayView<Int32> ids)
320327
{
321328
ConstituentConnectivityList* connectivity = m_all_env_data->componentConnectivityList();
322329
ConstArrayView<Int16> cells_nb_env = connectivity->cellsNbEnvironment();
323330
CellGroup all_cells = m_material_mng->mesh()->allCells();
324331
const bool is_add = m_work_info.isAdd();
325332

326-
ENUMERATE_ (Cell, icell, all_cells) {
333+
for (Int32 lid : ids) {
327334
bool do_transform = false;
328335
// En cas d'ajout on passe de pure à partiel s'il y a plusieurs milieux.
329336
// En cas de supression, on passe de partiel à pure si on est le seul milieu.
330337
if (is_add)
331-
do_transform = cells_nb_env[icell.itemLocalId()] > 1;
338+
do_transform = cells_nb_env[lid] > 1;
332339
else
333-
do_transform = cells_nb_env[icell.itemLocalId()] == 1;
334-
m_work_info.setTransformedCell(icell, do_transform);
340+
do_transform = cells_nb_env[lid] == 1;
341+
m_work_info.setTransformedCell(CellLocalId(lid), do_transform);
335342
}
336343
}
337344

arcane/src/arcane/materials/MeshMaterialVariableIndexer.cc

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -152,45 +152,6 @@ endUpdateAdd(const ComponentItemListBuilder& builder)
152152
/*---------------------------------------------------------------------------*/
153153
/*---------------------------------------------------------------------------*/
154154

155-
void MeshMaterialVariableIndexer::
156-
endUpdateRemove(ConstArrayView<bool> removed_local_ids_filter,Integer nb_remove)
157-
{
158-
// TODO: à supprimer et à remplacer par la version qui prend un
159-
// ConstituentModifierWorkInfo à la place
160-
161-
Integer nb_item = nbItem();
162-
Integer orig_nb_item = nb_item;
163-
164-
//ATTENTION: on modifie nb_item pendant l'itération.
165-
for (Integer i=0; i<nb_item; ++i) {
166-
Int32 lid = m_local_ids[i];
167-
if (removed_local_ids_filter[lid]) {
168-
// Déplace le dernier MatVarIndex vers l'élément courant.
169-
Int32 last = nb_item - 1;
170-
m_matvar_indexes[i] = m_matvar_indexes[last];
171-
m_local_ids[i] = m_local_ids[last];
172-
//info() << "REMOVE ITEM lid=" << lid << " i=" << i;
173-
--nb_item;
174-
--i; // Il faut refaire l'itération courante.
175-
}
176-
}
177-
m_matvar_indexes.resize(nb_item);
178-
m_local_ids.resize(nb_item);
179-
180-
// Vérifie qu'on a bien supprimé autant d'entité que prévu.
181-
Integer nb_remove_computed = (orig_nb_item - nb_item);
182-
if (nb_remove_computed!=nb_remove)
183-
ARCANE_FATAL("Bad number of removed material items expected={0} v={1} name={2}",
184-
nb_remove,nb_remove_computed,name());
185-
info(4) << "END_UPDATE_REMOVE nb_removed=" << nb_remove_computed;
186-
187-
// TODO: il faut recalculer m_max_index_in_multiple_array
188-
// et compacter éventuellement les variables. (pas indispensable)
189-
}
190-
191-
/*---------------------------------------------------------------------------*/
192-
/*---------------------------------------------------------------------------*/
193-
194155
void MeshMaterialVariableIndexer::
195156
endUpdateRemove(const ConstituentModifierWorkInfo& work_info,Integer nb_remove)
196157
{

arcane/src/arcane/materials/internal/ConstituentModifierWorkInfo.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
22
//-----------------------------------------------------------------------------
3-
// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
3+
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
44
// See the top-level COPYRIGHT file for details.
55
// SPDX-License-Identifier: Apache-2.0
66
//-----------------------------------------------------------------------------
77
/*---------------------------------------------------------------------------*/
8-
/* ConstituentModifierWorkInfo.h (C) 2000-2023 */
8+
/* ConstituentModifierWorkInfo.h (C) 2000-2024 */
99
/* */
1010
/* Structure de travail utilisée lors de la modification des constituants. */
1111
/*---------------------------------------------------------------------------*/
@@ -46,6 +46,11 @@ class ARCANE_MATERIALS_EXPORT ConstituentModifierWorkInfo
4646
UniqueArray<Int32> partial_indexes;
4747
bool is_verbose = false;
4848

49+
//! Liste des mailles d'un milieu qui vont être ajoutées ou supprimées lors d'une opération
50+
UniqueArray<Int32> cells_changed_in_env;
51+
//! Liste des mailles d'un milieu qui sont déjà présentes dans un milieu lors d'une opération
52+
UniqueArray<Int32> cells_unchanged_in_env;
53+
4954
public:
5055

5156
//! Initialise l'instance.
@@ -65,6 +70,12 @@ class ARCANE_MATERIALS_EXPORT ConstituentModifierWorkInfo
6570
m_cells_to_transform[local_id.localId()] = v;
6671
}
6772

73+
//! Positionne l'état de transformation de la maille \a local_id pour l'opération courante
74+
void resetTransformedCells(ConstArrayView<Int32> local_ids)
75+
{
76+
for( Int32 x : local_ids )
77+
m_cells_to_transform[x] = false;
78+
}
6879
//! Indique si la maille \a local_id est supprimée du matériaux pour l'opération courante.
6980
bool isRemovedCell(Int32 local_id) const { return m_removed_local_ids_filter[local_id]; }
7081

arcane/src/arcane/materials/internal/IncrementalComponentModifier.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
22
//-----------------------------------------------------------------------------
3-
// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
3+
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
44
// See the top-level COPYRIGHT file for details.
55
// SPDX-License-Identifier: Apache-2.0
66
//-----------------------------------------------------------------------------
77
/*---------------------------------------------------------------------------*/
8-
/* IncrementalComponentModifier.h (C) 2000-2023 */
8+
/* IncrementalComponentModifier.h (C) 2000-2024 */
99
/* */
1010
/* Modification incrémentale des constituants. */
1111
/*---------------------------------------------------------------------------*/
@@ -57,10 +57,12 @@ class ARCANE_MATERIALS_EXPORT IncrementalComponentModifier
5757

5858
private:
5959

60-
void _switchComponentItemsForEnvironments(const IMeshEnvironment* modified_env);
61-
void _switchComponentItemsForMaterials(const MeshMaterial* modified_mat);
62-
void _computeCellsToTransform(const MeshMaterial* mat);
63-
void _computeCellsToTransform();
60+
void _switchCellsForEnvironments(const IMeshEnvironment* modified_env,
61+
ConstArrayView<Int32> ids);
62+
void _switchCellsForMaterials(const MeshMaterial* modified_mat,
63+
ConstArrayView<Int32> ids);
64+
void _computeCellsToTransformForMaterial(const MeshMaterial* mat, ConstArrayView<Int32> ids);
65+
void _computeCellsToTransformForEnvironments(ConstArrayView<Int32> ids);
6466
void _removeItemsFromEnvironment(MeshEnvironment* env, MeshMaterial* mat,
6567
Int32ConstArrayView local_ids, bool update_env_indexer);
6668
void _addItemsToEnvironment(MeshEnvironment* env, MeshMaterial* mat,

arcane/src/arcane/materials/internal/MeshMaterialVariableIndexer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ class ARCANE_MATERIALS_EXPORT MeshMaterialVariableIndexer
9696

9797
void changeLocalIds(Int32ConstArrayView old_to_new_ids);
9898
void endUpdateAdd(const ComponentItemListBuilder& builder);
99-
void endUpdateRemove(ConstArrayView<bool> removed_local_ids_filter,Integer nb_remove);
10099
void endUpdateRemove(const ConstituentModifierWorkInfo& args,Integer nb_remove);
101100
//@}
102101

0 commit comments

Comments
 (0)