Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6' # remove at some point in the future, if needed.
- 'lts' # currently 1.10
- '1' # latest stable 1.x release
- 'pre'
os:
- ubuntu-latest
- macOS-latest
- windows-latest
exclude:
- version: '1.6'
os: macOS-latest
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/downgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
version: ['1.6']
version: ['1.10']
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
ChunkSplitters.jl Changelog
===========================

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

Version 3.1.2
-------------
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Aqua = "0.8.5"
BenchmarkTools = "1"
Documenter = "1"
OffsetArrays = "1"
Test = "1.6"
Test = "1.10"
TestItemRunner = "1"
TestItems = "1"
julia = "1.6"
julia = "1.10"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Expand Down
27 changes: 24 additions & 3 deletions src/internals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ Base.firstindex(::AbstractChunks) = 1
Base.lastindex(c::AbstractChunks) = length(c)

Base.length(c::AbstractChunks{T,FixedCount,S}) where {T,S} = c.n
Base.length(c::AbstractChunks{T,FixedSize,S}) where {T,S} = cld(length(c.collection), max(1, c.size))
Base.length(c::AbstractChunks{T,FixedSize,Consecutive}) where {T} = cld(length(c.collection), max(1, c.size))
function Base.length(c::AbstractChunks{T,FixedSize,RoundRobin}) where {T}
l = length(c.collection)
l == 0 && return 0
s = min(l, max(1, c.size))
return fld(l, s) + rem(l, s)
end

Base.getindex(c::IndexChunks{T,C,S}, i::Int) where {T,C,S} = getchunkindices(c, i)
Base.getindex(c::ViewChunks{T,C,S}, i::Int) where {T,C,S} = @view(c.collection[getchunkindices(c, i)])
Expand Down Expand Up @@ -158,7 +164,11 @@ function getchunkindices(c::AbstractChunks{T,C,S}, ichunk::Integer) where {T,C,S
size = c.size
l = length(c.collection)
size = min(l, size) # handle size>length(c.collection)
n = cld(l, size)
if S == Consecutive
n = cld(l, size)
else # RoundRobin
n = fld(l, size) + rem(l, size)
end
end
else
n = 0
Expand Down Expand Up @@ -192,7 +202,18 @@ function _getchunkindices(::Type{FixedSize}, ::Type{Consecutive}, collection, ic
end

function _getchunkindices(::Type{FixedSize}, ::Type{RoundRobin}, collection, ichunk; size, kwargs...)
throw(ArgumentError("split=RoundRobin() not yet supported in combination with size keyword argument."))
l = length(collection)
fi = firstindex(collection)
step = fld(l, size)
if ichunk <= step
first = fi + ichunk - 1
last = first + (size - 1) * step
return first:step:last
else
k = ichunk - step
pos = fi + step * size + k - 1
return pos:step:pos
end
end

end # module
30 changes: 13 additions & 17 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,14 @@ using TestItems: @testitem, @testsnippet

@testsnippet Testing begin
function test_index_chunks(; array_length, n, size, split, result)
if n === nothing
d, r = divrem(array_length, size)
nchunks = d + (r != 0)
elseif size === nothing
nchunks = n
else
throw(ArgumentError("both n and size === nothing"))
end
c = index_chunks(rand(Int, array_length); n=n, size=size, split=split)
nchunks = length(c)
ranges = [c[i] for i in 1:nchunks]
all(ranges .== result)
end

function sum_parallel(x, n, size, split, which)
if n === nothing
d, r = divrem(length(x), size)
nchunks = d + (r != 0)
elseif size === nothing
nchunks = n
else
throw(ArgumentError("both n and size === nothing"))
end
nchunks = length(index_chunks(x; n=n, size=size, split=split))
s = zeros(eltype(x), nchunks)
if which == "index_chunks"
Threads.@threads for (ichunk, range) in enumerate(index_chunks(x; n=n, size=size, split=split))
Expand Down Expand Up @@ -128,7 +114,17 @@ end
@test collect.(index_chunks(x; n=3, split=RoundRobin())) == [[-1, 2, 5], [0, 3], [1, 4]]

# FixedSize
@test_throws ArgumentError collect(index_chunks(1:10; size=2, split=RoundRobin())) # not supported (yet?)
@test collect(index_chunks([1,2,3,4,5,6,7]; size=2, split=RoundRobin())) == [[1,4], [2,5], [3,6], [7]]
@test collect(chunks(['a','b','c','d','e','f','g']; size=2, split=RoundRobin())) == [['a','d'], ['b','e'], ['c','f'], ['g']]
@test collect(index_chunks(1:7; size=2, split=RoundRobin())) == [1:3:4, 2:3:5, 3:3:6, 7:3:7]
@test collect(index_chunks(1:6; size=2, split=RoundRobin())) == [1:3:4, 2:3:5, 3:3:6]
@test collect(index_chunks(1:10; size=3, split=RoundRobin())) == [1:3:7, 2:3:8, 3:3:9, 10:3:10]
@test collect(index_chunks(1:9; size=3, split=RoundRobin())) == [1:3:7, 2:3:8, 3:3:9]
x = OffsetArray(1:7, -1:5)
@test collect(index_chunks(x; size=2, split=RoundRobin())) == [-1:3:2, 0:3:3, 1:3:4, 5:3:5]
@test test_sum(; array_length=7, n=nothing, size=2, split=RoundRobin())
@test test_sum(; array_length=15, n=nothing, size=4, split=RoundRobin())
@test test_sum(; array_length=117, n=nothing, size=10, split=RoundRobin())
end

@testitem "check input argument errors" begin
Expand Down
Loading