Skip to content

Test correct backend in examples test #597

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "KernelAbstractions"
uuid = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
authors = ["Valentin Churavy <[email protected]> and contributors"]
version = "0.10.0-dev"
version = "0.9.35"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version = "0.9.35"
version = "0.10.0-dev"

Revert to 0.10.0-dev before merging


[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
37 changes: 18 additions & 19 deletions examples/histogram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@ include(joinpath(dirname(pathof(KernelAbstractions)), "../examples/utils.jl")) #

# Function to use as a baseline for CPU metrics
function create_histogram(input)
histogram_output = zeros(Int, maximum(input))
histogram_output = zeros(eltype(input), maximum(input))
for i in input
histogram_output[i] += 1
end
return histogram_output
end

# This a 1D histogram kernel where the histogramming happens on shmem
@kernel function histogram_kernel!(histogram_output, input)
tid = @index(Global, Linear)
@kernel unsafe_indices = true function histogram_kernel!(histogram_output, input)
gid = @index(Group, Linear)
lid = @index(Local, Linear)

@uniform warpsize = Int(32)

@uniform gs = @groupsize()[1]
@uniform gs = prod(@groupsize())
tid = (gid - 1) * gs + lid
@uniform N = length(histogram_output)

shared_histogram = @localmem Int (gs)
shared_histogram = @localmem eltype(input) (gs)

# This will go through all input elements and assign them to a location in
# shmem. Note that if there is not enough shem, we create different shmem
# blocks to write to. For example, if shmem is of size 256, but it's
# possible to get a value of 312, then we will have 2 separate shmem blocks,
# one from 1->256, and another from 256->512
@uniform max_element = 1
for min_element in 1:gs:N

# Setting shared_histogram to 0
Expand All @@ -42,7 +40,7 @@ end
end

# Defining bin on shared memory and writing to it if possible
bin = input[tid]
bin = tid <= length(input) ? input[tid] : 0
if bin >= min_element && bin < max_element
bin -= min_element - 1
@atomic shared_histogram[bin] += 1
Expand All @@ -58,10 +56,10 @@ end

end

function histogram!(histogram_output, input)
function histogram!(histogram_output, input, groupsize = 256)
backend = get_backend(histogram_output)
# Need static block size
kernel! = histogram_kernel!(backend, (256,))
kernel! = histogram_kernel!(backend, (groupsize,))
kernel!(histogram_output, input, ndrange = size(input))
return
end
Expand All @@ -74,9 +72,10 @@ function move(backend, input)
end

@testset "histogram tests" begin
rand_input = [rand(1:128) for i in 1:1000]
linear_input = [i for i in 1:1024]
all_two = [2 for i in 1:512]
# Use Int32 as some backends don't support 64-bit atomics
rand_input = Int32.(rand(1:128, 1000))
linear_input = Int32.(1:1024)
all_two = fill(Int32(2), 512)

histogram_rand_baseline = create_histogram(rand_input)
histogram_linear_baseline = create_histogram(linear_input)
Expand All @@ -86,14 +85,14 @@ end
linear_input = move(backend, linear_input)
all_two = move(backend, all_two)

rand_histogram = KernelAbstractions.zeros(backend, Int, 128)
linear_histogram = KernelAbstractions.zeros(backend, Int, 1024)
two_histogram = KernelAbstractions.zeros(backend, Int, 2)
rand_histogram = KernelAbstractions.zeros(backend, eltype(rand_input), Int(maximum(rand_input)))
linear_histogram = KernelAbstractions.zeros(backend, eltype(linear_input), Int(maximum(linear_input)))
two_histogram = KernelAbstractions.zeros(backend, eltype(all_two), Int(maximum(all_two)))

histogram!(rand_histogram, rand_input)
histogram!(rand_histogram, rand_input, 6)
histogram!(linear_histogram, linear_input)
histogram!(two_histogram, all_two)
KernelAbstractions.synchronize(CPU())
KernelAbstractions.synchronize(backend)

@test isapprox(Array(rand_histogram), histogram_rand_baseline)
@test isapprox(Array(linear_histogram), histogram_linear_baseline)
Expand Down
4 changes: 2 additions & 2 deletions examples/memcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function mycopy!(A, B)
return
end

A = KernelAbstractions.zeros(backend, Float64, 128, 128)
B = KernelAbstractions.ones(backend, Float64, 128, 128)
A = KernelAbstractions.zeros(backend, f_type, 128, 128)
B = KernelAbstractions.ones(backend, f_type, 128, 128)
mycopy!(A, B)
KernelAbstractions.synchronize(backend)
@test A == B
4 changes: 2 additions & 2 deletions examples/memcopy_static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function mycopy_static!(A, B)
return
end

A = KernelAbstractions.zeros(backend, Float64, 128, 128)
B = KernelAbstractions.ones(backend, Float64, 128, 128)
A = KernelAbstractions.zeros(backend, f_type, 128, 128)
B = KernelAbstractions.ones(backend, f_type, 128, 128)
mycopy_static!(A, B)
KernelAbstractions.synchronize(backend)
@test A == B
2 changes: 1 addition & 1 deletion examples/performant_matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Test
using Random
include(joinpath(dirname(pathof(KernelAbstractions)), "../examples/utils.jl")) # Load backend

const TILE_DIM = 32
const TILE_DIM = 16

@kernel function coalesced_matmul_kernel!(
output, @Const(input1), @Const(input2), N, R, M,
Expand Down
18 changes: 11 additions & 7 deletions examples/utils.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# EXCLUDE FROM TESTING
if Base.find_package("CUDA") !== nothing
using CUDA
using CUDA.CUDAKernels
const backend = CUDABackend()
CUDA.allowscalar(false)
else
const backend = CPU()
if !(@isdefined backend)
if Base.find_package("CUDA") !== nothing
using CUDA
using CUDA.CUDAKernels
const backend = CUDABackend()
CUDA.allowscalar(false)
else
const backend = CPU()
end
end

const f_type = KernelAbstractions.supports_float64(backend) ? Float64 : Float32
3 changes: 2 additions & 1 deletion test/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function find_sources(path::String, sources = String[])
return sources
end

function examples_testsuite(backend_str)
function examples_testsuite(backend, backend_str)
@testset "examples" begin
examples_dir = joinpath(@__DIR__, "..", "examples")
examples = find_sources(examples_dir)
Expand All @@ -21,6 +21,7 @@ function examples_testsuite(backend_str)
@testset "$(basename(example))" for example in examples
@eval module $(gensym())
backend_str = $backend_str
const backend = ($backend)()
include($example)
end
@test true
Expand Down
2 changes: 1 addition & 1 deletion test/testsuite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function testsuite(backend, backend_str, backend_mod, AT, DAT; skip_tests = Set{
end

@conditional_testset "Examples" skip_tests begin
examples_testsuite(backend_str)
examples_testsuite(backend, backend_str)
end

return
Expand Down
Loading