I’m trying to understand the memory layout of a StructArray I’m constructing in my code. Specifically, it looks to me like the memory layout is still an array of structs, rather than a struct of arrays. I’m very new to Julia, though, so perhaps I’m just misunderstanding the output. Any help clarifying this would be greatly appreciated.
A basic example is (apologies for the verbose type names, they’re relevant to my application):
struct SpinEnvironment{T<:AbstractFloat}
t1::T
t2::T
end
struct Environments{T<:AbstractFloat}
relaxationConstants::StructVector{SpinEnvironment{T}}
function Environments{T}(e::Vector{SpinEnvironment{T}}) where
T<:AbstractFloat
new(StructVector{SpinEnvironment{T}}(
reinterpret(reshape,T, e), dims=1))
end
end
se = SpinEnvironment{Float64}(1,2)
env = Environments{Float64}([se,se,se])
The resulting StructArray seems right to me (although the view(reinterpret(...))
seems like a red flag regarding the underlying memory layout)
julia> env.relaxationConstants
3-element StructArray(view(reinterpret(reshape, Float64, ::Vector{Main.SpinStates.SpinEnvironment{Float64}}), 1, :), view(reinterpret(reshape, Float64, ::Vector{Main.SpinStates.SpinEnvironment{Float64}}), 2, :)) with eltype Main.SpinStates.SpinEnvironment{Float64}:
Main.SpinStates.SpinEnvironment{Float64}(1.0, 2.0)
Main.SpinStates.SpinEnvironment{Float64}(1.0, 2.0)
Main.SpinStates.SpinEnvironment{Float64}(1.0, 2.0)
However, when I use reinterpret
to try to visualize the memory layout, I see
julia> reinterpret(Float64,env.relaxationConstants)
6-element reinterpret(Float64, StructArray(view(reinterpret(reshape, Float64, ::Vector{Main.SpinStates.SpinEnvironment{Float64}}), 1, :), view(reinterpret(reshape, Float64, ::Vector{Main.SpinStates.SpinEnvironment{Float64}}), 2, :))):
1.0
2.0
1.0
2.0
1.0
2.0
which isn’t what I’d expect. I’d figured I’d see all the 1.0
entries (i.e., all the t1
fields) first, then all the 2.0
entries (i.e., all the t2
fields), since that would be the layout for a struct of arrays. What is the underlying memory layout here?
Thanks!