Skip to content

Commit 8159f05

Browse files
authored
Merge pull request #63 from JuliaFolds2/size_for_scatter
size for RoundRobin()
2 parents 2a49f3e + a05045d commit 8159f05

6 files changed

Lines changed: 43 additions & 28 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ jobs:
1212
fail-fast: false
1313
matrix:
1414
version:
15-
- '1.6' # remove at some point in the future, if needed.
1615
- 'lts' # currently 1.10
1716
- '1' # latest stable 1.x release
1817
- 'pre'
1918
os:
2019
- ubuntu-latest
2120
- macOS-latest
2221
- windows-latest
23-
exclude:
24-
- version: '1.6'
25-
os: macOS-latest
2622
steps:
2723
- uses: actions/checkout@v4
2824
- uses: julia-actions/setup-julia@v2

.github/workflows/downgrade.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
version: ['1.6']
18+
version: ['1.10']
1919
steps:
2020
- uses: actions/checkout@v4
2121
- uses: julia-actions/setup-julia@v2

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
ChunkSplitters.jl Changelog
22
===========================
33

4-
Version 3.1.3-DEV
4+
Version 3.2.0-DEV
55
-------------
6+
- ![FEATURE][badge-feature] Implement `size` option for `split=RoundRobin()`.
67
- ![INFO][badge-info] Improve documentation of multithreading by adding `index_chunks` examples.
8+
- ![INFO][badge-info] Drop support for Julia 1.6 (Requires 1.10.0 now).
79

810
Version 3.1.2
911
-------------

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ Aqua = "0.8.5"
1010
BenchmarkTools = "1"
1111
Documenter = "1"
1212
OffsetArrays = "1"
13-
Test = "1.6"
13+
Test = "1.10"
1414
TestItemRunner = "1"
1515
TestItems = "1"
16-
julia = "1.6"
16+
julia = "1.10"
1717

1818
[extras]
1919
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"

src/internals.jl

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ Base.firstindex(::AbstractChunks) = 1
9494
Base.lastindex(c::AbstractChunks) = length(c)
9595

9696
Base.length(c::AbstractChunks{T,FixedCount,S}) where {T,S} = c.n
97-
Base.length(c::AbstractChunks{T,FixedSize,S}) where {T,S} = cld(length(c.collection), max(1, c.size))
97+
Base.length(c::AbstractChunks{T,FixedSize,Consecutive}) where {T} = cld(length(c.collection), max(1, c.size))
98+
function Base.length(c::AbstractChunks{T,FixedSize,RoundRobin}) where {T}
99+
l = length(c.collection)
100+
l == 0 && return 0
101+
s = min(l, max(1, c.size))
102+
return fld(l, s) + rem(l, s)
103+
end
98104

99105
Base.getindex(c::IndexChunks{T,C,S}, i::Int) where {T,C,S} = getchunkindices(c, i)
100106
Base.getindex(c::ViewChunks{T,C,S}, i::Int) where {T,C,S} = @view(c.collection[getchunkindices(c, i)])
@@ -158,7 +164,11 @@ function getchunkindices(c::AbstractChunks{T,C,S}, ichunk::Integer) where {T,C,S
158164
size = c.size
159165
l = length(c.collection)
160166
size = min(l, size) # handle size>length(c.collection)
161-
n = cld(l, size)
167+
if S == Consecutive
168+
n = cld(l, size)
169+
else # RoundRobin
170+
n = fld(l, size) + rem(l, size)
171+
end
162172
end
163173
else
164174
n = 0
@@ -192,7 +202,18 @@ function _getchunkindices(::Type{FixedSize}, ::Type{Consecutive}, collection, ic
192202
end
193203

194204
function _getchunkindices(::Type{FixedSize}, ::Type{RoundRobin}, collection, ichunk; size, kwargs...)
195-
throw(ArgumentError("split=RoundRobin() not yet supported in combination with size keyword argument."))
205+
l = length(collection)
206+
fi = firstindex(collection)
207+
step = fld(l, size)
208+
if ichunk <= step
209+
first = fi + ichunk - 1
210+
last = first + (size - 1) * step
211+
return first:step:last
212+
else
213+
k = ichunk - step
214+
pos = fi + step * size + k - 1
215+
return pos:step:pos
216+
end
196217
end
197218

198219
end # module

test/runtests.jl

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,14 @@ using TestItems: @testitem, @testsnippet
55

66
@testsnippet Testing begin
77
function test_index_chunks(; array_length, n, size, split, result)
8-
if n === nothing
9-
d, r = divrem(array_length, size)
10-
nchunks = d + (r != 0)
11-
elseif size === nothing
12-
nchunks = n
13-
else
14-
throw(ArgumentError("both n and size === nothing"))
15-
end
168
c = index_chunks(rand(Int, array_length); n=n, size=size, split=split)
9+
nchunks = length(c)
1710
ranges = [c[i] for i in 1:nchunks]
1811
all(ranges .== result)
1912
end
2013

2114
function sum_parallel(x, n, size, split, which)
22-
if n === nothing
23-
d, r = divrem(length(x), size)
24-
nchunks = d + (r != 0)
25-
elseif size === nothing
26-
nchunks = n
27-
else
28-
throw(ArgumentError("both n and size === nothing"))
29-
end
15+
nchunks = length(index_chunks(x; n=n, size=size, split=split))
3016
s = zeros(eltype(x), nchunks)
3117
if which == "index_chunks"
3218
Threads.@threads for (ichunk, range) in enumerate(index_chunks(x; n=n, size=size, split=split))
@@ -128,7 +114,17 @@ end
128114
@test collect.(index_chunks(x; n=3, split=RoundRobin())) == [[-1, 2, 5], [0, 3], [1, 4]]
129115

130116
# FixedSize
131-
@test_throws ArgumentError collect(index_chunks(1:10; size=2, split=RoundRobin())) # not supported (yet?)
117+
@test collect(index_chunks([1,2,3,4,5,6,7]; size=2, split=RoundRobin())) == [[1,4], [2,5], [3,6], [7]]
118+
@test collect(chunks(['a','b','c','d','e','f','g']; size=2, split=RoundRobin())) == [['a','d'], ['b','e'], ['c','f'], ['g']]
119+
@test collect(index_chunks(1:7; size=2, split=RoundRobin())) == [1:3:4, 2:3:5, 3:3:6, 7:3:7]
120+
@test collect(index_chunks(1:6; size=2, split=RoundRobin())) == [1:3:4, 2:3:5, 3:3:6]
121+
@test collect(index_chunks(1:10; size=3, split=RoundRobin())) == [1:3:7, 2:3:8, 3:3:9, 10:3:10]
122+
@test collect(index_chunks(1:9; size=3, split=RoundRobin())) == [1:3:7, 2:3:8, 3:3:9]
123+
x = OffsetArray(1:7, -1:5)
124+
@test collect(index_chunks(x; size=2, split=RoundRobin())) == [-1:3:2, 0:3:3, 1:3:4, 5:3:5]
125+
@test test_sum(; array_length=7, n=nothing, size=2, split=RoundRobin())
126+
@test test_sum(; array_length=15, n=nothing, size=4, split=RoundRobin())
127+
@test test_sum(; array_length=117, n=nothing, size=10, split=RoundRobin())
132128
end
133129

134130
@testitem "check input argument errors" begin

0 commit comments

Comments
 (0)