def _sparse_distance_opt(lock, input_list, global_idx, rows, cols, data, func):
"""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 = input_list.shape[0]
# 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)
for i in range(idx, list_len-1):
_res = func(input_list[i], input_list[i + 1])
if _res > 0:
j, d = i+1, 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
评论列表
文章目录