Skip to content

Commit af3b310

Browse files
committed
Adding parallel range. First step towards GFOR like functionality.
1 parent a3553ef commit af3b310

File tree

3 files changed

+95
-35
lines changed

3 files changed

+95
-35
lines changed

arrayfire/index.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .library import *
1010
from .util import *
1111
from .base import *
12+
from .broadcast import *
1213

1314
class seq(ct.Structure):
1415
_fields_ = [("begin", ct.c_double),
@@ -35,6 +36,31 @@ def __init__ (self, S):
3536
else:
3637
raise IndexError("Invalid type while indexing arrayfire.array")
3738

39+
class parallel_range(seq):
40+
41+
def __init__(self, start, stop=None, step=None):
42+
43+
if (stop is None):
44+
stop = start
45+
start = 0
46+
47+
self.S = slice(start, stop, step)
48+
super(parallel_range, self).__init__(self.S)
49+
50+
def __iter__(self):
51+
return self
52+
53+
def next(self):
54+
if bcast.get() is True:
55+
bcast.toggle()
56+
raise StopIteration
57+
else:
58+
bcast.toggle()
59+
return self
60+
61+
def __next__(self):
62+
return self.next()
63+
3864
def slice_to_length(key, dim):
3965
tkey = [key.start, key.stop, key.step]
4066

@@ -71,6 +97,9 @@ def __init__ (self, idx):
7197
if isinstance(idx, base_array):
7298
self.idx.arr = idx.arr
7399
self.isSeq = False
100+
elif isinstance(idx, parallel_range):
101+
self.idx.seq = idx
102+
self.isBatch = True
74103
else:
75104
self.idx.seq = seq(idx)
76105

@@ -104,6 +133,9 @@ def get_assign_dims(key, idims):
104133
elif isinstance(key, slice):
105134
dims[0] = slice_to_length(key, idims[0])
106135
return dims
136+
elif isinstance(key, parallel_range):
137+
dims[0] = slice_to_length(key.S, idims[0])
138+
return dims
107139
elif isinstance(key, base_array):
108140
dims[0] = key.elements()
109141
return dims
@@ -120,6 +152,8 @@ def get_assign_dims(key, idims):
120152
dims[n] = key[n].elements()
121153
elif (isinstance(key[n], slice)):
122154
dims[n] = slice_to_length(key[n], idims[n])
155+
elif (isinstance(key[n], parallel_range)):
156+
dims[n] = slice_to_length(key[n].S, idims[n])
123157
else:
124158
raise IndexError("Invalid type while assigning to arrayfire.array")
125159

tests/simple_array.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,13 @@
2626
print(a.is_complex(), a.is_real(), a.is_double(), a.is_single())
2727
print(a.is_real_floating(), a.is_floating(), a.is_integer(), a.is_bool())
2828

29-
a = af.array(host.array('l', [7, 8, 9] * 4), (2, 5))
29+
a = af.array(host.array('l', [7, 8, 9] * 3), (3,3))
3030
af.display(a)
3131
print(a.elements(), a.type(), a.dims(), a.numdims())
3232
print(a.is_empty(), a.is_scalar(), a.is_column(), a.is_row())
3333
print(a.is_complex(), a.is_real(), a.is_double(), a.is_single())
3434
print(a.is_real_floating(), a.is_floating(), a.is_integer(), a.is_bool())
3535

36-
a = af.randu(5, 5)
37-
af.display(a)
38-
b = af.array(a)
39-
af.display(b)
40-
41-
c = a.copy()
42-
af.display(c)
43-
af.display(a[0,0])
44-
af.display(a[0])
45-
af.display(a[:])
46-
af.display(a[:,:])
47-
af.display(a[0:3,])
48-
af.display(a[-2:-1,-1])
49-
af.display(a[0:5])
50-
af.display(a[0:5:2])
51-
52-
idx = af.array(host.array('i', [0, 3, 2]))
53-
af.display(idx)
54-
aa = a[idx]
55-
af.display(aa)
56-
57-
a[0] = 1
58-
af.display(a)
59-
a[0] = af.randu(1, 5)
60-
af.display(a)
61-
a[:] = af.randu(5,5)
62-
af.display(a)
63-
a[:,-1] = af.randu(5,1)
64-
af.display(a)
65-
a[0:5:2] = af.randu(3, 5)
66-
af.display(a)
67-
a[idx, idx] = af.randu(3,3)
68-
af.display(a)
69-
7036
af.display(af.transpose(a))
7137

7238
af.transpose_inplace(a)

tests/simple_index.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/python
2+
#######################################################
3+
# Copyright (c) 2015, ArrayFire
4+
# All rights reserved.
5+
#
6+
# This file is distributed under 3-clause BSD license.
7+
# The complete license agreement can be obtained at:
8+
# http://arrayfire.com/licenses/BSD-3-Clause
9+
########################################################
10+
import arrayfire as af
11+
from arrayfire import parallel_range
12+
import array as host
13+
14+
a = af.randu(5, 5)
15+
af.display(a)
16+
b = af.array(a)
17+
af.display(b)
18+
19+
c = a.copy()
20+
af.display(c)
21+
af.display(a[0,0])
22+
af.display(a[0])
23+
af.display(a[:])
24+
af.display(a[:,:])
25+
af.display(a[0:3,])
26+
af.display(a[-2:-1,-1])
27+
af.display(a[0:5])
28+
af.display(a[0:5:2])
29+
30+
idx = af.array(host.array('i', [0, 3, 2]))
31+
af.display(idx)
32+
aa = a[idx]
33+
af.display(aa)
34+
35+
a[0] = 1
36+
af.display(a)
37+
a[0] = af.randu(1, 5)
38+
af.display(a)
39+
a[:] = af.randu(5,5)
40+
af.display(a)
41+
a[:,-1] = af.randu(5,1)
42+
af.display(a)
43+
a[0:5:2] = af.randu(3, 5)
44+
af.display(a)
45+
a[idx, idx] = af.randu(3,3)
46+
af.display(a)
47+
48+
49+
a = af.randu(5,1)
50+
b = af.randu(5,1)
51+
af.display(a)
52+
af.display(b)
53+
for ii in parallel_range(1,3):
54+
a[ii] = b[ii]
55+
56+
af.display(a)
57+
58+
for ii in parallel_range(2,5):
59+
b[ii] = 2
60+
af.display(b)

0 commit comments

Comments
 (0)