Arcane  v3.15.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
HipAccelerator.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 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/* HipAccelerator.cc (C) 2000-2024 */
9/* */
10/* Backend 'HIP' pour les accélérateurs. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/accelerator/hip/HipAccelerator.h"
15
16#include "arcane/utils/PlatformUtils.h"
17#include "arcane/utils/Array.h"
18#include "arcane/utils/TraceInfo.h"
19#include "arcane/utils/NotSupportedException.h"
20#include "arcane/utils/FatalErrorException.h"
21#include "arcane/utils/IMemoryAllocator.h"
22
23#include <iostream>
24
25namespace Arcane::Accelerator::Hip
26{
27
28using namespace Arccore;
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33void
34arcaneCheckHipErrors(const TraceInfo& ti,hipError_t e)
35{
36 if (e!=hipSuccess){
37 ARCANE_FATAL("HIP Error trace={0} e={1} str={2}",ti,e,hipGetErrorString(e));
38 }
39}
40
41void
42arcaneCheckHipErrorsNoThrow(const TraceInfo& ti,hipError_t e)
43{
44 if (e==hipSuccess)
45 return;
46 String str = String::format("HIP Error trace={0} e={1} str={2}",ti,e,hipGetErrorString(e));
47 FatalErrorException ex(ti,str);
48 ex.explain(std::cerr);
49}
50
51/*---------------------------------------------------------------------------*/
52/*---------------------------------------------------------------------------*/
58{
59 public:
60
63 {}
64
65 bool hasRealloc(MemoryAllocationArgs) const override { return true; }
67 {
68 void* out = nullptr;
69 ARCANE_CHECK_HIP(_allocate(&out, new_size, args));
70 Int64 a = reinterpret_cast<Int64>(out);
71 if ((a % 128) != 0)
72 ARCANE_FATAL("Bad alignment for HIP allocator: offset={0}", (a % 128));
73 return { out, new_size };
74 }
76 {
78 ARCANE_CHECK_HIP(hipMemcpy(a.baseAddress(), current_ptr.baseAddress(), current_ptr.size(), hipMemcpyDefault));
80 return a;
81 }
83 {
84 ARCANE_CHECK_HIP_NOTHROW(_deallocate(ptr.baseAddress(), args));
85 }
86
87 protected:
88
89 virtual hipError_t _allocate(void** ptr, size_t new_size, MemoryAllocationArgs) = 0;
90 virtual hipError_t _deallocate(void* ptr, MemoryAllocationArgs) = 0;
91};
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
98{
99 protected:
100
101 hipError_t _allocate(void** ptr, size_t new_size, MemoryAllocationArgs) override
102 {
103 return ::hipMallocManaged(ptr, new_size, hipMemAttachGlobal);
104 }
105 hipError_t _deallocate(void* ptr, MemoryAllocationArgs) override
106 {
107 return ::hipFree(ptr);
108 }
109 eMemoryResource memoryResource() const override { return eMemoryResource::UnifiedMemory; }
110};
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
117{
118 protected:
119
120 hipError_t _allocate(void** ptr, size_t new_size, MemoryAllocationArgs) override
121 {
122 return ::hipHostMalloc(ptr, new_size);
123 }
124 hipError_t _deallocate(void* ptr, MemoryAllocationArgs) override
125 {
126 return ::hipHostFree(ptr);
127 }
128 eMemoryResource memoryResource() const override { return eMemoryResource::HostPinned; }
129};
130
131/*---------------------------------------------------------------------------*/
132/*---------------------------------------------------------------------------*/
133
136{
137 protected:
138
139 hipError_t _allocate(void** ptr, size_t new_size, MemoryAllocationArgs) override
140 {
141 return ::hipMalloc(ptr, new_size);
142 }
143 hipError_t _deallocate(void* ptr, MemoryAllocationArgs) override
144 {
145 return ::hipFree(ptr);
146 }
147 eMemoryResource memoryResource() const override { return eMemoryResource::Device; }
148};
149
150/*---------------------------------------------------------------------------*/
151/*---------------------------------------------------------------------------*/
152
153namespace
154{
155 UnifiedMemoryHipMemoryAllocator unified_memory_hip_memory_allocator;
156 HostPinnedHipMemoryAllocator host_pinned_hip_memory_allocator;
157 DeviceHipMemoryAllocator device_hip_memory_allocator;
158}
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
164getHipMemoryAllocator()
165{
167}
168
170getHipDeviceMemoryAllocator()
171{
172 return &device_hip_memory_allocator;
173}
174
176getHipUnifiedMemoryAllocator()
177{
178 return &unified_memory_hip_memory_allocator;
179}
180
182getHipHostPinnedMemoryAllocator()
183{
184 return &host_pinned_hip_memory_allocator;
185}
186
187/*---------------------------------------------------------------------------*/
188/*---------------------------------------------------------------------------*/
189
190} // End namespace Arcane::accelerator::Hip
191
192/*---------------------------------------------------------------------------*/
193/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
eMemoryResource memoryResource() const override
Ressource mémoire fournie par l'allocateur.
Classe de base d'un allocateur spécifique pour 'Hip'.
AllocatedMemoryInfo allocate(MemoryAllocationArgs args, Int64 new_size) override
Alloue de la mémoire pour new_size octets et retourne le pointeur.
AllocatedMemoryInfo reallocate(MemoryAllocationArgs args, AllocatedMemoryInfo current_ptr, Int64 new_size) override
Réalloue de la mémoire pour new_size octets et retourne le pointeur.
void deallocate(MemoryAllocationArgs args, AllocatedMemoryInfo ptr) override
Libère la mémoire dont l'adresse de base est ptr.
bool hasRealloc(MemoryAllocationArgs) const override
Indique si l'allocateur supporte la sémantique de realloc.
eMemoryResource memoryResource() const override
Ressource mémoire fournie par l'allocateur.
eMemoryResource memoryResource() const override
Ressource mémoire fournie par l'allocateur.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:149
Allocateur mémoire avec alignement mémoire spécifique.
Informations sur une zone mémoire allouée.
Exception lorsqu'une erreur fatale est survenue.
Classe contenant des informations pour spécialiser les allocations.
Chaîne de caractères unicode.
Espace de nom de Arccore.
Definition ArcaneTypes.h:24
std::int64_t Int64
Type entier signé sur 64 bits.
eMemoryResource
Liste des ressources mémoire disponibles.