Skip to content

Commit 2d95302

Browse files
committed
Add pure Python implementation
1 parent 7d17ec4 commit 2d95302

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

source-code/cython/Numpy/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ build
22
*.so
33
array_init.c
44
array_sum.c
5+
array_init_pure.c
6+
array_sum_pure.c

source-code/cython/Numpy/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
VERSION = cpython-311-x86_64-linux-gnu
22
ARRAY_SUM_LIB = array_sum.$(VERSION).so
33
ARRAY_INIT_LIB = array_init.$(VERSION).so
4+
ARRAY_SUM_PURE_LIB = array_sum_pure.$(VERSION).so
5+
ARRAY_INIT_PURE_LIB = array_init_pure.$(VERSION).so
46
PROFILE = array_sums_prof.txt
57
override DIM = 1000
68
override ITER = 10
79

8-
all: $(ARRAY_SUM_LIB) $(ARRAY_INIT_LIB)
10+
all: $(ARRAY_SUM_LIB) $(ARRAY_SUM_PURE_LIB) \
11+
$(ARRAY_INIT_LIB) $(ARRAY_INIT_PURE_LIB)
912

1013
$(ARRAY_SUM_LIB): array_sum.pyx
1114
python setup.py build_ext --inplace
@@ -19,5 +22,4 @@ profile: $(ARRAY_SUM_LIB) compute_sums.py
1922

2023
clean:
2124
python setup.py clean
22-
rm -f array_sum.c $(ARRAY_SUM_LIB) $(PROFILE) \
23-
rm -f array_init.c $(ARRAY_INIT_LIB)
25+
$(RM) $(wildcard *.c) $(wildcard *.so)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import cython
2+
import numpy as np
3+
4+
def cons(dims: tuple[int]) -> np.array:
5+
array = np.empty(np.prod(dims))
6+
mv: cython.double[:] = array
7+
for i in range(mv.nbytes//mv.itemsize):
8+
array[i] = np.float64(i)
9+
return array.reshape(dims)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import cython
2+
import numpy as np
3+
4+
def array_sum(array: np.array) -> np.float64:
5+
shape = array.shape
6+
array.reshape((-1, ))
7+
mv: cython.double[:] = array.reshape((-1, ))
8+
total: cython.double = 0.0
9+
with cython.boundscheck(False):
10+
for i in range(mv.shape[0]):
11+
total += mv[i]
12+
return total

source-code/cython/Numpy/setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66
setup(
77
ext_modules=cythonize('*.pyx')
88
)
9+
setup(
10+
ext_modules=cythonize(['array_init.pyx', 'array_init_pure.py',
11+
'array_sum.pyx', 'array_sum_pure.py'],
12+
language_level='3str')
13+
)

0 commit comments

Comments
 (0)