Skip to content

Commit

Permalink
Merge pull request #12 from kalmarek/enh/precompute_cache
Browse files Browse the repository at this point in the history
Enh/precompute cache
  • Loading branch information
Marek Kaluba authored Sep 1, 2021
2 parents 1dd45b3 + 4477b11 commit bb8b954
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StarAlgebras"
uuid = "0c0c59c1-dc5f-42e9-9a8b-b5dc384a6cd1"
authors = ["Marek Kaluba <[email protected]>"]
version = "0.1.4"
version = "0.1.5"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
53 changes: 14 additions & 39 deletions src/mtables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,18 @@ function MTable{Tw}(basis::AbstractBasis; table_size) where {Tw}
return MTable{Tw}(table)
end

function complete!(table, basis, ::Val{false})
Threads.@threads for j in 1:size(table, 2)
y = basis[j]
for i in 1:size(table, 1)
table[i, j] = basis[_product(Val(false), basis[i], y)]
end
end
return table
end

function complete!(table, basis, ::Val{true})
Threads.@threads for i in 1:size(table, 1)
x = star(basis[i])
for j in 1:size(table, 2)
# star(x)*y
table[i, j] = basis[_product(Val(false), x, basis[j])]
for twisted in (:true, :false)
@eval begin
function complete!(table, basis, v::Val{$twisted})
Threads.@threads for j in 1:size(table, 2)
y = basis[j]
for i in 1:size(table, 1)
table[i, j] = basis[_product(v, basis[i], y)]
end
end
return table
end
end
return table
end

basis(mt::MTable) = throw("No basis is defined for a simple $(typeof(mt))")
Expand Down Expand Up @@ -121,11 +114,11 @@ Base.@propagate_inbounds function cache!(cmt::CachedMTable, i::Integer, j::Integ
return cmt
end

Base.@propagate_inbounds function cache!(
cmt::CachedMTable{T,I,B,M,false},
function cache!(
cmt::CachedMTable,
suppX::AbstractVector{<:Integer},
suppY::AbstractVector{<:Integer},
) where {T,I,B,M}
)
Threads.@threads for j in suppY
for i in suppX
if !_iscached(cmt, i, j)
Expand All @@ -136,22 +129,4 @@ Base.@propagate_inbounds function cache!(
return cmt
end

Base.@propagate_inbounds function cache!(
cmt::CachedMTable{T,I,B,M,true},
suppX::AbstractVector{<:Integer},
suppY::AbstractVector{<:Integer},
) where {T,I,B,M}
b = basis(cmt)
Threads.@threads for i in suppX
g = star(b[i])
for j in suppY
if !_iscached(cmt, i, j)
h = b[j]
gh = _product(Val(false), g, h)
gh in b || throw(ProductNotDefined(i, j, "$g · $h = $gh"))
cmt.table[i, j] = b[gh]
end
end
end
return cmt
end
complete!(cmt::CachedMTable) = cache!(cmt, 1:size(cmt, 1), 1:size(cmt, 2))
8 changes: 5 additions & 3 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ function StarAlgebra{Tw}(obj, basis::AbstractBasis) where {Tw}
end

# CachedMStructure:
StarAlgebra(obj, basis::AbstractBasis, cache_size::Tuple{<:Integer,Integer}) =
StarAlgebra{false}(obj, basis, cache_size)
StarAlgebra(obj, basis::AbstractBasis, cache_size::Tuple{<:Integer,Integer}; precompute=false) =
StarAlgebra{false}(obj, basis, cache_size, precompute=precompute)

function StarAlgebra{Tw}(
obj,
basis::AbstractBasis,
cache_size::Tuple{<:Integer,Integer},
cache_size::Tuple{<:Integer,Integer};
precompute=false
) where {Tw}
mstr = CachedMTable{Tw}(basis, table_size = cache_size)
precompute && complete!(mstr)
return StarAlgebra(obj, basis, mstr)
end

Expand Down
3 changes: 3 additions & 0 deletions test/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,7 @@ end
@test StarAlgebras._check(RG.mstructure)

@test all(!iszero, RG.mstructure.table)

RG = StarAlgebra(Word(A, Int[]), b, (k, k), precompute=true)
@test all(!iszero, RG.mstructure.table)
end

2 comments on commit bb8b954

@kalmarek
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/43984

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.5 -m "<description of version>" bb8b954799bc047d0f364c3a9f66cd630d080a5f
git push origin v0.1.5

Please sign in to comment.