Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: cluster: clean up the use of malloc() in the function identify_swaps(). #10027

Merged
merged 2 commits into from Apr 9, 2019

Conversation

WarrenWeckesser
Copy link
Member

This addresses one of the modules with unchecked malloc() calls listed in #9999.
The commit messages explain the details of the changes.

In the function identify_swaps() in _optimal_leaf_ordering.pyx, the
variables u_clusters, m_clusters, w_clusters and k_clusters are each
malloc'ed as malloc(sizeof(int) * 2).  For small fixed-size arrays
such as these, it makes more sense to declare them as arrays on the
stack and avoid malloc() completely.
Changes in the function identify_swaps() in _optimal_leaf_ordering.pyx:

* The code was malloc'ing and freeing arrays in an inner loop, but the
  size of the arrays was the same in each iteration of the inner loop.
  The malloc() and free() calls have been moved out to the level where
  the variables that define the size are set.
* A MemoryError is raised if any of the malloc() calls fail.
* The types of the numpy arrays used to create the memoryviews
  `swap_status` and `must_swap` was changed from np.int32 to np.intc,
  to match the declaration of the memoryviews (int[:, :, :] and int[:],
  respectively).
@rgommers
Copy link
Member

rgommers commented Apr 9, 2019

LGTM, merged. Thanks @WarrenWeckesser

@rgommers rgommers added this to the 1.3.0 milestone Apr 9, 2019
@WarrenWeckesser WarrenWeckesser deleted the cluster-malloc branch April 9, 2019 13:36
@WarrenWeckesser WarrenWeckesser added the Cython Issues with the internal Cython code base label Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cython Issues with the internal Cython code base maintenance Items related to regular maintenance tasks scipy.cluster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants