From 01be2fb031ca822c0187e6493d7a9d030d19a35b Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 11 Sep 2024 13:00:58 +0200 Subject: [PATCH] Replace to_host by Array. (#226) --- NEWS.md | 2 +- src/array.jl | 31 ++++++++++++++----------------- test/array.jl | 12 +++++------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/NEWS.md b/NEWS.md index 295d5406..52d93de7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -23,7 +23,7 @@ Breaking changes: - Argument conversion has been removed; the user should make sure Julia arguments passed to kernels match the OpenCL argument types (i.e., no empty types, 4-element tuples for a 3-element `float3` arguments). - +- The `to_host` function has been replaced by simply calling `Array` on the `CLArray`. New features: diff --git a/src/array.jl b/src/array.jl index d18626ea..1d6c7131 100644 --- a/src/array.jl +++ b/src/array.jl @@ -1,6 +1,6 @@ import LinearAlgebra -export CLArray, CLMatrix, CLVector, to_host +export CLArray, CLMatrix, CLVector mutable struct CLArray{T, N} <: CLObject ctx::cl.Context @@ -66,26 +66,24 @@ Base.size(A::CLArray) = A.size Base.size(A::CLArray, dim::Integer) = A.size[dim] Base.ndims(A::CLArray) = length(size(A)) Base.length(A::CLArray) = prod(size(A)) -Base.:(==)(A:: CLArray, B:: CLArray) = - buffer(A) == buffer(B) && size(A) == size(B) +Base.:(==)(A:: CLArray, B:: CLArray) = buffer(A) == buffer(B) && size(A) == size(B) + function Base.reshape(A::CLArray, dims...) @assert prod(dims) == prod(size(A)) return copy(A, size=dims) end -## show - -Base.show(io::IO, A::CLArray{T,N}) where {T, N} = - print(io, "CLArray{$T,$N}($(buffer(A)),$(size(A)))") - -## to_host - -function to_host(A::CLArray{T,N}) where {T, N} +function Base.Array(A::CLArray{T,N}) where {T, N} hA = Array{T}(undef, size(A)...) copy!(hA, buffer(A)) return hA end +## show + +Base.show(io::IO, A::CLArray{T,N}) where {T, N} = + print(io, "CLArray{$T,$N}($(buffer(A)),$(size(A)))") + ## other array operations const TRANSPOSE_FLOAT_PROGRAM_PATH = joinpath(@__DIR__, "kernels", "transpose_float.cl") @@ -107,8 +105,8 @@ function LinearAlgebra.transpose!(B::CLMatrix{Float32}, A::CLMatrix{Float32}) block_size=block_size) h, w = size(A) lmem = cl.LocalMem(Float32, block_size * (block_size + 1)) - cl.set_args!(kernel, buffer(B), buffer(A), UInt32(h), UInt32(w), lmem) - return cl.enqueue_kernel(kernel, (h, w), (block_size, block_size)) + return cl.call(kernel, buffer(B), buffer(A), UInt32(h), UInt32(w), lmem; + global_size=(h, w), local_size=(block_size, block_size)) end """Transpose CLMatrix A""" @@ -128,10 +126,9 @@ function LinearAlgebra.transpose!(B::CLMatrix{Float64}, A::CLMatrix{Float64}) kernel = get_kernel(TRANSPOSE_DOUBLE_PROGRAM_PATH, "transpose", block_size=block_size) h, w = size(A) - # lmem = cl.LocalMem(Float64, block_size * (block_size + 1)) - lmem = cl.LocalMem(Float64, block_size * block_size) - cl.set_args!(kernel, buffer(B), buffer(A), UInt32(h), UInt32(w), lmem) - return cl.enqueue_kernel(kernel, (h, w), (block_size, block_size)) + lmem = cl.LocalMem(Float32, block_size * (block_size + 1)) + return cl.call(kernel, buffer(B), buffer(A), UInt32(h), UInt32(w), lmem; + global_size=(h, w), local_size=(block_size, block_size)) end """Transpose CLMatrix A""" diff --git a/test/array.jl b/test/array.jl index 486e03d6..ff9ccbbf 100644 --- a/test/array.jl +++ b/test/array.jl @@ -18,10 +18,10 @@ using LinearAlgebra end @testset "fill" begin - @test to_host(OpenCL.fill(Float32, Float32(0.5), - 32, 64)) == fill(Float32(0.5), 32, 64) - @test to_host(OpenCL.zeros(Float32, 64)) == zeros(Float32, 64) - @test to_host(OpenCL.ones(Float32, 64)) == ones(Float32, 64) + @test Array(OpenCL.fill(Float32, Float32(0.5), + 32, 64)) == fill(Float32(0.5), 32, 64) + @test Array(OpenCL.zeros(Float32, 64)) == zeros(Float32, 64) + @test Array(OpenCL.ones(Float32, 64)) == ones(Float32, 64) end @testset "core functions" begin @@ -35,10 +35,8 @@ using LinearAlgebra @test reshape(B, 128, 64) == A # transpose - X = CLArray(rand(Float32, 32, 32)) B = OpenCL.zeros(Float32, 64, 128) ev = transpose!(B, A) - cl.wait(ev) - #@test to_host(copy(A')) == to_host(B) + @test Array(A)' == Array(B) end end