14#include "arcane/materials/internal/ConstituentItemVectorImpl.h"
16#include "arcane/utils/PlatformUtils.h"
17#include "arcane/utils/FatalErrorException.h"
19#include "arcane/core/materials/IMeshMaterialMng.h"
21#include "arcane/core/materials/internal/IMeshComponentInternal.h"
22#include "arcane/core/materials/internal/IMeshMaterialMngInternal.h"
24#include "arcane/accelerator/core/RunQueue.h"
25#include "arcane/accelerator/RunCommandLoop.h"
26#include "arcane/accelerator/Reduce.h"
27#include "arcane/accelerator/Partitioner.h"
39ConstituentItemVectorImpl::
41: m_material_mng(component->materialMng())
42, m_component(component)
45, m_part_data(std::make_unique<MeshComponentPartData>(component, String()))
46, m_recompute_part_data_functor(this, &ConstituentItemVectorImpl::_recomputePartData)
49 if (component->isMaterial())
50 level = LEVEL_MATERIAL;
51 else if (component->isEnvironment())
52 level = LEVEL_ENVIRONMENT;
55 m_component_shared_info = m_material_mng->_internalApi()->componentItemSharedInfo(level);
56 m_constituent_list = std::make_unique<ConstituentItemLocalIdList>(m_component_shared_info, String());
57 m_part_data->setRecomputeFunctor(&m_recompute_part_data_functor);
63ConstituentItemVectorImpl::
64ConstituentItemVectorImpl(
const ComponentItemVectorView& rhs)
65: ConstituentItemVectorImpl(rhs.component())
67 RunQueue& queue = m_material_mng->_internalApi()->runQueue();
68 m_constituent_list->copy(rhs._constituentItemListView());
69 m_matvar_indexes.copy(rhs._matvarIndexes());
70 m_items_local_id.copy(rhs._internalLocalIds());
71 m_part_data->_setFromMatVarIndexes(rhs._matvarIndexes(), queue);
87 explicit SetItemHelper(
bool use_new_impl)
88 : m_use_new_impl(use_new_impl)
93 template <
typename ConstituentGetterLambda>
94 void setItems(ConstituentItemVectorImpl* vector_impl,
95 ConstituentGetterLambda constituent_getter_lambda,
100 bool m_use_new_impl =
true;
106template <
typename ConstituentGetterLambda>
void
107ConstituentItemVectorImpl::SetItemHelper::
108setItems(ConstituentItemVectorImpl* vector_impl, ConstituentGetterLambda constituent_getter_lambda,
113 SmallSpan<Int32> items_local_id = vector_impl->m_items_local_id.smallSpan();
115 const bool is_env = vector_impl->m_component->isEnvironment();
116 const Int32 component_id = vector_impl->m_component->id();
118 const Int32 nb_pure = vector_impl->m_nb_pure;
120 Int32 pure_index = 0;
121 Int32 impure_index = nb_pure;
127 auto setter_unselected = [=] ARCCORE_HOST_DEVICE(
Int32,
Int32) {
129 auto generic_setter_lambda = [=] ARCCORE_HOST_DEVICE(
Int32 index, ComponentCell cc) {
132 item_indexes[index] = cii;
133 matvar_indexes[index] = idx;
138 if (m_use_new_impl) {
140 auto select_pure = [=] ARCCORE_HOST_DEVICE(
Int32 index) {
141 ComponentCell cc = constituent_getter_lambda(index);
147 auto select_impure = [=] ARCCORE_HOST_DEVICE(Int32 index) {
148 ComponentCell cc = constituent_getter_lambda(index);
151 return (cc._varIndex().arrayIndex() != 0);
153 auto setter_lambda = [=] ARCCORE_HOST_DEVICE(Int32 index, Int32 output_index) {
154 ComponentCell cc = constituent_getter_lambda(index);
156 generic_setter_lambda(output_index, cc);
158 auto setter_pure = [=] ARCCORE_HOST_DEVICE(Int32 index, Int32 output_index) {
159 setter_lambda(index, output_index);
161 auto setter_impure = [=] ARCCORE_HOST_DEVICE(Int32 index, Int32 output_index) {
162 setter_lambda(index, output_index + nb_pure);
165 generic_partitioner.applyWithIndex(nb_id, setter_pure, setter_impure, setter_unselected,
166 select_pure, select_impure, A_FUNCINFO);
174 AllEnvCell all_env_cell = *iallenvcell;
175 for (EnvCell ec : all_env_cell.subEnvItems()) {
176 if (ec.componentId() == component_id) {
177 MatVarIndex idx = ec._varIndex();
178 ConstituentItemIndex cii = ec._constituentItemIndex();
179 Int32& base_index = (idx.arrayIndex() == 0) ? pure_index : impure_index;
180 item_indexes[base_index] = cii;
181 matvar_indexes[base_index] = idx;
182 items_local_id[base_index] = all_env_cell.globalCellId();
191 AllEnvCell all_env_cell = *iallenvcell;
192 for (EnvCell env_cell : all_env_cell.subEnvItems()) {
193 for (MatCell mc : env_cell.subMatItems()) {
194 if (mc.componentId() == component_id) {
195 MatVarIndex idx = mc._varIndex();
196 ConstituentItemIndex cii = mc._constituentItemIndex();
197 Int32& base_index = (idx.arrayIndex() == 0) ? pure_index : impure_index;
198 item_indexes[base_index] = cii;
199 matvar_indexes[base_index] = idx;
200 items_local_id[base_index] = all_env_cell.globalCellId();
219void ConstituentItemVectorImpl::
220_setItems(SmallSpan<const Int32> local_ids)
222 const bool do_new_impl = m_material_mng->_internalApi()->isUseAcceleratorForConstituentItemVector();
224 Accelerator::eExecutionPolicy exec_policy = m_component->specificExecutionPolicy();
225 RunQueue queue = m_material_mng->_internalApi()->runQueue(exec_policy);
228 _computeNbPureAndImpure(local_ids, queue);
230 _computeNbPureAndImpureLegacy(local_ids);
232 const Int32 nb_pure = m_nb_pure;
233 const Int32 nb_impure = m_nb_impure;
234 const Int32 total_nb_pure_and_impure = nb_pure + nb_impure;
241 m_constituent_list->resize(total_nb_pure_and_impure);
246 m_matvar_indexes.resize(total_nb_pure_and_impure);
247 m_items_local_id.resize(total_nb_pure_and_impure);
249 const bool is_env = m_component->isEnvironment();
250 AllEnvCellVectorView all_env_cell_view = m_material_mng->view(local_ids);
251 const Int32 component_id = m_component->id();
254 auto env_component_getter_lambda = [=] ARCCORE_HOST_DEVICE(Int32 index) -> ComponentCell {
255 AllEnvCell all_env_cell = all_env_cell_view[index];
256 for (EnvCell ec : all_env_cell.subEnvItems()) {
257 if (ec.componentId() == component_id)
264 auto mat_component_getter_lambda = [=] ARCCORE_HOST_DEVICE(Int32 index) -> ComponentCell {
265 AllEnvCell all_env_cell = all_env_cell_view[index];
266 for (EnvCell ec : all_env_cell.subEnvItems()) {
267 for (MatCell mc : ec.subMatItems())
268 if (mc.componentId() == component_id)
275 SetItemHelper helper(do_new_impl);
277 helper.setItems(
this, env_component_getter_lambda, local_ids, queue);
279 helper.setItems(
this, mat_component_getter_lambda, local_ids, queue);
283 const bool do_lazy_evaluation =
true;
284 if (do_lazy_evaluation)
285 m_part_data->setNeedRecompute();
287 _recomputePartData();
293void ConstituentItemVectorImpl::
294_computeNbPureAndImpure(SmallSpan<const Int32> local_ids, RunQueue& queue)
296 IMeshComponent* component = m_component;
297 const bool is_env = component->isEnvironment();
298 AllEnvCellVectorView all_env_cell_view = m_material_mng->view(local_ids);
299 const Int32 component_id = m_component->id();
302 Accelerator::ReducerSum2<Int32> nb_pure(command);
303 Accelerator::ReducerSum2<Int32> nb_impure(command);
309 for (EnvCell ec : all_env_cell.subEnvItems()) {
310 if (ec.componentId() == component_id) {
311 MatVarIndex idx = ec._varIndex();
312 if (idx.arrayIndex() == 0)
315 nb_impure.combine(1);
323 for (EnvCell env_cell : all_env_cell.subEnvItems()) {
324 for (MatCell mc : env_cell.subMatItems()) {
325 if (mc.componentId() == component_id) {
326 MatVarIndex idx = mc._varIndex();
327 if (idx.arrayIndex() == 0)
330 nb_impure.combine(1);
337 m_nb_pure = nb_pure.reducedValue();
338 m_nb_impure = nb_impure.reducedValue();
347void ConstituentItemVectorImpl::
348_computeNbPureAndImpureLegacy(SmallSpan<const Int32> local_ids)
350 IMeshComponent* component = m_component;
351 const bool is_env = component->isEnvironment();
352 AllEnvCellVectorView all_env_cell_view = m_material_mng->view(local_ids);
353 const Int32 component_id = m_component->id();
361 AllEnvCell all_env_cell = *iallenvcell;
362 for (EnvCell ec : all_env_cell.subEnvItems()) {
363 if (ec.componentId() == component_id) {
364 MatVarIndex idx = ec._varIndex();
365 if (idx.arrayIndex() == 0)
375 AllEnvCell all_env_cell = *iallenvcell;
376 for (EnvCell env_cell : all_env_cell.subEnvItems()) {
377 for (MatCell mc : env_cell.subMatItems()) {
378 if (mc.componentId() == component_id) {
379 MatVarIndex idx = mc._varIndex();
380 if (idx.arrayIndex() == 0)
391 m_nb_impure = nb_impure;
397void ConstituentItemVectorImpl::
401 auto mvi_pure_view = m_matvar_indexes.subView(0, m_nb_pure);
402 auto mvi_impure_view = m_matvar_indexes.subView(m_nb_pure, m_nb_impure);
403 m_part_data->_setFromMatVarIndexes(mvi_pure_view, mvi_impure_view);
409ComponentItemVectorView ConstituentItemVectorImpl::
412 return { m_component, m_matvar_indexes,
413 m_constituent_list->view(), m_items_local_id };
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Types and macros to manage enumerations of materials and media on accelerators.
#define RUNCOMMAND_MAT_ENUMERATE(ConstituentItemNameType, iter_name, env_or_mat_container,...)
Macro for iterating over a material or an environment.
Generic algorithm for partitioning a list.
Execution queue for an accelerator.
Helper for positioning vector entities.
__host__ __device__ CellLocalId globalCellId() const
localId() of the global cell
__host__ __device__ bool null() const
Indicates if it is the null cell.
Interface of a component (material or environment) of a mesh.
Represents an index on material and environment variables.
constexpr __host__ __device__ Int32 arrayIndex() const
Returns the index of the value array in the list of variables.
View of an array of elements of type T.
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
RunCommand makeCommand(const RunQueue &run_queue)
Creates a command associated with the queue run_queue.
Always enables tracing in Arcane parts concerning materials.
std::int32_t Int32
Signed integer type of 32 bits.