Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FiniteElementContainers"
uuid = "d08262e4-672f-4e7f-a976-f2cea5767631"
version = "0.15.0"
version = "0.15.1"
authors = ["Craig M. Hamel <cmhamel32@gmail.com> and contributors"]

[deps]
Expand Down Expand Up @@ -73,6 +73,7 @@ TimerOutputs = "0.5"
julia = "1"

[extras]
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
Gmsh = "705231aa-382f-11e9-3f0c-b7cb4346fdeb"
JuliaC = "acedd4c2-ced6-4a15-accc-2607eb759ba2"
PartitionedArrays = "5a9dfac6-5c52-46f7-8278-5e2210713be9"
Expand All @@ -81,4 +82,4 @@ TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"

[targets]
test = ["Gmsh", "JuliaC", "PartitionedArrays", "Test", "TestItemRunner", "TestItems"]
test = ["AMDGPU", "Gmsh", "JuliaC", "PartitionedArrays", "Test", "TestItemRunner", "TestItems"]
24 changes: 14 additions & 10 deletions src/Enzyme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ function assemble_scalar_enzyme_safe!(
conns = fspace.elem_conns
# foreach_block(fspace, p) do physics, props, ref_fe, b
for (b, (
block_physics, ref_fe, props
block_physics, ref_fe
)) in enumerate(zip(
values(p.physics), values(fspace.ref_fes),
values(p.properties)
values(p.physics), values(fspace.ref_fes)
))
_assemble_scalar_block_enzyme_safe!(
KA.CPU(),
block_view(storage, b),
conns.data, conns.offsets[b],
func,
b,
block_physics, ref_fe,
X, t, Δt,
U, U_old,
block_view(p.state_old, b), block_view(p.state_new, b), props,
block_view(p.state_old, b), block_view(p.state_new, b),
p.properties,
return_type
)
end
Expand All @@ -38,6 +39,7 @@ function _assemble_scalar_block_enzyme_safe!(
field,
conns::Conn, coffset::Int,
func::Function,
b::Int,
physics::AbstractPhysics, ref_fe::ReferenceFE,
X::AbstractField, t::T, dt::T,
U::Solution, U_old::Solution,
Expand All @@ -54,7 +56,7 @@ function _assemble_scalar_block_enzyme_safe!(
for e in axes(state_old, 3)
conn = connectivity(ref_fe, conns, e, coffset)
x_el, u_el, u_el_old = element_level_fields(ref_fe, conn, X, U, U_old)
props_el = _element_level_properties(props, e)
props_el = properties(props, physics, e, b)
# val_el = _element_scratch(return_type, ref_fe, U)

for q in 1:num_cell_quadrature_points(ref_fe)
Expand Down Expand Up @@ -111,21 +113,22 @@ function assemble_vector_enzyme_safe!(
# return_type = AssembledVector()
conns = fspace.elem_conns
for (b, (
block_physics, ref_fe, props
block_physics, ref_fe
)) in enumerate(zip(
values(p.physics), values(fspace.ref_fes),
values(p.properties)
values(p.physics), values(fspace.ref_fes)
))
_assemble_vector_block_enzyme_safe!(
# KA.get_backend(storage),
KA.CPU(),
storage,
conns.data, conns.offsets[b],
func,
b,
block_physics, ref_fe,
X, t, Δt,
U, U_old,
block_view(p.state_old, b), block_view(p.state_new, b), props,
block_view(p.state_old, b), block_view(p.state_new, b),
p.properties
# return_type
)
end
Expand All @@ -147,6 +150,7 @@ function _assemble_vector_block_enzyme_safe!(
field,
conns::Conn, coffset::Int,
func::Function,
b::Int,
physics::AbstractPhysics, ref_fe::ReferenceFE,
X::AbstractField, t::T, dt::T,
U::Solution, U_old::Solution,
Expand All @@ -164,7 +168,7 @@ function _assemble_vector_block_enzyme_safe!(
conn = connectivity(ref_fe, conns, e, coffset)
x_el, u_el, u_el_old = element_level_fields(ref_fe, conn, X, U, U_old)

props_el = _element_level_properties(props, e)
props_el = properties(props, physics, e, b)
# # val_el = _element_scratch(return_type, ref_fe, U)

for q in 1:num_cell_quadrature_points(ref_fe)
Expand Down
73 changes: 56 additions & 17 deletions src/Fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,42 +463,78 @@ const PROPS_ELEMS = -2

struct PropertyField{
T <: Number,
D <: AbstractVector{T}
D <: AbstractVector{T},
I <: AbstractVector{Int}
} <: AbstractDiscontinuousField{T, D}
data::D
isblockconstant::Vector{Int}
isblockconstant::I
nblocks::Int
nfields::Vector{Int}
nepes::Vector{Int}
nelems::Vector{Int}
offsets::Vector{Int}
nepes::I
nelems::I
offsets::I

function PropertyField{T, D}(data, isblockconstant, nblocks, nfields, nepes, nelems, offsets) where {T, D}
new{T, D}(data, isblockconstant, nblocks, nfields, nepes, nelems, offsets)
function PropertyField{T, D, I}(data, isblockconstant, nblocks, nepes, nelems, offsets) where {T, D, I}
new{T, D, I}(data, isblockconstant, nblocks, nepes, nelems, offsets)
end

# case where all blocks have properties the same throughout
function PropertyField(arrs::Vector{<:Vector{T}}) where T <: Number
data = reduce(vcat, arrs)
isblockconstant = PROPS_CONST * ones(Int, length(arrs))
nblocks = length(arrs)
nfields = map(length, arrs)
nepes = -1 * ones(Int, nblocks)
nepes = map(length, arrs)
nelems = -1 * ones(Int, nblocks)
offsets = Vector{Int}(undef, 0)
offset = 1
for n in axes(arrs, 1)
push!(offsets, offset)
offset += nfields[n]
offset += nepes[n]
end
return PropertyField{T, typeof(data)}(data, isblockconstant, nblocks, nfields, nepes, nelems, offsets)
return PropertyField{T, typeof(data), typeof(isblockconstant)}(data, isblockconstant, nblocks, nepes, nelems, offsets)
end

# case where we have mixed constant or none constant
function PropertyField(arrs::Vector{<:Array{T}}) where T <: Number
data = mapreduce(vec, vcat, arrs)
isblockconstant = map(x -> begin
if isa(x, Matrix)
return PROPS_ELEMS
elseif isa(x, Vector)
return PROPS_CONST
else
@assert false "Property arrays should be Vector or Matrix"
end
end, arrs)
nblocks = length(arrs)
out = map(x -> begin
if isa(x, Matrix)
return size(x)
elseif isa(x, Vector)
return (length(x), -1)
end
end, arrs)
nepes = map(x -> x[1], out)
nelems = map(x -> x[2], out)
offsets = Vector{Int}(undef, 0)
offset = 1
for n in axes(arrs, 1)
push!(offsets, offset)
if isa(arrs[n], Matrix)
offset += nepes[n] * nelems[n]
elseif isa(arrs[n], Vector)
offset += nepes[n]
end
end
return PropertyField{T, typeof(data), typeof(isblockconstant)}(data, isblockconstant, nblocks, nepes, nelems, offsets)
end
end

function Adapt.adapt_structure(to, field::PropertyField)
data = adapt(to, field.data)
return PropertyField{eltype(field), typeof(data)}(
isblockconstant = adapt(to, field.isblockconstant)
return PropertyField{eltype(field), typeof(data), typeof(isblockconstant)}(
data,
adapt(to, field.isblockconstant),
isblockconstant,
field.nblocks,
adapt(to, field.nepes),
adapt(to, field.nelems),
Expand All @@ -507,19 +543,22 @@ function Adapt.adapt_structure(to, field::PropertyField)
end

function num_fields(field::PropertyField, b::Int)
return field.nfields[b]
return field.nepes[b]
end

function properties(field::PropertyField, e::Int, b::Int)
offset = field.offsets[b]
nfields = num_fields(field, b)
if field.isblockconstant[b] == PROPS_CONST
return view(field.data, offset:offset + nfields - 1)
start = offset
finish = offset + nfields - 1
elseif field.isblockconstant[b] == PROPS_ELEMS
@assert false finish me
start = offset + nfields * (e - 1)
finish = offset + nfields * e - 1
else
@assert false "Should never happen"
end
return view(field.data, start:finish)
end

######################################################################################################
Expand Down
Loading