Skip to content

Commit 02051a8

Browse files
committed
Add tests
1 parent 4b74ee7 commit 02051a8

File tree

3 files changed

+68
-37
lines changed

3 files changed

+68
-37
lines changed

src/trees.jl

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -513,20 +513,6 @@ function Base.finalize(bc::BranchChannel)
513513
finalize_except_wherewhence(bc.parentchannels)
514514
end
515515

516-
function permuteworkers(w,p,toprank)
517-
(length(w) == 1) && return w
518-
519-
rank = findfirst(isequal(p),w)
520-
if !isnothing(rank) && rank != toprank
521-
# Move the calling worker to the top of the tree
522-
w_rest = w[w .!= p]
523-
insert!(w_rest,toprank,p)
524-
return w_rest
525-
else
526-
return w
527-
end
528-
end
529-
530516
function createbranchchannels!(branches,::Type{Tmap},::Type{Tred},tree::OrderedBinaryTree,
531517
superbranch::BranchChannel) where {Tmap,Tred}
532518

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function nprocs_node(hostnames::Vector{String},nodes::Vector{String})
8585
Dict(node=>count(isequal(node),hostnames) for node in nodes)
8686
end
8787

88-
function nprocs_node(d::AbstractDict{String,AbstractVector{<:Integer}})
88+
function nprocs_node(d::AbstractDict{String,<:AbstractVector{<:Integer}})
8989
nphost = OrderedDict{String,Int}()
9090
for (node,pnode) in d
9191
nphost[node] = length(pnode)

test/runtests.jl

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ addprocs(workersused)
1515
SequentialBinaryTree, OrderedBinaryTree, SegmentedSequentialBinaryTree,
1616
parentnoderank, nchildren,
1717
maybepvalput!, createbranchchannels, nworkersactive, workersactive,
18-
procs_node
18+
procs_node, leafrankfoldedtree
1919
end
2020

2121
macro testsetwithinfo(str,ex)
@@ -28,7 +28,7 @@ end
2828
# Wrap a testset around this to get the result at the end
2929
@testset "ParallelUtilities" begin
3030

31-
@testset "ProductSplit" begin
31+
@testsetwithinfo "ProductSplit" begin
3232

3333
various_iters = [(1:10,),(1:10,4:6),(1:10,4:6,1:4),(1:2:10,4:1:6),
3434
(1:2,Base.OneTo(4),1:3:10)]
@@ -379,7 +379,7 @@ end
379379
end
380380
end;
381381

382-
@testset "ReverseLexicographicTuple" begin
382+
@testsetwithinfo "ReverseLexicographicTuple" begin
383383
@testset "isless" begin
384384
a = ParallelUtilities.ReverseLexicographicTuple((1,2,3))
385385
b = ParallelUtilities.ReverseLexicographicTuple((2,2,3))
@@ -407,7 +407,7 @@ end;
407407
end
408408
end;
409409

410-
@testset "utilities" begin
410+
@testsetwithinfo "utilities" begin
411411
@testset "workers active" begin
412412
@test nworkersactive((1:1,)) == 1
413413
@test nworkersactive((1:2,)) == min(2,nworkers())
@@ -455,11 +455,13 @@ end;
455455
pnode = workers()[findall(isequal(node),hostnames)]
456456
@test p1[node] == pnode
457457
end
458+
np4 = nprocs_node(p1)
459+
@test np1 == np4
458460
end
459461
end;
460462

461463
@testset "BinaryTree" begin
462-
@testset "BinaryTreeNode" begin
464+
@testsetwithinfo "BinaryTreeNode" begin
463465
@testset "Constructor" begin
464466
p = workers()[1]
465467
b = BinaryTreeNode(p,p,0)
@@ -727,6 +729,14 @@ end;
727729
@test length(tree) == 12 + 3
728730
end;
729731

732+
@testset "leafrankfoldedtree" begin
733+
@test leafrankfoldedtree(5,1) == 8
734+
@test leafrankfoldedtree(5,2) == 9
735+
@test leafrankfoldedtree(5,3) == 5
736+
@test leafrankfoldedtree(5,4) == 6
737+
@test leafrankfoldedtree(5,5) == 7
738+
end;
739+
730740
@testset "pid and parent" begin
731741
for imax = 2:100
732742
procs = 1:imax
@@ -740,6 +750,7 @@ end;
740750
@test topnoderank == 1
741751
@test tree[topnoderank] == ParallelUtilities.topnode(tree)
742752
@test tree[1].parent == 1
753+
@test parentnoderank(tree,1) == 1
743754
for (ind,rank) in enumerate(1:mid)
744755
node = tree[rank+1]
745756
parentnode = tree[parentnoderank(tree,rank+1)]
@@ -1213,15 +1224,17 @@ end;
12131224
@testset "at parent nodes" begin
12141225

12151226
# Put some known values on the self and children channels
1216-
function putselfchildren!(pipe::BranchChannel,::Unsorted)
1217-
put!(pipe.selfchannels.out,0)
1218-
put!(pipe.selfchannels.err,false)
1227+
function putselfchildren!(pipe::BranchChannel,::Unsorted,rank=1)
1228+
if rank >= 1
1229+
put!(pipe.selfchannels.out,0)
1230+
put!(pipe.selfchannels.err,false)
1231+
end
12191232
for i=1:nchildren(pipe)
12201233
put!(pipe.childrenchannels.out,i)
12211234
put!(pipe.childrenchannels.err,false)
12221235
end
12231236
end
1224-
function putselfchildren!(pipe::BranchChannel{<:pval},::Sorted)
1237+
function putselfchildren!(pipe::BranchChannel{<:pval},::Sorted,rank=1)
12251238
put!(pipe.selfchannels.out,pval(2,2))
12261239
put!(pipe.selfchannels.err,false)
12271240
N = nchildren(pipe)
@@ -1239,8 +1252,10 @@ end;
12391252
end
12401253
end
12411254

