Skip to content

avoid using threadid in landau example, instead use OhMyThreads + ChunkSplitters#1294

Merged
KnutAM merged 2 commits intomasterfrom
kc/landau_chunk
Mar 6, 2026
Merged

avoid using threadid in landau example, instead use OhMyThreads + ChunkSplitters#1294
KnutAM merged 2 commits intomasterfrom
kc/landau_chunk

Conversation

@KristofferC
Copy link
Copy Markdown
Collaborator

Indexing by threadid is not really valid. I removed the calcall because it felt kind of pointless to just have hanging there.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.25%. Comparing base (236eb50) to head (36300d5).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1294   +/-   ##
=======================================
  Coverage   94.25%   94.25%           
=======================================
  Files          40       40           
  Lines        6750     6750           
=======================================
  Hits         6362     6362           
  Misses        388      388           

☔ 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.

Copy link
Copy Markdown
Member

@KnutAM KnutAM left a comment

Choose a reason for hiding this comment

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

Nice 🚀 Added some more name-fixes thread->task and colors (I guess colored_indices is better but a bit long...)

I don't know all the OhMyThreads syntax, but looks good to me and nice to get rid of the @assemble macro!

I think adding a quick test would be nice at the end though

Comment thread docs/src/literate-gallery/landau.jl Outdated
@@ -47,7 +48,7 @@ struct ModelParams{V, T}
end

# ### ThreadCache
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
# ### ThreadCache
# ### TaskCache

Comment thread docs/src/literate-gallery/landau.jl Outdated
# ### ThreadCache
# This holds the values that each thread will use during the assembly.
# This holds the values that each task will use during the assembly.
struct ThreadCache{CV, T, DIM, F <: Function, GC <: GradientConfig, HC <: HessianConfig}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
struct ThreadCache{CV, T, DIM, F <: Function, GC <: GradientConfig, HC <: HessianConfig}
struct TaskCache{CV, T, DIM, F <: Function, GC <: GradientConfig, HC <: HessianConfig}

Comment thread docs/src/literate-gallery/landau.jl Outdated
dofs::Vector{T}
dofhandler::DH
boundaryconds::CH
threadindices::Vector{Vector{Int}}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
threadindices::Vector{Vector{Int}}
colors::Vector{Vector{Int}}

Comment thread docs/src/literate-gallery/landau.jl Outdated
function LandauModel(α, G, gridsize, left::Vec{DIM, T}, right::Vec{DIM, T}, elpotential) where {DIM, T}
function LandauModel(α, G, gridsize, left::Vec{DIM, T}, right::Vec{DIM, T}, elpotential, ntasks) where {DIM, T}
grid = generate_grid(Tetrahedron, gridsize, left, right)
threadindices = Ferrite.create_coloring(grid)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
threadindices = Ferrite.create_coloring(grid)
colors = create_coloring(grid)

Comment thread docs/src/literate-gallery/landau.jl Outdated
cpc = length(grid.cells[1].nodes)
caches = [ThreadCache(dpc, cpc, copy(cvP), ModelParams(α, G), elpotential) for t in 1:Threads.maxthreadid()]
caches = [ThreadCache(dpc, cpc, copy(cvP), ModelParams(α, G), elpotential) for _ in 1:ntasks]
return LandauModel(dofvector, dofhandler, boundaryconds, threadindices, caches)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
return LandauModel(dofvector, dofhandler, boundaryconds, threadindices, caches)
return LandauModel(dofvector, dofhandler, boundaryconds, colors, caches)

Comment thread docs/src/literate-gallery/landau.jl Outdated
# everything is combined into a model.
# Everything is combined into a model. The caches are pre-allocated (one per task)
# and indexed by chunk index during assembly.
mutable struct LandauModel{T, DH <: DofHandler, CH <: ConstraintHandler, TC <: ThreadCache}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
mutable struct LandauModel{T, DH <: DofHandler, CH <: ConstraintHandler, TC <: ThreadCache}
mutable struct LandauModel{T, DH <: DofHandler, CH <: ConstraintHandler, TC <: TaskCache}

Comment thread docs/src/literate-gallery/landau.jl Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
function TaskCache(dpc::Int, nodespercell, cvP::CellValues, modelparams, elpotential)

Comment thread docs/src/literate-gallery/landau.jl Outdated
@@ -72,16 +73,17 @@ function ThreadCache(dpc::Int, nodespercell, cvP::CellValues, modelparams, elpot
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
return TaskCache(cvP, element_indices, element_dofs, element_gradient, element_hessian, element_coords, potfunc, gradconf, hessconf)

Comment thread docs/src/literate-gallery/landau.jl Outdated
out = zero(T)
for indices in model.threadindices
partial = OhMyThreads.@tasks for (ichunk, range) in enumerate(chunks(indices; n = length(model.caches)))
@set reducer = +
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would this make sense to be extra clear where this macro comes from? (I assume it is from here)

Suggested change
@set reducer = +
OhMyThreads.@set reducer = +

right = Vec{3}((75.0, 25.0, 2.0))
model = LandauModel(α, G, (50, 50, 2), left, right, element_potential)
model = LandauModel(α, G, (50, 50, 2), left, right, element_potential, Threads.nthreads())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would be nice to add some quick tests to check that we don't make unintended changes (both here and for future)?

Suggested change
dh = model.dofhandler #hide
ddf = allocate_matrix(dh) #hide
df = zeros(ndofs(dh)) #hide
a = collect(range(0, 1, ndofs(dh))) #hide
@test F(a, model) ?? #hide
∇F!(df, a, model) #hide
@test norm(df) ?? #hide
∇²F!(ddf, a, model) #hide
@test norm(ddf) ?? #hide

@KristofferC
Copy link
Copy Markdown
Collaborator Author

Updated based on review comments. We will see if the result test passes everywhere.

Comment thread docs/src/literate-gallery/landau.jl
Copy link
Copy Markdown
Member

@termi-official termi-official left a comment

Choose a reason for hiding this comment

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

Other than one more note this PR is good from my side.

save_landau("landaufinal", model)

using Test # src
@test Optim.minimum(res) ≈ -10858.806775 # src
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isn't the tolerance here a bit tight? I.e. can we really guarantee machine precision, such that in the future we won't see failures here popping up due to changes in Ferrite (or e.g. changes in Optim.jl).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is using approx so not machine precision? I already cut off a bunch of decimals here from the answer I got locally.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess the approx tolerance is quite tight for an optimization run (which is why I originally suggested just to test the different assembly runs), but I'm also fine merging this as is and then adopt the tests if we see random failures in the future (should be fine see manually that it fails with small changes now that we have a reference solution). Of course, we could do

Suggested change
@test Optim.minimum(res) -10858.806775 # src
@test Optim.minimum(res) -10858.807f0 # src

to reduce the precision 😄

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We can mess with it if we see there is an actual problem in the future?

@KnutAM KnutAM merged commit eb76ddc into master Mar 6, 2026
15 of 16 checks passed
@KnutAM KnutAM deleted the kc/landau_chunk branch March 6, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants