Skip to content

Commit 624dcdf

Browse files
authored
make Distributed independent of indices starting at 1 (JuliaLang/julia#34886)
1 parent 530313d commit 624dcdf

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

src/macros.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,12 @@ function remotecall_eval(m::Module, pid::Int, ex)
241241
end
242242

243243

244-
# Statically split range [1,N] into equal sized chunks for np processors
245-
function splitrange(N::Int, np::Int)
246-
each = div(N,np)
247-
extras = rem(N,np)
244+
# Statically split range [firstIndex,lastIndex] into equal sized chunks for np processors
245+
function splitrange(firstIndex::Int, lastIndex::Int, np::Int)
246+
each, extras = divrem(lastIndex-firstIndex+1, np)
248247
nchunks = each > 0 ? np : extras
249248
chunks = Vector{UnitRange{Int}}(undef, nchunks)
250-
lo = 1
249+
lo = firstIndex
251250
for i in 1:nchunks
252251
hi = lo + each - 1
253252
if extras > 0
@@ -261,8 +260,7 @@ function splitrange(N::Int, np::Int)
261260
end
262261

263262
function preduce(reducer, f, R)
264-
N = length(R)
265-
chunks = splitrange(Int(N), nworkers())
263+
chunks = splitrange(Int(firstindex(R)), Int(lastindex(R)), nworkers())
266264
all_w = workers()[1:length(chunks)]
267265

268266
w_exec = Task[]
@@ -275,7 +273,7 @@ function preduce(reducer, f, R)
275273
end
276274

277275
function pfor(f, R)
278-
@async @sync for c in splitrange(length(R), nworkers())
276+
@async @sync for c in splitrange(Int(firstindex(R)), Int(lastindex(R)), nworkers())
279277
@spawnat :any f(R, first(c), last(c))
280278
end
281279
end

test/macros.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Test
2+
using Distributed
3+
using Distributed: splitrange
4+
5+
# testing function macros.jl:splitrange
6+
@test splitrange(1, 11, 1) == Array{UnitRange{Int64},1}([1:11])
7+
@test splitrange(0, 10, 1) == Array{UnitRange{Int64},1}([0:10])
8+
@test splitrange(-1, 9, 1) == Array{UnitRange{Int64},1}([-1:9])
9+
10+
@test splitrange(1, 11, 2) == Array{UnitRange{Int64},1}([1:6,7:11])
11+
@test splitrange(0, 10, 2) == Array{UnitRange{Int64},1}([0:5,6:10])
12+
@test splitrange(-1, 9, 2) == Array{UnitRange{Int64},1}([-1:4,5:9])
13+
14+
@test splitrange(1, 11, 3) == Array{UnitRange{Int64},1}([1:4,5:8,9:11])
15+
@test splitrange(0, 10, 3) == Array{UnitRange{Int64},1}([0:3,4:7,8:10])
16+
@test splitrange(-1, 9, 3) == Array{UnitRange{Int64},1}([-1:2,3:6,7:9])
17+
18+
@test splitrange(1, 3, 3) == Array{UnitRange{Int64},1}([1:1,2:2,3:3])
19+
@test splitrange(1, 3, 4) == Array{UnitRange{Int64},1}([1:1,2:2,3:3])
20+
@test splitrange(0, 2, 3) == Array{UnitRange{Int64},1}([0:0,1:1,2:2])
21+
@test splitrange(0, 2, 4) == Array{UnitRange{Int64},1}([0:0,1:1,2:2])
22+
@test splitrange(-1, 1, 3) == Array{UnitRange{Int64},1}([-1:-1,0:0,1:1])
23+
@test splitrange(-1, 1, 4) == Array{UnitRange{Int64},1}([-1:-1,0:0,1:1])
24+
25+
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
26+
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
27+
using .Main.OffsetArrays
28+
29+
oa=OffsetArray([-1,0], (-2,))
30+
@distributed for i in eachindex(oa)
31+
@test i <= 0
32+
end
33+
34+
# testing macros.jl:...
35+
# ... yet to be implemented
36+

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ if !success(pipeline(cmd; stdout=stdout, stderr=stderr)) && ccall(:jl_running_on
1111
error("Distributed test failed, cmd : $cmd")
1212
end
1313

14+
include("macros.jl")
1415
include("managers.jl")

0 commit comments

Comments
 (0)