Skip to content

Commit

Permalink
tests: Split array testsets
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsamaroo committed Apr 2, 2024
1 parent 912aab8 commit 3c80e3a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 67 deletions.
74 changes: 11 additions & 63 deletions test/array.jl → test/array/core.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
using LinearAlgebra, SparseArrays, Random, SharedArrays
import Dagger: DArray, chunks, domainchunks, treereduce_nd
import Distributed: myid, procs
import Statistics: mean, var, std
import OnlineStats

@testset "treereduce_nd" begin
xs = rand(1:10, 8,8,8)
concats = [(x...)->cat(x..., dims=n) for n in 1:3]
Expand Down Expand Up @@ -80,52 +74,6 @@ end
end
end

function test_mapreduce(f, init_func; no_init=true, zero_init=zero,
types=(Int32, Int64, Float32, Float64),
cmp=isapprox)
@testset "$T" for T in types
X = init_func(Blocks(10, 10), T, 100, 100)
inits = ()
if no_init
inits = (inits..., nothing)
end
if zero_init !== nothing
inits = (inits..., zero_init(T))
end
@testset "dims=$dims" for dims in (Colon(), 1, 2, (1,), (2,))
@testset "init=$init" for init in inits
if init === nothing
if dims == Colon()
@test cmp(f(X; dims), f(collect(X); dims))
else
@test cmp(collect(f(X; dims)), f(collect(X); dims))
end
else
if dims == Colon()
@test cmp(f(X; dims, init), f(collect(X); dims, init))
else
@test cmp(collect(f(X; dims, init)), f(collect(X); dims, init))
end
end
end
end
end
end

# Base
@testset "reduce" test_mapreduce((X; dims, init=Base._InitialValue())->reduce(+, X; dims, init), ones)
@testset "mapreduce" test_mapreduce((X; dims, init=Base._InitialValue())->mapreduce(x->x+1, +, X; dims, init), ones)
@testset "sum" test_mapreduce(sum, ones)
@testset "prod" test_mapreduce(prod, rand)
@testset "minimum" test_mapreduce(minimum, rand)
@testset "maximum" test_mapreduce(maximum, rand)
@testset "extrema" test_mapreduce(extrema, rand; cmp=Base.:(==), zero_init=T->(zero(T), zero(T)))

# Statistics
@testset "mean" test_mapreduce(mean, rand; zero_init=nothing, types=(Float32, Float64))
@testset "var" test_mapreduce(var, rand; zero_init=nothing, types=(Float32, Float64))
@testset "std" test_mapreduce(std, rand; zero_init=nothing, types=(Float32, Float64))

@testset "broadcast" begin
X1 = rand(Blocks(10), 100)
X2 = X1 .* 3.4
Expand All @@ -138,7 +86,7 @@ end

@testset "distributing an array" begin
function test_dist(X)
X1 = distribute(X, Blocks(10, 20))
X1 = Distribute(Blocks(10, 20), X)
Xc = fetch(X1)
@test Xc isa DArray{eltype(X),ndims(X)}
@test Xc == X
Expand All @@ -147,7 +95,7 @@ end
@test map(x->size(x) == (10, 20), domainchunks(Xc)) |> all
end
x = [1 2; 3 4]
@test distribute(x, Blocks(1,1)) == x
@test Distribute(Blocks(1,1), x) == x
test_dist(rand(100, 100))
test_dist(sprand(100, 100, 0.1))

Expand All @@ -174,7 +122,7 @@ end
@testset "matrix-matrix multiply" begin
function test_mul(X)
tol = 1e-12
X1 = distribute(X, Blocks(10, 20))
X1 = Distribute(Blocks(10, 20), X)
@test_throws DimensionMismatch X1*X1
X2 = X1'*X1
X3 = X1*X1'
Expand All @@ -188,7 +136,7 @@ end
test_mul(rand(40, 40))

x = rand(10,10)
X = distribute(x, Blocks(3,3))
X = Distribute(Blocks(3,3), x)
y = rand(10)
@test norm(collect(X*y) - x*y) < 1e-13
end
Expand All @@ -202,24 +150,24 @@ end

@testset "concat" begin
m = rand(75,75)
x = distribute(m, Blocks(10,20))
y = distribute(m, Blocks(10,10))
x = Distribute(Blocks(10,20), m)
y = Distribute(Blocks(10,10), m)
@test hcat(m,m) == collect(hcat(x,x)) == collect(hcat(x,y))
@test vcat(m,m) == collect(vcat(x,x))
@test_throws DimensionMismatch vcat(x,y)
end

@testset "scale" begin
x = rand(10,10)
X = distribute(x, Blocks(3,3))
X = Distribute(Blocks(3,3), x)
y = rand(10)

