-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task_local for duplicating scratch data for concurrent use
- Loading branch information
1 parent
5153307
commit 8fd03fb
Showing
20 changed files
with
250 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -425,3 +425,14 @@ end | |
function celldofs!(::Vector, ::FacetCache) | ||
throw(DeprecationError("celldofs!(v::Vector, fs::FacetCache)" => "celldofs!(v, celldofs(fc))")) | ||
end | ||
|
||
## Deprecations introduced in [email protected] (keep until 2.0) ## | ||
|
||
# Base.copy -> task_local, https://github.com/Ferrite-FEM/Ferrite.jl/pull/1070 | ||
import Base: copy | ||
for T in ( | ||
CellValues, FacetValues, FunctionValues, GeometryMapping, InterfaceValues, | ||
Interpolation, QuadratureRule, FacetQuadratureRule, | ||
) | ||
@eval @deprecate copy(x::$T) task_local(x) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,4 +184,7 @@ export | |
evaluate_at_points, | ||
PointIterator, | ||
PointLocation, | ||
PointValues | ||
PointValues, | ||
|
||
# Misc | ||
task_local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
function task_local end | ||
|
||
""" | ||
task_local(x::T) -> T | ||
Duplicate `x` for a new task such that it can be used concurrently with the original `x`. | ||
This is similar to `copy` but only the data that is known to be mutated, i.e. "scratch | ||
data", are duplicated. | ||
Typically, for concurrent assembly, there are some data structures that can't be shared | ||
between the tasks, for example the local element matrix/vector and the `CellValues`. | ||
`task_local` can thus be used to duplicate theses data structures for each task based on a | ||
"template" data structure. For example, | ||
```julia | ||
# "Template" local matrix and cell values | ||
Ke = zeros(...) | ||
cv = CellValues(...) | ||
# Spawn `ntasks` tasks for concurrent assembly | ||
@sync for i in 1:ntasks | ||
Threads.@spawn begin | ||
Ke_task = task_local(Ke) | ||
cv_task = task_local(Ke) | ||
for cell in cells_for_task | ||
assemble_element!(Ke_task, cv_task, ...) | ||
end | ||
end | ||
end | ||
``` | ||
See the how-to on [multi-threaded assembly](@ref tutorial-threaded-assembly) for a complete | ||
example. | ||
The following "user-facing" types define methods for `task_local`: | ||
- [`CellValues`](@ref), [`FacetValues`](@ref), [`InterfaceValues`](@ref) are duplicated | ||
such that they can be `reinit!`ed independently. | ||
- `DenseArray` (for e.g. the local matrix and vector) are duplicated such that they can be | ||
modified concurrently. | ||
- [`CellCache`](@ref) (for caching element nodes and dofs) are duplicated such that they | ||
can be `reinit!`ed independently. | ||
The following types also define methods for `task_local` but are typically not used directly | ||
by the user but instead used recursively by the above types: | ||
- [`QuadratureRule`](@ref) and [`FacetQuadratureRule`](@ref) | ||
- All types which are `isbitstype` (e.g. `Vec`, `Tensor`, `Int`, `Float64`, etc.) | ||
""" | ||
task_local(::Any) | ||
|
||
# DenseVector/DenseMatrix (e.g. local matrix and vector) | ||
function task_local(x::T)::T where {S, T <: DenseArray{S}} | ||
@assert !isbitstype(T) | ||
if isbitstype(S) | ||
# If the eltype isbitstype the normal shallow copy can be used... | ||
return copy(x)::T | ||
else | ||
# ... otherwise we recurse and call task_local on the elements | ||
return map(task_local, x)::T | ||
end | ||
end | ||
|
||
# FacetQuadratureRule can store the QuadratureRules as a tuple | ||
function task_local(x::T)::T where {T <: Tuple} | ||
if isbitstype(T) | ||
return x | ||
else | ||
return map(task_local, x)::T | ||
end | ||
end | ||
|
||
# General fallback for other types | ||
function task_local(x::T)::T where {T} | ||
if !isbitstype(T) | ||
error("MethodError: task_local(::$T) is not implemented") | ||
end | ||
return x | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.