Neko 1.99.3
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
metal_intf.F90
Go to the documentation of this file.
1! Copyright (c) 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_METAL
40
42 integer, parameter :: metalSuccess = 0
43
44 interface
45
47 integer(c_int) function metalalloc(ptr_d, s) &
48 bind(c, name = 'metal_alloc')
49 use, intrinsic :: iso_c_binding
50 implicit none
51 type(c_ptr) :: ptr_d
52 integer(c_size_t), value :: s
53 end function metalalloc
54
56 integer(c_int) function metalfree(ptr_d) &
57 bind(c, name = 'metal_free')
58 use, intrinsic :: iso_c_binding
59 implicit none
60 type(c_ptr), value :: ptr_d
61 end function metalfree
62
64 integer(c_int) function metalmemcpyhtod(dst_d, src, s) &
65 bind(c, name = 'metal_memcpy_htod')
66 use, intrinsic :: iso_c_binding
67 implicit none
68 type(c_ptr), value :: dst_d, src
69 integer(c_size_t), value :: s
70 end function metalmemcpyhtod
71
73 integer(c_int) function metalmemcpydtoh(dst, src_d, s) &
74 bind(c, name = 'metal_memcpy_dtoh')
75 use, intrinsic :: iso_c_binding
76 implicit none
77 type(c_ptr), value :: dst, src_d
78 integer(c_size_t), value :: s
79 end function metalmemcpydtoh
80
82 integer(c_int) function metalmemcpydtod(dst_d, src_d, s) &
83 bind(c, name = 'metal_memcpy_dtod')
84 use, intrinsic :: iso_c_binding
85 implicit none
86 type(c_ptr), value :: dst_d, src_d
87 integer(c_size_t), value :: s
88 end function metalmemcpydtod
89
91 integer(c_int) function metalmemset(buf_d, v, s) &
92 bind(c, name = 'metal_memset')
93 use, intrinsic :: iso_c_binding
94 implicit none
95 type(c_ptr), value :: buf_d
96 integer(c_int), value :: v
97 integer(c_size_t), value :: s
98 end function metalmemset
99
101 integer(c_int) function metaldevicesynchronize() &
102 bind(c, name = 'metal_device_sync')
103 use, intrinsic :: iso_c_binding
104 implicit none
105 end function metaldevicesynchronize
106
108 integer(c_int) function metalstreamsynchronize(stream) &
109 bind(c, name = 'metal_stream_sync')
110 use, intrinsic :: iso_c_binding
111 implicit none
112 type(c_ptr), value :: stream
113 end function metalstreamsynchronize
114
116 integer(c_int) function metalstreamcreate(stream) &
117 bind(c, name = 'metal_stream_create')
118 use, intrinsic :: iso_c_binding
119 implicit none
120 type(c_ptr) :: stream
121 end function metalstreamcreate
122
124 integer(c_int) function metalstreamdestroy(stream) &
125 bind(c, name = 'metal_stream_destroy')
126 use, intrinsic :: iso_c_binding
127 implicit none
128 type(c_ptr), value :: stream
129 end function metalstreamdestroy
130
132 integer(c_int) function metaleventcreate(event) &
133 bind(c, name = 'metal_event_create')
134 use, intrinsic :: iso_c_binding
135 implicit none
136 type(c_ptr) :: event
137 end function metaleventcreate
138
140 integer(c_int) function metaleventdestroy(event) &
141 bind(c, name = 'metal_event_destroy')
142 use, intrinsic :: iso_c_binding
143 implicit none
144 type(c_ptr), value :: event
145 end function metaleventdestroy
146
148 integer(c_int) function metaleventrecord(event, stream) &
149 bind(c, name = 'metal_event_record')
150 use, intrinsic :: iso_c_binding
151 implicit none
152 type(c_ptr), value :: event, stream
153 end function metaleventrecord
154
156 integer(c_int) function metaleventsynchronize(event) &
157 bind(c, name = 'metal_event_sync')
158 use, intrinsic :: iso_c_binding
159 implicit none
160 type(c_ptr), value :: event
161 end function metaleventsynchronize
162
164 integer(c_int) function metalstreamwaitevent(stream, event) &
165 bind(c, name = 'metal_stream_wait_event')
166 use, intrinsic :: iso_c_binding
167 implicit none
168 type(c_ptr), value :: stream, event
169 end function metalstreamwaitevent
170
172 subroutine metal_device_name_c(name, len) &
173 bind(c, name = 'metal_device_name')
174 use, intrinsic :: iso_c_binding
175 implicit none
176 type(c_ptr), value :: name
177 integer(c_int), value :: len
178 end subroutine metal_device_name_c
179
181 integer(c_int) function metal_device_count_c() &
182 bind(c, name = 'metal_device_count')
183 use, intrinsic :: iso_c_binding
184 implicit none
185 end function metal_device_count_c
186
187 end interface
188
189contains
190
192 subroutine metal_init(glb_cmd_queue, aux_cmd_queue)
193 type(c_ptr), intent(inout) :: glb_cmd_queue
194 type(c_ptr), intent(inout) :: aux_cmd_queue
195
196 interface
197 subroutine metal_init_c(glb_q, aux_q) &
198 bind(c, name = 'metal_init')
199 use, intrinsic :: iso_c_binding
200 implicit none
201 type(c_ptr) :: glb_q
202 type(c_ptr) :: aux_q
203 end subroutine metal_init_c
204 end interface
205
206 call metal_init_c(glb_cmd_queue, aux_cmd_queue)
207 end subroutine metal_init
208
210 subroutine metal_finalize(glb_cmd_queue, aux_cmd_queue)
211 type(c_ptr), intent(inout) :: glb_cmd_queue
212 type(c_ptr), intent(inout) :: aux_cmd_queue
213
214 interface
215 subroutine metal_finalize_c(glb_q, aux_q) &
216 bind(c, name = 'metal_finalize')
217 use, intrinsic :: iso_c_binding
218 implicit none
219 type(c_ptr), value :: glb_q
220 type(c_ptr), value :: aux_q
221 end subroutine metal_finalize_c
222 end interface
223
224 call metal_finalize_c(glb_cmd_queue, aux_cmd_queue)
225 end subroutine metal_finalize
226
228 subroutine metal_device_name(name)
229 character(len=*), intent(inout) :: name
230 character(kind=c_char, len=256), target :: c_name
231 integer :: end_pos
232
233 call metal_device_name_c(c_loc(c_name), 256)
234
235 end_pos = scan(c_name, c_null_char)
236 if (end_pos .ge. 2) then
237 name(1:end_pos-1) = c_name(1:end_pos-1)
238 end if
239 end subroutine metal_device_name
240
242 integer function metal_device_count()
243 metal_device_count = metal_device_count_c()
244 end function metal_device_count
245
246#endif
247
248end module metal_intf
Fortran interface to the Metal device layer (see device/metal/metal.m)
Utilities.
Definition utils.f90:35