13 USE iso_c_binding,
ONLY: c_associated,&
23 #include "../base/base_uses.f90"
29 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'offload_api'
38 TYPE offload_buffer_type
39 REAL(KIND=
dp),
DIMENSION(:),
CONTIGUOUS,
POINTER :: host_buffer => null()
40 TYPE(C_PTR) :: c_ptr = c_null_ptr
41 END TYPE offload_buffer_type
53 INTEGER(C_SIZE_T),
VALUE :: length
57 FUNCTION offload_malloc_pinned_mem_c(buffer, length) &
58 BIND(C, name="offload_host_malloc")
59 IMPORT c_size_t, c_ptr, c_int
61 INTEGER(C_SIZE_T),
VALUE :: length
62 INTEGER(KIND=C_INT) :: offload_malloc_pinned_mem_c
63 END FUNCTION offload_malloc_pinned_mem_c
66 res = offload_malloc_pinned_mem_c(buffer, length)
75 TYPE(c_ptr),
VALUE :: buffer
79 FUNCTION offload_free_pinned_mem_c(buffer) &
80 BIND(C, name="offload_host_free")
82 INTEGER(KIND=C_INT) :: offload_free_pinned_mem_c
83 TYPE(c_ptr),
VALUE :: buffer
84 END FUNCTION offload_free_pinned_mem_c
87 res = offload_free_pinned_mem_c(buffer)
97 SUBROUTINE offload_init_c() &
98 BIND(C, name="offload_init")
99 END SUBROUTINE offload_init_c
102 CALL offload_init_c()
115 FUNCTION offload_get_device_count_c() &
116 BIND(C, name="offload_get_device_count")
118 INTEGER(KIND=C_INT) :: offload_get_device_count_c
119 END FUNCTION offload_get_device_count_c
122 count = offload_get_device_count_c()
132 INTEGER,
INTENT(IN) :: device_id
135 SUBROUTINE offload_set_chosen_device_c(device_id) &
136 BIND(C, name="offload_set_chosen_device")
138 INTEGER(KIND=C_INT),
VALUE :: device_id
139 END SUBROUTINE offload_set_chosen_device_c
142 CALL offload_set_chosen_device_c(device_id=device_id)
155 FUNCTION offload_get_chosen_device_c() &
156 BIND(C, name="offload_get_chosen_device")
158 INTEGER(KIND=C_INT) :: offload_get_chosen_device_c
159 END FUNCTION offload_get_chosen_device_c
162 device_id = offload_get_chosen_device_c()
165 cpabort(
"No offload device has been chosen.")
176 SUBROUTINE offload_activate_chosen_device_c() &
177 BIND(C, name="offload_activate_chosen_device")
178 END SUBROUTINE offload_activate_chosen_device_c
181 CALL offload_activate_chosen_device_c()
191 CHARACTER(LEN=*),
INTENT(IN) :: routinen
194 SUBROUTINE offload_timeset_c(message)
BIND(C, name="offload_timeset")
196 CHARACTER(kind=C_CHAR),
DIMENSION(*),
INTENT(IN) :: message
197 END SUBROUTINE offload_timeset_c
200 CALL offload_timeset_c(trim(routinen)//c_null_char)
211 SUBROUTINE offload_timestop_c()
BIND(C, name="offload_timestop")
212 END SUBROUTINE offload_timestop_c
215 CALL offload_timestop_c()
226 INTEGER(KIND=int_8),
INTENT(OUT) :: free, total
228 INTEGER(KIND=C_SIZE_T) :: my_free, my_total
230 SUBROUTINE offload_mem_info_c(free, total)
BIND(C, name="offload_mem_info")
232 INTEGER(KIND=C_SIZE_T) :: free, total
233 END SUBROUTINE offload_mem_info_c
236 CALL offload_mem_info_c(my_free, my_total)
251 INTEGER,
INTENT(IN) :: length
252 TYPE(offload_buffer_type),
INTENT(INOUT) :: buffer
254 CHARACTER(LEN=*),
PARAMETER :: routinen =
'offload_create_buffer'
257 TYPE(c_ptr) :: host_buffer_c
259 SUBROUTINE offload_create_buffer_c(length, buffer) &
260 BIND(C, name="offload_create_buffer")
261 IMPORT :: c_ptr, c_int
262 INTEGER(KIND=C_INT),
VALUE :: length
263 TYPE(c_ptr) :: buffer
264 END SUBROUTINE offload_create_buffer_c
268 FUNCTION offload_get_buffer_host_pointer_c(buffer) &
269 BIND(C, name="offload_get_buffer_host_pointer")
271 TYPE(c_ptr),
VALUE :: buffer
272 TYPE(c_ptr) :: offload_get_buffer_host_pointer_c
273 END FUNCTION offload_get_buffer_host_pointer_c
276 CALL timeset(routinen, handle)
278 IF (
ASSOCIATED(buffer%host_buffer))
THEN
279 IF (
SIZE(buffer%host_buffer) == 0)
DEALLOCATE (buffer%host_buffer)
282 CALL offload_create_buffer_c(length=length, buffer=buffer%c_ptr)
283 cpassert(c_associated(buffer%c_ptr))
285 IF (length == 0)
THEN
287 ALLOCATE (buffer%host_buffer(0))
289 host_buffer_c = offload_get_buffer_host_pointer_c(buffer%c_ptr)
290 cpassert(c_associated(host_buffer_c))
291 CALL c_f_pointer(host_buffer_c, buffer%host_buffer, shape=(/length/))
294 CALL timestop(handle)
303 TYPE(offload_buffer_type),
INTENT(INOUT) :: buffer
305 CHARACTER(LEN=*),
PARAMETER :: routinen =
'offload_free_buffer'
309 SUBROUTINE offload_free_buffer_c(buffer) &
310 BIND(C, name="offload_free_buffer")
312 TYPE(c_ptr),
VALUE :: buffer
313 END SUBROUTINE offload_free_buffer_c
316 CALL timeset(routinen, handle)
318 IF (c_associated(buffer%c_ptr))
THEN
320 CALL offload_free_buffer_c(buffer%c_ptr)
322 buffer%c_ptr = c_null_ptr
324 IF (
SIZE(buffer%host_buffer) == 0)
THEN
325 DEALLOCATE (buffer%host_buffer)
327 NULLIFY (buffer%host_buffer)
331 CALL timestop(handle)
Defines the basic variable types.
integer, parameter, public int_8
integer, parameter, public dp
Fortran API for the offload package, which is written in C.
subroutine, public offload_timeset(routineN)
Starts a timing range.
subroutine, public offload_set_chosen_device(device_id)
Selects the chosen device to be used.
subroutine, public offload_free_buffer(buffer)
Deallocates given buffer.
subroutine, public offload_activate_chosen_device()
Activates the device selected via offload_set_chosen_device()
subroutine, public offload_timestop()
Ends a timing range.
integer function, public offload_free_pinned_mem(buffer)
free pinned memory
integer function, public offload_get_device_count()
Returns the number of available devices.
subroutine, public offload_create_buffer(length, buffer)
Allocates a buffer of given length, ie. number of elements.
integer function, public offload_malloc_pinned_mem(buffer, length)
allocate pinned memory.
subroutine, public offload_init()
Initialize runtime.
subroutine, public offload_mem_info(free, total)
Gets free and total device memory.
integer function, public offload_get_chosen_device()
Returns the chosen device.