Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Putting output before input in function arguments in some examples #475

Merged
merged 4 commits into from
Apr 29, 2024
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
16 changes: 8 additions & 8 deletions examples/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@ using KernelAbstractions, Test, Random
include(joinpath(dirname(pathof(KernelAbstractions)), "../examples/utils.jl")) # Load backend

# Simple kernel for matrix multiplication
@kernel function matmul_kernel!(a, b, c)
@kernel function matmul_kernel!(output, a, b)
i, j = @index(Global, NTuple)

# creating a temporary sum variable for matrix multiplication
tmp_sum = zero(eltype(c))
tmp_sum = zero(eltype(output))
for k = 1:size(a)[2]
tmp_sum += a[i,k] * b[k, j]
end

c[i,j] = tmp_sum
output[i,j] = tmp_sum
end

# Creating a wrapper kernel for launching with error checks
function matmul!(a, b, c)
function matmul!(output, a, b)
if size(a)[2] != size(b)[1]
println("Matrix size mismatch!")
return nothing
end
backend = KernelAbstractions.get_backend(a)
kernel! = matmul_kernel!(backend)
kernel!(a, b, c, ndrange=size(c))
kernel!(output, a, b, ndrange=size(output))
end

a = rand!(allocate(backend, Float32, 256, 123))
b = rand!(allocate(backend, Float32, 123, 45))
c = KernelAbstractions.zeros(backend, Float32, 256, 45)
output = KernelAbstractions.zeros(backend, Float32, 256, 45)

matmul!(a,b,c)
matmul!(output, a,b)
KernelAbstractions.synchronize(backend)

@test isapprox(c, a*b)
@test isapprox(output, a*b)
6 changes: 3 additions & 3 deletions examples/naive_transpose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include(joinpath(dirname(pathof(KernelAbstractions)), "../examples/utils.jl")) #

@kernel function naive_transpose_kernel!(a, b)
i, j = @index(Global, NTuple)
@inbounds b[i, j] = a[j, i]
@inbounds a[i, j] = b[j, i]
end

# create wrapper function to check inputs
Expand All @@ -24,8 +24,8 @@ end
res = 1024

# creating initial arrays
a = rand!(allocate(backend, Float32, res, res))
b = KernelAbstractions.zeros(backend, Float32, res, res)
b = rand!(allocate(backend, Float32, res, res))
a = KernelAbstractions.zeros(backend, Float32, res, res)

naive_transpose!(a,b)
KernelAbstractions.synchronize(backend)
Expand Down
Loading