def _sparse_distance(lock, input_list, global_idx, rows, cols, data, dist_function):
"""Parallelize a general computation of a sparse distance matrix.
Parameters
----------
lock : multiprocessing.synchronize.Lock
Value returned from multiprocessing.Lock().
input_list : list
List of values to compare to input_list[idx] (from 'idx' on).
shared_arr : array_like
Numpy array created as a shared object. Iteratively updated with the result.
Example:
shared_array = np.frombuffer(mp.Array('d', n*n).get_obj()).reshape((n,n))
Returns
-------
"""
list_len = len(input_list)
# PID = os.getpid()
# print("PID {} takes index {}".format(PID, index_i))
while global_idx.value < list_len:
with lock:
if not global_idx.value < list_len: return
idx = global_idx.value
global_idx.value += 1
if (idx) % 100 == 0: progressbar(idx, list_len)
elem_1 = input_list[idx]
for idx_j in range(idx+1, list_len):
_res = dist_function(elem_1, input_list[idx_j])
if _res > 0:
i, j, d = idx, idx_j, list_len
c_idx = d*(d-1)/2 - (d-i)*(d-i-1)/2 + j - i - 1
data[c_idx] = _res
rows[c_idx] = i
cols[c_idx] = j
评论列表
文章目录