-
Notifications
You must be signed in to change notification settings - Fork 204
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
Adds kernel computed field #1293
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d1496bc
[WIP] adds kernel computed field
tomchor 3dfb45d
Implements KernelComputedField
glwagner 6b2ffe2
added kernelcomputedfield to list of imported fields
tomchor 632a9d5
Adds status to KernelComputedField
glwagner 05a6fd6
Adds tests for KernelComputedField and changes filename for test_abst…
glwagner 0896ac0
Adds user-facing constructors and fixes bug in inner constructor for …
glwagner 3b11bb0
re-wrote docstring for KernelComputedField
tomchor 700bec9
Merge remote-tracking branch 'upstream/master' into tc/kernelcomputed…
glwagner d62b4eb
changed named of kernel in KernelComputedField example
tomchor ca31ad3
Merge branch 'tc/kernelcomputedfield' of https://github.com/tomchor/O…
tomchor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using Oceananigans: AbstractModel | ||
using Oceananigans.Grids | ||
using Oceananigans.Utils: tupleit | ||
|
||
struct KernelComputedField{X, Y, Z, S, A, G, K, F, P} <: AbstractField{X, Y, Z, A, G} | ||
data :: A | ||
grid :: G | ||
kernel :: K | ||
field_dependencies :: F | ||
parameters :: P | ||
status :: S | ||
|
||
""" | ||
KernelComputedField(loc, kernel, grid) | ||
|
||
Example | ||
======= | ||
|
||
```julia | ||
using KernelAbstractions: @index, @kernel | ||
using Oceananigans.Fields: AveragedField, KernelComputedField, compute! | ||
using Oceananigans.Grids: Cell, Face | ||
|
||
@inline ψ²(i, j, k, grid, ψ, Ψ) = @inbounds (ψ[i, j, k] - Ψ[i, j, k])^2 | ||
@kernel function compute_var!(var, grid, ϕ, Φ) | ||
i, j, k = @index(Global, NTuple) | ||
|
||
@inbounds var[i, j, k] = ψ′²(i, j, k, grid, ϕ, Φ) | ||
end | ||
|
||
u, v, w = model.velocities | ||
|
||
U = AveragedField(u, dims=(1, 2)) | ||
V = AveragedField(V, dims=(1, 2)) | ||
|
||
u′² = KernelComputedField(Face, Cell, Cell, compute_var!, model; field_dependencies=(u, U,)) | ||
v′² = KernelComputedField(Cell, Face, Cell, compute_var!, model; field_dependencies=(v, V,)) | ||
w′² = KernelComputedField(Cell, Cell, Face, compute_var!, model; field_dependencies=(w, 0,)) | ||
|
||
compute!(u′²) | ||
compute!(v′²) | ||
compute!(w′²) | ||
``` | ||
""" | ||
function KernelComputedField{X, Y, Z}(kernel::K, arch, grid; | ||
field_dependencies = (), | ||
parameters::P = nothing, | ||
data = nothing, | ||
recompute_safely = true) where {X, Y, Z, K, P} | ||
|
||
field_dependencies = tupleit(field_dependencies) | ||
|
||
if isnothing(data) | ||
data = new_data(arch, grid, (X, Y, Z)) | ||
end | ||
|
||
# Use FieldStatus if we want to avoid always recomputing | ||
status = recompute_safely ? nothing : FieldStatus(0.0) | ||
|
||
G = typeof(grid) | ||
A = typeof(data) | ||
S = typeof(status) | ||
F = typeof(field_dependencies) | ||
|
||
return new{X, Y, Z, S, | ||
A, G, K, F, P}(data, grid, kernel, field_dependencies, parameters) | ||
end | ||
end | ||
|
||
KernelComputedField(X, Y, Z, kernel, model::AbstractModel; kwargs...) = | ||
KernelComputedField{X, Y, Z}(kernel, model.architecture, model.grid; kwargs...) | ||
|
||
KernelComputedField(X, Y, Z, kernel, arch::AbstractArchitecture, grid::AbstractGrid; kwargs...) = | ||
KernelComputedField{X, Y, Z}(kernel, arch, grid; kwargs...) | ||
|
||
function compute!(kcf::KernelComputedField{X, Y, Z}) where {X, Y, Z} | ||
|
||
for dependency in kcf.field_dependencies | ||
compute!(dependency) | ||
end | ||
|
||
arch = architecture(kcf.data) | ||
|
||
workgroup, worksize = work_layout(kcf.grid, | ||
:xyz, | ||
location=(X, Y, Z), | ||
include_right_boundaries=true) | ||
|
||
compute_kernel! = kcf.kernel(device(arch), workgroup, worksize) | ||
|
||
event = isnothing(kcf.parameters) ? | ||
compute_kernel!(kcf.data, kcf.grid, kcf.field_dependencies...) : | ||
compute_kernel!(kcf.data, kcf.grid, kcf.field_dependencies..., kcf.parameters) | ||
|
||
wait(device(arch), event) | ||
|
||
return nothing | ||
end | ||
|
||
compute!(field::KernelComputedField{X, Y, Z, <:FieldStatus}, time) where {X, Y, Z} = | ||
conditional_compute!(field, time) | ||
|
||
Adapt.adapt_structure(to, kcf::KernelComputedField) = Adapt.adapt(to, kcf.data) |
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
File renamed without changes.
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,50 @@ | ||
using Oceananigans.Fields: KernelComputedField | ||
using KernelAbstractions: @kernel, @index | ||
|
||
grid = RegularCartesianGrid(size=(1, 1, 1), extent=(1, 1, 1), | ||
topology=(Periodic, Periodic, Bounded)) | ||
|
||
@kernel function double!(doubled_field, grid, single_field) | ||
i, j, k = @index(Global, NTuple) | ||
doubled_field[i, j, k] = 2 * single_field[i, j, k] | ||
end | ||
|
||
@kernel function multiply!(multiplied_field, grid, field, multiple) | ||
i, j, k = @index(Global, NTuple) | ||
multiplied_field[i, j, k] = multiple * field[i, j, k] | ||
end | ||
|
||
for arch in archs | ||
@testset "KernelComputedField [$(typeof(arch))]" begin | ||
@info " Testing KernelComputedField..." | ||
|
||
single_field = Field(Cell, Cell, Cell, arch, grid) | ||
|
||
doubled_field = KernelComputedField(Cell, Cell, Cell, | ||
double!, arch, grid, | ||
field_dependencies = single_field) | ||
|
||
# Test that the constructor worked | ||
@test doubled_field isa KernelComputedField | ||
|
||
multiple = 3 | ||
multiplied_field = KernelComputedField(Cell, Cell, Cell, | ||
multiply!, arch, grid, | ||
field_dependencies = doubled_field, | ||
parameters = multiple) | ||
|
||
# Test that the constructor worked | ||
@test multiplied_field isa KernelComputedField | ||
|
||
set!(single_field, π) | ||
|
||
compute!(doubled_field) | ||
@test doubled_field.data[1, 1, 1] == 2π | ||
|
||
set!(doubled_field, 0) | ||
compute!(multiplied_field) | ||
|
||
@test doubled_field.data[1, 1, 1] == 2π | ||
@test multiplied_field.data[1, 1, 1] == multiple * 2 * π | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's just an example but would
compute_perturbation!
be a better name?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to a change of names, but my idea was that it computes the variance, hence
compute_var!
. (Although in hindsight I should have just named itcompute_variance!
and be more explicit.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah haha sorry I thought it was short for
compute_variable!
. Indeedcompute_variance!
would be clearer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!