-
Notifications
You must be signed in to change notification settings - Fork 282
Allow kernel functions with fewer indices for KernelFunctionOperation at reduced locations #5659
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
Open
simone-silvestri
wants to merge
4
commits into
main
Choose a base branch
from
ss/allow-reduced-kernel-operations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -182,6 +182,48 @@ for arch in archs | |
| less_trivial_kernel_function(i, j, k, grid, u, v) = @inbounds u[i, j, k] * ℑxyᶠᶜᵃ(i, j, k, grid, v) | ||
| op = KernelFunctionOperation{Face, Center, Center}(less_trivial_kernel_function, grid, u, v) | ||
| @test op isa KernelFunctionOperation | ||
|
|
||
| two_index_kernel_function(i, j, grid) = i + j | ||
| op = KernelFunctionOperation{Center, Center, Nothing}(two_index_kernel_function, grid) | ||
| @test op isa KernelFunctionOperation | ||
| @test Array(interior(Field(op), :, :, 1)) == [i + j for i in 1:size(grid, 1), j in 1:size(grid, 2)] | ||
|
|
||
| one_index_kernel_function(k, grid) = 2k | ||
| op = KernelFunctionOperation{Nothing, Nothing, Center}(one_index_kernel_function, grid) | ||
| @test Array(interior(Field(op), 1, 1, :)) == [2k for k in 1:size(grid, 3)] | ||
|
|
||
| q = CenterField(grid) | ||
| set!(q, 2) | ||
| interior_pattern_kernel_function(i, k, grid, q) = @inbounds q[i, 1, k] * i * k | ||
| op = KernelFunctionOperation{Center, Nothing, Center}(interior_pattern_kernel_function, grid, q) | ||
| @test Array(interior(Field(op), :, 1, :)) == [2 * i * k for i in 1:size(grid, 1), k in 1:size(grid, 3)] | ||
|
|
||
| # Varargs kernel functions keep the full three-index convention... | ||
| varargs_kernel_function(arguments...) = arguments[1] + arguments[2] + arguments[3] | ||
| op = KernelFunctionOperation{Center, Center, Nothing}(varargs_kernel_function, grid) | ||
| @test Array(interior(Field(op), :, :, 1)) == [i + j + 1 for i in 1:size(grid, 1), j in 1:size(grid, 2)] | ||
|
|
||
| # ... unless only the reduced call is applicable (here the typed grid argument rejects `grid ← k`) | ||
| reduced_varargs_kernel_function(i, j, grid::Oceananigans.Grids.AbstractGrid, arguments...) = i * j + length(arguments) | ||
| op = KernelFunctionOperation{Center, Center, Nothing}(reduced_varargs_kernel_function, grid) | ||
| @test Array(interior(Field(op), :, :, 1)) == [i * j for i in 1:size(grid, 1), j in 1:size(grid, 2)] | ||
|
|
||
| # When the full three-index call is applicable it is preferred, even if a | ||
| # reduced-arity method also exists (heavily overloaded operators rely on this) | ||
| dual_kernel_function(i, j, grid) = i + j | ||
| dual_kernel_function(i, j, k, grid) = -7 | ||
| op = KernelFunctionOperation{Center, Center, Nothing}(dual_kernel_function, grid) | ||
| @test Array(interior(Field(op), :, :, 1)) == fill(-7, size(grid, 1), size(grid, 2)) | ||
|
|
||
| # Spacing operators (e.g. Δx) have reduced-arity helper methods that must not be | ||
| # mistaken for the reduced form at reduced locations | ||
| @test Array(interior(Field(xspacings(grid, Center(), Center(), Center())), :, 1, 1)) == | ||
| [Oceananigans.Operators.Δx(i, 1, 1, grid, Center(), Center(), Center()) for i in 1:size(grid, 1)] | ||
|
|
||
| # Three-index kernel functions at reduced locations still work | ||
| three_index_kernel_function(i, j, k, grid) = i + j | ||
| op = KernelFunctionOperation{Center, Center, Nothing}(three_index_kernel_function, grid) | ||
| @test Array(interior(compute!(Field(op))))[:, :, 1] == [i + j for i in 1:size(grid, 1), j in 1:size(grid, 2)] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to call The indexing is also messed up, so this can be |
||
| end | ||
|
|
||
| @testset "Fidelity of simple binary operations" begin | ||
|
|
||
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 suggest including a full example that illustrates how this works including the
KernelFunctionOperation{Center, Center, Nothing}constructor, to be fully explicit