Skip to content

Commit

Permalink
apply JuliaFormatter, and add github action for PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Apr 24, 2022
1 parent afed808 commit b1c5115
Show file tree
Hide file tree
Showing 13 changed files with 492 additions and 419 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/format_pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: format-pr
on:
schedule:
- cron: '0 0 * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install JuliaFormatter and format
run: |
julia -e 'import Pkg; Pkg.add("JuliaFormatter")'
julia -e 'using JuliaFormatter; format("."; style=BlueStyle())'
# https://github.com/marketplace/actions/create-pull-request
# https://github.com/peter-evans/create-pull-request#reference-example
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: Format .jl files
title: 'Automatic JuliaFormatter.jl run'
branch: auto-juliaformatter-pr
delete-branch: true
labels: formatting, automated pr, no changelog
- name: Check outputs
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
16 changes: 7 additions & 9 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using Documenter, DiskArrays

makedocs(
modules = [DiskArrays],
format = Documenter.HTML(; prettyurls = get(ENV, "CI", nothing) == "true"),
authors = "Fabian Gans",
sitename = "DiskArrays.jl",
pages = Any["index.md"]
makedocs(;
modules=[DiskArrays],
format=Documenter.HTML(; prettyurls=get(ENV, "CI", nothing) == "true"),
authors="Fabian Gans",
sitename="DiskArrays.jl",
pages=Any["index.md"],
# strict = true,
# clean = true,
# checkdocs = :exports,
)

deploydocs(
repo = "github.com/meggart/DiskArrays.jl.git",
)
deploydocs(; repo="github.com/meggart/DiskArrays.jl.git")
33 changes: 21 additions & 12 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,32 @@ macro implement_array_methods(t)
Base.copyto!(dest::$t, source::AbstractArray) = DiskArrays._copyto!(dest, source)
Base.copyto!(dest::AbstractArray, source::$t) = DiskArrays._copyto!(dest, source)
Base.copyto!(dest::$t, source::$t) = DiskArrays._copyto!(dest, source)
function Base.copyto!(dest::$t, Rdest::CartesianIndices, src::AbstractArray, Rsrc::CartesianIndices)
DiskArrays._copyto!(dest, Rdest, src, Rsrc)
function Base.copyto!(
dest::$t, Rdest::CartesianIndices, src::AbstractArray, Rsrc::CartesianIndices
)
return DiskArrays._copyto!(dest, Rdest, src, Rsrc)
end
function Base.copyto!(dest::AbstractArray, Rdest::CartesianIndices, src::$t, Rsrc::CartesianIndices)
DiskArrays._copyto!(dest, Rdest, src, Rsrc)
function Base.copyto!(
dest::AbstractArray, Rdest::CartesianIndices, src::$t, Rsrc::CartesianIndices
)
return DiskArrays._copyto!(dest, Rdest, src, Rsrc)
end
function Base.copyto!(dest::$t, Rdest::CartesianIndices, src::$t, Rsrc::CartesianIndices)
DiskArrays._copyto!(dest, Rdest, src, Rsrc)
function Base.copyto!(
dest::$t, Rdest::CartesianIndices, src::$t, Rsrc::CartesianIndices
)
return DiskArrays._copyto!(dest, Rdest, src, Rsrc)
end
Base.reverse(a::$t, dims=:) = DiskArrays._reverse(a, dims)
# Here we extend the unexported `_replace` method, but we replicate
# much less Base functionality by extending it rather than `replace`.
function Base._replace!(new::Base.Callable, res::AbstractArray, A::$t, count::Int)
DiskArrays._replace!(new, res, A, count)
return DiskArrays._replace!(new, res, A, count)
end
function Base._replace!(new::Base.Callable, res::$t, A::AbstractArray, count::Int)
DiskArrays._replace!(new, res, A, count)
return DiskArrays._replace!(new, res, A, count)
end
function Base._replace!(new::Base.Callable, res::$t, A::$t, count::Int)
DiskArrays._replace!(new, res, A, count)
return DiskArrays._replace!(new, res, A, count)
end
end
end
Expand All @@ -37,7 +43,9 @@ function _Array(a::AbstractArray{T,N}) where {T,N}
end

# Use broadcast to copy
_copyto!(dest::AbstractArray{<:Any,N}, source::AbstractArray{<:Any,N}) where N = dest .= source
function _copyto!(dest::AbstractArray{<:Any,N}, source::AbstractArray{<:Any,N}) where {N}
return dest .= source
end
function _copyto!(dest::AbstractArray, source::AbstractArray)
# TODO make this more specific so we are reshaping the Non-DiskArray more often.
reshape(dest, size(source)) .= source
Expand All @@ -59,6 +67,7 @@ end
# Use broadcast instead of a loop.
# The `count` argument is disallowed as broadcast is not sequential.
function _replace!(new, res::AbstractArray, A::AbstractArray, count::Int)
count < length(res) && throw(ArgumentError("`replace` on DiskArrays objects cannot use a count value"))
broadcast!(new, res, A)
count < length(res) &&
throw(ArgumentError("`replace` on DiskArrays objects cannot use a count value"))
return broadcast!(new, res, A)
end
46 changes: 25 additions & 21 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,57 @@ import Base.Broadcast: Broadcasted, AbstractArrayStyle, DefaultArrayStyle, flatt

struct ChunkStyle{N} <: Base.Broadcast.AbstractArrayStyle{N} end

Base.BroadcastStyle(::ChunkStyle{N}, ::ChunkStyle{M}) where {N, M}= ChunkStyle{max(N, M)}()
Base.BroadcastStyle(::ChunkStyle{N}, ::DefaultArrayStyle{M}) where {N, M}= ChunkStyle{max(N, M)}()
Base.BroadcastStyle(::DefaultArrayStyle{M}, ::ChunkStyle{N}) where {N, M}= ChunkStyle{max(N, M)}()

Base.BroadcastStyle(::ChunkStyle{N}, ::ChunkStyle{M}) where {N,M} = ChunkStyle{max(N, M)}()
function Base.BroadcastStyle(::ChunkStyle{N}, ::DefaultArrayStyle{M}) where {N,M}
return ChunkStyle{max(N, M)}()
end
function Base.BroadcastStyle(::DefaultArrayStyle{M}, ::ChunkStyle{N}) where {N,M}
return ChunkStyle{max(N, M)}()
end

struct BroadcastDiskArray{T,N,BC<:Broadcasted{<:ChunkStyle{N}}} <: AbstractDiskArray{T,N}
bc::BC
end
function BroadcastDiskArray(bcf::B) where {B<:Broadcasted{<:ChunkStyle{N}}} where N
function BroadcastDiskArray(bcf::B) where {B<:Broadcasted{<:ChunkStyle{N}}} where {N}
ElType = Base.Broadcast.combine_eltypes(bcf.f, bcf.args)
BroadcastDiskArray{ElType, N, B}(bcf)
return BroadcastDiskArray{ElType,N,B}(bcf)
end

# Base methods

Base.size(bc::BroadcastDiskArray) = size(bc.bc)
function DiskArrays.readblock!(a::BroadcastDiskArray, aout, i::OrdinalRange...)
argssub = map(arg->subsetarg(arg, i), a.bc.args)
aout .= a.bc.f.(argssub...)
argssub = map(arg -> subsetarg(arg, i), a.bc.args)
return aout .= a.bc.f.(argssub...)
end
Base.broadcastable(bc::BroadcastDiskArray) = bc.bc
function Base.copy(bc::Broadcasted{ChunkStyle{N}}) where N
BroadcastDiskArray(flatten(bc))
function Base.copy(bc::Broadcasted{ChunkStyle{N}}) where {N}
return BroadcastDiskArray(flatten(bc))
end
Base.copy(a::BroadcastDiskArray) = copyto!(zeros(eltype(a), size(a)), a.bc)
function Base.copyto!(dest::AbstractArray, bc::Broadcasted{ChunkStyle{N}}) where N
function Base.copyto!(dest::AbstractArray, bc::Broadcasted{ChunkStyle{N}}) where {N}
bcf = flatten(bc)
gcd = common_chunks(size(bcf), dest, bcf.args...)
foreach(gcd) do cnow
# Possible optimization would be to use a LRU cache here, so that data has not
# to be read twice in case of repeating indices
argssub = map(i->subsetarg(i, cnow), bcf.args)
argssub = map(i -> subsetarg(i, cnow), bcf.args)
dest[cnow...] .= bcf.f.(argssub...)
end
dest
return dest
end

# DiskArrays interface

haschunks(a::BroadcastDiskArray) = Chunked()
function eachchunk(a::BroadcastDiskArray)
common_chunks(size(a.bc), a.bc.args...)
return common_chunks(size(a.bc), a.bc.args...)
end
function common_chunks(s, args...)
N = length(s)
chunkedars = filter(i->haschunks(i)===Chunked(), collect(args))
all(ar->isa(eachchunk(ar), GridChunks), chunkedars) || error("Currently only chunks of type GridChunks can be merged by broadcast")
chunkedars = filter(i -> haschunks(i) === Chunked(), collect(args))
all(ar -> isa(eachchunk(ar), GridChunks), chunkedars) ||
error("Currently only chunks of type GridChunks can be merged by broadcast")
if isempty(chunkedars)
totalsize = sum(sizeof eltype, args)
return estimate_chunksize(s, totalsize)
Expand All @@ -62,7 +66,7 @@ function common_chunks(s, args...)
end
isempty(csnow) && return RegularChunks(1, 0, s[n])
cs = first(csnow).chunks[n]
if all(s -> s.chunks[n] == cs, csnow)
if all(s -> s.chunks[n] == cs, csnow)
return cs
else
return merge_chunks(csnow, n)
Expand All @@ -83,7 +87,7 @@ function merge_chunks(csnow, n)
while true
# Get the largest chunk end point
cur_chunks = map(chpos, csnow) do i, ch
ch.chunks[n][i]
ch.chunks[n][i]
end
chend = maximum(last.(cur_chunks))
# @show chpos chend# cur_chunks
Expand Down Expand Up @@ -111,13 +115,13 @@ end
subsetarg(x, a) = x
function subsetarg(x::AbstractArray, a)
ashort = maybeonerange(size(x), a)
view(x, ashort...) # Maybe making a copy here would be faster, need to check...
return view(x, ashort...) # Maybe making a copy here would be faster, need to check...
end
repsingle(s, r) = s==1 ? (1:1) : r
repsingle(s, r) = s == 1 ? (1:1) : r
function maybeonerange(out, sizes, ranges)
s1, sr = splittuple(sizes...)
r1, rr = splittuple(ranges...)
maybeonerange((out..., repsingle(s1, r1)), sr, rr)
return maybeonerange((out..., repsingle(s1, r1)), sr, rr)
end
maybeonerange(out, ::Tuple{}, ranges) = out
maybeonerange(sizes, ranges) = maybeonerange((), sizes, ranges)
Expand Down
34 changes: 16 additions & 18 deletions src/chunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ end

# Base methods

function Base.getindex(r::RegularChunks, i::Int)
function Base.getindex(r::RegularChunks, i::Int)
@boundscheck checkbounds(r, i)
max((i - 1) * r.cs + 1 - r.offset, 1):min(i * r.cs - r.offset, r.s)
return max((i - 1) * r.cs + 1 - r.offset, 1):min(i * r.cs - r.offset, r.s)
end
Base.size(r::RegularChunks, _) = div(r.s + r.offset - 1, r.cs) + 1
Base.size(r::RegularChunks) = (size(r, 1), )
Base.size(r::RegularChunks) = (size(r, 1),)

# DiskArrays interface

Expand All @@ -42,17 +42,16 @@ function subsetchunks(r::RegularChunks, subs::AbstractUnitRange)
end
function subsetchunks(r::RegularChunks, subs::AbstractRange)
# This is a method only to make "reverse" work and should error for all other cases
if step(subs) == -1 && first(subs)==r.s && last(subs)==1
if step(subs) == -1 && first(subs) == r.s && last(subs) == 1
lastlen = length(last(r))
newoffset = r.cs-lastlen
newoffset = r.cs - lastlen
return RegularChunks(r.cs, newoffset, r.s)
end
end
approx_chunksize(r::RegularChunks) = r.cs
grid_offset(r::RegularChunks) = r.offset
max_chunksize(r::RegularChunks) = r.cs


"""
IrregularChunks <: ChunkType
Expand All @@ -75,33 +74,32 @@ end

# Base methods

function Base.getindex(r::IrregularChunks, i::Int)
function Base.getindex(r::IrregularChunks, i::Int)
@boundscheck checkbounds(r, i)
return (r.offsets[i] + 1):r.offsets[i + 1]
end
Base.size(r::IrregularChunks) = (length(r.offsets) - 1, )
Base.size(r::IrregularChunks) = (length(r.offsets) - 1,)

# DiskArrays interface

function subsetchunks(r::IrregularChunks, subs::UnitRange)
c1 = searchsortedfirst(r.offsets, first(subs)) - 1
c2 = searchsortedfirst(r.offsets, last(subs))
offsnew = r.offsets[c1:c2]
firstoffset = first(subs)-r.offsets[c1] - 1
firstoffset = first(subs) - r.offsets[c1] - 1
offsnew[end] = last(subs)
offsnew[2:end] .= offsnew[2:end] .- firstoffset
offsnew .= offsnew .- first(offsnew)
return IrregularChunks(offsnew)
end
function approx_chunksize(r::IrregularChunks)
round(Int, sum(diff(r.offsets)) / (length(r.offsets) - 1))
return round(Int, sum(diff(r.offsets)) / (length(r.offsets) - 1))
end
grid_offset(r::IrregularChunks) = 0
max_chunksize(r::IrregularChunks) = maximum(diff(r.offsets))


struct GridChunks{N} <: AbstractArray{NTuple{N,UnitRange{Int64}},N}
chunks::Tuple{Vararg{ChunkType, N}}
chunks::Tuple{Vararg{ChunkType,N}}
end
GridChunks(ct::ChunkType...) = GridChunks(ct)
GridChunks(a, chunksize; offset=(_ -> 0).(size(a))) = GridChunks(size(a), chunksize; offset)
Expand All @@ -114,7 +112,7 @@ end

# Base methods

function Base.getindex(g::GridChunks{N}, i::Vararg{Int,N}) where N
function Base.getindex(g::GridChunks{N}, i::Vararg{Int,N}) where {N}
@boundscheck checkbounds(g, i...)
return getindex.(g.chunks, i)
end
Expand Down Expand Up @@ -161,7 +159,7 @@ const fallback_element_size = Ref(100)
# be over-ridden by the package that implements the interface

function eachchunk(a::AbstractArray)
estimate_chunksize(a)
return estimate_chunksize(a)
end

# Chunked trait
Expand All @@ -179,7 +177,7 @@ Returns the approximate size of an element of a in bytes. This falls back to cal
the element type or to the value stored in `DiskArrays.fallback_element_size`. Methods can be added for
custom containers.
"""
function element_size(a::AbstractArray)
function element_size(a::AbstractArray)
if isbitstype(eltype(a))
return sizeof(eltype(a))
elseif isbitstype(Base.nonmissingtype(eltype(a)))
Expand All @@ -194,12 +192,12 @@ estimate_chunksize(a::AbstractArray) = estimate_chunksize(size(a), element_size(
function estimate_chunksize(s, si)
ii = searchsortedfirst(cumprod(collect(s)), default_chunk_size[] * 1e6 / si)
cs = ntuple(length(s)) do idim
if idim<ii
if idim < ii
return s[idim]
elseif idim>ii
elseif idim > ii
return 1
else
sbefore = idim == 1 ? 1 : prod(s[1:idim - 1])
sbefore = idim == 1 ? 1 : prod(s[1:(idim - 1)])
return floor(Int, default_chunk_size[] * 1e6 / si / sbefore)
end
end
Expand Down
Loading

0 comments on commit b1c5115

Please sign in to comment.