Skip to content

Commit 75c1d43

Browse files
AntonAnton
authored andcommitted
Refactoring of basic functionality to create an empty Array
1 parent 33b0558 commit 75c1d43

File tree

13 files changed

+511
-36
lines changed

13 files changed

+511
-36
lines changed

.github/workflows/build.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Run Tests
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v1
11+
12+
- name: Set up Python 3.9
13+
uses: actions/setup-python@v1
14+
with:
15+
python-version: 3.9
16+
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install -r requirements.txt
21+
- name: Test with pytest
22+
run: |
23+
pytest

.gitignore

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ htmlcov/
4242
nosetests.xml
4343
coverage.xml
4444
*,cover
45+
.pytest_cache
4546

4647
# Translations
4748
*.mo
@@ -56,6 +57,8 @@ docs/_build/
5657
# PyBuilder
5758
target/
5859

59-
# IDE
60-
.idea
61-
.vscode
60+
# mypy
61+
.mypy_cache
62+
63+
# Virtual environment
64+
venv

arrayfire/array.py

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
Array class and helper functions.
1212
"""
1313

14+
from .algorithm import sum, count
15+
from .arith import cast
1416
import inspect
1517
import os
1618
from .library import *
@@ -25,6 +27,7 @@
2527

2628
_display_dims_limit = None
2729

30+
2831
def set_display_dims_limit(*dims):
2932
"""
3033
Sets the dimension limit after which array's data won't get
@@ -44,6 +47,7 @@ def set_display_dims_limit(*dims):
4447
global _display_dims_limit
4548
_display_dims_limit = dims
4649

50+
4751
def get_display_dims_limit():
4852
"""
4953
Gets the dimension limit after which array's data won't get
@@ -67,6 +71,7 @@ def get_display_dims_limit():
6771
"""
6872
return _display_dims_limit
6973

74+
7075
def _in_display_dims_limit(dims):
7176
if _is_running_in_py_charm:
7277
return False
@@ -80,6 +85,7 @@ def _in_display_dims_limit(dims):
8085
return False
8186
return True
8287

88+
8389
def _create_array(buf, numdims, idims, dtype, is_device):
8490
out_arr = c_void_ptr_t(0)
8591
c_dims = dim4(idims[0], idims[1], idims[2], idims[3])
@@ -91,6 +97,7 @@ def _create_array(buf, numdims, idims, dtype, is_device):
9197
numdims, c_pointer(c_dims), dtype.value))
9298
return out_arr
9399

100+
94101
def _create_strided_array(buf, numdims, idims, dtype, is_device, offset, strides):
95102
out_arr = c_void_ptr_t(0)
96103
c_dims = dim4(idims[0], idims[1], idims[2], idims[3])
@@ -112,16 +119,15 @@ def _create_strided_array(buf, numdims, idims, dtype, is_device, offset, strides
112119
location.value))
113120
return out_arr
114121

122+
115123
def _create_empty_array(numdims, idims, dtype):
116124
out_arr = c_void_ptr_t(0)
117-
118-
if numdims == 0: return out_arr
119-
120125
c_dims = dim4(idims[0], idims[1], idims[2], idims[3])
121126
safe_call(backend.get().af_create_handle(c_pointer(out_arr),
122127
numdims, c_pointer(c_dims), dtype.value))
123128
return out_arr
124129

130+
125131
def constant_array(val, d0, d1=None, d2=None, d3=None, dtype=Dtype.f32):
126132
"""
127133
Internal function to create a C array. Should not be used externall.
@@ -176,6 +182,7 @@ def _binary_func(lhs, rhs, c_func):
176182

177183
return out
178184

185+
179186
def _binary_funcr(lhs, rhs, c_func):
180187
out = Array()
181188
other = lhs
@@ -192,9 +199,10 @@ def _binary_funcr(lhs, rhs, c_func):
192199

193200
return out
194201

202+
195203
def _ctype_to_lists(ctype_arr, dim, shape, offset=0):
196204
if (dim == 0):
197-
return list(ctype_arr[offset : offset + shape[0]])
205+
return list(ctype_arr[offset: offset + shape[0]])
198206
else:
199207
dim_len = shape[dim]
200208
res = [[]] * dim_len
@@ -203,6 +211,7 @@ def _ctype_to_lists(ctype_arr, dim, shape, offset=0):
203211
offset += shape[0]
204212
return res
205213

214+
206215
def _slice_to_length(key, dim):
207216
tkey = [key.start, key.stop, key.step]
208217

@@ -221,6 +230,7 @@ def _slice_to_length(key, dim):
221230

222231
return int(((tkey[1] - tkey[0] - 1) / tkey[2]) + 1)
223232

233+
224234
def _get_info(dims, buf_len):
225235
elements = 1
226236
numdims = 0
@@ -250,6 +260,7 @@ def _get_indices(key):
250260

251261
return inds
252262

263+
253264
def _get_assign_dims(key, idims):
254265

255266
dims = [1]*4
@@ -296,6 +307,7 @@ def _get_assign_dims(key, idims):
296307
else:
297308
raise IndexError("Invalid type while assigning to arrayfire.array")
298309

310+
299311
def transpose(a, conj=False):
300312
"""
301313
Perform the transpose on an input.
@@ -318,6 +330,7 @@ def transpose(a, conj=False):
318330
safe_call(backend.get().af_transpose(c_pointer(out.arr), a.arr, conj))
319331
return out
320332

