Skip to content
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

support for active_cells_map in kernels #3920

Open
wants to merge 48 commits into
base: main
Choose a base branch
from

Conversation

simone-silvestri
Copy link
Collaborator

I am not sure how hacky this is or if it will work throughout the board, but this is supposed to achieve what was discussed in #3918:

for example, the current script:

using Oceananigans
grid = RectilinearGrid(size = (3, 3, 3), extent = (1, 1, 1))

using Oceananigans.Utils
using KernelAbstractions: @kernel, @index

@kernel function _test_indices(a)
    i, j, k = @index(Global, NTuple)
    @show a[i, j, k]
end

a = [(i, j, k) for i in 1:3, j in 1:3, k in 1:3]

returns

julia> launch!(CPU(), grid, :xyz, _test_indices, a)
a[i, j, k] = (1, 1, 1)
a[i, j, k] = (2, 1, 1)
a[i, j, k] = (3, 1, 1)
a[i, j, k] = (1, 2, 1)
a[i, j, k] = (2, 2, 1)
a[i, j, k] = (3, 2, 1)
a[i, j, k] = (1, 3, 1)
a[i, j, k] = (2, 3, 1)
a[i, j, k] = (3, 3, 1)
a[i, j, k] = (1, 1, 2)
a[i, j, k] = (2, 1, 2)
a[i, j, k] = (3, 1, 2)
a[i, j, k] = (1, 2, 2)
a[i, j, k] = (2, 2, 2)
a[i, j, k] = (3, 2, 2)
a[i, j, k] = (1, 3, 2)
a[i, j, k] = (2, 3, 2)
a[i, j, k] = (3, 3, 2)
a[i, j, k] = (1, 1, 3)
a[i, j, k] = (2, 1, 3)
a[i, j, k] = (3, 1, 3)
a[i, j, k] = (1, 2, 3)
a[i, j, k] = (2, 2, 3)
a[i, j, k] = (3, 2, 3)
a[i, j, k] = (1, 3, 3)
a[i, j, k] = (2, 3, 3)
a[i, j, k] = (3, 3, 3)

julia> launch!(CPU(), grid, :xyz, _test_indices, a; active_cells_map = [(1, 2, 3), (3, 2, 1)])
a[i, j, k] = (1, 2, 3)
a[i, j, k] = (3, 2, 1)

@simone-silvestri
Copy link
Collaborator Author

simone-silvestri commented Nov 12, 2024

Probably this is very hacky, a better way to go about it would be to add a map to the Kernel

#####

struct IndexMap{M}
imap :: M
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
imap :: M
index_map :: M


struct MappedFunction{F, M} <: Function
f::F
imap::M
Copy link
Member

Choose a reason for hiding this comment

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

index_map?

test.jl Outdated

grid = RectilinearGrid(arch, size = (3, 3, 3), extent = (1, 1, 1))
array = zeros(arch, 3, 3, 3)
imap = on_architecture(arch, [(i, j, k) for i in 1:3, j in 1:3, k in 1:2])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
imap = on_architecture(arch, [(i, j, k) for i in 1:3, j in 1:3, k in 1:2])
imap = on_architecture(arch, [(i, j, k) for i in 1:3, j in 1:3, k in 1:3])

@simone-silvestri simone-silvestri marked this pull request as ready for review December 11, 2024 10:08
@simone-silvestri
Copy link
Collaborator Author

simone-silvestri commented Dec 11, 2024

This solution seems to work.

PROS: makes the codebase much simpler and allows to use of active_cells_map in many more kernels with very little change
CONS: The solution is a bit convoluted in the kernel_launching.jl file

I am in favor of merging, it will make future low-hanging fruit optimization (using active cells map in compute_diffusivities, the catke TKE time stepping, and the split explicit free surface as well as kernels in other packages like flux computation in ClimaOcean) much more straightforward.

but I am happy to see what people think

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.

2 participants