Skip to content

Commit f87efac

Browse files
author
KDr2
committed
unit test cases and benchmarks
1 parent 9001c24 commit f87efac

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ julia = "1.3"
1616

1717
[extras]
1818
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
19+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
1920

2021
[targets]
21-
test = ["Test"]
22+
test = ["Test", "BenchmarkTools"]

src/tarray.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ end
3131

3232
TArray{T}(d::Integer...) where T = TArray(T, d)
3333
TArray{T}(::UndefInitializer, d::Integer...) where T = TArray(T, d)
34+
TArray{T}(::UndefInitializer, dim::NTuple{N,Int}) where {T,N} = TArray(T, dim)
3435
TArray{T,N}(d::Vararg{<:Integer,N}) where {T,N} = TArray(T, d)
3536
TArray{T,N}(::UndefInitializer, d::Vararg{<:Integer,N}) where {T,N} = TArray{T,N}(d)
3637
TArray{T,N}(dim::NTuple{N,Int}) where {T,N} = TArray(T, dim)
@@ -136,7 +137,7 @@ function Base.show(io::IO, ::MIME"text/plain", x::TArray)
136137
arr = x.orig_task.storage[x.ref][2]
137138
@warn "Here shows the originating task's storage, " *
138139
"not the current task's storage. " *
139-
"Please explictly call show(::TArray) to display the current task's version of a TArray."
140+
"Please explicitly call show(::TArray) to display the current task's version of a TArray."
140141
show(io, MIME("text/plain"), arr)
141142
end
142143

@@ -220,13 +221,14 @@ Base.:-(x::TArray) = (- _get(x)) |> localize
220221
Base.transpose(x::TArray) = transpose(_get(x)) |> localize
221222
Base.adjoint(x::TArray) = adjoint(_get(x)) |> localize
222223
Base.repeat(x::TArray; kw...) = repeat(_get(x); kw...) |> localize
223-
Base.hcat(x::TArray, rest...) = hcat(_get(x), _get.(rest)...) |> localize
224-
Base.hcat(x::AbstractArray, y::TArray, rest...) = hcat(x, _get(y), _get.(rest)...) |> localize
225-
Base.vcat(x::TArray, rest...) = vcat(_get(x), _get.(rest)...) |> localize
226-
Base.vcat(x::AbstractArray, y::TArray, rest...) = vcat(x, _get(y), _get.(rest)...) |> localize
227-
Base.cat(x::TArray, rest...; dims) = cat(_get(x), _get.(rest)...; dims = dims) |> localize
228-
Base.cat(x::AbstractArray, y::TArray, rest...; dims) =
229-
cat(x, _get(y), _get.(rest)...; dims = dims) |> localize
224+
225+
Base.hcat(xs::Union{TArray{T,1}, TArray{T,2}}...) where T =
226+
hcat(_get.(xs)...) |> localize
227+
Base.vcat(xs::Union{TArray{T,1}, TArray{T,2}}...) where T =
228+
vcat(_get.(xs)...) |> localize
229+
Base.cat(xs::Union{TArray{T,1}, TArray{T,2}}...; dims) where T =
230+
cat(_get.(xs)...; dims = dims) |> localize
231+
230232

231233
Base.reshape(x::TArray, dims::Union{Colon,Int}...) = reshape(_get(x), dims) |> localize
232234
Base.reshape(x::TArray, dims::Tuple{Vararg{Union{Int,Colon}}}) =
@@ -256,6 +258,9 @@ Base.:*(x::TArray, y::TArray) = _get(x) * _get(y) |> localize
256258
Base.:*(x::AbstractArray, y::TArray) = x * _get(y) |> localize
257259
Base.:*(x::TArray, y::AbstractArray) = _get(x) * y |> localize
258260

261+
# broadcast
262+
Base.BroadcastStyle(::Type{TArray{T, N}}) where {T, N} = Broadcast.ArrayStyle{TArray}()
263+
Broadcast.broadcasted(::Broadcast.ArrayStyle{TArray}, f, args...) = f.(_get.(args)...) |> localize
259264

260265
import LinearAlgebra
261266
import LinearAlgebra: \, /, inv, det, logdet, logabsdet, norm

test/benchmarks.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using BenchmarkTools
2+
using Libtask
3+
4+
println("= Benchmarks on Arrays =")
5+
A = rand(100, 100)
6+
x, y = abs.(rand(Int, 2) .% 100)
7+
print("indexing: ")
8+
@btime $A[$x, $y] + $A[$x, $y]
9+
print("set indexing: ")
10+
@btime $A[$x, $y] = 1
11+
print("broadcast: ")
12+
@btime $A .+ $A
13+
14+
println("= Benchmarks on TArrays =")
15+
TA = Libtask.localize(deepcopy(A))
16+
print("indexing: ")
17+
@btime $TA[$x, $y] + $TA[$x, $y]
18+
print("set indexing: ")
19+
@btime $TA[$x, $y] = 1
20+
print("broadcast: ")
21+
@btime $TA .+ $TA

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ using Test
44
include("ctask.jl")
55
include("tarray.jl")
66
include("tref.jl")
7+
8+
if get(ENV, "BENCHMARK", nothing) != nothing
9+
include("benchmarks.jl")
10+
end

test/tarray.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
@test pop!(ta1_2) == 2
3535
end
3636

37-
@testset "other methods" begin
37+
@testset "constructors and conversion" begin
3838
ta2 = TArray{Int}(4, 4)
3939
@test ta2 isa TArray{Int,2}
4040
@test size(ta2) == (4, 4)
@@ -101,6 +101,24 @@
101101
ta7 = TArray{Int, 2}((2, 2))
102102
end
103103

104+
@testset "stdlib functions" begin
105+
ta = TArray{Int}(4, 4)
106+
107+
@test view(ta, 3:5) isa TArray{Int, 1}
108+
@test view(ta, 3:5) == ta[3:5]
109+
110+
@test -ta isa TArray{Int, 2}
111+
@test -(-ta) == ta
112+
113+
@test transpose(ta)[2, 3] == ta[3, 2]
114+
115+
@test repeat(ta, 2) == vcat(ta, ta)
116+
117+
@test repeat(ta, 1, 2) == hcat(ta, ta)
118+
119+
@test ta .+ ta == Libtask._get(ta) .+ Libtask._get(ta)
120+
end
121+
104122
@testset "task copy" begin
105123
function f()
106124
t = TArray(Int, 1)

0 commit comments

Comments
 (0)