Skip to content

Add missing parallel=:threads implementations #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

topolarity
Copy link

These implementations are basic, but I tried to follow the patterns in the other parts of the Parallel module.

My real motivation is to be able to move the Distributed implementations into an extension, so that Graphs.jl does not depend on Distributed.

@simonschoelly
Copy link
Member

What is the issue with using Distrubuted? As far as I know it is a standard library.

@oscardssmith
Copy link
Member

Multithreading typically has lower overhead since it's shared memory. Distributed is usually only worthwhile if you are trying to distribute across multiple machines.

@oscardssmith
Copy link
Member

Also, Distributed is a stdlib, but stdlibs are still dependencies, and as we have started moving stlibs out of the sysimage, they are becoming more like regular packages.

@topolarity topolarity force-pushed the ct/missing-threaded-impls branch from b5b55a0 to c109686 Compare May 14, 2025 13:14
@Krastanov
Copy link
Member

Would this require also bumping the minimum requirement to julia=1.9?

I am generally in favor of moving more dependencies to optional dependencies. It makes compilation times much more pleasant as well.

@topolarity topolarity force-pushed the ct/missing-threaded-impls branch 2 times, most recently from 2e9b9f3 to a7db672 Compare May 14, 2025 14:19
Copy link

codecov bot commented May 14, 2025

Codecov Report

Attention: Patch coverage is 88.67925% with 6 lines in your changes missing coverage. Please review.

Project coverage is 97.24%. Comparing base (6130332) to head (cbda59e).
Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
src/Parallel/distance.jl 89.47% 2 Missing ⚠️
src/Parallel/shortestpaths/dijkstra.jl 88.88% 2 Missing ⚠️
src/Parallel/traversals/greedy_color.jl 87.50% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #429      +/-   ##
==========================================
- Coverage   97.31%   97.24%   -0.07%     
==========================================
  Files         117      117              
  Lines        6956     7004      +48     
==========================================
+ Hits         6769     6811      +42     
- Misses        187      193       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

These implementations are extremely basic, but they try to follow the
patterns in the other parts of the Parallel module. My real motivation
is to be able to move the Distributed implementations into an extension,
so that Graphs.jl does not depend on Distributed.
@topolarity topolarity force-pushed the ct/missing-threaded-impls branch from fc4a1b9 to cbda59e Compare May 14, 2025 16:49
@topolarity
Copy link
Author

Would this require also bumping the minimum requirement to julia=1.9?

There are compatibility pathways where we only switch to an extension on 1.9+ and keep the hard dependency for earlier versions

I think this PR should be ready for review now - it is passing tests for me locally

Copy link
Member

@Krastanov Krastanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I am a bit new to the maintainer team, and this does introduce a new public feature (the parallel kwarg), so I will wait a bit for dissent from folks with more seniority.

@topolarity
Copy link
Author

topolarity commented May 14, 2025

a new public feature (the parallel kwarg)

FWIW, that kwarg is already there on a lot of other methods, so it's not a new design choice exactly - it was just never provided for these particular operations (because they only had a parallel=:distributed implementation)

@simonschoelly
Copy link
Member

LGTM, but I am a bit new to the maintainer team, and this does introduce a new public feature (the parallel kwarg), so I will wait a bit for dissent from folks with more seniority.

There is some new multi threaded code here - so we probably should take a bit of time to review it - as it can lead to notoriously difficult to spot bugs.

g::AbstractGraph,
vs=vertices(g),
distmx::AbstractMatrix{T}=weights(g);
parallel=:distributed,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parallel=:distributed,
parallel::Symbol=:distributed,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we haven't done that in other places - but perhaps to make this code more future proof would it make sense to use a specific structure to specify the parallel policy? Then we could later add things such as the number of threads/cores used.

distr_eccentricity(g, vs, distmx)
else
error(
"Unsupported parallel argument '$(repr(parallel))' (supported: ':threads' or ':distributed')",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add a test for the case that someone uses the wrong argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We repeat the same error multiple times - Perhaps we can throw a custom error type instead?

g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix{T}=weights(g)
) where {T<:Number}
vlen = length(vs)
eccs = SharedVector{T}(vlen)
@sync @distributed for i in 1:vlen
eccs[i] = maximum(Graphs.dijkstra_shortest_paths(g, vs[i], distmx).dists)
d′ = Graphs.dijkstra_shortest_paths(g, vs[i], distmx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit unsure about the usage of d′ - it is hardly distinguishable from d' - in that case ' is the adjoint operator.

[1, 3]
@test @inferred(Graphs.radius(y)) == @inferred(Parallel.radius(g, distmx1)) == 4.2
@test @inferred(Graphs.center(y)) == @inferred(Parallel.center(g, distmx1)) == [2]
for parallel in [:threads, :distributed]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to write the test as

@testset "Parallel.Distance" for parallel in [:threads, :distributed]
[...]
end

end

function threaded_random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T<:Integer}
local_best = Any[nothing for _ in 1:reps]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to use Any[nothing for _ in 1:reps] instead of

Vector{Graphs.Coloring{T}}(undef, reps)

or

Vector{Union{Nothing, Graphs.Coloring{T}}}(nothing, reps)

?

Copy link
Member

@simonschoelly simonschoelly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly good to me. I don't see any race condition

I have added some comments. They are mostly suggestions. In addition:

  • We probably should write some documentation for the new parallel argument. Although I admit that this module is fairly undocumented anyway - and as we don't export anything yet we don't have to be so rigorous.

@simonschoelly simonschoelly added the enhancement New feature or request label May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants