Skip to content

Commit

Permalink
Merge pull request #24 from AtilaSaraiva/abstractArrays
Browse files Browse the repository at this point in the history
AbstractArrays and non-unit strides
  • Loading branch information
milankl authored Jul 9, 2024
2 parents 448913f + 35de3a8 commit 387fc5b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/zfp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ end
ZfpField(field::Ptr) = unsafe_load(Ptr{ZfpField}(field))

"""Pass a 1-D array into a zfp_field in C, in Julia only as Ptr{Cvoid}."""
function zfp_field(A::Array{T,1}) where {T}
function zfp_field(A::AbstractArray{T,1}) where {T}
n = length(A)
field = ccall((:zfp_field_1d, libzfp), Ptr{Cvoid},
(Ptr{Cvoid}, Clong, Cuint), A, zfp_type(T), n)
Expand All @@ -67,7 +67,7 @@ function zfp_field(A::Array{T,1}) where {T}
end

"""Pass a 2-D array into a zfp_field in C, in Julia only as Ptr{Cvoid}."""
function zfp_field(A::Array{T,2}) where {T}
function zfp_field(A::AbstractArray{T,2}) where {T}
nx, ny = size(A)
field = ccall((:zfp_field_2d, libzfp), Ptr{Cvoid},
(Ptr{Cvoid}, Clong, Cuint, Cuint), A, zfp_type(T), nx, ny)
Expand All @@ -77,7 +77,7 @@ function zfp_field(A::Array{T,2}) where {T}
end

"""Pass a 3-D array into a zfp_field in C, in Julia only as Ptr{Cvoid}."""
function zfp_field(A::Array{T,3}) where {T}
function zfp_field(A::AbstractArray{T,3}) where {T}
nx, ny, nz = size(A)
field = ccall((:zfp_field_3d, libzfp), Ptr{Cvoid},
(Ptr{Cvoid}, Clong, Cuint, Cuint, Cuint), A, zfp_type(T), nx, ny, nz)
Expand All @@ -88,7 +88,7 @@ function zfp_field(A::Array{T,3}) where {T}
end

"""Pass a 4-D array into a zfp_field in C, in Julia only as Ptr{Cvoid}."""
function zfp_field(A::Array{T,4}) where {T}
function zfp_field(A::AbstractArray{T,4}) where {T}
nx, ny, nz, nw = size(A)
field = ccall((:zfp_field_4d, libzfp), Ptr{Cvoid},
(Ptr{Cvoid}, Clong, Cuint, Cuint, Cuint, Cuint), A, zfp_type(T), nx, ny, nz, nw)
Expand Down Expand Up @@ -260,7 +260,7 @@ function zfp_stream_set_execution(stream::Ptr{Cvoid}, execution::Symbol)
elseif execution == :openmp
exec_policy = ZfpExecPolicy(1)
elseif execution == :cuda
# exec_policy = ZfpExecPolicy(2)
# exec_policy = ZfpExecPolicy(2)
throw("CUDA currently unsupported for ZfpCompression.jl.")
else
throw("Execution $execution unsupported.")
Expand Down Expand Up @@ -326,7 +326,7 @@ function zfp_compress(src::AbstractArray{T};
end

function zfp_decompress!(dest::AbstractArray{T},
src::Vector{UInt8};
src::AbstractVector{UInt8};
kws...) where {T<:Union{Int32,Int64,Float32,Float64}}

ndims = length(size(dest))
Expand Down Expand Up @@ -354,7 +354,7 @@ function zfp_decompress!(dest::AbstractArray{T},
return nothing
end

function zfp_decompress(src::Vector{UInt8})
function zfp_decompress(src::AbstractVector{UInt8})

field = zfp_field_alloc()
bitstream = stream_open(pointer(src), length(src))
Expand Down
32 changes: 30 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,37 @@ using Test
for T in (Float32,Float64)
# test 1-4D
for sizes in [(100,),(100,50),(20,30,40),(20,20,20,20)]

# for arrays
A = rand(T,sizes...)
Ac = zfp_compress(A)
Ad = zfp_decompress(Ac)
@test Ad == A

# for views but with unit stride
A_view = reshape(view(A, :), size(A))
Ac_view = zfp_compress(A_view)
Ad_view = zfp_decompress(view(Ac,:))

@test Ac == Ac_view
@test Ad == Ad_view == A
end

# non-unit strides
for (size,stride) in zip(((100,),(100,100),(100,100,100),(20,30,40,50)),
((1:2:100,), (1:2:100,1:3:100), (1:2:100,1:3:100,1:4:100), (1:2:20, 1:3:30, 1:4:40,1:5:50)))

A = rand(T,size...)
A_view = view(A,stride...)
Ac = zfp_compress(A_view)
Ad = zfp_decompress(Ac)
@test A_view == Ad

# same array but collect the view, to check that exactly the same happens with/without view
A2 = collect(A_view)
Ac2 = zfp_compress(A2)
Ad2 = zfp_decompress(Ac2)
@test Ac2 == Ac
@test A2 == A_view == Ad2
end
end

Expand All @@ -19,7 +46,8 @@ using Test
A = rand(T,sizes...)
Ac = zfp_compress(A)
Ad = zfp_decompress(Ac)
@test Ad == A
Adviews = zfp_decompress(view(Ac,:))
@test Ad == Adviews == A
end
end
end
Expand Down

0 comments on commit 387fc5b

Please sign in to comment.