333+
321334
def transpose_inplace(a, conj=False):
322335
"""
323336
Perform inplace transpose on an input.
@@ -338,6 +351,7 @@ def transpose_inplace(a, conj=False):
338351
"""
339352
safe_call(backend.get().af_transpose_inplace(a.arr, conj))
340353

354+
341355
class Array(BaseArray):
342356

343357
"""
@@ -447,8 +461,8 @@ def __init__(self, src=None, dims=None, dtype=None, is_device=False, offset=None
447461

448462
super(Array, self).__init__()
449463

450-
buf=None
451-
buf_len=0
464+
buf = None
465+
buf_len = 0
452466

453467
if dtype is not None:
454468
if isinstance(dtype, str):
@@ -458,7 +472,7 @@ def __init__(self, src=None, dims=None, dtype=None, is_device=False, offset=None
458472
else:
459473
type_char = None
460474

461-
_type_char='f'
475+
_type_char = 'f'
462476

463477
if src is not None:
464478

@@ -469,12 +483,12 @@ def __init__(self, src=None, dims=None, dtype=None, is_device=False, offset=None
469483
host = __import__("array")
470484

471485
if isinstance(src, host.array):
472-
buf,buf_len = src.buffer_info()
486+
buf, buf_len = src.buffer_info()
473487
_type_char = src.typecode
474488
numdims, idims = _get_info(dims, buf_len)
475489
elif isinstance(src, list):
476490
tmp = host.array('f', src)
477-
buf,buf_len = tmp.buffer_info()
491+
buf, buf_len = tmp.buffer_info()
478492
_type_char = tmp.typecode
479493
numdims, idims = _get_info(dims, buf_len)
480494
elif isinstance(src, int) or isinstance(src, c_void_ptr_t):
@@ -498,7 +512,7 @@ def __init__(self, src=None, dims=None, dtype=None, is_device=False, offset=None
498512
raise TypeError("src is an object of unsupported class")
499513

500514
if (type_char is not None and
501-
type_char != _type_char):
515+
type_char != _type_char):
502516
raise TypeError("Can not create array of requested type from input data type")
503517
if(offset is None and strides is None):
504518
self.arr = _create_array(buf, numdims, idims, to_dtype[_type_char], is_device)
@@ -620,8 +634,8 @@ def strides(self):
620634
s2 = c_dim_t(0)
621635
s3 = c_dim_t(0)
622636
safe_call(backend.get().af_get_strides(c_pointer(s0), c_pointer(s1),
623-
c_pointer(s2), c_pointer(s3), self.arr))
624-
strides = (s0.value,s1.value,s2.value,s3.value)
637+
c_pointer(s2), c_pointer(s3), self.arr))
638+
strides = (s0.value, s1.value, s2.value, s3.value)
625639
return strides[:self.numdims()]
626640

627641
def elements(self):
@@ -680,8 +694,8 @@ def dims(self):
680694
d2 = c_dim_t(0)
681695
d3 = c_dim_t(0)
682696
safe_call(backend.get().af_get_dims(c_pointer(d0), c_pointer(d1),
683-
c_pointer(d2), c_pointer(d3), self.arr))
684-
dims = (d0.value,d1.value,d2.value,d3.value)
697+
c_pointer(d2), c_pointer(d3), self.arr))
698+
dims = (d0.value, d1.value, d2.value, d3.value)
685699
return dims[:self.numdims()]
686700

687701
@property
@@ -906,7 +920,7 @@ def __itruediv__(self, other):
906920
"""
907921
Perform self /= other.
908922
"""
909-
self = _binary_func(self, other, backend.get().af_div)
923+
self = _binary_func(self, other, backend.get().af_div)
910924
return self
911925

912926
def __rtruediv__(self, other):
@@ -925,7 +939,7 @@ def __idiv__(self, other):
925939
"""
926940
Perform other / self.
927941
"""
928-
self = _binary_func(self, other, backend.get().af_div)
942+
self = _binary_func(self, other, backend.get().af_div)
929943
return self
930944

931945
def __rdiv__(self, other):
@@ -944,7 +958,7 @@ def __imod__(self, other):
944958
"""
945959
Perform self %= other.
946960
"""
947-
self = _binary_func(self, other, backend.get().af_mod)
961+
self = _binary_func(self, other, backend.get().af_mod)
948962
return self
949963

950964
def __rmod__(self, other):
@@ -963,7 +977,7 @@ def __ipow__(self, other):
963977
"""
964978
Perform self **= other.
965979
"""
966-
self = _binary_func(self, other, backend.get().af_pow)
980+
self = _binary_func(self, other, backend.get().af_pow)
967981
return self
968982

969983
def __rpow__(self, other):
@@ -1106,15 +1120,15 @@ def logical_and(self, other):
11061120
Return self && other.
11071121
"""
11081122
out = Array()
1109-
safe_call(backend.get().af_and(c_pointer(out.arr), self.arr, other.arr)) #TODO: bcast var?
1123+
safe_call(backend.get().af_and(c_pointer(out.arr), self.arr, other.arr)) # TODO: bcast var?
11101124
return out
11111125

