avoid using threadid in landau example, instead use OhMyThreads + ChunkSplitters#1294
avoid using threadid in landau example, instead use OhMyThreads + ChunkSplitters#1294
threadid in landau example, instead use OhMyThreads + ChunkSplitters#1294Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
69f8150 to
c98643a
Compare
KnutAM
left a comment
There was a problem hiding this comment.
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
| @@ -47,7 +48,7 @@ struct ModelParams{V, T} | |||
| end | |||
|
|
|||
| # ### ThreadCache | |||
There was a problem hiding this comment.
| # ### ThreadCache | |
| # ### TaskCache |
| # ### 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} |
There was a problem hiding this comment.
| struct ThreadCache{CV, T, DIM, F <: Function, GC <: GradientConfig, HC <: HessianConfig} | |
| struct TaskCache{CV, T, DIM, F <: Function, GC <: GradientConfig, HC <: HessianConfig} |
| dofs::Vector{T} | ||
| dofhandler::DH | ||
| boundaryconds::CH | ||
| threadindices::Vector{Vector{Int}} |
There was a problem hiding this comment.
| threadindices::Vector{Vector{Int}} | |
| colors::Vector{Vector{Int}} |
| 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) |
There was a problem hiding this comment.
| threadindices = Ferrite.create_coloring(grid) | |
| colors = create_coloring(grid) |
| 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) |
There was a problem hiding this comment.
| return LandauModel(dofvector, dofhandler, boundaryconds, threadindices, caches) | |
| return LandauModel(dofvector, dofhandler, boundaryconds, colors, caches) |
| # 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} |
There was a problem hiding this comment.
| mutable struct LandauModel{T, DH <: DofHandler, CH <: ConstraintHandler, TC <: ThreadCache} | |
| mutable struct LandauModel{T, DH <: DofHandler, CH <: ConstraintHandler, TC <: TaskCache} |
There was a problem hiding this comment.
| function TaskCache(dpc::Int, nodespercell, cvP::CellValues, modelparams, elpotential) |
| @@ -72,16 +73,17 @@ function ThreadCache(dpc::Int, nodespercell, cvP::CellValues, modelparams, elpot | |||
There was a problem hiding this comment.
| return TaskCache(cvP, element_indices, element_dofs, element_gradient, element_hessian, element_coords, potfunc, gradconf, hessconf) |
| out = zero(T) | ||
| for indices in model.threadindices | ||
| partial = OhMyThreads.@tasks for (ichunk, range) in enumerate(chunks(indices; n = length(model.caches))) | ||
| @set reducer = + |
There was a problem hiding this comment.
Would this make sense to be extra clear where this macro comes from? (I assume it is from here)
| @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()) | ||
|
|
There was a problem hiding this comment.
Would be nice to add some quick tests to check that we don't make unintended changes (both here and for future)?
| 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 |
|
Updated based on review comments. We will see if the result test passes everywhere. |
termi-official
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
This is using approx so not machine precision? I already cut off a bunch of decimals here from the answer I got locally.
There was a problem hiding this comment.
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
| @test Optim.minimum(res) ≈ -10858.806775 # src | |
| @test Optim.minimum(res) ≈ -10858.807f0 # src |
to reduce the precision 😄
There was a problem hiding this comment.
We can mess with it if we see there is an actual problem in the future?
Indexing by
threadidis not really valid. I removed thecalcallbecause it felt kind of pointless to just have hanging there.