@test Diagonal(y)*x == collect(Diagonal(y)*X)
end

@testset "Getindex" begin
function test_getindex(x)
X = distribute(x, Blocks(3,3))
X = Distribute(Blocks(3,3), x)
@test collect(X[3:8, 2:7]) == x[3:8, 2:7]
ragged_idx = [1,2,9,7,6,2,4,5]
@test collect(X[ragged_idx, 2:7]) == x[ragged_idx, 2:7]
Expand Down Expand Up @@ -248,7 +196,7 @@ end


@testset "cleanup" begin
X = distribute(rand(10,10), Blocks(10,10))
X = Distribute(Blocks(10,10), rand(10,10))
@test collect(sin.(X)) == collect(sin.(X))
end

Expand All @@ -269,7 +217,7 @@ end
x=rand(10,10)
y=copy(x)
y[3:8, 2:7] .= 1.0
X = distribute(x, Blocks(3,3))
X = Distribute(Blocks(3,3), x)
@test collect(setindex(X,1.0, 3:8, 2:7)) == y
@test collect(X) == x
end
Expand All @@ -292,7 +240,7 @@ end
@test collect(sort(y)) == x

x = ones(10)
y = distribute(x, Blocks(3))
y = Distribute(Blocks(3), x)
@test_broken map(x->length(collect(x)), sort(y).chunks) == [3,3,3,1]
end

Expand Down
2 changes: 0 additions & 2 deletions test/linalg.jl → test/array/linalg.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using LinearAlgebra

@testset "Linear Algebra" begin
@testset "GEMM: $T" for T in (Float32, Float64, ComplexF32, ComplexF64)
A = rand(T, 128, 128)
Expand Down
45 changes: 45 additions & 0 deletions test/array/mapreduce.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function test_mapreduce(f, init_func; no_init=true, zero_init=zero,
types=(Int32, Int64, Float32, Float64),
cmp=isapprox)
@testset "$T" for T in types
X = init_func(Blocks(10, 10), T, 100, 100)
inits = ()
if no_init
inits = (inits..., nothing)
end
if zero_init !== nothing
inits = (inits..., zero_init(T))
end
@testset "dims=$dims" for dims in (Colon(), 1, 2, (1,), (2,))
@testset "init=$init" for init in inits
if init === nothing
if dims == Colon()
@test cmp(f(X; dims), f(collect(X); dims))
else
@test cmp(collect(f(X; dims)), f(collect(X); dims))
end
else
if dims == Colon()
@test cmp(f(X; dims, init), f(collect(X); dims, init))
else
@test cmp(collect(f(X; dims, init)), f(collect(X); dims, init))
end
end
end
end
end
end

# Base
@testset "reduce" test_mapreduce((X; dims, init=Base._InitialValue())->reduce(+, X; dims, init), ones)
@testset "mapreduce" test_mapreduce((X; dims, init=Base._InitialValue())->mapreduce(x->x+1, +, X; dims, init), ones)
@testset "sum" test_mapreduce(sum, ones)
@testset "prod" test_mapreduce(prod, rand)
@testset "minimum" test_mapreduce(minimum, rand)
@testset "maximum" test_mapreduce(maximum, rand)
@testset "extrema" test_mapreduce(extrema, rand; cmp=Base.:(==), zero_init=T->(zero(T), zero(T)))

# Statistics
@testset "mean" test_mapreduce(mean, rand; zero_init=nothing, types=(Float32, Float64))
@testset "var" test_mapreduce(var, rand; zero_init=nothing, types=(Float32, Float64))
@testset "std" test_mapreduce(std, rand; zero_init=nothing, types=(Float32, Float64))
5 changes: 5 additions & 0 deletions test/imports.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using LinearAlgebra, SparseArrays, Random, SharedArrays
import Dagger: DArray, chunks, domainchunks, treereduce_nd
import Distributed: myid, procs
import Statistics: mean, var, std
import OnlineStats
6 changes: 4 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ tests = [
("Task Queues", "task-queues.jl"),
("Datadeps", "datadeps.jl"),
("Domain Utilities", "domain.jl"),
("Array", "array.jl"),
("Linear Algebra", "linalg.jl"),
("Array - Core", "array/core.jl"),
("Array - MapReduce", "array/mapreduce.jl"),
("Array - LinearAlgebra", "array/linalg.jl"),
("Caching", "cache.jl"),
("Disk Caching", "diskcaching.jl"),
("File IO", "file-io.jl"),
Expand Down Expand Up @@ -70,6 +71,7 @@ end
using Distributed
addprocs(3)

include("imports.jl")
include("util.jl")
include("fakeproc.jl")

Expand Down

0 comments on commit 3c80e3a

Please sign in to comment.