def dense_dm_dual(list1, list2, dist_function, condensed=False):
"""Compute in a parallel way a distance matrix for a 1-d array.
Parameters
----------
input_array : array_like
1-dimensional array for which to compute the distance matrix.
dist_function : function
Function to use for the distance computation.
Returns
-------
dist_matrix : array_like
Symmetric NxN distance matrix for each input_array element.
"""
n, m = len(list1), len(list2)
n_proc = min(mp.cpu_count(), n)
index = mp.Value('i', 0)
shared_array = np.frombuffer(mp.Array('d', n*m).get_obj()).reshape((n,m))
ps = []
lock = mp.Lock()
try:
for _ in range(n_proc):
p = mp.Process(target=_dense_distance_dual,
args=(lock, list1, list2, index, shared_array, dist_function))
p.start()
ps.append(p)
for p in ps:
p.join()
except (KeyboardInterrupt, SystemExit): _terminate(ps,'Exit signal received\n')
except Exception as e: _terminate(ps,'ERROR: %s\n' % e)
except: _terminate(ps,'ERROR: Exiting with unknown exception\n')
dist_matrix = shared_array.flatten() if condensed else shared_array
# progressbar(n,n)
return dist_matrix
评论列表
文章目录