From a91124511a57c63ebfe5fe36cefd458a09b1a6ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pawela?= Date: Fri, 12 May 2023 15:43:40 +0200 Subject: [PATCH 1/5] initial upgrades for pyramidindices --- Project.toml | 2 +- pi.jl | 42 +++++++++++++++++++++++++++++++++++++++++ src/SymmetricTensors.jl | 1 - src/symmetrictensor.jl | 25 +++++++++--------------- test/runtests.jl | 1 - 5 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 pi.jl diff --git a/Project.toml b/Project.toml index f148592..dfbd0b5 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SymmetricTensors" uuid = "1ab33d94-6c6c-50cc-93f0-e3f623a46aa0" authors = ["Krzysztof Domino ", "Ɓukasz Pawela ", "Piotr Gawron "] -version = "1.0.6" +version = "1.0.7" [deps] Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" diff --git a/pi.jl b/pi.jl new file mode 100644 index 0000000..ad94a44 --- /dev/null +++ b/pi.jl @@ -0,0 +1,42 @@ +using Base.Cartesian + +function pi2(dims::Int, tensize::Int) + multinds = Tuple{fill(Int, dims)...,}[] + quote + @nloops $dims i x -> (x==dims) ? (1:tensize) : (i_{x+1}:tensize) begin + @inbounds multind = @ntuple $dims x -> i_{dims-x+1} + push!($multinds, multind) + end + end + multinds +end + + +a = rand(2,2,2) +dims, tensize = ndims(a), size(a, 1) +@time pi2(dims, tensize) +@time pi2(dims, tensize) + + +b = rand(3,3,3) +dims, tensize = ndims(b), size(b, 1) +@time pi2(dims, tensize) +@time pi2(dims, tensize) + +c = rand(4,4,4,4) +dims, tensize = ndims(c), size(c, 1) +@time pi2(dims, tensize) +@time pi2(dims, tensize) + +@time d = rand(10, 100, 100, 100, 100) +dims, tensize = ndims(d), size(d, 1) +@time pi2(dims, tensize) +@time pi2(dims, tensize) + +@time e = rand(20, 20, 20) +dims, tensize = ndims(e), size(e, 1) +@time pi2(dims, tensize) +@time pi2(dims, tensize) + + +@show pi2(dims, tensize) \ No newline at end of file diff --git a/src/SymmetricTensors.jl b/src/SymmetricTensors.jl index 02fdaa6..5949297 100644 --- a/src/SymmetricTensors.jl +++ b/src/SymmetricTensors.jl @@ -4,7 +4,6 @@ module SymmetricTensors using StatsBase using Random using LinearAlgebra - using Memoization import Base: +, -, *, /, size, getindex, rand, setindex! if VERSION >= v"1.3" using CompilerSupportLibraries_jll diff --git a/src/symmetrictensor.jl b/src/symmetrictensor.jl index f8690c0..e43fd57 100644 --- a/src/symmetrictensor.jl +++ b/src/symmetrictensor.jl @@ -122,24 +122,17 @@ julia> pyramidindices(2,3) (3,3) ``` """ -@memoize function pyramidindices(dims::Int, tensize::Int) - multinds = Tuple{fill(Int,dims)...,}[] - @eval begin - @nloops $dims i x -> (x==$dims) ? (1:$tensize) : (i_{x+1}:$tensize) begin - @inbounds multind = @ntuple $dims x -> i_{$dims-x+1} - push!($multinds, multind) - end - end - multinds +function pyramidindices(dims::Int, tensize::Int) + quote + multinds = Tuple{fill(Int, dims)...,}[] + @nloops $dims i x -> (x==$dims) ? (1:tensize) : (i_{x+1}:tensize) begin + @inbounds multind = @ntuple $dims x -> i_{$dims-x+1} + push!(multinds, multind) + end + multinds + end end -""" - pyramidindices(st::SymmetricTensor) - -Return the indices of the unique elements of the given symmetric tensor. -""" -pyramidindices(st::SymmetricTensor{<:Any, N}) where N = pyramidindices(N, st.dats) - """ sizetest(dats::Int, bls::Int) diff --git a/test/runtests.jl b/test/runtests.jl index 5bdc6f5..0e0a769 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -57,7 +57,6 @@ t1 = randsymarray(7, 3) end end @test found == length(pinds) - @test pyramidindices(st) == pinds end end From 31d0e044d84c4269750039b411a7b16c8b272d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pawela?= Date: Mon, 15 May 2023 11:45:56 +0200 Subject: [PATCH 2/5] remove `collect`from generator --- src/randgendat.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/randgendat.jl b/src/randgendat.jl index c7c0ef3..aa15f23 100644 --- a/src/randgendat.jl +++ b/src/randgendat.jl @@ -10,7 +10,7 @@ function randsymarray(::Type{T}, dim::Int, N::Int = 4) where T<:Real t = zeros(fill(dim, N)...,) for i in pyramidindices(N,dim) n = rand(T) - for j in collect(permutations(i)) + for j in permutations(i) @inbounds t[j...] = n end end From 4bb418b5069aca97a4938e7f1228d59b01100ff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pawela?= Date: Mon, 15 May 2023 17:14:17 +0200 Subject: [PATCH 3/5] update pyramid indices to compile only once --- pi.jl | 27 ++++++++++++++++++++------- src/symmetrictensor.jl | 26 ++++++++++++++++++-------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/pi.jl b/pi.jl index ad94a44..8bd1b6c 100644 --- a/pi.jl +++ b/pi.jl @@ -1,16 +1,27 @@ using Base.Cartesian +using ResumableFunctions -function pi2(dims::Int, tensize::Int) - multinds = Tuple{fill(Int, dims)...,}[] +struct PyramidIndices{N} + size::Int +end + +@generated function _all_indices(p::PyramidIndices{N}) where {N} quote - @nloops $dims i x -> (x==dims) ? (1:tensize) : (i_{x+1}:tensize) begin - @inbounds multind = @ntuple $dims x -> i_{dims-x+1} - push!($multinds, multind) + # multinds = Tuple{fill(Int, $N)...,}[] + tensize = p.size + @nloops $N i x -> (x==$N) ? (1:tensize) : (i_{x+1}:tensize) begin + @inbounds multind = @ntuple $N x -> i_{$N-x+1} + @yield multind + # push!(multinds, multind) end + # multinds end - multinds end +function pi2(dims, tensize) + p = PyramidIndices{dims}(tensize) + return _all_indices(p) +end a = rand(2,2,2) dims, tensize = ndims(a), size(a, 1) @@ -23,11 +34,14 @@ dims, tensize = ndims(b), size(b, 1) @time pi2(dims, tensize) @time pi2(dims, tensize) +@show pi2(dims, tensize) + c = rand(4,4,4,4) dims, tensize = ndims(c), size(c, 1) @time pi2(dims, tensize) @time pi2(dims, tensize) +@show "asdasd" @time d = rand(10, 100, 100, 100, 100) dims, tensize = ndims(d), size(d, 1) @time pi2(dims, tensize) @@ -39,4 +53,3 @@ dims, tensize = ndims(e), size(e, 1) @time pi2(dims, tensize) -@show pi2(dims, tensize) \ No newline at end of file diff --git a/src/symmetrictensor.jl b/src/symmetrictensor.jl index e43fd57..ccb54e6 100644 --- a/src/symmetrictensor.jl +++ b/src/symmetrictensor.jl @@ -29,6 +29,22 @@ mutable struct SymmetricTensor{T <: AbstractFloat, N} end end +struct PyramidIndices{N} + size::Int +end + +@generated function _all_indices(p::PyramidIndices{N}) where {N} + quote + multinds = Tuple{fill(Int, $N)...,}[] + tensize = p.size + @nloops $N i x -> (x==$N) ? (1:tensize) : (i_{x+1}:tensize) begin + @inbounds multind = @ntuple $N x -> i_{$N-x+1} + push!(multinds, multind) + end + multinds + end +end + """ unfold(ar::Array{T,N}, mode::Int) @@ -123,14 +139,8 @@ julia> pyramidindices(2,3) ``` """ function pyramidindices(dims::Int, tensize::Int) - quote - multinds = Tuple{fill(Int, dims)...,}[] - @nloops $dims i x -> (x==$dims) ? (1:tensize) : (i_{x+1}:tensize) begin - @inbounds multind = @ntuple $dims x -> i_{$dims-x+1} - push!(multinds, multind) - end - multinds - end + p = PyramidIndices{dims}(tensize) + return _all_indices(p) end """ From c96ab2a973a14bb9953b3c7466a654244857c17c Mon Sep 17 00:00:00 2001 From: Krzysztof Domino Date: Tue, 16 May 2023 15:54:34 +0200 Subject: [PATCH 4/5] remove Memoization from Project.toml --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index dfbd0b5..f35095c 100644 --- a/Project.toml +++ b/Project.toml @@ -9,13 +9,13 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" CompilerSupportLibraries_jll = "e66e0078-7015-5450-92f7-15fbd957f2ae" -Memoization = "6fafb56a-5788-4b4e-91ca-c0cea6611c73" + [compat] julia = "1" Combinatorics = "1" StatsBase = "0.33" -Memoization = "0.2" + [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" From d915d2c1d044e80e3be94f5c662126ef5b00e41b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pawela?= <3093117+lpawela@users.noreply.github.com> Date: Tue, 16 May 2023 22:11:47 +0200 Subject: [PATCH 5/5] Delete pi.jl --- pi.jl | 55 ------------------------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 pi.jl diff --git a/pi.jl b/pi.jl deleted file mode 100644 index 8bd1b6c..0000000 --- a/pi.jl +++ /dev/null @@ -1,55 +0,0 @@ -using Base.Cartesian -using ResumableFunctions - -struct PyramidIndices{N} - size::Int -end - -@generated function _all_indices(p::PyramidIndices{N}) where {N} - quote - # multinds = Tuple{fill(Int, $N)...,}[] - tensize = p.size - @nloops $N i x -> (x==$N) ? (1:tensize) : (i_{x+1}:tensize) begin - @inbounds multind = @ntuple $N x -> i_{$N-x+1} - @yield multind - # push!(multinds, multind) - end - # multinds - end -end - -function pi2(dims, tensize) - p = PyramidIndices{dims}(tensize) - return _all_indices(p) -end - -a = rand(2,2,2) -dims, tensize = ndims(a), size(a, 1) -@time pi2(dims, tensize) -@time pi2(dims, tensize) - - -b = rand(3,3,3) -dims, tensize = ndims(b), size(b, 1) -@time pi2(dims, tensize) -@time pi2(dims, tensize) - -@show pi2(dims, tensize) - -c = rand(4,4,4,4) -dims, tensize = ndims(c), size(c, 1) -@time pi2(dims, tensize) -@time pi2(dims, tensize) - -@show "asdasd" -@time d = rand(10, 100, 100, 100, 100) -dims, tensize = ndims(d), size(d, 1) -@time pi2(dims, tensize) -@time pi2(dims, tensize) - -@time e = rand(20, 20, 20) -dims, tensize = ndims(e), size(e, 1) -@time pi2(dims, tensize) -@time pi2(dims, tensize) - -