Neko  0.9.99
A portable framework for high-order spectral element flow simulations
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 !
34 module hip_intf
35  use utils, only : neko_error
36  use, intrinsic :: iso_c_binding
37  implicit none
38 
39 #ifdef HAVE_HIP
40 
42  type(c_ptr), bind(c) :: glb_cmd_queue = c_null_ptr
43 
45  type(c_ptr), bind(c) :: aux_cmd_queue = c_null_ptr
46 
48  integer :: strm_high_prio
49 
51  integer :: strm_low_prio
52 
54  enum, bind(c)
55  enumerator :: hipsuccess = 0
56  enumerator :: hiperrorinvalidcontext = 1
57  enumerator :: hiperrorinvalidkernelfile = 2
58  enumerator :: hiperrormemoryallocation = 3
59  enumerator :: hiperrorinitializationerror = 4
60  enumerator :: hiperrorlaunchfailure = 5
61  enumerator :: hiperrorlaunchoutofresources = 6
62  enumerator :: hiperrorinvaliddevice = 7
63  enumerator :: hiperrorinvalidvalue = 8
64  enumerator :: hiperrorinvaliddevicepointer = 9
65  enumerator :: hiperrorinvalidmemcpydirection = 10
66  enumerator :: hiperrorunknown = 11
67  enumerator :: hiperrorinvalidresourcehandle = 12
68  enumerator :: hiperrornotready = 13
69  enumerator :: hiperrornodevice = 14
71  enumerator :: hiperrorpeeraccessnotenabled = 16
72  enumerator :: hiperrorruntimememory = 17
73  enumerator :: hiperrorruntimeother = 18
76  enumerator :: hiperrormapbufferobjectfailed = 21
77  enumerator :: hiperrortbd = 22
78  end enum
79 
81  enum, bind(c)
82  enumerator :: hipmemcpyhosttohost = 0
83  enumerator :: hipmemcpyhosttodevice = 1
84  enumerator :: hipmemcpydevicetohost = 2
85  enumerator :: hipmemcpydevicetodevice = 3
86  enumerator :: hipmemcpydefault = 4
87  end enum
88 
89  interface
90  integer(c_int) function hipmalloc(ptr_d, s) &
91  bind(c, name = 'hipMalloc')
92  use, intrinsic :: iso_c_binding
93  implicit none
94  type(c_ptr) :: ptr_d
95  integer(c_size_t), value :: s
96  end function hipmalloc
97 
98  integer(c_int) function hipfree(ptr_d) &
99  bind(c, name = 'hipFree')
100  use, intrinsic :: iso_c_binding
101  implicit none
102  type(c_ptr), value :: ptr_d
103  end function hipfree
104 
105  integer(c_int) function hipmemcpy(ptr_dst, ptr_src, s, dir) &
106  bind(c, name = 'hipMemcpy')
107  use, intrinsic :: iso_c_binding
108  implicit none
109  type(c_ptr), value :: ptr_dst, ptr_src
110  integer(c_size_t), value :: s
111  integer(c_int), value :: dir
112  end function hipmemcpy
113 
114  integer(c_int) function hipmemcpyasync(ptr_dst, ptr_src, s, dir, stream) &
115  bind(c, name = 'hipMemcpyAsync')
116  use, intrinsic :: iso_c_binding
117  implicit none
118  type(c_ptr), value :: ptr_dst, ptr_src, stream
119  integer(c_size_t), value :: s
120  integer(c_int), value :: dir
121  end function hipmemcpyasync
122 
123  integer(c_int) function hipdevicesynchronize() &
124  bind(c, name = 'hipDeviceSynchronize')
125  use, intrinsic :: iso_c_binding
126  implicit none
127  end function hipdevicesynchronize
128 
129  integer(c_int) function hipdevicegetname(name, len, device) &
130  bind(c, name = 'hipDeviceGetName')
131  use, intrinsic :: iso_c_binding
132  implicit none
133  type(c_ptr), value :: name
134  integer(c_int), value :: len
135  integer(c_int), value :: device
136  end function hipdevicegetname
137 
138  integer(c_int) function hipgetdevicecount(amount) &
139  bind(c, name = 'hipGetDeviceCount')
140  use, intrinsic :: iso_c_binding
141  implicit none
142  integer(c_int) :: amount
143  end function hipgetdevicecount
144 
145  integer(c_int) function hipstreamcreate(stream) &
146  bind(c, name = 'hipStreamCreate')
147  use, intrinsic :: iso_c_binding
148  implicit none
149  type(c_ptr) :: stream
150  end function hipstreamcreate
151 
152  integer(c_int) function hipstreamcreatewithflags(stream, flags) &
153  bind(c, name = 'hipStreamCreateWithFlags')
154  use, intrinsic :: iso_c_binding
155  implicit none
156  type(c_ptr) :: stream
157  integer(c_int), value :: flags
158  end function hipstreamcreatewithflags
159 
160  integer(c_int) function hipstreamcreatewithpriority(stream, flags, prio) &
161  bind(c, name = 'hipStreamCreateWithPriority')
162  use, intrinsic :: iso_c_binding
163  implicit none
164  type(c_ptr) :: stream
165  integer(c_int), value :: flags, prio
166  end function hipstreamcreatewithpriority
167 
168  integer(c_int) function hipstreamdestroy(steam) &
169  bind(c, name = 'hipStreamDestroy')
170  use, intrinsic :: iso_c_binding
171  implicit none
172  type(c_ptr), value :: steam
173  end function hipstreamdestroy
174 
175  integer(c_int) function hipstreamsynchronize(stream) &
176  bind(c, name = 'hipStreamSynchronize')
177  use, intrinsic :: iso_c_binding
178  implicit none
179  type(c_ptr), value :: stream
180  end function hipstreamsynchronize
181 
182  integer(c_int) function hipstreamwaitevent(stream, event, flags) &
183  bind(c, name = 'hipStreamWaitEvent')
184  use, intrinsic :: iso_c_binding
185  implicit none
186  type(c_ptr), value :: stream, event
187  integer(c_int), value :: flags
188  end function hipstreamwaitevent
189 
190  integer(c_int) function hipdevicegetstreampriorityrange(low_prio, &
191  high_prio) &
192  bind(c, name = 'hipDeviceGetStreamPriorityRange')
193  use, intrinsic :: iso_c_binding
194  implicit none
195  integer(c_int) :: low_prio, high_prio
196  end function hipdevicegetstreampriorityrange
197 
198  integer(c_int) function hipeventcreate(event) &
199  bind(c, name = 'hipEventCreate')
200  use, intrinsic :: iso_c_binding
201  implicit none
202  type(c_ptr) :: event
203  end function hipeventcreate
204 
205  integer(c_int) function hipeventdestroy(event) &
206  bind(c, name = 'hipEventDestroy')
207  use, intrinsic :: iso_c_binding
208  implicit none
209  type(c_ptr), value :: event
210  end function hipeventdestroy
211 
212  integer(c_int) function hipeventcreatewithflags(event, flags) &
213  bind(c, name = 'hipEventCreateWithFlags')
214  use, intrinsic :: iso_c_binding
215  implicit none
216  type(c_ptr) :: event
217  integer(c_int), value :: flags
218  end function hipeventcreatewithflags
219 
220  integer(c_int) function hipeventrecord(event, stream) &
221  bind(c, name = 'hipEventRecord')
222  use, intrinsic :: iso_c_binding
223  implicit none
224  type(c_ptr), value :: event, stream
225  end function hipeventrecord
226 
227  integer(c_int) function hipeventsynchronize(event) &
228  bind(c, name = 'hipEventSynchronize')
229  use, intrinsic :: iso_c_binding
230  implicit none
231  type(c_ptr), value :: event
232  end function hipeventsynchronize
233  end interface
234 
235 contains
236 
237  subroutine hip_init
238 
239  if (hipdevicegetstreampriorityrange(strm_low_prio, strm_high_prio) &
240  .ne. hipsuccess) then
241  call neko_error('Error retrieving stream priority range')
242  end if
243 
244  if (hipstreamcreatewithpriority(glb_cmd_queue, 1, strm_high_prio) &
245  .ne. hipsuccess) then
246  call neko_error('Error creating main stream')
247  end if
248 
249  if (hipstreamcreatewithpriority(aux_cmd_queue, 1, strm_low_prio) &
250  .ne. hipsuccess) then
251  call neko_error('Error creating main stream')
252  end if
253  end subroutine hip_init
254 
255  subroutine hip_finalize
256  if (hipstreamdestroy(glb_cmd_queue) .ne. hipsuccess) then
257  call neko_error('Error destroying main stream')
258  end if
259 
260  if (hipstreamdestroy(aux_cmd_queue) .ne. hipsuccess) then
261  call neko_error('Error destroying aux stream')
262  end if
263  end subroutine hip_finalize
264 
265  subroutine hip_device_name(name)
266  character(len=*), intent(inout) :: name
267  character(kind=c_char, len=1024), target :: c_name
268  integer :: end_pos
269 
270  if (hipdevicegetname(c_loc(c_name), 1024, 0) .ne. hipsuccess) then
271  call neko_error('Failed to query device')
272  end if
273 
274  end_pos = scan(c_name, c_null_char)
275  if (end_pos .ge. 2) then
276  name(1:end_pos-1) = c_name(1:end_pos-1)
277  end if
278 
279  end subroutine hip_device_name
280 
282  integer function hip_device_count()
283  type(c_ptr) :: hip_count_ptr
284  integer :: amount
285 
286  if (hipgetdevicecount(amount) .ne. hipsuccess) then
287  call neko_error('Failed to query device count')
288  end if
289 
290  hip_device_count = amount
291  end function hip_device_count
292 
293 #endif
294 
295 end module hip_intf
Device abstraction, common interface for various accelerators.
Definition: device.F90:34
Fortran HIP interface.
Definition: hip_intf.F90:34
type(c_ptr), bind(C) aux_cmd_queue
Aux HIP command queue.
Definition: hip_intf.F90:45
integer strm_low_prio
Low priority stream setting.
Definition: hip_intf.F90:51
subroutine hip_device_name(name)
Definition: hip_intf.F90:266
subroutine hip_init
Definition: hip_intf.F90:238
@ hipmemcpyhosttohost
Definition: hip_intf.F90:82
@ hipmemcpydevicetohost
Definition: hip_intf.F90:84
@ hipmemcpydefault
Definition: hip_intf.F90:86
@ hipmemcpydevicetodevice
Definition: hip_intf.F90:85
@ hipmemcpyhosttodevice
Definition: hip_intf.F90:83
subroutine hip_finalize
Definition: hip_intf.F90:256
integer strm_high_prio
High priority stream setting.
Definition: hip_intf.F90:48
@ hiperrortbd
Definition: hip_intf.F90:77
@ hiperrormapbufferobjectfailed
Definition: hip_intf.F90:76
@ hiperrorinvaliddevicepointer
Definition: hip_intf.F90:64
@ hiperrorlaunchfailure
Definition: hip_intf.F90:60
@ hiperrorpeeraccessnotenabled
Definition: hip_intf.F90:71
@ hiperrorinvalidkernelfile
Definition: hip_intf.F90:57
@ hiperrorpeeraccessalreadyenabled
Definition: hip_intf.F90:70
@ hiperrorinvalidresourcehandle
Definition: hip_intf.F90:67
@ hiperrorhostmemorynotregistered
Definition: hip_intf.F90:75
@ hiperrormemoryallocation
Definition: hip_intf.F90:58
@ hiperrorruntimeother
Definition: hip_intf.F90:73
@ hiperrorlaunchoutofresources
Definition: hip_intf.F90:61
@ hiperrorinvalidmemcpydirection
Definition: hip_intf.F90:65
@ hiperrornotready
Definition: hip_intf.F90:68
@ hipsuccess
Definition: hip_intf.F90:55
@ hiperrorruntimememory
Definition: hip_intf.F90:72
@ hiperrornodevice
Definition: hip_intf.F90:69
@ hiperrorinvalidvalue
Definition: hip_intf.F90:63
@ hiperrorinitializationerror
Definition: hip_intf.F90:59
@ hiperrorinvaliddevice
Definition: hip_intf.F90:62
@ hiperrorinvalidcontext
Definition: hip_intf.F90:56
@ hiperrorhostmemoryalreadyregistered
Definition: hip_intf.F90:74
@ hiperrorunknown
Definition: hip_intf.F90:66
integer function hip_device_count()
Return the number of available HIP devices.
Definition: hip_intf.F90:283
Utilities.
Definition: utils.f90:35