Parallel Distances
TanayLabUtilities.ParallelDistances
—
Module
Parallel versions of functions from
Distances.jl
.
TanayLabUtilities.ParallelDistances.parallel_pairwise
—
Function
parallel_pairwise(
distance, X[, Y];
dims::Integer,
policy::Symbol = :greedy_sticky,
progress::Maybe{AbstractProgress} = nothing
progress_chunk::Maybe{Integer} = nothing,
)::AbstractMatrix
A parallel version of
pairwise
. This will use
parallel_loop_wo_rng
over the columns of
Y
, with the specified
policy
and
progress
. If
policy
is
:serial
, then the standard version of
pairwise
is called and
progress
is ignored.
using Distances
using Random
Random.seed!(123456)
m1 = rand(10, 20)
m2 = rand(10, 30)
d = pairwise(Euclidean(), m1; dims = 2)
@assert maximum(abs.(d .- parallel_pairwise(Euclidean(), m1; dims = 2, policy = :serial))) < 1e-6
@assert maximum(abs.(d .- parallel_pairwise(Euclidean(), m1; dims = 2, policy = :greedy))) < 1e-6
@assert maximum(abs.(d .- parallel_pairwise(Euclidean(), m1; dims = 2, policy = :dynamic))) < 1e-6
@assert maximum(abs.(d .- parallel_pairwise(Euclidean(), m1; dims = 2, policy = :static))) < 1e-6
@assert maximum(abs.(d .- parallel_pairwise(Euclidean(), m1; dims = 2, policy = :greedy_sticky))) < 1e-6
d = pairwise(Euclidean(), m1, m2; dims = 2)
@assert maximum(abs.(d .- parallel_pairwise(Euclidean(), m1, m2; dims = 2, policy = :serial))) < 1e-6
@assert maximum(abs.(d .- parallel_pairwise(Euclidean(), m1, m2; dims = 2, policy = :greedy))) < 1e-6
@assert maximum(abs.(d .- parallel_pairwise(Euclidean(), m1, m2; dims = 2, policy = :dynamic))) < 1e-6
@assert maximum(abs.(d .- parallel_pairwise(Euclidean(), m1, m2; dims = 2, policy = :static))) < 1e-6
@assert maximum(abs.(d .- parallel_pairwise(Euclidean(), m1, m2; dims = 2, policy = :greedy_sticky))) < 1e-6
# output
TanayLabUtilities.ParallelDistances.parallel_pairwise_closest
—
Function
parallel_pairwise_closest(
distance, X, Y;
dims::Integer,
closest_index::Maybe{AbstractVector{<:Integer}} = nothing,
closest_distance::Maybe{AbstractVector} = nothing,
policy::Symbol = :greedy,
progress::Maybe{AbstractProgress} = nothing,
progress_chunk::Maybe{Integer} = nothing,
)::Nothing
For each entry of
Y
, find the closest entry of
X
. This is equivalent to computing
parallel_pairwise
and reducing each column to its minimal value and its location, except that the full distances matrix is never stored. The distances are computed and reduced on the fly using
parallel_loop_wo_rng
over the entries of
Y
, with the specified
policy
and
progress
.
The results are written into the pre-allocated
closest_index
and/or
closest_distance
vectors (whose length must be the number of entries of
Y
); at least one of them must be specified. This performs no allocations of its own.
using Distances
using Random
Random.seed!(123456)
m1 = rand(10, 20)
m2 = rand(10, 30)
d = pairwise(Euclidean(), m1, m2; dims = 2)
closest_index = Vector{Int}(undef, 30)
closest_distance = Vector{Float64}(undef, 30)
parallel_pairwise_closest(Euclidean(), m1, m2; dims = 2, closest_index, closest_distance)
@assert closest_index == [argmin(d[:, column]) for column in 1:30]
@assert maximum(abs.(closest_distance .- [minimum(d[:, column]) for column in 1:30])) < 1e-6
# output
TanayLabUtilities.ParallelDistances.parallel_colwise
—
Function
parallel_colwise(
distance, X, Y;
policy::Symbol = :greedy_sticky,
progress::Maybe{AbstractProgress} = nothing,
)::AbstractVector
A parallel version of
colwise
. This will use
parallel_loop_wo_rng
over the columns of
X
and
Y
, using the specified
policy
and
progress
. If
policy
is
:serial
, then the standard version of
pairwise
is called and
progress
is ignored.
using Distances
using Random
Random.seed!(123456)
m1 = rand(10, 20)
m2 = rand(10, 20)
d = colwise(Euclidean(), m1, m2)
@assert maximum(abs.(d .- parallel_colwise(Euclidean(), m1, m2; policy = :serial))) < 1e-6
@assert maximum(abs.(d .- parallel_colwise(Euclidean(), m1, m2; policy = :greedy))) < 1e-6
@assert maximum(abs.(d .- parallel_colwise(Euclidean(), m1, m2; policy = :dynamic))) < 1e-6
@assert maximum(abs.(d .- parallel_colwise(Euclidean(), m1, m2; policy = :static))) < 1e-6
@assert maximum(abs.(d .- parallel_colwise(Euclidean(), m1, m2; policy = :greedy_sticky))) < 1e-6
# output