Neko 1.99.1
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 hipdevicegetname(name, len, device) &
127 bind(c, name = 'hipDeviceGetName')
128 use, intrinsic :: iso_c_binding
129 implicit none
130 type(c_ptr), value :: name
131 integer(c_int), value :: len
132 integer(c_int), value :: device
133 end function hipdevicegetname
134
135 integer(c_int) function hipgetdevicecount(amount) &
136 bind(c, name = 'hipGetDeviceCount')
137 use, intrinsic :: iso_c_binding
138 implicit none
139 integer(c_int) :: amount
140 end function hipgetdevicecount
141
142 integer(c_int) function hipstreamcreate(stream) &
143 bind(c, name = 'hipStreamCreate')
144 use, intrinsic :: iso_c_binding
145 implicit none
146 type(c_ptr) :: stream
147 end function hipstreamcreate
148
149 integer(c_int) function hipstreamcreatewithflags(stream, flags) &
150 bind(c, name = 'hipStreamCreateWithFlags')
151 use, intrinsic :: iso_c_binding
152 implicit none
153 type(c_ptr) :: stream
154 integer(c_int), value :: flags
155 end function hipstreamcreatewithflags
156
157 integer(c_int) function hipstreamcreatewithpriority(stream, flags, prio) &
158 bind(c, name = 'hipStreamCreateWithPriority')
159 use, intrinsic :: iso_c_binding
160 implicit none
161 type(c_ptr) :: stream
162 integer(c_int), value :: flags, prio
163 end function hipstreamcreatewithpriority
164
165 integer(c_int) function hipstreamdestroy(steam) &
166 bind(c, name = 'hipStreamDestroy')
167 use, intrinsic :: iso_c_binding
168 implicit none
169 type(c_ptr), value :: steam
170 end function hipstreamdestroy
171
172 integer(c_int) function hipstreamsynchronize(stream) &
173 bind(c, name = 'hipStreamSynchronize')
174 use, intrinsic :: iso_c_binding
175 implicit none
176 type(c_ptr), value :: stream
177 end function hipstreamsynchronize
178
179 integer(c_int) function hipstreamwaitevent(stream, event, flags) &
180 bind(c, name = 'hipStreamWaitEvent')
181 use, intrinsic :: iso_c_binding
182 implicit none
183 type(c_ptr), value :: stream, event
184 integer(c_int), value :: flags
185 end function hipstreamwaitevent
186
187 integer(c_int) function hipdevicegetstreampriorityrange(low_prio, &
188 high_prio) &
189 bind(c, name = 'hipDeviceGetStreamPriorityRange')
190 use, intrinsic :: iso_c_binding
191 implicit none
192 integer(c_int) :: low_prio, high_prio
194
195 integer(c_int) function hipeventcreate(event) &
196 bind(c, name = 'hipEventCreate')
197 use, intrinsic :: iso_c_binding
198 implicit none
199 type(c_ptr) :: event
200 end function hipeventcreate
201
202 integer(c_int) function hipeventdestroy(event) &
203 bind(c, name = 'hipEventDestroy')
204 use, intrinsic :: iso_c_binding
205 implicit none
206 type(c_ptr), value :: event
207 end function hipeventdestroy
208
209 integer(c_int) function hipeventcreatewithflags(event, flags) &
210 bind(c, name = 'hipEventCreateWithFlags')
211 use, intrinsic :: iso_c_binding
212 implicit none
213 type(c_ptr) :: event
214 integer(c_int), value :: flags
215 end function hipeventcreatewithflags
216
217 integer(c_int) function hipeventrecord(event, stream) &
218 bind(c, name = 'hipEventRecord')
219 use, intrinsic :: iso_c_binding
220 implicit none
221 type(c_ptr), value :: event, stream
222 end function hipeventrecord
223
224 integer(c_int) function hipeventsynchronize(event) &
225 bind(c, name = 'hipEventSynchronize')
226 use, intrinsic :: iso_c_binding
227 implicit none
228 type(c_ptr), value :: event
229 end function hipeventsynchronize
230 end interface
231
232contains
233
234 subroutine hip_init(glb_cmd_queue, aux_cmd_queue, &
235 STRM_HIGH_PRIO, STRM_LOW_PRIO)
236 type(c_ptr), intent(inout) :: glb_cmd_queue
237 type(c_ptr), intent(inout) :: aux_cmd_queue
238 integer, intent(inout) :: STRM_HIGH_PRIO
239 integer, intent(inout) :: STRM_LOW_PRIO
240
241 if (hipdevicegetstreampriorityrange(strm_low_prio, strm_high_prio) &
242 .ne. hipsuccess) then
243 call neko_error('Error retrieving stream priority range')
244 end if
245
246 if (hipstreamcreatewithpriority(glb_cmd_queue, 1, strm_high_prio) &
247 .ne. hipsuccess) then
248 call neko_error('Error creating main stream')
249 end if
250
251 if (hipstreamcreatewithpriority(aux_cmd_queue, 1, strm_low_prio) &
252 .ne. hipsuccess) then
253 call neko_error('Error creating main stream')
254 end if
255 end subroutine hip_init
256
257 subroutine hip_finalize(glb_cmd_queue, aux_cmd_queue)
258 type(c_ptr), intent(inout) :: glb_cmd_queue
259 type(c_ptr), intent(inout) :: aux_cmd_queue
260
261 if (hipstreamdestroy(glb_cmd_queue) .ne. hipsuccess) then
262 call neko_error('Error destroying main stream')
263 end if
264
265 if (hipstreamdestroy(aux_cmd_queue) .ne. hipsuccess) then
266 call neko_error('Error destroying aux stream')
267 end if
268 end subroutine hip_finalize
269
270 subroutine hip_device_name(name)
271 character(len=*), intent(inout) :: name
272 character(kind=c_char, len=1024), target :: c_name
273 integer :: end_pos
274
275 if (hipdevicegetname(c_loc(c_name), 1024, 0) .ne. hipsuccess) then
276 call neko_error('Failed to query device')
277 end if
278
279 end_pos = scan(c_name, c_null_char)
280 if (end_pos .ge. 2) then
281 name(1:end_pos-1) = c_name(1:end_pos-1)
282 end if
283
284 end subroutine hip_device_name
285
287 integer function hip_device_count()
288 type(c_ptr) :: hip_count_ptr
289 integer :: amount
290
291 if (hipgetdevicecount(amount) .ne. hipsuccess) then
292 call neko_error('Failed to query device count')
293 end if
294
295 hip_device_count = amount
296 end function hip_device_count
297
298#endif
299
300end 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:271
@ 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:236
subroutine hip_finalize(glb_cmd_queue, aux_cmd_queue)
Definition hip_intf.F90:258
@ 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:288
Utilities.
Definition utils.f90:35