Neko 1.99.3
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
hip_intf.F90
Go to the documentation of this file.
1! Copyright (c) 2021-2025, The Neko Authors
2! All rights reserved.
3!
4! Redistribution and use in source and binary forms, with or without
5! modification, are permitted provided that the following conditions
6! are met:
7!
8! * Redistributions of source code must retain the above copyright
9! notice, this list of conditions and the following disclaimer.
10!
11! * Redistributions in binary form must reproduce the above
12! copyright notice, this list of conditions and the following
13! disclaimer in the documentation and/or other materials provided
14! with the distribution.
15!
16! * Neither the name of the authors nor the names of its
17! contributors may be used to endorse or promote products derived
18! from this software without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31! POSSIBILITY OF SUCH DAMAGE.
32!
35 use utils, only : neko_error
36 use, intrinsic :: iso_c_binding
37 implicit none
38
39#ifdef HAVE_HIP
40
42 enum, bind(c)
43 enumerator :: hipsuccess = 0
44 enumerator :: hiperrorinvalidcontext = 1
45 enumerator :: hiperrorinvalidkernelfile = 2
46 enumerator :: hiperrormemoryallocation = 3
48 enumerator :: hiperrorlaunchfailure = 5
50 enumerator :: hiperrorinvaliddevice = 7
51 enumerator :: hiperrorinvalidvalue = 8
54 enumerator :: hiperrorunknown = 11
56 enumerator :: hiperrornotready = 13
57 enumerator :: hiperrornodevice = 14
60 enumerator :: hiperrorruntimememory = 17
61 enumerator :: hiperrorruntimeother = 18
65 enumerator :: hiperrortbd = 22
66 end enum
67
69 enum, bind(c)
70 enumerator :: hipmemcpyhosttohost = 0
71 enumerator :: hipmemcpyhosttodevice = 1
72 enumerator :: hipmemcpydevicetohost = 2
73 enumerator :: hipmemcpydevicetodevice = 3
74 enumerator :: hipmemcpydefault = 4
75 end enum
76
77 interface
78 integer(c_int) function hipmalloc(ptr_d, s) &
79 bind(c, name = 'hipMalloc')
80 use, intrinsic :: iso_c_binding
81 implicit none
82 type(c_ptr) :: ptr_d
83 integer(c_size_t), value :: s
84 end function hipmalloc
85
86 integer(c_int) function hipfree(ptr_d) &
87 bind(c, name = 'hipFree')
88 use, intrinsic :: iso_c_binding
89 implicit none
90 type(c_ptr), value :: ptr_d
91 end function hipfree
92
93 integer(c_int) function hipmemcpy(ptr_dst, ptr_src, s, dir) &
94 bind(c, name = 'hipMemcpy')
95 use, intrinsic :: iso_c_binding
96 implicit none
97 type(c_ptr), value :: ptr_dst, ptr_src
98 integer(c_size_t), value :: s
99 integer(c_int), value :: dir
100 end function hipmemcpy
101
102 integer(c_int) function hipmemcpyasync(ptr_dst, ptr_src, s, dir, stream) &
103 bind(c, name = 'hipMemcpyAsync')
104 use, intrinsic :: iso_c_binding
105 implicit none
106 type(c_ptr), value :: ptr_dst, ptr_src, stream
107 integer(c_size_t), value :: s
108 integer(c_int), value :: dir
109 end function hipmemcpyasync
110
111 integer(c_int) function hipmemsetasync(ptr, v, s, stream) &
112 bind(c, name = 'hipMemsetAsync')
113 use, intrinsic :: iso_c_binding
114 implicit none
115 type(c_ptr), value :: ptr, stream
116 integer(c_int), value :: v
117 integer(c_size_t), value :: s
118 end function hipmemsetasync
119
120 integer(c_int) function hipdevicesynchronize() &
121 bind(c, name = 'hipDeviceSynchronize')
122 use, intrinsic :: iso_c_binding
123 implicit none
124 end function hipdevicesynchronize
125
126 integer(c_int) function hipdevicereset() &
127 bind(c, name = 'hipDeviceReset')
128 use, intrinsic :: iso_c_binding
129 implicit none
130 end function hipdevicereset
131
132 integer(c_int) function hipdevicegetname(name, len, device) &
133 bind(c, name = 'hipDeviceGetName')
134 use, intrinsic :: iso_c_binding
135 implicit none
136 type(c_ptr), value :: name
137 integer(c_int), value :: len
138 integer(c_int), value :: device
139 end function hipdevicegetname
140
141 integer(c_int) function hipgetdevicecount(amount) &
142 bind(c, name = 'hipGetDeviceCount')
143 use, intrinsic :: iso_c_binding
144 implicit none
145 integer(c_int) :: amount
146 end function hipgetdevicecount
147
148 integer(c_int) function hipstreamcreate(stream) &
149 bind(c, name = 'hipStreamCreate')
150 use, intrinsic :: iso_c_binding
151 implicit none
152 type(c_ptr) :: stream
153 end function hipstreamcreate
154
155 integer(c_int) function hipstreamcreatewithflags(stream, flags) &
156 bind(c, name = 'hipStreamCreateWithFlags')
157 use, intrinsic :: iso_c_binding
158 implicit none
159 type(c_ptr) :: stream
160 integer(c_int), value :: flags
161 end function hipstreamcreatewithflags
162
163 integer(c_int) function hipstreamcreatewithpriority(stream, flags, prio) &
164 bind(c, name = 'hipStreamCreateWithPriority')
165 use, intrinsic :: iso_c_binding
166 implicit none
167 type(c_ptr) :: stream
168 integer(c_int), value :: flags, prio
169 end function hipstreamcreatewithpriority
170
171 integer(c_int) function hipstreamdestroy(steam) &
172 bind(c, name = 'hipStreamDestroy')
173 use, intrinsic :: iso_c_binding
174 implicit none
175 type(c_ptr), value :: steam
176 end function hipstreamdestroy
177
178 integer(c_int) function hipstreamsynchronize(stream) &
179 bind(c, name = 'hipStreamSynchronize')
180 use, intrinsic :: iso_c_binding
181 implicit none
182 type(c_ptr), value :: stream
183 end function hipstreamsynchronize
184
185 integer(c_int) function hipstreamwaitevent(stream, event, flags) &
186 bind(c, name = 'hipStreamWaitEvent')
187 use, intrinsic :: iso_c_binding
188 implicit none
189 type(c_ptr), value :: stream, event
190 integer(c_int), value :: flags
191 end function hipstreamwaitevent
192
193 integer(c_int) function hipdevicegetstreampriorityrange(low_prio, &
194 high_prio) &
195 bind(c, name = 'hipDeviceGetStreamPriorityRange')
196 use, intrinsic :: iso_c_binding
197 implicit none
198 integer(c_int) :: low_prio, high_prio
200
201 integer(c_int) function hipeventcreate(event) &
202 bind(c, name = 'hipEventCreate')
203 use, intrinsic :: iso_c_binding
204 implicit none
205 type(c_ptr) :: event
206 end function hipeventcreate
207
208 integer(c_int) function hipeventdestroy(event) &
209 bind(c, name = 'hipEventDestroy')
210 use, intrinsic :: iso_c_binding
211 implicit none
212 type(c_ptr), value :: event
213 end function hipeventdestroy
214
215 integer(c_int) function hipeventcreatewithflags(event, flags) &
216 bind(c, name = 'hipEventCreateWithFlags')
217 use, intrinsic :: iso_c_binding
218 implicit none
219 type(c_ptr) :: event
220 integer(c_int), value :: flags
221 end function hipeventcreatewithflags
222
223 integer(c_int) function hipeventrecord(event, stream) &
224 bind(c, name = 'hipEventRecord')
225 use, intrinsic :: iso_c_binding
226 implicit none
227 type(c_ptr), value :: event, stream
228 end function hipeventrecord
229
230 integer(c_int) function hipeventsynchronize(event) &
231 bind(c, name = 'hipEventSynchronize')
232 use, intrinsic :: iso_c_binding
233 implicit none
234 type(c_ptr), value :: event
235 end function hipeventsynchronize
236 end interface
237
238contains
239
240 subroutine hip_init(glb_cmd_queue, aux_cmd_queue, &
241 STRM_HIGH_PRIO, STRM_LOW_PRIO)
242 type(c_ptr), intent(inout) :: glb_cmd_queue
243 type(c_ptr), intent(inout) :: aux_cmd_queue
244 integer, intent(inout) :: STRM_HIGH_PRIO
245 integer, intent(inout) :: STRM_LOW_PRIO
246
247 if (hipdevicegetstreampriorityrange(strm_low_prio, strm_high_prio) &
248 .ne. hipsuccess) then
249 call neko_error('Error retrieving stream priority range')
250 end if
251
252 if (hipstreamcreatewithpriority(glb_cmd_queue, 1, strm_high_prio) &
253 .ne. hipsuccess) then
254 call neko_error('Error creating main stream')
255 end if
256
257 if (hipstreamcreatewithpriority(aux_cmd_queue, 1, strm_low_prio) &
258 .ne. hipsuccess) then
259 call neko_error('Error creating main stream')
260 end if
261 end subroutine hip_init
262
263 subroutine hip_finalize(glb_cmd_queue, aux_cmd_queue)
264 type(c_ptr), intent(inout) :: glb_cmd_queue
265 type(c_ptr), intent(inout) :: aux_cmd_queue
266 integer(c_int) :: ierr
267
268 if (hipstreamdestroy(glb_cmd_queue) .ne. hipsuccess) then
269 call neko_error('Error destroying main stream')
270 end if
271
272 if (hipstreamdestroy(aux_cmd_queue) .ne. hipsuccess) then
273 call neko_error('Error destroying aux stream')
274 end if
275
276 ! Best-effort context teardown to release runtime-owned allocations.
277 ierr = hipdevicesynchronize()
278 ierr = hipdevicereset()
279 end subroutine hip_finalize
280
281 subroutine hip_device_name(name)
282 character(len=*), intent(inout) :: name
283 character(kind=c_char, len=1024), target :: c_name
284 integer :: end_pos
285
286 if (hipdevicegetname(c_loc(c_name), 1024, 0) .ne. hipsuccess) then
287 call neko_error('Failed to query device')
288 end if
289
290 end_pos = scan(c_name, c_null_char)
291 if (end_pos .ge. 2) then
292 name(1:end_pos-1) = c_name(1:end_pos-1)
293 end if
294
295 end subroutine hip_device_name
296
298 integer function hip_device_count()
299 type(c_ptr) :: hip_count_ptr
300 integer :: amount
301
302 if (hipgetdevicecount(amount) .ne. hipsuccess) then
303 call neko_error('Failed to query device count')
304 end if
305
306 hip_device_count = amount
307 end function hip_device_count
308
309#endif
310
311end module hip_intf
Device abstraction, common interface for various accelerators.
Definition device.F90:34
Fortran HIP interface.
Definition hip_intf.F90:34
subroutine hip_device_name(name)
Definition hip_intf.F90:282
@ hipmemcpyhosttohost
Definition hip_intf.F90:70
@ hipmemcpydevicetohost
Definition hip_intf.F90:72
@ hipmemcpydefault
Definition hip_intf.F90:74
@ hipmemcpydevicetodevice
Definition hip_intf.F90:73
@ hipmemcpyhosttodevice
Definition hip_intf.F90:71
subroutine hip_init(glb_cmd_queue, aux_cmd_queue, strm_high_prio, strm_low_prio)
Definition hip_intf.F90:242
subroutine hip_finalize(glb_cmd_queue, aux_cmd_queue)
Definition hip_intf.F90:264
@ hiperrortbd
Definition hip_intf.F90:65
@ hiperrormapbufferobjectfailed
Definition hip_intf.F90:64
@ hiperrorinvaliddevicepointer
Definition hip_intf.F90:52
@ hiperrorlaunchfailure
Definition hip_intf.F90:48
@ hiperrorpeeraccessnotenabled
Definition hip_intf.F90:59
@ hiperrorinvalidkernelfile
Definition hip_intf.F90:45
@ hiperrorpeeraccessalreadyenabled
Definition hip_intf.F90:58
@ hiperrorinvalidresourcehandle
Definition hip_intf.F90:55
@ hiperrorhostmemorynotregistered
Definition hip_intf.F90:63
@ hiperrormemoryallocation
Definition hip_intf.F90:46
@ hiperrorruntimeother
Definition hip_intf.F90:61
@ hiperrorlaunchoutofresources
Definition hip_intf.F90:49
@ hiperrorinvalidmemcpydirection
Definition hip_intf.F90:53
@ hiperrornotready
Definition hip_intf.F90:56
@ hiperrorruntimememory
Definition hip_intf.F90:60
@ hiperrornodevice
Definition hip_intf.F90:57
@ hiperrorinvalidvalue
Definition hip_intf.F90:51
@ hiperrorinitializationerror
Definition hip_intf.F90:47
@ hiperrorinvaliddevice
Definition hip_intf.F90:50
@ hiperrorinvalidcontext
Definition hip_intf.F90:44
@ hiperrorhostmemoryalreadyregistered
Definition hip_intf.F90:62
@ hiperrorunknown
Definition hip_intf.F90:54
integer function hip_device_count()
Return the number of available HIP devices.
Definition hip_intf.F90:299
Utilities.
Definition utils.f90:35