[python-arrayfire] 17/250: Adding device helper functions

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:26 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository python-arrayfire.

commit e84cbb7c8f308245a0bb1df99c82879cf63f10dc
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Mon Jun 22 10:01:15 2015 -0400

    Adding device helper functions
---
 arrayfire/__init__.py  |  4 +---
 arrayfire/device.py    | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++
 arrayfire/util.py      |  6 ++++--
 tests/simple_device.py | 22 +++++++++++++++++++
 4 files changed, 85 insertions(+), 5 deletions(-)

diff --git a/arrayfire/__init__.py b/arrayfire/__init__.py
index 2f1423d..357f33f 100644
--- a/arrayfire/__init__.py
+++ b/arrayfire/__init__.py
@@ -2,6 +2,4 @@ from .library import *
 from .data import *
 from .util import *
 from .algorithm import *
-
-def info():
-    clib.af_info()
+from .device import *
diff --git a/arrayfire/device.py b/arrayfire/device.py
new file mode 100644
index 0000000..92311ae
--- /dev/null
+++ b/arrayfire/device.py
@@ -0,0 +1,58 @@
+from .library import *
+from ctypes import *
+from .util import (safe_call, to_str)
+
+def info():
+    safe_call(clib.af_info())
+
+def device_info():
+    c_char_256 = c_char * 256
+    device_name = c_char_256()
+    backend_name = c_char_256()
+    toolkit = c_char_256()
+    compute = c_char_256()
+
+    safe_call(clib.af_device_info(pointer(device_name), pointer(backend_name), \
+                                  pointer(toolkit), pointer(compute)))
+    dev_info = {}
+    dev_info['device'] = to_str(device_name)
+    dev_info['backend'] = to_str(backend_name)
+    dev_info['toolkit'] = to_str(toolkit)
+    dev_info['compute'] = to_str(compute)
+
+    return dev_info
+
+def get_device_count():
+    c_num = c_int(0)
+    safe_call(clib.af_get_device_count(pointer(c_num)))
+    return c_num.value
+
+def get_device():
+    c_dev = c_int(0)
+    safe_call(clib.af_get_device(pointer(c_dev)))
+    return c_dev.value
+
+def set_device(num):
+    safe_call(clib.af_set_device(num))
+
+def is_dbl_supported(device=None):
+    dev = device if device is not None else get_device()
+    res = c_bool(False)
+    safe_call(clib.af_get_dbl_support(pointer(res), dev))
+    return res.value
+
+def sync(device=None):
+    dev = device if device is not None else get_device()
+    safe_call(clib.af_sync(dev))
+
+def device_mem_info():
+    alloc_bytes = c_size_t(0)
+    alloc_buffers = c_size_t(0)
+    lock_bytes = c_size_t(0)
+    lock_buffers = c_size_t(0)
+    safe_call(clib.af_device_mem_info(pointer(alloc_bytes), pointer(alloc_buffers),\
+                                      pointer(lock_bytes), pointer(lock_buffers)))
+    mem_info = {}
+    mem_info['alloc'] = {'buffers' : alloc_buffers.value, 'bytes' : alloc_bytes.value}
+    mem_info['lock'] = {'buffers' : lock_buffers.value, 'bytes' : lock_bytes.value}
+    return mem_info
diff --git a/arrayfire/util.py b/arrayfire/util.py
index ed961be..dbb5047 100644
--- a/arrayfire/util.py
+++ b/arrayfire/util.py
@@ -1,4 +1,3 @@
-import inspect
 from .library import *
 
 def dim4(d0=1, d1=1, d2=1, d3=1):
@@ -22,9 +21,12 @@ def dim4_tuple(dims):
 def is_valid_scalar(a):
     return isinstance(a, float) or isinstance(a, int) or isinstance(a, complex)
 
+def to_str(c_str):
+    return str(c_str.value.decode('utf-8'))
+
 def safe_call(af_error):
     if (af_error != AF_SUCCESS.value):
         c_err_str = c_char_p(0)
         c_err_len = c_longlong(0)
         clib.af_get_last_error(pointer(c_err_str), pointer(c_err_len))
-        raise RuntimeError(c_err_str.value, af_error)
+        raise RuntimeError('test', to_str(c_err_str), af_error)
diff --git a/tests/simple_device.py b/tests/simple_device.py
new file mode 100644
index 0000000..e703a2b
--- /dev/null
+++ b/tests/simple_device.py
@@ -0,0 +1,22 @@
+#!/usr/bin/python
+import arrayfire as af
+
+af.info()
+print(af.device_info())
+print(af.get_device_count())
+print(af.is_dbl_supported())
+af.sync()
+
+print('starting the loop')
+for k in range(af.get_device_count()):
+    af.set_device(k)
+    dev = af.get_device()
+    assert(k == dev)
+
+    print(af.is_dbl_supported(k))
+
+    a = af.randu(100, 100)
+    af.sync(dev)
+    mem_info = af.device_mem_info()
+    assert(mem_info['alloc']['buffers'] == 1)
+    assert(mem_info[ 'lock']['buffers'] == 1)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-arrayfire.git



More information about the debian-science-commits mailing list