File tree Expand file tree Collapse file tree 5 files changed +33
-3
lines changed
Expand file tree Collapse file tree 5 files changed +33
-3
lines changed Original file line number Diff line number Diff line change 22* .so
33array_init.c
44array_sum.c
5+ array_init_pure.c
6+ array_sum_pure.c
Original file line number Diff line number Diff line change 11VERSION = cpython-311-x86_64-linux-gnu
22ARRAY_SUM_LIB = array_sum.$(VERSION ) .so
33ARRAY_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
46PROFILE = array_sums_prof.txt
57override DIM = 1000
68override 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
2023clean :
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)
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 66setup (
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+ )
You can’t perform that action at this time.
0 commit comments