12#ifndef ARCANE_ACCELERATOR_COMMONUTILS_H
13#define ARCANE_ACCELERATOR_COMMONUTILS_H
17#include "arcane/utils/Array.h"
19#include "arcane/accelerator/AcceleratorGlobal.h"
20#include "arcane/accelerator/core/RunQueue.h"
21#include "arcane/accelerator/AcceleratorUtils.h"
23#if defined(ARCANE_COMPILING_HIP)
24#include "arcane/accelerator/hip/HipAccelerator.h"
25#include <rocprim/rocprim.hpp>
27#if defined(ARCANE_COMPILING_CUDA)
28#include "arcane/accelerator/cuda/CudaAccelerator.h"
31#if defined(ARCANE_COMPILING_SYCL)
32#include "arcane/accelerator/sycl/SyclAccelerator.h"
33#if defined(__INTEL_LLVM_COMPILER)
34#include <oneapi/dpl/execution>
35#include <oneapi/dpl/algorithm>
45namespace Arcane::Accelerator::impl
68template <
typename LambdaType>
71 static constexpr size_t SizeofLambda =
sizeof(LambdaType);
78 std::memcpy(bytes, &v, SizeofLambda);
81 ARCCORE_HOST_DEVICE
operator const LambdaType&()
const {
return *
reinterpret_cast<const LambdaType*
>(&bytes); }
85 char bytes[SizeofLambda];
106 void* address() {
return m_storage.data(); }
107 size_t size()
const {
return m_storage.largeSize(); }
108 void* allocate(
size_t new_size)
110 m_storage.resize(new_size);
111 return m_storage.data();
121 return m_storage.span();
153template <
typename DataType, Int32 N = 1>
159 DataType* address() {
return reinterpret_cast<DataType*
>(m_storage.address()); }
160 size_t size()
const {
return m_storage.size(); }
163 m_storage.allocate(
sizeof(DataType) * N);
166 void deallocate() { m_storage.deallocate(); }
186 using value_type =
Int32;
187 using iterator_category = std::random_access_iterator_tag;
188 using reference = value_type&;
189 using difference_type = ptrdiff_t;
190 using pointer = void;
222 return m_value - x.m_value;
225 ARCCORE_HOST_DEVICE
Int32 operator[](
Int32 x)
const {
return m_value + x; }
228 return a.m_value == b.m_value;
232 return iter1.m_value < iter2.m_value;
244template <
typename DataType>
249 constexpr ARCCORE_HOST_DEVICE DataType operator()(
const DataType& a,
const DataType& b)
const
253 static DataType defaultValue() {
return {}; }
254#if defined(ARCANE_COMPILING_SYCL)
255 static sycl::plus<DataType> syclFunctor() {
return {}; }
263template <
typename DataType>
268 constexpr ARCCORE_HOST_DEVICE DataType operator()(
const DataType& a,
const DataType& b)
const
270 return (a < b) ? a : b;
272 static DataType defaultValue() {
return std::numeric_limits<DataType>::max(); }
273#if defined(ARCANE_COMPILING_SYCL)
274 static sycl::minimum<DataType> syclFunctor() {
return {}; }
282template <
typename DataType>
287 constexpr ARCCORE_HOST_DEVICE DataType operator()(
const DataType& a,
const DataType& b)
const
289 return (a < b) ? b : a;
291 static DataType defaultValue() {
return std::numeric_limits<DataType>::lowest(); }
292#if defined(ARCANE_COMPILING_SYCL)
293 static sycl::maximum<DataType> syclFunctor() {
return {}; }
302template <
typename DataType,
typename GetterLambda>
307 using value_type = DataType;
308 using iterator_category = std::random_access_iterator_tag;
309 using reference = DataType&;
310 using difference_type = ptrdiff_t;
311 using pointer = void;
326 ARCCORE_HOST_DEVICE ThatClass& operator++()
331 ARCCORE_HOST_DEVICE ThatClass& operator+=(
Int32 x)
336 ARCCORE_HOST_DEVICE
friend ThatClass operator+(
const ThatClass& iter,
Int32 x)
338 return ThatClass(iter.m_lambda, iter.m_index + x);
340 ARCCORE_HOST_DEVICE
friend ThatClass operator+(
Int32 x,
const ThatClass& iter)
342 return ThatClass(iter.m_lambda, iter.m_index + x);
344 ARCCORE_HOST_DEVICE
friend bool operator<(
const ThatClass& iter1,
const ThatClass& iter2)
346 return iter1.m_index < iter2.m_index;
349 ARCCORE_HOST_DEVICE ThatClass operator-(
Int32 x)
const
351 return ThatClass(m_lambda, m_index - x);
353 ARCCORE_HOST_DEVICE
Int32 operator-(
const ThatClass& x)
const
355 return m_index - x.m_index;
357 ARCCORE_HOST_DEVICE value_type
operator*()
const
359 return m_lambda(m_index);
361 ARCCORE_HOST_DEVICE value_type operator[](
Int32 x)
const {
return m_lambda(m_index + x); }
362 ARCCORE_HOST_DEVICE
friend bool operator!=(
const ThatClass& a,
const ThatClass& b)
364 return a.m_index != b.m_index;
366 ARCCORE_HOST_DEVICE
friend bool operator==(
const ThatClass& a,
const ThatClass& b)
368 return a.m_index == b.m_index;
374 GetterLambda m_lambda;
384template <
typename SetterLambda>
394 ARCCORE_HOST_DEVICE
explicit Setter(
const SetterLambda& s,
Int32 output_index)
395 : m_output_index(output_index)
398 ARCCORE_HOST_DEVICE
void operator=(
Int32 input_index)
400 m_lambda(input_index, m_output_index);
402 Int32 m_output_index = 0;
403 SetterLambda m_lambda;
406 using value_type =
Int32;
407 using iterator_category = std::random_access_iterator_tag;
409 using difference_type = ptrdiff_t;
410 using pointer = void;
416#ifdef ARCANE_USE_LAMBDA_STORAGE
419 using StorageType = SetterLambda;
435#ifdef ARCANE_USE_LAMBDA_STORAGE
436 ARCCORE_HOST_DEVICE SetterLambdaIterator(
const LambdaStorage<SetterLambda>& s, Int32 v)
444 ARCCORE_HOST_DEVICE SetterLambdaIterator<SetterLambda>& operator++()
449 ARCCORE_HOST_DEVICE SetterLambdaIterator<SetterLambda>& operator--()
454 ARCCORE_HOST_DEVICE reference
operator*()
const
456 return Setter(m_lambda, m_index);
458 ARCCORE_HOST_DEVICE reference operator[](Int32 x)
const {
return Setter(m_lambda, m_index + x); }
459 ARCCORE_HOST_DEVICE
friend ThatClass operator+(Int32 x,
const ThatClass& iter)
461 return ThatClass(iter.m_lambda, iter.m_index + x);
463 ARCCORE_HOST_DEVICE
friend ThatClass operator+(
const ThatClass& iter, Int32 x)
465 return ThatClass(iter.m_lambda, iter.m_index + x);
467 ARCCORE_HOST_DEVICE
friend ThatClass operator-(
const ThatClass& iter, Int32 x)
469 return ThatClass(iter.m_lambda, iter.m_index - x);
471 ARCCORE_HOST_DEVICE
friend Int32 operator-(
const ThatClass& iter1,
const ThatClass& iter2)
473 return iter1.m_index - iter2.m_index;
475 ARCCORE_HOST_DEVICE
friend bool operator<(
const ThatClass& iter1,
const ThatClass& iter2)
477 return iter1.m_index < iter2.m_index;
483 StorageType m_lambda;
500template <
typename DataType>
using ScannerSumOperator = impl::SumOperator<DataType>;
501template <
typename DataType>
using ScannerMaxOperator = impl::MaxOperator<DataType>;
502template <
typename DataType>
using ScannerMinOperator = impl::MinOperator<DataType>;
512#ifdef ARCANE_USE_LAMBDA_STORAGE
513#undef ARCANE_USE_LAMBDA_STORAGE
File d'exécution pour un accélérateur.
void copyToAsync(SmallSpan< DataType > dest_ptr, const RunQueue &queue)
Copie l'instance dans dest_ptr.
Itérateur sur une lambda pour récupérer une valeur via un index.
Classe pour gérer la conservation d'une lambda dans un itérateur.
Opérateur de Scan/Reduce pour le maximum.
Opérateur de Scan/Reduce pour le minimum.
Permet de positionner un élément de l'itérateur de sortie.
Itérateur sur une lambda pour positionner une valeur via un index.
Opérateur de Scan/Reduce pour les sommes.
Vue d'un tableau d'éléments de type T.
Vue d'un tableau d'éléments de type T.
Vecteur 1D de données avec sémantique par valeur (style STL).
Espace de nom pour l'utilisation des accélérateurs.
Real2 operator*(Real sca, const Real2Proxy &vec)
Multiplication par un scalaire.
bool operator<(const Item &item1, const Item &item2)
Compare deux entités.
detail::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converti la vue en un tableau d'octets modifiables.
std::int32_t Int32
Type entier signé sur 32 bits.