From 5d10d697556859af96d2de641e4c00b6e3ace6b4 Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Mon, 14 Oct 2019 17:09:01 +1100 Subject: [PATCH 1/2] add copy method to force dispatch on parent type --- src/array.jl | 1 + test/benchmarks.jl | 28 +++++++++++++++------------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/array.jl b/src/array.jl index 15b51fe25..d20e8c70a 100644 --- a/src/array.jl +++ b/src/array.jl @@ -31,6 +31,7 @@ Base.@propagate_inbounds Base.view(A::AbDimArray, I::Vararg{<:StandardIndices}) Base.convert(::Type{Array{T,N}}, A::AbDimArray{T,N}) where {T,N} = convert(Array{T,N}, parent(A)) +Base.copy(A::AbDimArray) = rebuild(A, copy(parent(A))) Base.copy!(dst::AbDimArray, src::AbDimArray) = copy!(parent(src), parent(dst)) # Similar. TODO this need a rethink. How do we know what the new dims are? diff --git a/test/benchmarks.jl b/test/benchmarks.jl index 8f869220e..d9c0b658a 100644 --- a/test/benchmarks.jl +++ b/test/benchmarks.jl @@ -91,20 +91,22 @@ using SparseArrays, Statistics @dim Var "Variable" @dim Obs "Observation" -sparsear = sprand(10000, 10000, 0.1) -sparsed = DimensionalArray( - sparsear, - (Var <| ["var$i" for i in 1:10000], Obs <| ["obs$i" for i in 1:10000]) -) +sparse_a = sprand(1000, 1000, 0.1) +sparse_d = DimensionalArray(sparse_a, (Var <| 1:1000, Obs <| 1:1000)) # Jit warmups -mean(sparsear, dims=1) -mean(sparsear, dims=2) -mean(sparsed, dims=Var()) -mean(sparsed, dims=Obs()) +mean(sparse_a, dims=1) +mean(sparse_a, dims=2) +mean(sparse_d, dims=Var()) +mean(sparse_d, dims=Obs()) # Benchmarks -println("Regular sparse") -@btime mean($sparsear, dims=$1) -println("Dims sparse") -@btime mean($sparsed, dims=$Var) +println("mean: regular sparse") +@btime mean($sparse_a, dims=$1) +println("mean: dims sparse") +@btime mean($sparse_d, dims=$(Var())) + +println("copy: regular sparse") +@btime copy($sparse_a) +println("copy: dims sparse") +@btime copy($sparse_d) From 6b41d10d939705a648be8c42ea023144b95914e2 Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Mon, 14 Oct 2019 19:59:19 +1100 Subject: [PATCH 2/2] add regular no-dimension math methods --- src/methods.jl | 3 +++ test/benchmarks.jl | 12 ++++++++---- test/runtests.jl | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/methods.jl b/src/methods.jl index f95a1031c..e22f734f1 100644 --- a/src/methods.jl +++ b/src/methods.jl @@ -2,6 +2,7 @@ for (mod, fname) in ((:Base, :sum), (:Base, :prod), (:Base, :maximum), (:Base, :minimum), (:Statistics, :mean)) _fname = Symbol('_', fname) @eval begin + @inline ($mod.$fname)(A::AbDimArray) = ($mod.$fname)(parent(A)) @inline ($mod.$_fname)(A::AbstractArray, dims::AllDimensions) = rebuild(A, ($mod.$_fname)(parent(A), dimnum(A, dims)), reducedims(A, dims)) @inline ($mod.$_fname)(f, A::AbstractArray, dims::AllDimensions) = @@ -16,6 +17,7 @@ end for fname in (:std, :var) _fname = Symbol('_', fname) @eval begin + @inline (Statistics.$fname)(A::AbDimArray) = (Statistics.$fname)(parent(A)) @inline (Statistics.$_fname)(A::AbstractArray, corrected::Bool, mean, dims::AllDimensions) = rebuild(A, (Statistics.$_fname)(A, corrected, mean, dimnum(A, dims)), reducedims(A, dims)) @inline (Statistics.$_fname)(A::AbDimArray, corrected::Bool, mean, dims::Union{Int,Base.Dims}) = @@ -23,6 +25,7 @@ for fname in (:std, :var) end end +Statistics.median(A::AbDimArray) = Statistics.median(parent(A)) Statistics._median(A::AbstractArray, dims::AllDimensions) = rebuild(A, Statistics._median(parent(A), dimnum(A, dims)), reducedims(A, dims)) Statistics._median(A::AbDimArray, dims::Union{Int,Base.Dims}) = diff --git a/test/benchmarks.jl b/test/benchmarks.jl index d9c0b658a..3f1d1eca0 100644 --- a/test/benchmarks.jl +++ b/test/benchmarks.jl @@ -85,8 +85,7 @@ println("reverse: normal, numbers + rebuild, dims + rebuild") @btime reverse($da; dims=1) @btime reverse($da; dims=Y()) -# Sparse -using SparseArrays, Statistics +# Sparse (and similar specialised arrays) @dim Var "Variable" @dim Obs "Observation" @@ -101,11 +100,16 @@ mean(sparse_d, dims=Var()) mean(sparse_d, dims=Obs()) # Benchmarks -println("mean: regular sparse") +println("mean with dims arge: regular sparse") @btime mean($sparse_a, dims=$1) -println("mean: dims sparse") +println("mean with dims arge: dims sparse") @btime mean($sparse_d, dims=$(Var())) +println("mean: regular sparse") +@btime mean($sparse_a) +println("mean: dims sparse") +@btime mean($sparse_d) + println("copy: regular sparse") @btime copy($sparse_a) println("copy: dims sparse") diff --git a/test/runtests.jl b/test/runtests.jl index 24f002b21..690152e14 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using DimensionalData, Statistics, Test, BenchmarkTools, Unitful +using DimensionalData, Statistics, Test, BenchmarkTools, Unitful, SparseArrays using DimensionalData: val, basetype, slicedims, dims2indices, formatdims, @dim, reducedims, dimnum, basetype, X, Y, Z, Time, Forward