[python-arrayfire] 51/250: Adding parallel range. First step towards GFOR like functionality.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:30 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 af3b310ea18487206d5f7206475edcb120af3c07
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Tue Jul 28 19:40:18 2015 -0400

    Adding parallel range. First step towards GFOR like functionality.
---
 arrayfire/index.py    | 34 +++++++++++++++++++++++++++++
 tests/simple_array.py | 36 +------------------------------
 tests/simple_index.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+), 35 deletions(-)

diff --git a/arrayfire/index.py b/arrayfire/index.py
index 7b264a9..d37949e 100644
--- a/arrayfire/index.py
+++ b/arrayfire/index.py
@@ -9,6 +9,7 @@
 from .library import *
 from .util import *
 from .base import *
+from .broadcast import *
 
 class seq(ct.Structure):
     _fields_ = [("begin", ct.c_double),
@@ -35,6 +36,31 @@ class seq(ct.Structure):
         else:
             raise IndexError("Invalid type while indexing arrayfire.array")
 
+class parallel_range(seq):
+
+    def __init__(self, start, stop=None, step=None):
+
+        if (stop is None):
+            stop = start
+            start = 0
+
+        self.S = slice(start, stop, step)
+        super(parallel_range, self).__init__(self.S)
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        if bcast.get() is True:
+            bcast.toggle()
+            raise StopIteration
+        else:
+            bcast.toggle()
+            return self
+
+    def __next__(self):
+        return self.next()
+
 def slice_to_length(key, dim):
     tkey = [key.start, key.stop, key.step]
 
@@ -71,6 +97,9 @@ class index(ct.Structure):
         if isinstance(idx, base_array):
             self.idx.arr = idx.arr
             self.isSeq   = False
+        elif isinstance(idx, parallel_range):
+            self.idx.seq = idx
+            self.isBatch = True
         else:
             self.idx.seq = seq(idx)
 
@@ -104,6 +133,9 @@ def get_assign_dims(key, idims):
     elif isinstance(key, slice):
         dims[0] = slice_to_length(key, idims[0])
         return dims
+    elif isinstance(key, parallel_range):
+        dims[0] = slice_to_length(key.S, idims[0])
+        return dims
     elif isinstance(key, base_array):
         dims[0] = key.elements()
         return dims
@@ -120,6 +152,8 @@ def get_assign_dims(key, idims):
                 dims[n] = key[n].elements()
             elif (isinstance(key[n], slice)):
                 dims[n] = slice_to_length(key[n], idims[n])
+            elif (isinstance(key[n], parallel_range)):
+                dims[n] = slice_to_length(key[n].S, idims[n])
             else:
                 raise IndexError("Invalid type while assigning to arrayfire.array")
 
diff --git a/tests/simple_array.py b/tests/simple_array.py
index 95426a5..8f4f0f3 100755
--- a/tests/simple_array.py
+++ b/tests/simple_array.py
@@ -26,47 +26,13 @@ print(a.is_empty(), a.is_scalar(), a.is_column(), a.is_row())
 print(a.is_complex(), a.is_real(), a.is_double(), a.is_single())
 print(a.is_real_floating(), a.is_floating(), a.is_integer(), a.is_bool())
 
-a = af.array(host.array('l', [7, 8, 9] * 4), (2, 5))
+a = af.array(host.array('l', [7, 8, 9] * 3), (3,3))
 af.display(a)
 print(a.elements(), a.type(), a.dims(), a.numdims())
 print(a.is_empty(), a.is_scalar(), a.is_column(), a.is_row())
 print(a.is_complex(), a.is_real(), a.is_double(), a.is_single())
 print(a.is_real_floating(), a.is_floating(), a.is_integer(), a.is_bool())
 
-a = af.randu(5, 5)
-af.display(a)
-b = af.array(a)
-af.display(b)
-
-c = a.copy()
-af.display(c)
-af.display(a[0,0])
-af.display(a[0])
-af.display(a[:])
-af.display(a[:,:])
-af.display(a[0:3,])
-af.display(a[-2:-1,-1])
-af.display(a[0:5])
-af.display(a[0:5:2])
-
-idx = af.array(host.array('i', [0, 3, 2]))
-af.display(idx)
-aa = a[idx]
-af.display(aa)
-
-a[0] = 1
-af.display(a)
-a[0] = af.randu(1, 5)
-af.display(a)
-a[:] = af.randu(5,5)
-af.display(a)
-a[:,-1] = af.randu(5,1)
-af.display(a)
-a[0:5:2] = af.randu(3, 5)
-af.display(a)
-a[idx, idx] = af.randu(3,3)
-af.display(a)
-
 af.display(af.transpose(a))
 
 af.transpose_inplace(a)
diff --git a/tests/simple_index.py b/tests/simple_index.py
new file mode 100755
index 0000000..1ab9a66
--- /dev/null
+++ b/tests/simple_index.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python
+#######################################################
+# Copyright (c) 2015, ArrayFire
+# All rights reserved.
+#
+# This file is distributed under 3-clause BSD license.
+# The complete license agreement can be obtained at:
+# http://arrayfire.com/licenses/BSD-3-Clause
+########################################################
+import arrayfire as af
+from arrayfire import parallel_range
+import array as host
+
+a = af.randu(5, 5)
+af.display(a)
+b = af.array(a)
+af.display(b)
+
+c = a.copy()
+af.display(c)
+af.display(a[0,0])
+af.display(a[0])
+af.display(a[:])
+af.display(a[:,:])
+af.display(a[0:3,])
+af.display(a[-2:-1,-1])
+af.display(a[0:5])
+af.display(a[0:5:2])
+
+idx = af.array(host.array('i', [0, 3, 2]))
+af.display(idx)
+aa = a[idx]
+af.display(aa)
+
+a[0] = 1
+af.display(a)
+a[0] = af.randu(1, 5)
+af.display(a)
+a[:] = af.randu(5,5)
+af.display(a)
+a[:,-1] = af.randu(5,1)
+af.display(a)
+a[0:5:2] = af.randu(3, 5)
+af.display(a)
+a[idx, idx] = af.randu(3,3)
+af.display(a)
+
+
+a = af.randu(5,1)
+b = af.randu(5,1)
+af.display(a)
+af.display(b)
+for ii in parallel_range(1,3):
+    a[ii] = b[ii]
+
+af.display(a)
+
+for ii in parallel_range(2,5):
+    b[ii] = 2
+af.display(b)

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