From b1a466f6d238e9f138bd2d37330b96e11cdba5f0 Mon Sep 17 00:00:00 2001 From: Dan Stahlke Date: Sat, 15 Feb 2025 22:44:53 -0800 Subject: [PATCH 1/2] reduce doesn't query src backend if backend specified (to support MappedArrays) --- src/reduce/mapreduce_1d.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/reduce/mapreduce_1d.jl b/src/reduce/mapreduce_1d.jl index ce623e9..910cd64 100644 --- a/src/reduce/mapreduce_1d.jl +++ b/src/reduce/mapreduce_1d.jl @@ -125,14 +125,14 @@ function mapreduce_1d( blocks = (len + num_per_block - 1) รท num_per_block if !isnothing(temp) - @argcheck get_backend(temp) === get_backend(src) + @argcheck get_backend(temp) === backend @argcheck eltype(temp) === typeof(init) @argcheck length(temp) >= blocks * 2 dst = temp else # Figure out type for destination dst_type = typeof(init) - dst = similar(src, dst_type, blocks * 2) + dst = KernelAbstractions.allocate(backend, dst_type, blocks * 2) end # Later the kernel will be compiled for views anyways, so use same types From 3acadd0dd55e2d85d032eb27b6a6fb98c466ca59 Mon Sep 17 00:00:00 2001 From: Dan Stahlke Date: Mon, 17 Feb 2025 12:12:51 -0800 Subject: [PATCH 2/2] unit tests for reduce_1d of range and mappedarray --- test/Project.toml | 1 + test/runtests.jl | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/test/Project.toml b/test/Project.toml index d77e276..cee97ca 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -2,6 +2,7 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c" +MappedArrays = "dbb5928d-eab1-5f90-85c2-b9b0edb7c900" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl index 53dfe27..4f01f87 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,6 +2,7 @@ import AcceleratedKernels as AK using KernelAbstractions using Test using Random +using MappedArrays import Pkg @@ -756,6 +757,12 @@ end max_tasks=16, min_elems=1000, ) + + # Range input + @test AK.sum(1234:100_000, backend) == sum(1234:100000) + + # MappedArrays input + @test AK.sum(mappedarray(x->x*x, 1234:100_000), backend) == sum(x->x*x, 1234:100000) end @@ -778,6 +785,7 @@ end end end + # Fuzzy correctness testing for _ in 1:100 for dims in 1:3 @@ -964,6 +972,12 @@ end v = array_from_host([Point(rand(Float32), rand(Float32)) for _ in 1:10_042]) temp = similar(v, Tuple{Float32, Float32}) f(v, temp) + + # Range input + @test AK.mapreduce(x->x*x, +, 1234:100_000, backend, init=Int64(10)) == sum(x->x*x, 1234:100000, init=Int64(10)) + + # MappedArrays input + @test AK.mapreduce(x->2*x, +, mappedarray(x->x*x, 1234:100_000), backend, init=Int64(10)) == sum(x->2*x*x, 1234:100000, init=Int64(10)) end