Arcane  v3.14.10.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};
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
116{
117 protected:
118
119 hipError_t _allocate(void** ptr, size_t new_size, MemoryAllocationArgs) override
120 {
121 return ::hipHostMalloc(ptr, new_size);
122 }
123 hipError_t _deallocate(void* ptr, MemoryAllocationArgs) override
124 {
125 return ::hipHostFree(ptr);
126 }
127};
128
129/*---------------------------------------------------------------------------*/
130/*---------------------------------------------------------------------------*/
131
134{
135 protected:
136
137 hipError_t _allocate(void** ptr, size_t new_size, MemoryAllocationArgs) override
138 {
139 return ::hipMalloc(ptr, new_size);
140 }
141 hipError_t _deallocate(void* ptr, MemoryAllocationArgs) override
142 {
143 return ::hipFree(ptr);
144 }
145};
146
147/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149
150namespace
151{
155}
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
161getHipMemoryAllocator()
162{
164}
165
167getHipDeviceMemoryAllocator()
168{
170}
171
173getHipUnifiedMemoryAllocator()
174{
175 return &unified_memory_hip_memory_allocator;
176}
177
179getHipHostPinnedMemoryAllocator()
180{
181 return &host_pinned_hip_memory_allocator;
182}
183
184/*---------------------------------------------------------------------------*/
185/*---------------------------------------------------------------------------*/
186
187} // End namespace Arcane::accelerator::Hip
188
189/*---------------------------------------------------------------------------*/
190/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
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.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
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.