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