14#include "arcane/materials/internal/MeshMaterialVariableSynchronizer.h"
16#include "arcane/utils/PlatformUtils.h"
17#include "arcane/utils/MemoryUtils.h"
18#include "arcane/utils/ValueConvert.h"
20#include "arcane/core/IMesh.h"
21#include "arcane/core/IVariableSynchronizer.h"
22#include "arcane/core/ItemGroup.h"
23#include "arcane/core/IParallelMng.h"
24#include "arcane/core/IItemFamily.h"
25#include "arcane/core/internal/IParallelMngInternal.h"
27#include "arcane/materials/IMeshMaterialMng.h"
28#include "arcane/materials/MatItemEnumerator.h"
29#include "arcane/materials/IMeshMaterialSynchronizeBuffer.h"
30#include "arcane/core/materials/internal/IMeshMaterialMngInternal.h"
32#include "arcane/accelerator/core/RunQueue.h"
33#include "arcane/accelerator/Scan.h"
44MeshMaterialVariableSynchronizer::
45MeshMaterialVariableSynchronizer(IMeshMaterialMng* material_mng,
46 IVariableSynchronizer* var_syncer,
48: TraceAccessor(material_mng->traceMng())
49, m_material_mng(material_mng)
50, m_variable_synchronizer(var_syncer)
54 if (
auto v = Convert::Type<Int32>::tryParseFromEnvironment(
"ARCANE_MATERIALSYNCHRONIZER_ACCELERATOR_MODE",
true))
55 m_use_accelerator_mode = v.value();
65IVariableSynchronizer* MeshMaterialVariableSynchronizer::
68 return m_variable_synchronizer;
74ConstArrayView<MatVarIndex> MeshMaterialVariableSynchronizer::
75sharedItems(Int32 index)
77 return m_shared_items[index];
83ConstArrayView<MatVarIndex> MeshMaterialVariableSynchronizer::
84ghostItems(Int32 index)
86 return m_ghost_items[index];
95void MeshMaterialVariableSynchronizer::
96_fillCells(Array<MatVarIndex>& items, AllEnvCellVectorView view, RunQueue& queue)
107 if (view.size() == 0)
110 bool use_accelerator = queue.isAcceleratorPolicy();
111 if (m_use_accelerator_mode == 1)
112 use_accelerator =
true;
113 if (m_use_accelerator_mode == 0)
114 use_accelerator =
false;
117 _fillCellsAccelerator(items, view, queue);
119 _fillCellsSequential(items, view);
128void MeshMaterialVariableSynchronizer::
129_fillCellsSequential(Array<MatVarIndex>& items, AllEnvCellVectorView view)
131 bool has_mat = m_var_space == MatVarSpace::MaterialAndEnvironment;
134 AllEnvCell all_env_cell = *iallenvcell;
135 for (EnvCell env_cell : all_env_cell.subEnvItems()) {
136 items.add(env_cell._varIndex());
138 for (MatCell mat_cell : env_cell.subMatItems()) {
139 items.add(mat_cell._varIndex());
145 items.add(MatVarIndex(0, view.localId(iallenvcell.index())));
155void MeshMaterialVariableSynchronizer::
156_fillCellsAccelerator(Array<MatVarIndex>& items, AllEnvCellVectorView view, RunQueue& queue)
158 bool has_mat = m_var_space == MatVarSpace::MaterialAndEnvironment;
162 Int32 nb_item = view.size();
163 UniqueArray<Int32> indexes(queue.allocationOptions());
164 indexes.resize(nb_item + 1);
166 Accelerator::GenericScanner scanner(queue);
167 Accelerator::ScannerSumOperator<Int32> op;
168 Span<Int32> out_indexes = indexes;
169 auto getter = [=] ARCCORE_HOST_DEVICE(Int32 index) -> Int32 {
170 if (index == nb_item)
172 AllEnvCell all_env_cell = view[index];
174 for (EnvCell env_cell : all_env_cell.subEnvItems()) {
177 n += env_cell.nbSubItem();
185 NumArray<Int32, MDDim1> host_total_storage(1, eMemoryRessource::HostPinned);
186 SmallSpan<Int32> in_host_total_storage(host_total_storage);
188 auto setter = [=] ARCCORE_HOST_DEVICE(Int32 index, Int32 value) {
189 out_indexes[index] = value;
190 if (index == nb_item)
191 in_host_total_storage[0] = value;
193 scanner.applyWithIndexExclusive(nb_item + 1, 0, getter, setter, op);
194 Int32 total = host_total_storage[0];
200 Span<const Int32> in_indexes = indexes;
201 Span<MatVarIndex> out_mat_var_indexes = items;
204 auto [index] = iter();
205 AllEnvCell all_env_cell = view[index];
206 Int32 pos = in_indexes[index];
207 for (EnvCell env_cell : all_env_cell.subEnvItems()) {
208 out_mat_var_indexes[pos] = env_cell._varIndex();
211 for (MatCell mat_cell : env_cell.subMatItems()) {
212 out_mat_var_indexes[pos] = mat_cell._varIndex();
219 out_mat_var_indexes[pos] = MatVarIndex(0, view.localId(index));
227void MeshMaterialVariableSynchronizer::
230 Int64 ts = m_material_mng->timestamp();
231 if (m_timestamp != ts)
239void MeshMaterialVariableSynchronizer::
242 IVariableSynchronizer* var_syncer = m_variable_synchronizer;
248 IItemFamily* family = var_syncer->itemGroup().itemFamily();
249 IParallelMng* pm = var_syncer->parallelMng();
250 if (!pm->isParallel())
252 ItemGroup all_items = family->allItems();
255 Integer nb_rank = ranks.size();
257 m_common_buffer->setNbRank(nb_rank);
259 m_shared_items.resize(nb_rank);
260 m_ghost_items.resize(nb_rank);
262 RunQueue queue = m_material_mng->_internalApi()->runQueue();
269 for (Int32 i = 0; i < nb_rank; ++i) {
270 m_shared_items[i] = UniqueArray<MatVarIndex>(a);
271 m_ghost_items[i] = UniqueArray<MatVarIndex>(a);
277 for (Integer i = 0; i < nb_rank; ++i) {
282 AllEnvCellVectorView view = m_material_mng->view(shared_cells);
283 Array<MatVarIndex>& items = m_shared_items[i];
284 _fillCells(items, view, queue);
285 info(4) <<
"SIZE SHARED FOR rank=" << ranks[i] <<
" n=" << items.size();
291 AllEnvCellVectorView view = m_material_mng->view(ghost_cells);
292 Array<MatVarIndex>& items = m_ghost_items[i];
293 _fillCells(items, view, queue);
294 info(4) <<
"SIZE GHOST FOR rank=" << ranks[i] <<
" n=" << items.size();
302void MeshMaterialVariableSynchronizer::
305 IParallelMng* pm = m_variable_synchronizer->parallelMng();
306 if (pm->_internalApi()->isAcceleratorAware()) {
307 m_buffer_memory_ressource = eMemoryRessource::Device;
308 info() <<
"MeshMaterialVariableSynchronizer: Using device memory for buffer";
310 m_common_buffer = impl::makeOneBufferMeshMaterialSynchronizeBufferRef(m_buffer_memory_ressource);
#define RUNCOMMAND_LOOP1(iter_name, x1,...)
Boucle sur accélérateur avec arguments supplémentaires pour les réductions.
MemoryAllocationOptions allocationOptions() const
Options d'allocation associée à cette file.
ItemVectorViewT< Cell > CellVectorView
Vue sur un vecteur de mailles.
RunCommand makeCommand(const RunQueue &run_queue)
Créé une commande associée à la file run_queue.
Active toujours les traces dans les parties Arcane concernant les matériaux.
MatVarSpace
Espace de définition d'une variable matériau.
ConstArrayView< Int32 > Int32ConstArrayView
Equivalent C d'un tableau à une dimension d'entiers 32 bits.
Int32 Integer
Type représentant un entier.