11121126
def logical_or(self, other):
11131127
"""
11141128
Return self || other.
11151129
"""
11161130
out = Array()
1117-
safe_call(backend.get().af_or(c_pointer(out.arr), self.arr, other.arr)) #TODO: bcast var?
1131+
safe_call(backend.get().af_or(c_pointer(out.arr), self.arr, other.arr)) # TODO: bcast var?
11181132
return out
11191133

11201134
def __nonzero__(self):
@@ -1144,12 +1158,11 @@ def __getitem__(self, key):
11441158
inds = _get_indices(key)
11451159

11461160
safe_call(backend.get().af_index_gen(c_pointer(out.arr),
1147-
self.arr, c_dim_t(n_dims), inds.pointer))
1161+
self.arr, c_dim_t(n_dims), inds.pointer))
11481162
return out
11491163
except RuntimeError as e:
11501164
raise IndexError(str(e))
11511165

1152-
11531166
def __setitem__(self, key, val):
11541167
"""
11551168
Perform self[key] = val
@@ -1175,14 +1188,14 @@ def __setitem__(self, key, val):
11751188
n_dims = 1
11761189
other_arr = constant_array(val, int(num), dtype=self.type())
11771190
else:
1178-
other_arr = constant_array(val, tdims[0] , tdims[1], tdims[2], tdims[3], self.type())
1191+
other_arr = constant_array(val, tdims[0], tdims[1], tdims[2], tdims[3], self.type())
11791192
del_other = True
11801193
else:
11811194
other_arr = val.arr
11821195
del_other = False
11831196

11841197
out_arr = c_void_ptr_t(0)
1185-
inds = _get_indices(key)
1198+
inds = _get_indices(key)
11861199

11871200
safe_call(backend.get().af_assign_gen(c_pointer(out_arr),
11881201
self.arr, c_dim_t(n_dims), inds.pointer,
@@ -1401,6 +1414,7 @@ def to_ndarray(self, output=None):
14011414
safe_call(backend.get().af_get_data_ptr(c_void_ptr_t(output.ctypes.data), tmp.arr))
14021415
return output
14031416

1417+
14041418
def display(a, precision=4):
14051419
"""
14061420
Displays the contents of an array.
@@ -1426,6 +1440,7 @@ def display(a, precision=4):
14261440
safe_call(backend.get().af_print_array_gen(name.encode('utf-8'),
14271441
a.arr, c_int_t(precision)))
14281442

1443+
14291444
def save_array(key, a, filename, append=False):
14301445
"""
14311446
Save an array to disk.
@@ -1457,6 +1472,7 @@ def save_array(key, a, filename, append=False):
14571472
append))
14581473
return index.value
14591474

1475+
14601476
def read_array(filename, index=None, key=None):
14611477
"""
14621478
Read an array from disk.
@@ -1490,6 +1506,3 @@ def read_array(filename, index=None, key=None):
14901506
key.encode('utf-8')))
14911507

14921508
return out
1493-
1494-
from .algorithm import (sum, count)
1495-
from .arith import cast

arrayfire/array_api/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
__all__ = [
2+
# array objects
3+
"Array",
4+
# dtypes
5+
"int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float32", "float64",
6+
"complex64", "complex128", "bool"]
7+
8+
from ._array_object import Array
9+
from ._dtypes import (
10+
bool, complex64, complex128, float32, float64, int16, int32, int64, uint8, uint16, uint32, uint64)

0 commit comments

Comments
 (0)