42 use,
intrinsic :: iso_c_binding, only : c_ptr, c_int, c_null_ptr, &
43 c_associated, c_sizeof, c_size_t
49 type(c_ptr) :: local_gs_d = c_null_ptr
50 type(c_ptr) :: local_dof_gs_d = c_null_ptr
51 type(c_ptr) :: local_gs_dof_d = c_null_ptr
52 type(c_ptr) :: shared_gs_d = c_null_ptr
53 type(c_ptr) :: shared_dof_gs_d = c_null_ptr
54 type(c_ptr) :: shared_gs_dof_d = c_null_ptr
55 type(c_ptr) :: local_blk_len_d = c_null_ptr
56 type(c_ptr) :: shared_blk_len_d = c_null_ptr
57 type(c_ptr) :: local_blk_off_d = c_null_ptr
58 type(c_ptr) :: shared_blk_off_d = c_null_ptr
70 subroutine hip_gather_kernel(v, m, o, dg, u, n, gd, nb, b, bo, op, strm) &
71 bind(c, name =
'hip_gather_kernel')
72 use,
intrinsic :: iso_c_binding
74 integer(c_int) :: m, n, nb, o, op
75 type(c_ptr),
value :: v, u, dg, gd, b, bo, strm
81 bind(c, name =
'hip_scatter_kernel')
82 use,
intrinsic :: iso_c_binding
84 integer(c_int) :: m, n, nb
85 type(c_ptr),
value :: v, u, dg, gd, b, bo, strm
91 subroutine cuda_gather_kernel(v, m, o, dg, u, n, gd, nb, b, bo, op, strm) &
92 bind(c, name =
'cuda_gather_kernel')
93 use,
intrinsic :: iso_c_binding
95 integer(c_int) :: m, n, nb, o, op
96 type(c_ptr),
value :: v, u, dg, gd, b, bo, strm
102 bind(c, name =
'cuda_scatter_kernel')
103 use,
intrinsic :: iso_c_binding
105 integer(c_int) :: m, n, nb
106 type(c_ptr),
value :: v, u, dg, gd, b, bo, strm
111 subroutine opencl_gather_kernel(v, m, o, dg, u, n, gd, nb, b, bo, op, &
112 strm) bind(c, name = 'opencl_gather_kernel')
113 use,
intrinsic :: iso_c_binding
115 integer(c_int) :: m, n, nb, o, op
116 type(c_ptr),
value :: v, u, dg, gd, b, bo, strm
122 bind(c, name =
'opencl_scatter_kernel')
123 use,
intrinsic :: iso_c_binding
125 integer(c_int) :: m, n, nb
126 type(c_ptr),
value :: v, u, dg, gd, b, bo, strm
131 subroutine metal_gather_kernel(v, m, o, dg, u, n, gd, nb, b, bo, op, &
132 strm) bind(c, name = 'metal_gather_kernel')
133 use,
intrinsic :: iso_c_binding
135 integer(c_int) :: m, n, nb, o, op
136 type(c_ptr),
value :: v, u, dg, gd, b, bo, strm
137 end subroutine metal_gather_kernel
141 subroutine metal_scatter_kernel(v, m, dg, u, n, gd, nb, b, bo, strm) &
142 bind(c, name =
'metal_scatter_kernel')
143 use,
intrinsic :: iso_c_binding
145 integer(c_int) :: m, n, nb
146 type(c_ptr),
value :: v, u, dg, gd, b, bo, strm
147 end subroutine metal_scatter_kernel
156 integer,
intent(in) :: nlocal
157 integer,
intent(in) :: nshared
158 integer,
intent(in) :: nlcl_blks
159 integer,
intent(in) :: nshrd_blks
164 this%nshared = nshared
166 this%local_gs_d = c_null_ptr
167 this%local_dof_gs_d = c_null_ptr
168 this%local_gs_dof_d = c_null_ptr
169 this%local_blk_len_d = c_null_ptr
170 this%local_blk_off_d = c_null_ptr
171 this%shared_gs_d = c_null_ptr
172 this%shared_dof_gs_d = c_null_ptr
173 this%shared_gs_dof_d = c_null_ptr
174 this%shared_blk_len_d = c_null_ptr
175 this%shared_blk_off_d = c_null_ptr
177 this%shared_on_host = .true.
179#if defined(HAVE_HIP) || defined(HAVE_CUDA) || defined(HAVE_METAL)
192 if (c_associated(this%local_gs_d))
then
196 if (c_associated(this%local_dof_gs_d))
then
200 if (c_associated(this%local_gs_dof_d))
then
204 if (c_associated(this%local_blk_len_d))
then
208 if (c_associated(this%shared_blk_len_d))
then
212 if (c_associated(this%local_blk_off_d))
then
216 if (c_associated(this%shared_blk_off_d))
then
223#if defined(HAVE_HIP) || defined(HAVE_CUDA) || defined(HAVE_METAL)
224 if (c_associated(this%gather_event))
then
228 if (c_associated(this%scatter_event))
then
233 if (c_associated(this%gs_stream))
then
234 this%gs_stream = c_null_ptr
240 subroutine gs_gather_device(this, v, m, o, dg, u, n, gd, nb, b, bo, op, shrd)
241 integer,
intent(in) :: m
242 integer,
intent(in) :: n
243 integer,
intent(in) :: nb
245 real(kind=
rp),
dimension(m),
intent(inout) :: v
246 integer,
dimension(m),
intent(inout) :: dg
247 real(kind=
rp),
dimension(n),
intent(inout) :: u
248 integer,
dimension(m),
intent(inout) :: gd
249 integer,
dimension(nb),
intent(inout) :: b
250 integer,
dimension(nb),
intent(inout) :: bo
251 integer,
intent(in) :: o
252 integer,
intent(in) :: op
253 logical,
intent(in) :: shrd
260 associate(v_d => this%local_gs_d, dg_d => this%local_dof_gs_d, &
261 gd_d => this%local_gs_dof_d, b_d => this%local_blk_len_d, &
262 bo_d => this%local_blk_off_d, strm => this%gs_stream)
264 if (.not. c_associated(v_d))
then
267 real(c_rp) :: rp_dummy
268 integer(c_size_t) :: s
269 s = c_sizeof(rp_dummy) * m
274 if (.not. c_associated(dg_d))
then
277 sync = .false., strm = strm)
280 if (.not. c_associated(gd_d))
then
283 sync = .false., strm = strm)
287 if (.not. c_associated(b_d))
then
290 sync = .false., strm = strm)
293 if (.not. c_associated(bo_d))
then
296 sync = .false., strm = strm)
302 nb, b_d, bo_d, op, strm)
305 nb, b_d, bo_d, op, strm)
308 nb, b_d, bo_d, op, strm)
310 call metal_gather_kernel(v_d, m, o, dg_d, u_d, n, gd_d, &
311 nb, b_d, bo_d, op, strm)
313 call neko_error(
'No device backend configured')
318 associate(v_d => this%shared_gs_d, dg_d => this%shared_dof_gs_d, &
319 gd_d => this%shared_gs_dof_d, b_d => this%shared_blk_len_d, &
320 bo_d => this%shared_blk_off_d, strm => this%gs_stream)
322 if (.not. c_associated(v_d))
then
325 real(c_rp) :: rp_dummy
326 integer(c_size_t) :: s
327 s = c_sizeof(rp_dummy) * m
332 if (.not. c_associated(dg_d))
then
335 sync = .false., strm = strm)
338 if (.not. c_associated(gd_d))
then
341 sync = .false., strm = strm)
345 if (.not. c_associated(b_d))
then
348 sync = .false., strm = strm)
351 if (.not. c_associated(bo_d))
then
354 sync = .false., strm = strm)
361 nb, b_d, bo_d, op, strm)
364 nb, b_d, bo_d, op, strm)
367 nb, b_d, bo_d, op, strm)
369 call metal_gather_kernel(v_d, m, o, dg_d, u_d, n, gd_d, &
370 nb, b_d, bo_d, op, strm)
372 call neko_error(
'No device backend configured')
375#if defined(HAVE_HIP) || defined(HAVE_CUDA) || \
376 defined(have_opencl) || defined(have_metal)
380 if (this%shared_on_host)
then
381 if (this%nshared .eq. m)
then
383 sync = .true., strm = strm)
393 subroutine gs_scatter_device(this, v, m, dg, u, n, gd, nb, b, bo, shrd, event)
394 integer,
intent(in) :: m
395 integer,
intent(in) :: n
396 integer,
intent(in) :: nb
398 real(kind=rp),
dimension(m),
intent(inout) :: v
399 integer,
dimension(m),
intent(inout) :: dg
400 real(kind=rp),
dimension(n),
intent(inout) :: u
401 integer,
dimension(m),
intent(inout) :: gd
402 integer,
dimension(nb),
intent(inout) :: b
403 integer,
dimension(nb),
intent(inout) :: bo
404 logical,
intent(in) :: shrd
408 u_d = device_get_ptr(u)
411 associate(v_d => this%local_gs_d, dg_d => this%local_dof_gs_d, &
412 gd_d => this%local_gs_dof_d, b_d => this%local_blk_len_d, &
413 bo_d => this%local_blk_off_d, strm => this%gs_stream)
424 call metal_scatter_kernel(v_d, m, dg_d, u_d, n, gd_d, nb, b_d, &
427 call neko_error(
'No device backend configured')
431 associate(v_d => this%shared_gs_d, dg_d => this%shared_dof_gs_d, &
432 gd_d => this%shared_gs_dof_d, b_d => this%shared_blk_len_d, &
433 bo_d => this%shared_blk_off_d, strm => this%gs_stream)
435 if (this%shared_on_host)
then
436 call device_memcpy(v, v_d, m, host_to_device, &
437 sync = .false., strm = strm)
450 call metal_scatter_kernel(v_d, m, dg_d, u_d, n, gd_d, nb, b_d, &
453 call neko_error(
'No device backend configured')
458#if defined(HAVE_HIP) || defined(HAVE_CUDA) || \
459 defined(have_opencl) || defined(have_metal)
460 if (c_associated(event))
then
461 call device_event_record(event, this%gs_stream)
463 call device_sync(this%gs_stream)
void opencl_gather_kernel(void *v, int *m, int *o, void *dg, void *u, int *n, void *gd, int *nb, void *b, void *bo, int *op, void *cmd_queue)
void opencl_scatter_kernel(void *v, int *m, void *dg, void *u, int *n, void *gd, int *nb, void *b, void *bo, void *cmd_queue)
void cuda_gather_kernel(void *v, int *m, int *o, void *dg, void *u, int *n, void *gd, int *nb, void *b, void *bo, int *op, cudaStream_t stream)
void cuda_scatter_kernel(void *v, int *m, void *dg, void *u, int *n, void *gd, int *nb, void *b, void *bo, cudaStream_t stream)
Return the device pointer for an associated Fortran array.
Map a Fortran array to a device (allocate and associate)
Copy data between host and device (or device and device)
Synchronize a device or stream.
Device abstraction, common interface for various accelerators.
subroutine, public device_event_record(event, stream)
Record a device event.
integer, parameter, public host_to_device
subroutine, public device_free(x_d)
Deallocate memory on the device.
integer, parameter, public device_to_host
subroutine, public device_event_destroy(event)
Destroy a device event.
type(c_ptr), bind(C), public glb_cmd_queue
Global command queue.
subroutine, public device_event_create(event, flags)
Create a device event queue.
subroutine, public device_memset(x_d, v, s, sync, strm)
Set memory on the device to a value.
Defines a gather-scatter backend.
Generic Gather-scatter backend for accelerators.
subroutine gs_device_init(this, nlocal, nshared, nlcl_blks, nshrd_blks)
Accelerator backend initialisation.
subroutine gs_gather_device(this, v, m, o, dg, u, n, gd, nb, b, bo, op, shrd)
Gather kernel.
subroutine gs_device_free(this)
Dummy backend deallocation.
subroutine gs_scatter_device(this, v, m, dg, u, n, gd, nb, b, bo, shrd, event)
Scatter kernel.
integer, parameter, public c_rp
integer, parameter, public rp
Global precision used in computations.
Gather-scatter backend for offloading devices.