12#ifndef ARCANE_ACCELERATOR_RUNCOMMANDMATERIALENUMERATE_H
13#define ARCANE_ACCELERATOR_RUNCOMMANDMATERIALENUMERATE_H
18#include "arcane/core/materials/ComponentItemVectorView.h"
19#include "arcane/core/materials/MaterialsCoreGlobal.h"
20#include "arcane/core/materials/MatItem.h"
22#include "arcane/core/materials/ConstituentItemIndexedSelectionView.h"
24#include "arcane/accelerator/KernelLauncher.h"
25#include "arcane/accelerator/RunCommand.h"
26#include "arcane/accelerator/RunCommandLaunchInfo.h"
28#include "arccore/common/HostKernelRemainingArgsHelper.h"
45template <
typename ConstituentItemLocalIdType_>
46class ConstituentAndGlobalCellIteratorValue
50 using ConstituentItemLocalIdType = ConstituentItemLocalIdType_;
51 using ComponentItemLocalId = Arcane::Materials::ComponentItemLocalId;
61 constexpr ARCCORE_HOST_DEVICE Data(ConstituentItemLocalIdType mvi, CellLocalId cid)
68 ConstituentItemLocalIdType m_mvi;
74 constexpr ARCCORE_HOST_DEVICE ConstituentAndGlobalCellIteratorValue(ConstituentItemLocalIdType mvi, CellLocalId cid,
Int32 index)
75 : m_internal_data{ mvi, cid }
100 return m_internal_data;
104 constexpr ARCCORE_HOST_DEVICE ConstituentItemLocalIdType
varIndex()
const {
return m_internal_data.m_mvi; };
107 constexpr ARCCORE_HOST_DEVICE CellLocalId
globalCellId()
const {
return m_internal_data.m_cid; }
110 constexpr ARCCORE_HOST_DEVICE
Int32 index()
const {
return m_index; }
114 Data m_internal_data;
132namespace Arcane::Accelerator::Impl
141template <
typename ContainerType_>
142class ConstituentRunCommandBase2
146 using ContainerType = ContainerType_;
147 using ThatClass = ConstituentRunCommandBase2<ContainerType_>;
148 using CommandType = ThatClass;
152 static CommandType create(
RunCommand& run_command,
const ContainerType& items)
154 return CommandType(run_command, items);
160 explicit ConstituentRunCommandBase2(
RunCommand& command,
const ContainerType& items)
169 ContainerType m_items;
178class AllEnvCellRunCommandContainer
182 using ThatClass = AllEnvCellRunCommandContainer;
184 using ContainerCreateViewType = AllEnvCellVectorView;
190 explicit AllEnvCellRunCommandContainer(ContainerCreateViewType view)
197 constexpr ARCCORE_HOST_DEVICE
Int32 size()
const {
return m_view.size(); }
217class ConstituentCommandContainerBase
222 using ComponentItemLocalId = Arcane::Materials::ComponentItemLocalId;
227 explicit ConstituentCommandContainerBase(ComponentItemVectorView view)
230 m_nb_item = m_items.nbItem();
231 m_matvar_indexes = m_items._matvarIndexes();
232 m_global_cells_local_id = m_items._internalLocalIds();
237 constexpr ARCCORE_HOST_DEVICE
Int32 size()
const {
return m_nb_item; }
241 ComponentItemVectorView m_items;
254template <
typename ConstituentItemLocalIdType_,
typename ContainerCreateViewType_>
255class ConstituentRunCommandContainer
256:
public ConstituentCommandContainerBase
260 using ThatClass = ConstituentRunCommandContainer;
261 using IteratorValueType = ConstituentItemLocalIdType_;
263 using ContainerCreateViewType = ContainerCreateViewType_;
267 explicit ConstituentRunCommandContainer(ContainerCreateViewType view)
277 return { ComponentItemLocalId(m_matvar_indexes[i]) };
281using EnvCellRunCommandContainer = ConstituentRunCommandContainer<Arcane::Materials::EnvItemLocalId, Arcane::Materials::EnvCellVectorView>;
282using MatCellRunCommandContainer = ConstituentRunCommandContainer<Arcane::Materials::MatItemLocalId, Arcane::Materials::MatCellVectorView>;
291template <
typename ConstituentItemLocalIdType_,
typename ContainerCreateViewType_>
292class ConstituentAndGlobalCellRunCommandContainer
293:
public ConstituentCommandContainerBase
297 using ThatClass = ConstituentAndGlobalCellRunCommandContainer;
300 using ContainerCreateViewType = ContainerCreateViewType_;
304 explicit ConstituentAndGlobalCellRunCommandContainer(ContainerCreateViewType_ view)
305 : ConstituentCommandContainerBase(view)
314 return { ComponentItemLocalId(m_matvar_indexes[i]), CellLocalId(m_global_cells_local_id[i]), i };
321using EnvAndGlobalCellRunCommandContainer = ConstituentAndGlobalCellRunCommandContainer<Arcane::Materials::EnvItemLocalId, Arcane::Materials::EnvCellVectorView>;
322using MatAndGlobalCellRunCommandContainer = ConstituentAndGlobalCellRunCommandContainer<Arcane::Materials::MatItemLocalId, Arcane::Materials::MatCellVectorView>;
331template <
typename ConstituentItemLocalIdType_,
typename ContainerCreateViewType_>
332class ConstituentIndexedSelectionRunCommandContainer
336 using ThatClass = ConstituentIndexedSelectionRunCommandContainer;
337 using IteratorValueType = ConstituentItemLocalIdType_;
339 using ContainerCreateViewType = ContainerCreateViewType_;
343 explicit ConstituentIndexedSelectionRunCommandContainer(ContainerCreateViewType view)
353 return { ComponentItemLocalId(m_view[i]) };
356 ARCCORE_HOST_DEVICE
Int32 size() {
return m_view.size(); }
360 ContainerCreateViewType m_view;
366using EnvIndexedSelectionRunCommandContainer = ConstituentIndexedSelectionRunCommandContainer<Arcane::Materials::EnvItemLocalId, Arcane::Materials::EnvCellVectorSelectionView>;
371#if defined(ARCANE_COMPILING_CUDA_OR_HIP)
375template <
typename ContainerType,
typename Lambda,
typename... RemainingArgs> __global__
void
376doMatContainerGPULambda(
ContainerType items, Lambda func, RemainingArgs... remaining_args)
378 auto privatizer = Impl::privatize(func);
379 auto& body = privatizer.privateCopy();
380 Int32 i = blockDim.x * blockIdx.x + threadIdx.x;
382 if (i < items.size()) {
383 body(items[i], remaining_args...);
393#if defined(ARCANE_COMPILING_SYCL)
395template <
typename ContainerType,
typename Lambda,
typename... RemainingArgs>
396class DoMatContainerSYCLLambda
400 void operator()(sycl::nd_item<1> x, SmallSpan<std::byte> shm_view,
401 ContainerType items, Lambda func,
402 RemainingArgs... remaining_args)
const
404 auto privatizer = Impl::privatize(func);
405 auto& body = privatizer.privateCopy();
407 Int32 i =
static_cast<Int32
>(x.get_global_id(0));
408 Impl::SyclKernelRemainingArgsHelper::applyAtBegin(x, shm_view, remaining_args...);
409 if (i < items.size()) {
410 body(items[i], remaining_args...);
412 Impl::SyclKernelRemainingArgsHelper::applyAtEnd(x, shm_view, remaining_args...);
415 void operator()(sycl::id<1> x, ContainerType items, Lambda func)
const
417 auto privatizer = Impl::privatize(func);
418 auto& body = privatizer.privateCopy();
420 Int32 i =
static_cast<Int32
>(x);
421 if (i < items.size()) {
432template <
typename ContainerType,
typename Lambda,
typename... RemainingArgs>
433void _doConstituentItemsLambda(
Int32 base_index,
Int32 size, ContainerType items,
434 const Lambda& func, RemainingArgs... remaining_args)
436 auto privatizer = Impl::privatize(func);
437 auto& body = privatizer.privateCopy();
440 Int32 last_value = base_index + size;
441 for (
Int32 i = base_index; i < last_value; ++i) {
442 body(items[i], remaining_args...);
450template <
typename ContainerType_,
typename... RemainingArgs>
451class GenericConstituentCommandArgs
455 using ContainerType = ContainerType_;
456 using IteratorValueType = ContainerType::IteratorValueType;
460 explicit GenericConstituentCommandArgs(
const ContainerType& container,
const RemainingArgs&... remaining_args)
461 : m_container(container)
462 , m_remaining_args(remaining_args...)
467 ContainerType m_container;
468 std::tuple<RemainingArgs...> m_remaining_args;
474template <
typename ContainerType_,
typename... RemainingArgs>
475class GenericConstituentCommand
479 using ContainerType = ContainerType_;
480 using ConstituentCommandType = ContainerType::CommandType;
484 explicit GenericConstituentCommand(
const ConstituentCommandType& command)
487 explicit GenericConstituentCommand(
const ConstituentCommandType& command,
488 const std::tuple<RemainingArgs...>& remaining_args)
490 , m_remaining_args(remaining_args)
495 ConstituentCommandType m_command;
496 std::tuple<RemainingArgs...> m_remaining_args;
511template <
typename ContainerType,
typename Lambda,
typename... RemainingArgs>
void
516 Int32 vsize = items.size();
523 switch (exec_policy) {
525 ARCCORE_KERNEL_CUDA_FUNC((doMatContainerGPULambda<ContainerType, Lambda, RemainingArgs...>),
526 launch_info, func, items, remaining_args...);
529 ARCCORE_KERNEL_HIP_FUNC((doMatContainerGPULambda<ContainerType, Lambda, RemainingArgs...>),
530 launch_info, func, items, remaining_args...);
533 ARCCORE_KERNEL_SYCL_FUNC((DoMatContainerSYCLLambda<ContainerType, Lambda, RemainingArgs...>{}),
534 launch_info, func, items, remaining_args...);
537 _doConstituentItemsLambda(0, vsize, items, func, remaining_args...);
542 _doConstituentItemsLambda(begin, size, items, func, remaining_args...);
546 ARCCORE_FATAL(
"Invalid execution policy '{0}'", exec_policy);
553template <
typename ConstituentCommandType,
typename... RemainingArgs,
typename Lambda>
554void operator<<(
const GenericConstituentCommand<ConstituentCommandType, RemainingArgs...>& c,
const Lambda& func)
556 if constexpr (
sizeof...(RemainingArgs) > 0) {
557 std::apply([&](
auto... vs) {
563 Impl::_applyConstituentCells(c.m_command.m_command, c.m_command.m_items, func);
569template <
typename ConstituentItemType,
typename ConstituentItemContainerType,
typename... RemainingArgs>
auto
570makeExtendedConstituentItemEnumeratorLoop(ConstituentItemType x,
571 const ConstituentItemContainerType& container,
572 const RemainingArgs&... remaining_args)
575 using TraitsType =
decltype(container_instance);
594inline Accelerator::Impl::EnvAndGlobalCellRunCommandContainer
597 return Accelerator::Impl::EnvAndGlobalCellRunCommandContainer{ env->
envView() };
599inline Accelerator::Impl::EnvAndGlobalCellRunCommandContainer
602 return Accelerator::Impl::EnvAndGlobalCellRunCommandContainer{ view };
609inline Accelerator::Impl::MatAndGlobalCellRunCommandContainer
612 return Accelerator::Impl::MatAndGlobalCellRunCommandContainer{ mat->
matView() };
614inline Accelerator::Impl::MatAndGlobalCellRunCommandContainer
617 return Accelerator::Impl::MatAndGlobalCellRunCommandContainer{ mat };
624inline Accelerator::Impl::AllEnvCellRunCommandContainer
634inline Accelerator::Impl::EnvCellRunCommandContainer
637 return Accelerator::Impl::EnvCellRunCommandContainer(env->
envView());
639inline Accelerator::Impl::EnvCellRunCommandContainer
642 return Accelerator::Impl::EnvCellRunCommandContainer(view);
649inline Accelerator::Impl::MatCellRunCommandContainer
652 return Accelerator::Impl::MatCellRunCommandContainer(mat->
matView());
654inline Accelerator::Impl::MatCellRunCommandContainer
657 return Accelerator::Impl::MatCellRunCommandContainer(view);
663inline Accelerator::Impl::EnvIndexedSelectionRunCommandContainer
666 return Accelerator::Impl::EnvIndexedSelectionRunCommandContainer{ view };
683template <
typename TraitsType,
typename... RemainingArgs>
auto
687 using CommandType =
typename ContainerType::CommandType;
689 return GenericCommandType(CommandType::create(command, args.m_container), args.m_remaining_args);
697operator<<(
RunCommand& command,
const Impl::MatAndGlobalCellRunCommandContainer& view)
699 using CommandType = Impl::MatAndGlobalCellRunCommandContainer::CommandType;
708operator<<(
RunCommand& command,
const Impl::EnvAndGlobalCellRunCommandContainer& view)
710 using CommandType = Impl::EnvAndGlobalCellRunCommandContainer::CommandType;
719operator<<(
RunCommand& command,
const Impl::EnvCellRunCommandContainer& view)
721 using CommandType = Impl::EnvCellRunCommandContainer::CommandType;
730operator<<(
RunCommand& command,
const Impl::MatCellRunCommandContainer& view)
732 using CommandType = Impl::MatCellRunCommandContainer::CommandType;
744#define A_RUNCOMMAND_MAT_ENUMERATE_BUILDER_HELPER(ConstituentItemNameType, env_or_mat_container, ...) \
745 ::Arcane::Accelerator::Impl::makeExtendedConstituentItemEnumeratorLoop(ConstituentItemNameType{}, env_or_mat_container __VA_OPT__(, __VA_ARGS__))
767#define RUNCOMMAND_MAT_ENUMERATE(ConstituentItemNameType, iter_name, env_or_mat_container, ...) \
768 A_FUNCINFO << A_RUNCOMMAND_MAT_ENUMERATE_BUILDER_HELPER(ConstituentItemNameType, env_or_mat_container __VA_OPT__(, __VA_ARGS__)) \
769 << [=] ARCCORE_HOST_DEVICE(typename decltype(A_RUNCOMMAND_MAT_ENUMERATE_BUILDER_HELPER(ConstituentItemNameType, env_or_mat_container __VA_OPT__(, __VA_ARGS__)))::IteratorValueType iter_name \
770 __VA_OPT__(ARCCORE_RUNCOMMAND_REMAINING_FOR_EACH(__VA_ARGS__)))
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
Classes, Types, and macros for managing concurrency.
void _applyConstituentCells(RunCommand &command, ContainerType items, const Lambda &func, const RemainingArgs &... remaining_args)
Applies the enumeration func over the entity list items.
Container for a command on AllEnvCell.
__host__ __device__ IteratorValueType operator[](Int32 i) const
Accessor for the i-th element of the list.
constexpr __host__ __device__ IteratorValueType operator[](Int32 i) const
Accessor for the i-th element of the list.
Base class of containers for commands on constituents (except for AllEnvCell).
constexpr __host__ __device__ IteratorValueType operator[](Int32 i) const
Accessor for the i-th element of the list.
Base class for commands on constituents.
constexpr __host__ __device__ IteratorValueType operator[](Int32 i) const
Accessor for the i-th element of the list.
static ARCCORE_DEVICE void applyAtEnd(Int32 index, RemainingArgs &... remaining_args)
Applies the functors of additional arguments at the end of the kernel.
static ARCCORE_DEVICE void applyAtBegin(Int32 index, RemainingArgs &... remaining_args)
Applies the functors of additional arguments at the beginning of the kernel.
void beginExecute()
Indicates that command execution is starting.
void endExecute()
Signals the end of execution.
Management of an accelerator command.
static void applyAtEnd(RemainingArgs &... remaining_args)
Applies the functors of additional arguments at the end of the iteration.
static void applyAtBegin(RemainingArgs &... remaining_args)
Applies the functors of additional arguments at the beginning of the iteration.
Arcane cell with material and environment information.
View over a vector of entities of a component.
Index of an accelerator loop over materials or media.
constexpr __host__ __device__ Int32 index() const
Index of the current iteration.
constexpr __host__ __device__ CellLocalId globalCellId() const
Accessor for the cell local id part.
constexpr __host__ __device__ Data operator()()
This operator allows returning the pair [ConstituentItemLocalIdType, CellLocalId].
constexpr __host__ __device__ ConstituentItemLocalIdType varIndex() const
Accessor for the MatVarIndex part.
Selection over an EnvCellVectorView.
Arcane cell of an environment.
Interface of a mesh environment.
virtual EnvItemVectorView envView() const =0
View associated with this environment.
Interface of a mesh material.
virtual MatItemVectorView matView() const =0
View associated with this material.
Represents a material in a multi-material cell.
Represents an index on material and environment variables.
View of an array of elements of type T.
void arcaneParallelFor(Integer i0, Integer size, InstanceType *itype, void(InstanceType::*lambda_function)(Integer i0, Integer size))
Applies the lambda function lambda_function concurrently over the iteration range [i0,...
Namespace for accelerator usage.
eExecutionPolicy
Execution policy for a Runner.
@ SYCL
Execution policy using the SYCL environment.
@ HIP
Execution policy using the HIP environment.
@ CUDA
Execution policy using the CUDA environment.
@ Sequential
Sequential execution policy.
@ Thread
Multi-threaded execution policy.
Always enables tracing in Arcane parts concerning materials.
EnvItemVectorView EnvCellVectorView
View type for an EnvCellVector.
ConstituentAndGlobalCellIteratorValue< EnvItemLocalId > EnvAndGlobalCellIteratorValue
Type of the iterator value for RUNCOMMAND_MAT_ENUMERATE(EnvAndGlobalCell,...).
ConstituentAndGlobalCellIteratorValue< MatItemLocalId > MatAndGlobalCellIteratorValue
Type of the iterator value for RUNCOMMAND_MAT_ENUMERATE(MatAndGlobalCell,...).
MatItemVectorView MatCellVectorView
View type for a MatCellVector.
Accelerator::Impl::EnvAndGlobalCellRunCommandContainer arcaneCreateRunCommandMaterialContainer(Arcane::Materials::EnvAndGlobalCell, Arcane::Materials::IMeshEnvironment *env)
Specialization for a view on an environment and the associated global cell.
std::int32_t Int32
Signed integer type of 32 bits.