-
Notifications
You must be signed in to change notification settings - Fork 194
/
set!.jl
103 lines (77 loc) · 3.14 KB
/
set!.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using CUDA
using KernelAbstractions: @kernel, @index, CUDADevice
using Oceananigans.Architectures: device, GPU
using Oceananigans.Utils: work_layout
function set!(Φ::NamedTuple; kwargs...)
for (fldname, value) in kwargs
ϕ = getproperty(Φ, fldname)
set!(ϕ, value)
end
return nothing
end
set!(u::AbstractField, v::Number) = @. u.data.parent = v
set!(u::AbstractField{X, Y, Z, A}, v::AbstractField{X, Y, Z, A}) where {X, Y, Z, A} =
@. u.data.parent = v.data.parent
# Niceties
const AbstractCPUField =
AbstractField{X, Y, Z, A, G} where {X, Y, Z, A<:OffsetArray{T, D, <:Array} where {T, D}, G}
const AbstractReducedCPUField =
AbstractReducedField{X, Y, Z, A, G} where {X, Y, Z, A<:OffsetArray{T, D, <:Array} where {T, D}, G}
"Set the CPU field `u` to the array `v`."
function set!(u::AbstractCPUField, v::Array)
Sx, Sy, Sz = size(u)
for k in 1:Sz, j in 1:Sy, i in 1:Sx
u[i, j, k] = v[i, j, k]
end
return nothing
end
""" Returns an AbstractReducedField on the CPU. """
function similar_cpu_field(u::AbstractReducedField)
FieldType = typeof(u).name.wrapper
return FieldType(location(u), CPU(), u.grid; dims=u.dims)
end
""" Set the CPU field `u` data to the function `f(x, y, z)`. """
set!(u::AbstractCPUField, f::Function) = interior(u) .= f.(nodes(u; reshape=true)...)
#####
##### set! for fields on the GPU
#####
@hascuda begin
const AbstractGPUField =
AbstractField{X, Y, Z, A, G} where {X, Y, Z, A<:OffsetArray{T, D, <:CuArray} where {T, D}, G}
const AbstractReducedGPUField =
AbstractReducedField{X, Y, Z, A, G} where {X, Y, Z, A<:OffsetArray{T, D, <:CuArray} where {T, D}, G}
""" Returns a field on the CPU with `nothing` boundary conditions. """
function similar_cpu_field(u)
FieldType = typeof(u).name.wrapper
return FieldType(location(u), CPU(), u.grid, nothing)
end
""" Set the GPU field `u` to the array `v`. """
function set!(u::AbstractGPUField, v::Array)
v_field = similar_cpu_field(u)
set!(v_field, v)
set!(u, v_field)
return nothing
end
""" Set the GPU field `u` to the CuArray `v`. """
function set!(u::AbstractGPUField, v::CuArray)
launch!(GPU(), u.grid, :xyz, _set_gpu!, u.data, v, u.grid,
include_right_boundaries=true, location=location(u))
return nothing
end
@kernel function _set_gpu!(u, v, grid)
i, j, k = @index(Global, NTuple)
@inbounds u[i, j, k] = v[i, j, k]
end
""" Set the CPU field `u` data to the GPU field data of `v`. """
set!(u::AbstractCPUField, v::AbstractGPUField) = u.data.parent .= Array(v.data.parent)
""" Set the GPU field `u` data to the CPU field data of `v`. """
set!(u::AbstractGPUField, v::AbstractCPUField) = copyto!(u.data.parent, v.data.parent)
""" Set the GPU field `u` data to the function `f(x, y, z)`. """
function set!(u::AbstractGPUField, f::Function)
# Create a temporary field with bcs = nothing.
v_field = similar_cpu_field(u)
set!(v_field, f)
set!(u, v_field)
return nothing
end
end