Skip to content

Commit 5a12efb

Browse files
defragmented temporary memory allocations in sparse_dot_topn routines
1 parent d6f3127 commit 5a12efb

9 files changed

+1257
-1239
lines changed

setup.py

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def finalize_options(self):
3535
'./sparse_dot_topn/sparse_dot_topn_source.cpp'
3636
],
3737
extra_compile_args=extra_compile_args,
38+
define_macros=[('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')],
3839
language='c++')
3940

4041
original_ext = Extension('sparse_dot_topn.sparse_dot_topn',
@@ -43,6 +44,7 @@ def finalize_options(self):
4344
'./sparse_dot_topn/sparse_dot_topn_source.cpp'
4445
],
4546
extra_compile_args=extra_compile_args,
47+
define_macros=[('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')],
4648
language='c++')
4749

4850
threaded_ext = Extension('sparse_dot_topn.sparse_dot_topn_threaded',
@@ -52,6 +54,7 @@ def finalize_options(self):
5254
'./sparse_dot_topn/sparse_dot_topn_parallel.cpp'
5355
],
5456
extra_compile_args=extra_compile_args,
57+
define_macros=[('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')],
5558
language='c++')
5659

5760
setup(

sparse_dot_topn/array_wrappers.pxd

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ from libcpp.vector cimport vector
22

33
# define a Cython array wrapper class to hold a C++ vector of ints, adhering to numpy's buffer protocol:
44
cdef class ArrayWrapper_int:
5-
cdef int view_count
6-
cdef vector[int] vec
7-
cdef Py_ssize_t shape[2]
8-
cdef Py_ssize_t strides[2]
5+
cdef int view_count
6+
cdef vector[int] vec
7+
cdef Py_ssize_t shape[2]
8+
cdef Py_ssize_t strides[2]
99

1010

1111
# define a Cython array wrapper class to hold a C++ vector of doubles, adhering to numpy's buffer protocol:
1212
cdef class ArrayWrapper_double:
13-
cdef int view_count
14-
cdef vector[double] vec
15-
cdef Py_ssize_t shape[2]
16-
cdef Py_ssize_t strides[2]
13+
cdef int view_count
14+
cdef vector[double] vec
15+
cdef Py_ssize_t shape[2]
16+
cdef Py_ssize_t strides[2]
1717

1818

sparse_dot_topn/array_wrappers.pyx

+58-58
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,71 @@ from libcpp.vector cimport vector
33

44
# define a Cython array wrapper class to hold a C++ vector of ints, adhering to numpy's buffer protocol:
55
cdef class ArrayWrapper_int:
6-
# constructor and destructor are fairly unimportant now since
7-
# vec will be destroyed automatically.
6+
# constructor and destructor are fairly unimportant now since
7+
# vec will be destroyed automatically.
88

9-
def __cinit__(self, vector[int]& data):
10-
self.vec.swap(data)
11-
self.view_count = 0
9+
def __cinit__(self, vector[int]& data):
10+
self.vec.swap(data)
11+
self.view_count = 0
1212

13-
# now implement the buffer protocol for the class
14-
# which makes it generally useful to anything that expects an array
15-
def __getbuffer__(self, Py_buffer *buffer, int flags):
16-
# relevant documentation http://cython.readthedocs.io/en/latest/src/userguide/buffer.html#a-matrix-class
17-
cdef Py_ssize_t itemsize = sizeof(self.vec[0])
13+
# now implement the buffer protocol for the class
14+
# which makes it generally useful to anything that expects an array
15+
def __getbuffer__(self, Py_buffer *buffer, int flags):
16+
# relevant documentation http://cython.readthedocs.io/en/latest/src/userguide/buffer.html#a-matrix-class
17+
cdef Py_ssize_t itemsize = sizeof(self.vec[0])
1818

19-
self.shape[1] = self.vec.size()
20-
self.shape[0] = 1
21-
self.strides[1] = <Py_ssize_t>( <char *>&(self.vec[1]) - <char *>&(self.vec[0]))
22-
self.strides[0] = self.vec.size() * self.strides[1]
23-
buffer.buf = <char *>&(self.vec[0])
24-
buffer.format = 'i'
25-
buffer.internal = NULL
26-
buffer.itemsize = itemsize
27-
buffer.len = self.vec.size() * itemsize # product(shape) * itemsize
28-
buffer.ndim = 2
29-
buffer.obj = self
30-
buffer.readonly = 0
31-
buffer.shape = self.shape
32-
buffer.strides = self.strides
33-
buffer.suboffsets = NULL
34-
self.view_count += 1
35-
36-
def __releasebuffer__(self, Py_buffer *buffer):
37-
self.view_count -= 1
19+
self.shape[1] = self.vec.size()
20+
self.shape[0] = 1
21+
self.strides[1] = <Py_ssize_t>( <char *>&(self.vec[1]) - <char *>&(self.vec[0]))
22+
self.strides[0] = self.vec.size() * self.strides[1]
23+
buffer.buf = <char *>&(self.vec[0])
24+
buffer.format = 'i'
25+
buffer.internal = NULL
26+
buffer.itemsize = itemsize
27+
buffer.len = self.vec.size() * itemsize # product(shape) * itemsize
28+
buffer.ndim = 2
29+
buffer.obj = self
30+
buffer.readonly = 0
31+
buffer.shape = self.shape
32+
buffer.strides = self.strides
33+
buffer.suboffsets = NULL
34+
self.view_count += 1
35+
36+
def __releasebuffer__(self, Py_buffer *buffer):
37+
self.view_count -= 1
3838

3939

4040
# define a Cython array wrapper class to hold a C++ vector of doubles, adhering to numpy's buffer protocol:
4141
cdef class ArrayWrapper_double:
42-
# constructor and destructor are fairly unimportant now since
43-
# vec will be destroyed automatically.
42+
# constructor and destructor are fairly unimportant now since
43+
# vec will be destroyed automatically.
4444

45-
def __cinit__(self, vector[double]& data):
46-
self.vec.swap(data)
47-
self.view_count = 0
45+
def __cinit__(self, vector[double]& data):
46+
self.vec.swap(data)
47+
self.view_count = 0
4848

49-
# now implement the buffer protocol for the class
50-
# which makes it generally useful to anything that expects an array
51-
def __getbuffer__(self, Py_buffer *buffer, int flags):
52-
# relevant documentation http://cython.readthedocs.io/en/latest/src/userguide/buffer.html#a-matrix-class
53-
cdef Py_ssize_t itemsize = sizeof(self.vec[0])
49+
# now implement the buffer protocol for the class
50+
# which makes it generally useful to anything that expects an array
51+
def __getbuffer__(self, Py_buffer *buffer, int flags):
52+
# relevant documentation http://cython.readthedocs.io/en/latest/src/userguide/buffer.html#a-matrix-class
53+
cdef Py_ssize_t itemsize = sizeof(self.vec[0])
5454

55-
self.shape[1] = self.vec.size()
56-
self.shape[0] = 1
57-
self.strides[1] = <Py_ssize_t>( <char *>&(self.vec[1]) - <char *>&(self.vec[0]))
58-
self.strides[0] = self.vec.size() * self.strides[1]
59-
buffer.buf = <char *>&(self.vec[0])
60-
buffer.format = 'd'
61-
buffer.internal = NULL
62-
buffer.itemsize = itemsize
63-
buffer.len = self.vec.size() * itemsize # product(shape) * itemsize
64-
buffer.ndim = 2
65-
buffer.obj = self
66-
buffer.readonly = 0
67-
buffer.shape = self.shape
68-
buffer.strides = self.strides
69-
buffer.suboffsets = NULL
70-
self.view_count += 1
71-
72-
def __releasebuffer__(self, Py_buffer *buffer):
73-
self.view_count -= 1
55+
self.shape[1] = self.vec.size()
56+
self.shape[0] = 1
57+
self.strides[1] = <Py_ssize_t>( <char *>&(self.vec[1]) - <char *>&(self.vec[0]))
58+
self.strides[0] = self.vec.size() * self.strides[1]
59+
buffer.buf = <char *>&(self.vec[0])
60+
buffer.format = 'd'
61+
buffer.internal = NULL
62+
buffer.itemsize = itemsize
63+
buffer.len = self.vec.size() * itemsize # product(shape) * itemsize
64+
buffer.ndim = 2
65+
buffer.obj = self
66+
buffer.readonly = 0
67+
buffer.shape = self.shape
68+
buffer.strides = self.strides
69+
buffer.suboffsets = NULL
70+
self.view_count += 1
71+
72+
def __releasebuffer__(self, Py_buffer *buffer):
73+
self.view_count -= 1

0 commit comments

Comments
 (0)