Skip to content

Commit

Permalink
Merge pull request scipy#18833 from ThomasLoke/lsap-gil
Browse files Browse the repository at this point in the history
ENH: optimize: release the GIL while computing the LSAP solution

[skip ci]
  • Loading branch information
rgommers authored Jul 12, 2023
2 parents bd8ef0c + 77c33ca commit 19212c4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 16 additions & 0 deletions benchmarks/benchmarks/optimize_lap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from concurrent.futures import ThreadPoolExecutor, wait

import numpy as np
from .common import Benchmark, safe_import

Expand Down Expand Up @@ -50,3 +52,17 @@ def setup(self, shape, cost_type):

def time_evaluation(self, *args):
linear_sum_assignment(self.cost_matrix)


class ParallelLinearAssignment(Benchmark):
shape = (100, 100)
param_names = ['threads']
params = [[1, 2, 4]]

def setup(self, threads):
self.cost_matrices = [random_uniform(self.shape) for _ in range(20)]

def time_evaluation(self, threads):
with ThreadPoolExecutor(max_workers=threads) as pool:
wait({pool.submit(linear_sum_assignment, cost_matrix)
for cost_matrix in self.cost_matrices})
15 changes: 11 additions & 4 deletions scipy/optimize/_lsap.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,17 @@ linear_sum_assignment(PyObject* self, PyObject* args, PyObject* kwargs)
if (!b)
goto cleanup;

int ret = solve_rectangular_linear_sum_assignment(
num_rows, num_cols, cost_matrix, maximize,
PyArray_DATA((PyArrayObject*)a),
PyArray_DATA((PyArrayObject*)b));
int64_t* a_ptr = PyArray_DATA((PyArrayObject*)a);
int64_t* b_ptr = PyArray_DATA((PyArrayObject*)b);
int ret;

Py_BEGIN_ALLOW_THREADS

ret = solve_rectangular_linear_sum_assignment(
num_rows, num_cols, cost_matrix, maximize, a_ptr, b_ptr);

Py_END_ALLOW_THREADS

if (ret == RECTANGULAR_LSAP_INFEASIBLE) {
PyErr_SetString(PyExc_ValueError, "cost matrix is infeasible");
goto cleanup;
Expand Down

0 comments on commit 19212c4

Please sign in to comment.