Skip to content

Commit

Permalink
Extend table to support arbitrary vector types
Browse files Browse the repository at this point in the history
  • Loading branch information
amartinhuertas committed Jul 3, 2020
1 parent a8aa850 commit cce0534
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 58 deletions.
70 changes: 31 additions & 39 deletions src/Arrays/Tables.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@


"""
struct Table{T,P} <: AbstractVector{Vector{T}}
data::Vector{T}
ptrs::Vector{P}
end
struct Table{T,Vd<:AbstractVector{T},Vp<:AbstractVector} <: AbstractVector{Vector{T}}
data::Vd
ptrs::Vp
end
Type representing a list of lists (i.e., a table) in
compressed format.
"""
struct Table{T,P} <: AbstractVector{Vector{T}}
data::Vector{T}
ptrs::Vector{P}
function Table(data::Vector{T},ptrs::Vector{P}) where {T,P}
new{T,P}(data,ptrs)
end
end

@doc """
Table(data::AbstractVector{T},ptrs::AbstractVector{P}) where {T,P}
Build a table from the given data and pointers. If the arguments are not of
type `Vector`, they will be converted.
"""
function Table(data::AbstractVector{T},ptrs::AbstractVector{P}) where {T,P}
Table(Vector{T}(data),Vector{P}(ptrs))
struct Table{T,Vd<:AbstractVector{T},Vp<:AbstractVector} <: AbstractVector{Vector{T}}
data::Vd
ptrs::Vp
function Table(data::AbstractVector,ptrs::AbstractVector)
new{eltype(data),typeof(data),typeof(ptrs)}(data,ptrs)
end
end

"""
Expand All @@ -42,13 +32,13 @@ function Table(a::Table)
a
end

function Base.convert(::Type{Table{T,P}},table::Table{Ta,Pa}) where {T,P,Ta,Pa}
data = convert(Vector{T},table.data)
ptrs = convert(Vector{P},table.ptrs)
function Base.convert(::Type{Table{T,Vd,Vp}},table::Table{Ta,Vda,Vpa}) where {T,Vd,Vp,Ta,Vda,Vpa}
data = convert(Vd,table.data)
ptrs = convert(Vp,table.ptrs)
Table(data,ptrs)
end

function Base.convert(::Type{Table{T,P}},table::Table{T,P}) where {T,P}
function Base.convert(::Type{Table{T,Vd,Vp}},table::Table{T,Vd,Vp}) where {T,Vd,Vp}
table
end

Expand Down Expand Up @@ -103,9 +93,11 @@ function getindex!(c,a::Table,i::Integer)
r
end

function getindex(a::Table,i::Integer)
cache = array_cache(a)
getindex!(cache,a,i)
@inline function Base.getindex(a::Table,i::Integer)
@inbounds pini = a.ptrs[i]
@inbounds pend = a.ptrs[i+1]-1
ids = pini:pend
view(a.data,ids)
end

function Base.getindex(a::Table,i::UnitRange)
Expand Down Expand Up @@ -243,7 +235,7 @@ end

"""
"""
function append_tables_globally(tables::Table{T,P}...) where {T,P}
function append_tables_globally(tables::Table{T,Vd,Vp}...) where {T,Vd,Vp}
first_table, = tables
data = copy(first_table.data)
ptrs = copy(first_table.ptrs)
Expand All @@ -262,13 +254,13 @@ end

"""
"""
get_ptrs_eltype(::Table{T,P}) where {T,P} = P
get_ptrs_eltype(::Type{Table{T,P}}) where {T,P} = P
get_ptrs_eltype(::Table{T,Vd,Vp}) where {T,Vd,Vp} = eltype(Vp)
get_ptrs_eltype(::Type{Table{T,Vd,Vp}}) where {T,Vd,Vp} = eltype(Vp)

"""
"""
get_data_eltype(::Table{T,P}) where {T,P} = T
get_data_eltype(::Type{Table{T,P}}) where {T,P} = T
get_data_eltype(::Table{T,Vd,Vp}) where {T,Vd,Vp} = T
get_data_eltype(::Type{Table{T,Vd,Vp}}) where {T,Vd,Vp} = T

"""
append_tables_locally(tables::Table...)
Expand Down Expand Up @@ -360,8 +352,8 @@ function get_local_item(a_to_lb_to_b::Table, lb::AbstractArray{<:Integer})
a_to_b
end

struct LocalItemFromTable{T,P,A} <: AbstractVector{T}
a_to_lb_to_b::Table{T,P}
struct LocalItemFromTable{T,Vd,Vp,A} <: AbstractVector{T}
a_to_lb_to_b::Table{T,Vd,Vp}
lb::A
end

Expand All @@ -386,9 +378,9 @@ function find_local_index(a_to_b, b_to_la_to_a::Table)
a_to_la
end

struct LocalIndexFromTable{T,P,V<:AbstractVector} <: AbstractVector{T}
struct LocalIndexFromTable{T,Vd,Vp,V<:AbstractVector} <: AbstractVector{T}
a_to_b::V
b_to_la_to_a::Table{T,P}
b_to_la_to_a::Table{T,Vd,Vp}
end

