From 0c2c2d401312cfbaf05b0278a21e9c70cb48c717 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Sun, 25 Oct 2020 14:28:02 -0500 Subject: [PATCH] Handle empty reductions (Closes gh-330) --- pyopencl/reduction.py | 8 +++++++- test/test_algorithm.py | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pyopencl/reduction.py b/pyopencl/reduction.py index 00995450..6eebe729 100644 --- a/pyopencl/reduction.py +++ b/pyopencl/reduction.py @@ -386,10 +386,16 @@ class ReductionKernel: else: allocator = repr_vec.allocator - if sz <= stage_inf.group_size*SMALL_SEQ_COUNT*MAX_GROUP_COUNT: + if sz == 0: + result = empty(use_queue, (), self.dtype_out, allocator=allocator) + group_count = 1 + seq_count = 0 + + elif sz <= stage_inf.group_size*SMALL_SEQ_COUNT*MAX_GROUP_COUNT: total_group_size = SMALL_SEQ_COUNT*stage_inf.group_size group_count = (sz + total_group_size - 1) // total_group_size seq_count = SMALL_SEQ_COUNT + else: group_count = MAX_GROUP_COUNT macrogroup_size = group_count*stage_inf.group_size diff --git a/test/test_algorithm.py b/test/test_algorithm.py index 660c7dfc..353af281 100644 --- a/test/test_algorithm.py +++ b/test/test_algorithm.py @@ -247,15 +247,21 @@ def test_sum(ctx_factory): slice(1000, -3000), slice(1000, None), slice(1000, None, 3), + slice(1000, 1000), ]: sum_a = np.sum(a[slc]) + if sum_a: + ref_divisor = abs(sum_a) + else: + ref_divisor = 1 + if slc.step is None: sum_a_gpu = cl_array.sum(a_gpu[slc]).get() - assert abs(sum_a_gpu - sum_a) / abs(sum_a) < 1e-4 + assert abs(sum_a_gpu - sum_a) / ref_divisor < 1e-4 sum_a_gpu_2 = cl_array.sum(a_gpu, slice=slc).get() - assert abs(sum_a_gpu_2 - sum_a) / abs(sum_a) < 1e-4 + assert abs(sum_a_gpu_2 - sum_a) / ref_divisor < 1e-4 def test_sum_without_data(ctx_factory): -- GitLab