[python-arrayfire] 02/250: Porting a few basic functions to python

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:24 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 988345064343747bedec7d278994130b6606b82f
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Wed Jun 17 14:09:17 2015 -0400

    Porting a few basic functions to python
---
 arrayfire/__init__.py  |   7 +++
 arrayfire/algorithm.py |  52 ++++++++++++++++++++
 arrayfire/array.py     |  10 ++++
 arrayfire/data.py      |  47 ++++++++++++++++++
 arrayfire/library.py   | 130 +++++++++++++++++++++++++++++++++++++++++++++++++
 arrayfire/util.py      |  14 ++++++
 examples/helloworld.py |  16 ++++++
 7 files changed, 276 insertions(+)

diff --git a/arrayfire/__init__.py b/arrayfire/__init__.py
new file mode 100644
index 0000000..2f1423d
--- /dev/null
+++ b/arrayfire/__init__.py
@@ -0,0 +1,7 @@
+from .library import *
+from .data import *
+from .util import *
+from .algorithm import *
+
+def info():
+    clib.af_info()
diff --git a/arrayfire/algorithm.py b/arrayfire/algorithm.py
new file mode 100644
index 0000000..b2ea16f
--- /dev/null
+++ b/arrayfire/algorithm.py
@@ -0,0 +1,52 @@
+from .library import *
+from .array import *
+
+def sum(A, dim=0):
+    out = array()
+    clib.af_sum(pointer(out.arr), A.arr, c_int(dim))
+    return out
+
+def min(A, dim=0):
+    out = array()
+    clib.af_min(pointer(out.arr), A.arr, c_int(dim))
+    return out
+
+def max(A, dim=0):
+    out = array()
+    clib.af_max(pointer(out.arr), A.arr, c_int(dim))
+    return out
+
+def any_true(A, dim=0):
+    out = array()
+    clib.af_any_true(pointer(out.arr), A.arr, c_int(dim))
+    return out
+
+def all_true(A, dim=0):
+    out = array()
+    clib.af_all_true(pointer(out.arr), A.arr, c_int(dim))
+    return out
+
+def accum(A, dim=0):
+    out = array()
+    clib.af_accum(pointer(out.arr), A.arr, c_int(dim))
+    return out
+
+def sort(A, dim=0):
+    out = array()
+    clib.af_sort(pointer(out.arr), A.arr, c_int(dim))
+    return out
+
+def diff1(A, dim=0):
+    out = array()
+    clib.af_diff1(pointer(out.arr), A.arr, c_int(dim))
+    return out
+
+def diff2(A, dim=0):
+    out = array()
+    clib.af_diff2(pointer(out.arr), A.arr, c_int(dim))
+    return out
+
+def where(A):
+    out = array()
+    clib.af_where(pointer(out.arr), A.arr)
+    return out
diff --git a/arrayfire/array.py b/arrayfire/array.py
new file mode 100644
index 0000000..d463944
--- /dev/null
+++ b/arrayfire/array.py
@@ -0,0 +1,10 @@
+from .library import *
+
+class array(object):
+
+    def __init__(self):
+        self.arr = c_longlong(0)
+
+    def __del__(self):
+        if (self.arr.value != 0):
+            clib.af_release_array(self.arr)
diff --git a/arrayfire/data.py b/arrayfire/data.py
new file mode 100644
index 0000000..fda37cc
--- /dev/null
+++ b/arrayfire/data.py
@@ -0,0 +1,47 @@
+from .library import *
+from .array import *
+from .util import *
+
+def randu(d0, d1=None, d2=None, d3=None, dtype=f32):
+
+    if not isinstance(dtype, c_int):
+        raise TypeError("Invalid dtype")
+
+    out = array()
+    dims = dim4(d0, d1, d2, d3)
+
+    clib.af_randu(pointer(out.arr), 4, pointer(dims), dtype)
+    return out
+
+def randn(d0, d1=None, d2=None, d3=None, dtype=f32):
+
+    if not isinstance(dtype, c_int):
+        raise TypeError("Invalid dtype")
+
+    out = array()
+    dims = dim4(d0, d1, d2, d3)
+
+    clib.af_randn(pointer(out.arr), 4, pointer(dims), dtype)
+    return out
+
+def identity(d0, d1=None, d2=None, d3=None, dtype=f32):
+
+    if not isinstance(dtype, c_int):
+        raise TypeError("Invalid dtype")
+
+    out = array()
+    dims = dim4(d0, d1, d2, d3)
+
+    clib.af_identity(pointer(out.arr), 4, pointer(dims), dtype)
+    return out
+
+def constant(val, d0, d1=None, d2=None, d3=None, dtype=f32):
+
+    if not isinstance(dtype, c_int):
+        raise TypeError("Invalid dtype")
+
+    out = array()
+    dims = dim4(d0, d1, d2, d3)
+    c_val = c_double(val)
+    clib.af_constant(pointer(out.arr), c_val, 4, pointer(dims), dtype)
+    return out
diff --git a/arrayfire/library.py b/arrayfire/library.py
new file mode 100644
index 0000000..fca32ad
--- /dev/null
+++ b/arrayfire/library.py
@@ -0,0 +1,130 @@
+import platform
+from ctypes import *
+
+def load_backend(name):
+    platform_name = platform.system()
+
+    libname = 'libaf' + name
+    if platform_name == 'Linux':
+        libname += '.so'
+    elif platform_name == 'Darwin':
+        libname += '.dylib'
+    else:
+        raise OSError(platform_name + ' not supported')
+
+    cdll.LoadLibrary(libname)
+    clib = CDLL(libname)
+    print("Using %s backend" % name)
+    return clib
+
+try:
+    clib = load_backend('cuda')
+except:
+    try:
+        clib = load_backend('opencl')
+    except:
+        clib = load_backend('cpu')
+
+
+AF_SUCCESS            =   c_int(0)
+
+#100-199 Errors in environment
+AF_ERR_NO_MEM         = c_int(101)
+AF_ERR_DRIVER         = c_int(102)
+AF_ERR_RUNTIME        = c_int(103)
+
+# 200-299 Errors in input parameters
+AF_ERR_INVALID_ARRAY  = c_int(201)
+AF_ERR_ARG            = c_int(202)
+AF_ERR_SIZE           = c_int(203)
+AF_ERR_TYPE           = c_int(204)
+AF_ERR_DIFF_TYPE      = c_int(205)
+AF_ERR_BATCH          = c_int(207)
+
+# 300-399 Errors for missing software features
+AF_ERR_NOT_SUPPORTED  = c_int(301)
+AF_ERR_NOT_CONFIGURED = c_int(302)
+
+# 400-499 Errors for missing hardware features
+AF_ERR_NO_DBL         = c_int(401)
+AF_ERR_NO_GFX         = c_int(402)
+
+# 900-999 Errors from upstream libraries and runtimes
+AF_ERR_INTERNAL       = c_int(998)
+AF_ERR_UNKNOWN        = c_int(999)
+
+f32 = c_int(0)
+c32 = c_int(1)
+f64 = c_int(2)
+c64 = c_int(3)
+b8  = c_int(4)
+s32 = c_int(5)
+u32 = c_int(6)
+u8  = c_int(7)
+s64 = c_int(8)
+u64 = c_int(9)
+
+afDevice = c_int(0)
+afHost   = c_int(1)
+
+AF_INTERP_NEAREST   = c_int(0)
+AF_INTERP_LINEAR    = c_int(1)
+AF_INTERP_BILINEAR  = c_int(2)
+AF_INTERP_CUBIC     = c_int(3)
+
+AF_PAD_ZERO = c_int(0)
+AF_PAD_SYM  = c_int(1)
+
+AF_CONNECTIVITY_4 = c_int(4)
+AF_CONNECTIVITY_8 = c_int(8)
+
+AF_CONV_DEFAULT = c_int(0)
+AF_CONV_EXPAND  = c_int(1)
+
+AF_CONV_AUTO    = c_int(0)
+AF_CONV_SPATIAL = c_int(1)
+AF_CONV_FREQ    = c_int(2)
+
+AF_SAD  = c_int(0)
+AF_ZSAD = c_int(1)
+AF_LSAD = c_int(2)
+AF_SSD  = c_int(3)
+AF_ZSSD = c_int(4)
+AF_LSSD = c_int(5)
+AF_NCC  = c_int(6)
+AF_ZNCC = c_int(7)
+AF_SHD  = c_int(8)
+
+AF_GRAY = c_int(0)
+AF_RGB  = c_int(1)
+AF_HSV  = c_int(2)
+
+AF_MAT_NONE       = c_int(0)
+AF_MAT_TRANS      = c_int(1)
+AF_MAT_CTRANS     = c_int(2)
+AF_MAT_UPPER      = c_int(32)
+AF_MAT_LOWER      = c_int(64)
+AF_MAT_DIAG_UNIT  = c_int(128)
+AF_MAT_SYM        = c_int(512)
+AF_MAT_POSDEF     = c_int(1024)
+AF_MAT_ORTHOG     = c_int(2048)
+AF_MAT_TRI_DIAG   = c_int(4096)
+AF_MAT_BLOCK_DIAG = c_int(8192)
+
+AF_NORM_VECTOR_1    = c_int(0)
+AF_NORM_VECTOR_INF  = c_int(1)
+AF_NORM_VECTOR_2    = c_int(2)
+AF_NORM_VECTOR_P    = c_int(3)
+AF_NORM_MATRIX_1    = c_int(4)
+AF_NORM_MATRIX_INF  = c_int(5)
+AF_NORM_MATRIX_2    = c_int(6)
+AF_NORM_MATRIX_L_PQ = c_int(7)
+AF_NORM_EUCLID      = AF_NORM_VECTOR_2
+
+AF_COLORMAP_DEFAULT  = c_int(0)
+AF_COLORMAP_SPECTRUM = c_int(1)
+AF_COLORMAP_COLORS   = c_int(2)
+AF_COLORMAP_RED      = c_int(3)
+AF_COLORMAP_MOOD     = c_int(4)
+AF_COLORMAP_HEAT     = c_int(5)
+AF_COLORMAP_BLUE     = c_int(6)
diff --git a/arrayfire/util.py b/arrayfire/util.py
new file mode 100644
index 0000000..cf0321d
--- /dev/null
+++ b/arrayfire/util.py
@@ -0,0 +1,14 @@
+from .library import *
+from .array import *
+
+def dim4(d0=1, d1=1, d2=1, d3=1):
+    c_dim4 = c_longlong * 4
+    out = c_dim4(1, 1, 1, 1)
+
+    for i, dim in enumerate((d0, d1, d2, d3)):
+        if (dim is not None): out[i] = dim
+
+    return out
+
+def print_array(a):
+    clib.af_print_array(a.arr)
diff --git a/examples/helloworld.py b/examples/helloworld.py
new file mode 100644
index 0000000..50f129f
--- /dev/null
+++ b/examples/helloworld.py
@@ -0,0 +1,16 @@
+#!/usr/bin/python
+import arrayfire as af
+
+af.info()
+
+print('\nGenerate a random matrix a:')
+a = af.randu(5, 1)
+af.print_array(a)
+
+print('\nMin value of a')
+a_min = af.min(a)
+af.print_array(a_min)
+
+print('\nMax value of a')
+a_max = af.max(a)
+af.print_array(a_max)

-- 
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