Base.size(m::LocalIndexFromTable) = size(m.a_to_b)
Expand Down Expand Up @@ -437,9 +429,9 @@ function to_dict(table::Table)
dict
end

function from_dict(::Type{Table{T,P}}, dict::Dict{Symbol,Any}) where {T,P}
data::Vector{T} = dict[:data]
ptrs::Vector{P} = dict[:ptrs]
function from_dict(::Type{Table{T,Vd,Vp}}, dict::Dict{Symbol,Any}) where {T,Vd,Vp}
data::Vd = dict[:data]
ptrs::Vp = dict[:ptrs]
Table(data,ptrs)
end

Expand Down
13 changes: 7 additions & 6 deletions src/FESpaces/ConformingFESpaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,12 @@ end
function _generate_face_to_own_dofs(
n_faces,
cell_to_ctype,
d_to_cell_to_dfaces::Vector{Table{T,P}},
d_to_dface_to_cells::Vector{Table{T,P}},
d_to_cell_to_dfaces::Vector{Table{T,Vd,Vp}},
d_to_dface_to_cells::Vector{Table{T,Vd,Vp}},
d_to_offset,
d_to_ctype_to_ldface_to_own_ldofs) where {T,P}
d_to_ctype_to_ldface_to_own_ldofs) where {T,Vd,Vp}

P=eltype(Vp)
face_to_own_dofs_ptrs = zeros(P,n_faces+1)

