Arcane  v4.1.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
LocalMemory.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* LocalMemory.h (C) 2000-2025 */
9/* */
10/* Mémoire locale à une RunCommand. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_ACCELERATOR_LOCALMEMORY_H
13#define ARCANE_ACCELERATOR_LOCALMEMORY_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/accelerator/core/RunCommand.h"
18
19#include "arccore/base/Span.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane::Accelerator::Impl
25{
26#if defined(ARCANE_COMPILING_CUDA) || defined(ARCANE_COMPILING_HIP)
27inline __device__ std::byte* _getAcceleratorSharedMemory()
28{
29 extern __shared__ Int64 shared_memory_ptr[];
30 return reinterpret_cast<std::byte*>(shared_memory_ptr);
31}
32#endif
33
34} // namespace Arcane::Accelerator::Impl
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39namespace Arcane::Accelerator
40{
41
42/*---------------------------------------------------------------------------*/
43/*---------------------------------------------------------------------------*/
50template <typename T, Int32 Extent>
51class LocalMemory
52{
53 friend ::Arcane::Impl::HostKernelRemainingArgsHelper;
55
56 public:
57
58 static_assert(std::is_trivially_copyable_v<T>, "type T is not trivially copiable");
59
60 public:
61
62 using SpanType = SmallSpan<T, Extent>;
63
64 public:
65
66 LocalMemory(RunCommand& command, Int32 size)
67 : m_size(size)
68 {
69 _addShareMemory(command);
70 }
71
72 explicit LocalMemory(RunCommand& command) requires(Extent != DynExtent)
73 {
74 _addShareMemory(command);
75 }
76
77 constexpr ARCCORE_HOST_DEVICE SmallSpan<T, Extent> span()
78 {
79 return { m_ptr, m_size.size() };
80 }
81
82 private:
83
84#if defined(ARCANE_COMPILING_CUDA) || defined(ARCANE_COMPILING_HIP)
85 ARCCORE_DEVICE void _internalExecWorkItemAtBegin(Int32)
86 {
87 std::byte* begin = Impl::_getAcceleratorSharedMemory() + m_offset;
88 m_ptr = reinterpret_cast<T*>(begin);
89 }
90 ARCCORE_DEVICE void _internalExecWorkItemAtEnd(Int32){};
91#endif
92
93#if defined(ARCANE_COMPILING_SYCL)
94 void _internalExecWorkItemAtBegin(sycl::nd_item<1>, SmallSpan<std::byte> shm_view)
95 {
96 std::byte* begin = shm_view.ptrAt(m_offset);
97 m_ptr = reinterpret_cast<T*>(begin);
98 }
99 void _internalExecWorkItemAtEnd(sycl::nd_item<1>, SmallSpan<std::byte>) {}
100#endif
101
102 void _internalHostExecWorkItemAtBegin()
103 {
104 m_ptr = new T[m_size.size()];
105 }
106 void _internalHostExecWorkItemAtEnd()
107 {
108 delete[] m_ptr;
109 }
110
111 private:
112
113 T* m_ptr = nullptr;
114 // TODO: l'offset n'est utilisé on pourrait supprimer l'offset en le passant
119
120 protected:
121
122 void _addShareMemory(RunCommand& command)
123 {
124 m_offset = command._addSharedMemory(static_cast<Int32>(sizeof(T) * m_size.size()));
125 }
126};
127
128/*---------------------------------------------------------------------------*/
129/*---------------------------------------------------------------------------*/
130
131} // namespace Arcane::Accelerator
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
136#endif
Classe pour appliquer la finalisation pour les arguments supplémentaires.
Int32 m_offset
Offset depuis le début de la mémoire shared
::Arcane::Impl::ExtentStorage< Int32, Extent > m_size
Nombre d'éléments du tableau.
Gestion d'une commande sur accélérateur.
Spécialisation pour le nombre d'éléments connu à la compilation.
Definition Span.h:100
Vue d'un tableau d'éléments de type T.
Definition Span.h:774
constexpr __host__ __device__ pointer ptrAt(SizeType index) const
Adresse du index-ème élément.
Definition Span.h:339
Espace de nom pour l'utilisation des accélérateurs.
std::int64_t Int64
Type entier signé sur 64 bits.
constexpr Int32 DynExtent
Constante pour indiquer que la dimension d'un tableau est dynamique.
Definition BaseTypes.h:54
std::int32_t Int32
Type entier signé sur 32 bits.