Arcane  v3.16.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/*---------------------------------------------------------------------------*/
56class HipMemoryAllocatorBase
57: public Arccore::AlignedMemoryAllocator3
58{
59 public:
60
61 HipMemoryAllocatorBase()
62 : AlignedMemoryAllocator3(128)
63 {}
64
65 bool hasRealloc(MemoryAllocationArgs) const override { return true; }
66 AllocatedMemoryInfo allocate(MemoryAllocationArgs args, Int64 new_size) override
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 }
75 AllocatedMemoryInfo reallocate(MemoryAllocationArgs args, AllocatedMemoryInfo current_ptr, Int64 new_size) override
76 {
77 AllocatedMemoryInfo a = allocate(args, new_size);
78 ARCANE_CHECK_HIP(hipMemcpy(a.baseAddress(), current_ptr.baseAddress(), current_ptr.size(), hipMemcpyDefault));
79 deallocate(args, current_ptr);
80 return a;
81 }
82 void deallocate(MemoryAllocationArgs args, AllocatedMemoryInfo ptr) override
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
97: public HipMemoryAllocatorBase
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 }
110};
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
116: public HipMemoryAllocatorBase
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 }
129};
130
131/*---------------------------------------------------------------------------*/
132/*---------------------------------------------------------------------------*/
133
135: public HipMemoryAllocatorBase
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 }
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{
166 return &unified_memory_hip_memory_allocator;
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.
eMemoryResource memoryResource() const override
Ressource mémoire fournie par l'allocateur.
eMemoryResource memoryResource() const override
Ressource mémoire fournie par l'allocateur.
Informations sur une zone mémoire allouée.
void * baseAddress() const
Adresse du début de la zone allouée.
Int64 size() const
Taille en octets de la zone mémoire utilisée. (-1) si inconnue.
Exception lorsqu'une erreur fatale est survenue.
Classe contenant des informations pour spécialiser les allocations.
Chaîne de caractères unicode.
eMemoryResource
Liste des ressources mémoire disponibles.
@ HostPinned
Alloue sur l'hôte.
@ UnifiedMemory
Alloue en utilisant la mémoire unifiée.
@ Device
Alloue sur le device.