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