1242-
function clearerrors!(pipe::BranchChannel)
1243-
take!(pipe.selfchannels.err)
1255+
function clearerrors!(pipe::BranchChannel,rank=1)
1256+
if rank >= 1
1257+
take!(pipe.selfchannels.err)
1258+
end
12441259
for i=1:nchildren(pipe)
12451260
take!(pipe.childrenchannels.err)
12461261
end
@@ -1249,27 +1264,26 @@ end;
12491264
@testset "reducedvalue" begin
12501265

12511266
function testreduction(freduce::Function,pipe::BranchChannel,
1252-
ifsorted::Ordering,res_exp)
1267+
ifsorted::Ordering,res_exp,rank=2)
12531268

12541269
p = pipe.p
1255-
rank = 2
12561270

12571271
try
1258-
putselfchildren!(pipe,ifsorted)
1272+
putselfchildren!(pipe,ifsorted,rank)
12591273
@test value(reducedvalue(freduce,rank,pipe,ifsorted)) == res_exp
1260-
clearerrors!(pipe)
1274+
clearerrors!(pipe,rank)
12611275

1262-
@fetchfrom p putselfchildren!(pipe,ifsorted)
1276+
@fetchfrom p putselfchildren!(pipe,ifsorted,rank)
12631277
@test value(@fetchfrom p reducedvalue(freduce,rank,pipe,ifsorted)) == res_exp
1264-
clearerrors!(pipe)
1278+
clearerrors!(pipe,rank)
12651279

1266-
@fetchfrom p putselfchildren!(pipe,ifsorted)
1280+
@fetchfrom p putselfchildren!(pipe,ifsorted,rank)
12671281
@test value(reducedvalue(freduce,rank,pipe,ifsorted)) == res_exp
1268-
clearerrors!(pipe)
1282+
clearerrors!(pipe,rank)
12691283

1270-
putselfchildren!(pipe,ifsorted)
1284+
putselfchildren!(pipe,ifsorted,rank)
12711285
@test value(@fetchfrom p reducedvalue(freduce,rank,pipe,ifsorted)) == res_exp
1272-
clearerrors!(pipe)
1286+
clearerrors!(pipe,rank)
12731287
catch
12741288
rethrow()
12751289
end
@@ -1279,7 +1293,8 @@ end;
12791293
@testset "Unsorted" begin
12801294
pipe = BranchChannel{Int,Int}(myid(),nchildren)
12811295
res_exp = sum(0:nchildren)
1282-
testreduction(sum,pipe,Unsorted(),res_exp)
1296+
testreduction(sum,pipe,Unsorted(),res_exp,2)
1297+
testreduction(sum,pipe,Unsorted(),res_exp,0)
12831298
end
12841299
@testset "Sorted" begin
12851300
pipe = BranchChannel{pval,pval}(myid(),nchildren)
@@ -1291,6 +1306,13 @@ end;
12911306
testreduction(sum,pipe,Sorted(),res_exp)
12921307
end
12931308
end
1309+
1310+
# The top tree must have children by definition
1311+
pipe = BranchChannel{Int,Int}(myid(),0)
1312+
putselfchildren!(pipe,Unsorted(),0)
1313+
err = ErrorException("nodes with rank <=0 must have children")
1314+
@test_throws err reducedvalue(sum,0,pipe,Unsorted())
1315+
clearerrors!(pipe,0)
12941316
end
12951317

12961318
@testset "reduceTreeNode" begin
@@ -1362,6 +1384,10 @@ end;
13621384
progress = RemoteChannel(()->Channel{rettype}(1))
13631385
ParallelUtilities.indicatereduceprogress!(progress,10)
13641386
@test take!(progress) == (false,true,10)
1387+
1388+
@test isnothing(ParallelUtilities.indicatefailure!(nothing,1))
1389+
ParallelUtilities.indicatefailure!(progress,10)
1390+
@test take!(progress) == (false,false,10)
13651391
end
13661392
end;
13671393

@@ -1731,6 +1757,25 @@ end;
17311757
strexp = "could not find the task $((5,)) in the list $((1:4,))"
17321758
@test String(take!(io)) == strexp
17331759
end
1760+
1761+
@testset "BranchChannel" begin
1762+
io = IOBuffer()
1763+
1764+
b = BranchChannel{Any,Any}(1,0)
1765+
show(io,b)
1766+
strexp = "Leaf : 1 ← 1"
1767+
@test String(take!(io)) == strexp
1768+
1769+
b = BranchChannel{Any,Any}(1,1)
1770+
show(io,b)
1771+
strexp = "Branch: 1 ← 1 ← 1 child"
1772+
@test String(take!(io)) == strexp
1773+
1774+
b = BranchChannel{Any,Any}(1,2)
1775+
show(io,b)
1776+
strexp = "Branch: 1 ← 1 ⇇ 2 children"
1777+
@test String(take!(io)) == strexp
1778+
end
17341779
end;
17351780

17361781
end; # wrapper

0 commit comments

Comments
 (0)