D = length(d_to_offset)-1
Expand Down Expand Up @@ -517,12 +518,12 @@ function _convert_dirichlet_components(dirichlet_tags::String,dirichlet_componen
end

struct CellDofsNonOriented <:AbstractVector{Vector{Int}}
cell_to_faces::Table{Int,Int32}
cell_to_lface_to_pindex::Table{Int8,Int32}
cell_to_faces::Table{Int,Vector{Int},Vector{Int32}}
cell_to_lface_to_pindex::Table{Int8,Vector{Int8},Vector{Int32}}
cell_to_ctype::Vector{Int8}
ctype_to_lface_to_own_ldofs::Vector{Vector{Vector{Int}}}
ctype_to_num_dofs::Vector{Int}
face_to_own_dofs::Table{Int,Int32}
face_to_own_dofs::Table{Int,Vector{Int},Vector{Int32}}
ctype_to_lface_to_pindex_to_pdofs::Vector{Vector{Vector{Vector{Int}}}}
end

Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/CartesianDiscreteModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ get_face_labeling(model::CartesianDiscreteModel) = model.face_labeling
# These needed to be type stable

function get_face_nodes(model::CartesianDiscreteModel,d::Integer)
face_nodes::Table{Int,Int32} = compute_face_nodes(model,d)
face_nodes::Table{Int,Vector{Int},Vector{Int32}} = compute_face_nodes(model,d)
face_nodes
end

Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/GenericBoundaryTriangulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct FaceToCellGlue{O} <: GridapType
face_to_cell::Vector{Int}
face_to_lface::Vector{Int8}
cell_to_ctype::Vector{Int8}
cell_to_lface_to_pindex::Table{Int8,Int32}
cell_to_lface_to_pindex::Table{Int8,Vector{Int8},Vector{Int32}}
ctype_to_lface_to_ftype::Vector{Vector{Int8}}
end

Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/GridPortions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct GridPortion{Dc,Dp,G} <: Grid{Dc,Dp}
oldgrid::G
cell_to_oldcell::Vector{Int}
node_to_oldnode::Vector{Int}
cell_to_nodes::Table{Int,Int32}
cell_to_nodes::Table{Int,Vector{Int},Vector{Int32}}
@doc """
GridPortion(oldgrid::Grid{Dc,Dp},cell_to_oldcell::Vector{Int}) where {Dc,Dp}
"""
Expand Down
4 changes: 2 additions & 2 deletions src/Geometry/UnstructuredDiscreteModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ function from_dict(T::Type{UnstructuredDiscreteModel},dict::Dict{Symbol,Any})
vertex_coordinates = get_node_coordinates(grid)[vertex_to_node]

nvertices = length(vertex_to_node)
d_dface_to_vertices = Vector{Table{Int,Int32}}(undef,D+1)
d_dface_to_vertices = Vector{Table{Int,Vector{Int},Vector{Int32}}}(undef,D+1)
d_dface_to_vertices[0+1] = identity_table(Int,Int32,nvertices)
for d in 1:D
k = Symbol("face_vertices_$d")
dface_to_vertices = from_dict(Table{Int,Int32},dict[k])
dface_to_vertices = from_dict(Table{Int,Vector{Int},Vector{Int32}},dict[k])
d_dface_to_vertices[d+1] = dface_to_vertices
end

Expand Down
6 changes: 3 additions & 3 deletions src/Geometry/UnstructuredGridTopologies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
struct UnstructuredGridTopology{Dc,Dp,T,O} <: GridTopology{Dc,Dp}
vertex_coordinates::Vector{Point{Dp,T}}
n_m_to_nface_to_mfaces::Matrix{Table{Int,Int32}}
n_m_to_nface_to_mfaces::Matrix{Table{Int,Vector{Int},Vector{Int32}}}
cell_type::Vector{Int8}
polytopes::Vector{Polytope{Dc}}
end
Expand All @@ -30,7 +30,7 @@ function UnstructuredGridTopology(

D = num_dims(first(polytopes))
n = D+1
n_m_to_nface_to_mfaces = Matrix{Table{Int,Int32}}(undef,n,n)
n_m_to_nface_to_mfaces = Matrix{Table{Int,Vector{Int},Vector{Int32}}}(undef,n,n)
n_m_to_nface_to_mfaces[D+1,0+1] = cell_vertices
vertex_cells = generate_cells_around(cell_vertices,length(vertex_coordinates))
n_m_to_nface_to_mfaces[0+1,D+1] = vertex_cells
Expand Down Expand Up @@ -64,7 +64,7 @@ function UnstructuredGridTopology(

D = num_dims(first(polytopes))
n = D+1
n_m_to_nface_to_mfaces = Matrix{Table{Int,Int32}}(undef,n,n)
n_m_to_nface_to_mfaces = Matrix{Table{Int,Vector{Int},Vector{Int32}}}(undef,n,n)
nvertices = length(vertex_coordinates)
for d in 0:D
dface_to_vertices = d_to_dface_vertices[d+1]
Expand Down
4 changes: 2 additions & 2 deletions src/Geometry/UnstructuredGrids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""
struct UnstructuredGrid{Dc,Dp,Tp,O} <: Grid{Dc,Dp}
node_coordinates::Vector{Point{Dp,Tp}}
cell_nodes::Table{Int,Int32}
cell_nodes::Table{Int,Vector{Int},Vector{Int32}}
reffes::Vector{LagrangianRefFE{Dc}}
cell_types::Vector{Int8}
@doc """
Expand Down Expand Up @@ -140,7 +140,7 @@ function from_dict(::Type{UnstructuredGrid},dict::Dict{Symbol,Any})
T = eltype(x)
Dp = dict[:Dp]
node_coordinates::Vector{Point{Dp,T}} = reinterpret(Point{Dp,T},x)
cell_nodes = from_dict(Table{Int,Int32},dict[:cell_nodes])
cell_nodes = from_dict(Table{Int,Vector{Int},Vector{Int32}},dict[:cell_nodes])
reffes = [ from_dict(LagrangianRefFE,reffe) for reffe in dict[:reffes]]
cell_type::Vector{Int8} = dict[:cell_type]
O::Bool = dict[:orientation]
Expand Down
36 changes: 33 additions & 3 deletions test/ArraysTests/TablesTests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TablesTests

using Test
using Gridap
using Gridap.Arrays
using Gridap.Io
using JSON
Expand All @@ -12,7 +13,7 @@ a = Table(data,ptrs)
b = [ data[ptrs[i]:ptrs[i+1]-1] for i in 1:length(ptrs)-1]
test_array(a,b)

c = convert(Table{Float64,Int32},a)
c = convert(Table{Float64,Vector{Float64},Vector{Int32}},a)
test_array(c,b)

data = Fill(1.3,12)
Expand All @@ -22,6 +23,35 @@ e = [ data[ptrs[i]:ptrs[i+1]-1] for i in 1:length(ptrs)-1]
test_array(d,e)


data = Int64[2,3,1,3,6,7,3,2,5,6,3,4]
ptrs = [1,4,4,7,13]
a = Table(data,ptrs)

data = reinterpret(Int64,Vector{Float64}(undef,12))
data[1:6] .= a.data[7:12]
data[7:9] .= a.data[4:6]
data[10:12] .= a.data[1:3]

perm = Vector{Int64}(undef,12)
perm[1:3] .= 10:12
perm[4:6] .= 7:9
perm[7:12] .= 1:6
data = reindex(data,perm)
b = Table(data,ptrs)
test_array(a,b)

k=1
data = rand(Int,12)
c=Table(data,ptrs)
for i in 1:length(b)
for j in 1:length(b[i])
b[i][j] = data[k]
global k=k+1
end
end
test_array(c,b)


vv = Array{Array{Int,2},2}(undef,2,2)
vv[1,1] = rand(1:10,2,2)
vv[2,1] = rand(1:10,2,2)
Expand Down Expand Up @@ -124,11 +154,11 @@ ptrs = [1,4,4,7,13]
a = Table(data,ptrs)

dict = to_dict(a)
b = from_dict(Table{Float64,Int32},dict)
b = from_dict(Table{Float64,Vector{Float64},Vector{Int32}},dict)
@test a == b

s = to_json(a)
b = from_json(Table{Float64,Int32},s)
b = from_json(Table{Float64,Vector{Float64},Vector{Int32}},s)
@test a == b

d = mktempdir()
Expand Down

0 comments on commit cce0534

Please sign in to comment.