Skip to content

Use eachindex in Coloring #2559

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

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/Nonlinear/ReverseAD/Coloring/Coloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
function UndirectedGraph(I, J, nel)
adjcount = zeros(Int, nel)
n_edges = 0
for k in 1:length(I)
for k in eachindex(I)

Check warning on line 79 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L79

Added line #L79 was not covered by tests
i = I[k]
j = J[k]
if i == j
Expand All @@ -96,7 +96,7 @@
adjlist = Vector{Int}(undef, offsets[nel+1] - 1)
edgeindex = Vector{Int}(undef, length(adjlist))
edge_count = 0
for k in 1:length(I)
for k in eachindex(I)

Check warning on line 99 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L99

Added line #L99 was not covered by tests
i = I[k]
j = J[k]
if i == j
Expand Down Expand Up @@ -306,7 +306,7 @@
seen_twocolors = 0
# count of edges in each subgraph
edge_count = Int[]
for k in 1:length(g.edges)
for k in eachindex(g.edges)

Check warning on line 309 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L309

Added line #L309 was not covered by tests
u, v = g.edges[k]
i = min(color[u], color[v])
j = max(color[u], color[v])
Expand All @@ -324,7 +324,7 @@
sorted_edges[idx] = Tuple{Int,Int}[]
sizehint!(sorted_edges[idx], edge_count[idx])
end
for i in 1:length(g.edges)
for i in eachindex(g.edges)

Check warning on line 327 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L327

Added line #L327 was not covered by tests
u, v = g.edges[i]
i = min(color[u], color[v])
j = max(color[u], color[v])
Expand All @@ -343,7 +343,7 @@
my_edges = sorted_edges[idx]
vlist = Int[]
# build up the vertex list and adjacency count
for k in 1:length(my_edges)
for k in eachindex(my_edges)

Check warning on line 346 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L346

Added line #L346 was not covered by tests
u, v = my_edges[k]
# seen these vertices yet?
if revmap[u] == 0
Expand All @@ -362,7 +362,7 @@
# set up offsets for adjlist
offset = Vector{Int}(undef, length(vlist) + 1)
offset[1] = 1
for k in 1:length(vlist)
for k in eachindex(vlist)

Check warning on line 365 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L365

Added line #L365 was not covered by tests
offset[k+1] = offset[k] + adjcount[vlist[k]]
adjcount[vlist[k]] = 0
end
Expand All @@ -371,7 +371,7 @@
# u has global index vlist[u]
vec = Vector{Int}(undef, offset[length(vlist)+1] - 1)
# now fill in
for k in 1:length(my_edges)
for k in eachindex(my_edges)

Check warning on line 374 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L374

Added line #L374 was not covered by tests
u, v = my_edges[k]
u_rev = revmap[u] # indices in the subgraph
v_rev = revmap[v]
Expand All @@ -383,7 +383,7 @@
resize!(cmap, length(vlist))
order, parent =
reverse_topological_sort_by_dfs(vec, offset, length(vlist), cmap)
for k in 1:length(vlist)
for k in eachindex(vlist)

Check warning on line 386 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L386

Added line #L386 was not covered by tests
revmap[vlist[k]] = 0 # clear for reuse
end
postorder[idx] = order
Expand Down Expand Up @@ -412,11 +412,11 @@
k += 1
I[k] = J[k] = i
end
for t in 1:length(rinfo.postorder)
for t in eachindex(rinfo.postorder)

Check warning on line 415 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L415

Added line #L415 was not covered by tests
vmap = rinfo.vertexmap[t]
order = rinfo.postorder[t]
parent = rinfo.parents[t]
for z in 1:length(order)
for z in eachindex(order)

Check warning on line 419 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L419

Added line #L419 was not covered by tests
v = order[z]
p = parent[v]
if p == 0
Expand Down Expand Up @@ -456,11 +456,11 @@
local_indices = sort!(seen_idx.nzidx[1:seen_idx.nnz])
empty!(seen_idx)
global_to_local_idx = seen_idx.nzidx # steal for storage
for k in 1:length(local_indices)
for k in eachindex(local_indices)

Check warning on line 459 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L459

Added line #L459 was not covered by tests
global_to_local_idx[local_indices[k]] = k
end
# only do the coloring on the local indices
for k in 1:length(I)
for k in eachindex(I)

Check warning on line 463 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L463

Added line #L463 was not covered by tests
I[k] = global_to_local_idx[I[k]]
J[k] = global_to_local_idx[J[k]]
end
Expand All @@ -470,7 +470,7 @@
rinfo = recovery_preprocess(g, color, num_colors, local_indices)
I, J = _indirect_recover_structure(rinfo)
# convert back to global indices
for k in 1:length(I)
for k in eachindex(I)

Check warning on line 473 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L473

Added line #L473 was not covered by tests
I[k] = local_indices[I[k]]
J[k] = local_indices[J[k]]
end
Expand Down Expand Up @@ -525,14 +525,14 @@
k += 1
V[k] = R[i, rinfo.color[i]]
end
for t in 1:length(rinfo.vertexmap)
for t in eachindex(rinfo.vertexmap)

Check warning on line 528 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L528

Added line #L528 was not covered by tests
vmap = rinfo.vertexmap[t]
order = rinfo.postorder[t]
parent = rinfo.parents[t]
for z in 1:length(order)
for z in eachindex(order)

Check warning on line 532 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L532

Added line #L532 was not covered by tests
@inbounds stored_values[z] = 0.0
end
@inbounds for z in 1:length(order)
@inbounds for z in eachindex(order)

Check warning on line 535 in src/Nonlinear/ReverseAD/Coloring/Coloring.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/Coloring/Coloring.jl#L535

Added line #L535 was not covered by tests
v = order[z]
p = parent[v]
if p == 0
Expand Down
Loading