Skip to content

Commit

Permalink
Rename backend to get_backend to avoid collisions with variables
Browse files Browse the repository at this point in the history
  • Loading branch information
utkinis committed Feb 7, 2024
1 parent ca7ca80 commit e50bd53
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 36 deletions.
6 changes: 3 additions & 3 deletions examples/batcher.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ using KernelAbstractions

arch = Arch(CPU())

grid = UniformGrid(arch; origin=(0, 0), extent=(1, 1), dims=(100, 100))
grid = UniformGrid(arch; origin=(0, 0, 0), extent=(1, 1, 1), dims=(10, 10, 10))

C = Field(CPU(), grid, Center())
P = Field(CPU(), grid, Center(), Int)

set!(C, grid, (_, _) -> rand())
set!(P, grid, (_, _) -> rand(Int))
set!(C, grid, (_, _, _) -> rand())
set!(P, grid, (_, _, _) -> rand(Int))

bc!(arch, grid, C => Neumann(), P => (y=(Dirichlet(), nothing), x=Neumann()))

Expand Down
39 changes: 21 additions & 18 deletions examples/distributed_boundary_conditions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ using Chmy.Architectures, Chmy.Grids, Chmy.Fields, Chmy.BoundaryConditions, Chmy
using KernelAbstractions
using MPI

MPI.Init()

arch = Arch(CPU(), MPI.COMM_WORLD, (0, 0))
topo = topology(arch)

grid = UniformGrid(arch; origin=(0, 0), extent=(1, 1), dims=(12, 12))

field = Field(backend(arch), grid, Center())
fill!(parent(field), NaN)

set!(field, global_rank(topo))

bc!(arch, grid, field => Neumann(); replace=true)

KernelAbstractions.synchronize(backend(arch))

sleep(0.2global_rank(topo))
display(interior(field; with_halo=true))
function main(backend=CPU())
arch = Arch(backend, MPI.COMM_WORLD, (0, 0, 0))
topo = topology(arch)

grid = UniformGrid(arch; origin=(0, 0, 0), extent=(1, 1, 1), dims=(3*100, 3*100, 3*100))

field = Field(backend, grid, Center())
fill!(parent(field), NaN)

for _ in 1:10
@time set!(field, global_rank(topo))
@time bc!(arch, grid, field => Neumann(); replace=true)
end

KernelAbstractions.synchronize(backend)

# sleep(0.2global_rank(topo))
# display(interior(field; with_halo=true))
end

MPI.Init()
main()
MPI.Finalize()
14 changes: 7 additions & 7 deletions src/Architectures.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Architectures

export Architecture, SingleDeviceArchitecture
export Arch, backend, device, activate!, set_device!, heuristic_groupsize
export Arch, get_backend, get_device, activate!, set_device!, heuristic_groupsize

using KernelAbstractions

Expand Down Expand Up @@ -32,23 +32,23 @@ Create an architecture object for the specified backend and device.
- `device_id=1`: The ID of the device to use.
"""
function Arch(backend::Backend; device_id::Integer=1)
dev = device(backend, device_id)
dev = get_device(backend, device_id)
return SingleDeviceArchitecture(backend, dev)
end

"""
backend(arch::SingleDeviceArchitecture)
get_backend(arch::SingleDeviceArchitecture)
Get the backend associated with a SingleDeviceArchitecture.
"""
backend(arch::SingleDeviceArchitecture) = arch.backend
get_backend(arch::SingleDeviceArchitecture) = arch.backend

"""
device(arch::SingleDeviceArchitecture)
get_device(arch::SingleDeviceArchitecture)
Get the device associated with a SingleDeviceArchitecture.
"""
device(arch::SingleDeviceArchitecture) = arch.device
get_device(arch::SingleDeviceArchitecture) = arch.device

"""
activate!(arch::SingleDeviceArchitecture; priority=:normal)
Expand All @@ -61,7 +61,7 @@ function activate!(arch::SingleDeviceArchitecture; priority=:normal)
end

# CPU
device(::CPU, device_id) = nothing
get_device(::CPU, device_id) = nothing
set_device!(::Nothing) = nothing
heuristic_groupsize(::CPU, ::Val{N}) where {N} = 256

Expand Down
2 changes: 1 addition & 1 deletion src/BoundaryConditions/batch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ end

function bc!(side::Val, dim::Val, arch::Architecture, grid::SG, batch::FieldBatch)
worksize = remove_dim(dim, size(grid, Center()) .+ 2)
bc_kernel!(backend(arch), 256, worksize)(side, dim, grid, batch)
bc_kernel!(Architectures.get_backend(arch), 256, worksize)(side, dim, grid, batch)
return
end
6 changes: 3 additions & 3 deletions src/Distributed/boundary_conditions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ function BoundaryConditions.bc!(side::Val{S}, dim::Val{D},

tle = task_local_exchanger()

init!(tle, batch, backend(arch), dim, side)
init!(tle, batch, Architectures.get_backend(arch), dim, side)

# initiate non-blocking MPI recieve and device-to-device copy to the send buffer
for idx in eachindex(batch.fields)
tle.recv_reqs[idx] = MPI.Irecv!(tle.recv_bufs[idx], comm; source=nbrank)
send_view = get_send_view(Val(S), Val(D), batch.fields[idx])
copyto!(tle.send_bufs[idx], send_view)
end
KernelAbstractions.synchronize(backend(arch))
KernelAbstractions.synchronize(Architectures.get_backend(arch))

# initiate non-blocking MPI send
for idx in eachindex(batch.fields)
Expand All @@ -41,7 +41,7 @@ function BoundaryConditions.bc!(side::Val{S}, dim::Val{D},
end

reset_allocators!(tle)
async || KernelAbstractions.synchronize(backend(arch))
async || KernelAbstractions.synchronize(Architectures.get_backend(arch))

return
end
Expand Down
6 changes: 3 additions & 3 deletions src/Distributed/distributed_architecture.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ Create a distributed Architecture using backend `backend` and `comm`. For GPU ba
"""
function Architectures.Arch(backend::Backend, comm::MPI.Comm, dims)
topology = CartesianTopology(comm, dims)
dev = device(backend, shared_rank(topology) + 1)
dev = get_device(backend, shared_rank(topology) + 1)
child_arch = SingleDeviceArchitecture(backend, dev)
return DistributedArchitecture(child_arch, topology)
end

topology(arch::DistributedArchitecture) = arch.topology

# Implement Architecture API
Architectures.backend(arch::DistributedArchitecture) = backend(arch.child_arch)
Architectures.device(arch::DistributedArchitecture) = device(arch.child_arch)
Architectures.get_backend(arch::DistributedArchitecture) = Architectures.get_backend(arch.child_arch)
Architectures.get_device(arch::DistributedArchitecture) = get_device(arch.child_arch)
Architectures.activate!(arch::DistributedArchitecture) = activate!(arch.child_arch)
2 changes: 1 addition & 1 deletion src/Distributed/stack_allocator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function allocate(sa::StackAllocator, T::DataType, dims, align::Integer=sizeof(T
resize!(sa.buffer, aligned + nbytes)
end
# get a slice of the buffer
backend = get_backend(sa.buffer)
backend = KernelAbstractions.get_backend(sa.buffer)
data_ptr = convert(Chmy.pointertype(backend, T), pointer(sa.buffer) + aligned)
sa.offset = aligned + nbytes
sa.nallocs += 1
Expand Down

0 comments on commit e50bd53

Please sign in to comment.