From 548177d433bb0359b79f2500b4e25e8a466c10be Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 13 Sep 2018 18:58:26 -0500 Subject: [PATCH 1/6] Support vector arguments with offset in scan kernels. --- pyopencl/scan.py | 22 ++++++++++++++++++---- test/test_algorithm.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/pyopencl/scan.py b/pyopencl/scan.py index 6a598081..ac8052e8 100644 --- a/pyopencl/scan.py +++ b/pyopencl/scan.py @@ -35,7 +35,8 @@ from pyopencl.tools import (dtype_to_ctype, bitlog2, KernelTemplateBase, _process_code_for_macro, get_arg_list_scalar_arg_dtypes, context_dependent_memoize, - _NumpyTypesKeyBuilder) + _NumpyTypesKeyBuilder, + get_arg_offset_adjuster_code) import pyopencl._mymako as mako from pyopencl._cluda import CLUDA_PREAMBLE @@ -148,6 +149,8 @@ void ${kernel_name}( %endif ) { + ${arg_offset_adjustment} + // index K in first dimension used for carry storage %if use_bank_conflict_avoidance: // Avoid bank conflicts by adding a single 32-bit value to the size of @@ -618,6 +621,8 @@ void ${name_prefix}_final_update( %endif ) { + ${arg_offset_adjustment} + %if use_lookbehind_update: LOCAL_MEM scan_type ldata[WG_SIZE]; %endif @@ -998,7 +1003,7 @@ class _GenericScanKernelBase(object): resulting in a C `bool` value that determines whether a new scan segments starts at index *i*. If given, makes the scan a segmented scan. Has access to the current index `i`, the result - of *input_expr* as a, and in addition may use *arguments* and + of *input_expr* as `a`, and in addition may use *arguments* and *input_fetch_expr* variables just like *input_expr*. If it returns true, then previous sums will not spill over into the @@ -1346,6 +1351,7 @@ class GenericScanKernel(_GenericScanKernelBase): final_update_src = str(final_update_tpl.render( wg_size=update_wg_size, output_statement=self.output_statement, + arg_offset_adjustment=get_arg_offset_adjuster_code(self.parsed_args), argument_signature=", ".join( arg.declarator() for arg in self.parsed_args), is_segment_start_expr=self.is_segment_start_expr, @@ -1421,6 +1427,7 @@ class GenericScanKernel(_GenericScanKernelBase): wg_size=wg_size, input_expr=input_expr, k_group_size=k_group_size, + arg_offset_adjustment=get_arg_offset_adjuster_code(arguments), argument_signature=", ".join(arg.declarator() for arg in arguments), is_segment_start_expr=is_segment_start_expr, input_fetch_exprs=input_fetch_exprs, @@ -1475,7 +1482,9 @@ class GenericScanKernel(_GenericScanKernelBase): from pyopencl.tools import VectorArg for arg_descr, arg_val in zip(self.parsed_args, args): if isinstance(arg_descr, VectorArg): - data_args.append(arg_val.data) + data_args.append(arg_val.base_data) + if arg_descr.with_offset: + data_args.append(arg_val.offset) else: data_args.append(arg_val) @@ -1583,6 +1592,8 @@ void ${name_prefix}_debug_scan( scan_type current = ${neutral}; scan_type prev; + ${arg_offset_adjustment} + for (index_type i = 0; i < N; ++i) { %for name, arg_name, ife_offset in input_fetch_exprs: @@ -1636,6 +1647,7 @@ class GenericDebugScanKernel(_GenericScanKernelBase): scan_tpl = _make_template(DEBUG_SCAN_TEMPLATE) scan_src = str(scan_tpl.render( output_statement=self.output_statement, + arg_offset_adjustment=get_arg_offset_adjuster_code(self.parsed_args), argument_signature=", ".join( arg.declarator() for arg in self.parsed_args), is_segment_start_expr=self.is_segment_start_expr, @@ -1680,7 +1692,9 @@ class GenericDebugScanKernel(_GenericScanKernelBase): from pyopencl.tools import VectorArg for arg_descr, arg_val in zip(self.parsed_args, args): if isinstance(arg_descr, VectorArg): - data_args.append(arg_val.data) + data_args.append(arg_val.base_data) + if arg_descr.with_offset: + data_args.append(arg_val.offset) else: data_args.append(arg_val) diff --git a/test/test_algorithm.py b/test/test_algorithm.py index 5264767c..d63ed788 100644 --- a/test/test_algorithm.py +++ b/test/test_algorithm.py @@ -569,6 +569,38 @@ def test_scan(ctx_factory, dtype, scan_cls): collect() +@pytest.mark.parametrize("scan_cls", (GenericScanKernel, GenericDebugScanKernel)) +def test_scan_with_vectorargs_with_offsets(ctx_factory, scan_cls): + context = ctx_factory() + queue = cl.CommandQueue(context) + + from pyopencl.tools import VectorArg + + knl = scan_cls( + context, float, + arguments=[ + VectorArg(float, "input", with_offset=True), + VectorArg(int, "segment", with_offset=True), + ], + input_expr="input[i]", + is_segment_start_expr="segment[i]", + scan_expr="a+b", neutral="0", + output_statement=""" + input[i] = item; + """) + + n = 20 + + host_data = np.random.randint(0, 10, n).astype(float) + dev_data = cl.array.to_device(queue, host_data) + segment_data = np.zeros(n, dtype=int) + dev_segment_data = cl.array.to_device(queue, segment_data) + + knl(dev_data, dev_segment_data) + + assert (dev_data.get() == np.cumsum(host_data)).all() + + def test_copy_if(ctx_factory): from pytest import importorskip importorskip("mako") @@ -654,7 +686,6 @@ def test_index_preservation(ctx_factory): context = ctx_factory() queue = cl.CommandQueue(context) - from pyopencl.scan import GenericScanKernel, GenericDebugScanKernel classes = [GenericScanKernel] dev = context.devices[0] -- GitLab From 749e6a3e8c69b4b676877e9f32489678554679b7 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 13 Sep 2018 22:21:31 -0500 Subject: [PATCH 2/6] Bump version --- pyopencl/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyopencl/version.py b/pyopencl/version.py index f46939df..e0ea93b6 100644 --- a/pyopencl/version.py +++ b/pyopencl/version.py @@ -1,3 +1,3 @@ -VERSION = (2018, 2) +VERSION = (2018, 2, 1) VERSION_STATUS = "" VERSION_TEXT = ".".join(str(x) for x in VERSION) + VERSION_STATUS -- GitLab From ea24cc5191a03818bb3e5457e0dcfe89c01a51a2 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 13 Sep 2018 22:27:34 -0500 Subject: [PATCH 3/6] Update history --- doc/misc.rst | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/doc/misc.rst b/doc/misc.rst index b4e30db7..f186998c 100644 --- a/doc/misc.rst +++ b/doc/misc.rst @@ -209,14 +209,22 @@ other software to be turned into the corresponding :mod:`pyopencl` objects. User-visible Changes ==================== -Version 2017.2 --------------- +Version 2018.2.1 +---------------- .. note:: This version is currently under development. You can get snapshots from PyOpenCL's `git repository `_ +* Support array arguments with offsets in scan kernels. + +Version 2018.2 +-------------- + +* Use pybind11. +* Many bug fixes. + Version 2018.1 -------------- -- GitLab From 0a370912baa68dac0152adf044cb11f5b68a19dd Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 13 Sep 2018 22:38:20 -0500 Subject: [PATCH 4/6] Revert "Bump version" This reverts commit 749e6a3e8c69b4b676877e9f32489678554679b7. --- pyopencl/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyopencl/version.py b/pyopencl/version.py index e0ea93b6..f46939df 100644 --- a/pyopencl/version.py +++ b/pyopencl/version.py @@ -1,3 +1,3 @@ -VERSION = (2018, 2, 1) +VERSION = (2018, 2) VERSION_STATUS = "" VERSION_TEXT = ".".join(str(x) for x in VERSION) + VERSION_STATUS -- GitLab From 16e207363783d08da490498dc9d5c5df5574e39d Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 13 Sep 2018 22:38:27 -0500 Subject: [PATCH 5/6] Revert "Update history" This reverts commit ea24cc5191a03818bb3e5457e0dcfe89c01a51a2. --- doc/misc.rst | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/doc/misc.rst b/doc/misc.rst index f186998c..b4e30db7 100644 --- a/doc/misc.rst +++ b/doc/misc.rst @@ -209,22 +209,14 @@ other software to be turned into the corresponding :mod:`pyopencl` objects. User-visible Changes ==================== -Version 2018.2.1 ----------------- +Version 2017.2 +-------------- .. note:: This version is currently under development. You can get snapshots from PyOpenCL's `git repository `_ -* Support array arguments with offsets in scan kernels. - -Version 2018.2 --------------- - -* Use pybind11. -* Many bug fixes. - Version 2018.1 -------------- -- GitLab From 9e8b638abe20f19967ca71550c1c49d8286941bd Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 13 Sep 2018 22:39:40 -0500 Subject: [PATCH 6/6] Update history, making the ongoing version 2018.2 --- doc/misc.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/misc.rst b/doc/misc.rst index b4e30db7..71aea792 100644 --- a/doc/misc.rst +++ b/doc/misc.rst @@ -209,7 +209,7 @@ other software to be turned into the corresponding :mod:`pyopencl` objects. User-visible Changes ==================== -Version 2017.2 +Version 2018.2 -------------- .. note:: @@ -217,6 +217,10 @@ Version 2017.2 This version is currently under development. You can get snapshots from PyOpenCL's `git repository `_ +* Use pybind11. +* Many bug fixes. +* Support arrays with offsets in scan kernels. + Version 2018.1 -------------- -- GitLab