Skip to content

Commit 842e427

Browse files
committed
FEAT: Functions to allocate and free memory on host, device
1 parent 6545b5a commit 842e427

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

arrayfire/device.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,52 @@ def unlock_array(a):
282282
"""
283283
safe_call(backend.get().af_unlock_array(a.arr))
284284

285+
def alloc_device(num_bytes):
286+
"""
287+
Allocate a buffer on the device with specified number of bytes.
288+
"""
289+
ptr = ct.c_void_p(0)
290+
c_num_bytes = ct.c_longlong(num_bytes)
291+
safe_call(backend.get().af_alloc_device(ct.pointer(ptr), c_num_bytes))
292+
return ptr.value
293+
294+
def alloc_host(num_bytes):
295+
"""
296+
Allocate a buffer on the host with specified number of bytes.
297+
"""
298+
ptr = ct.c_void_p(0)
299+
c_num_bytes = ct.c_longlong(num_bytes)
300+
safe_call(backend.get().af_alloc_host(ct.pointer(ptr), c_num_bytes))
301+
return ptr.value
302+
303+
def alloc_pinned(num_bytes):
304+
"""
305+
Allocate a buffer on the host using pinned memory with specified number of bytes.
306+
"""
307+
ptr = ct.c_void_p(0)
308+
c_num_bytes = ct.c_longlong(num_bytes)
309+
safe_call(backend.get().af_alloc_pinned(ct.pointer(ptr), c_num_bytes))
310+
return ptr.value
311+
312+
def free_device(ptr):
313+
"""
314+
Free the device memory allocated by alloc_device
315+
"""
316+
cptr = ct.c_void_p(ptr)
317+
safe_call(backend.get().af_free_device(cptr))
318+
319+
def free_host(ptr):
320+
"""
321+
Free the host memory allocated by alloc_host
322+
"""
323+
cptr = ct.c_void_p(ptr)
324+
safe_call(backend.get().af_free_host(cptr))
325+
326+
def free_pinned(ptr):
327+
"""
328+
Free the pinned memory allocated by alloc_pinned
329+
"""
330+
cptr = ct.c_void_p(ptr)
331+
safe_call(backend.get().af_free_pinned(cptr))
332+
285333
from .array import Array

0 commit comments

Comments
 (0)