code: def test_segmented_scan(ctx_factory):
      from pytest import importorskip
  
      importorskip("mako")
  
      context = ctx_factory()
      queue = cl.CommandQueue(context)
  
      from pyopencl.tools import dtype_to_ctype
  
      dtype = np.int32
      ctype = dtype_to_ctype(dtype)
  
      # for is_exclusive in [False, True]:
      for is_exclusive in [True, False]:
          if is_exclusive:
              output_statement = "out[i] = prev_item"
          else:
              output_statement = "out[i] = item"
  
          knl = GenericScanKernel(
              context,
              dtype,
              arguments="__global %s *ary, __global char *segflags, "
              "__global %s *out" % (ctype, ctype),
              input_expr="ary[i]",
              scan_expr="across_seg_boundary ? b : (a+b)",
              neutral="0",
              is_segment_start_expr="segflags[i]",
              output_statement=output_statement,
              options=[],
          )
  
          np.set_printoptions(threshold=2000)
          from random import randrange
          from pyopencl.clrandom import rand as clrand
  
          for n in scan_test_counts:
              a_dev = clrand(queue, (n,), dtype=dtype, a=0, b=10)
              a = a_dev.get()
  
              if 10 <= n < 20:
                  seg_boundaries_values = [
                      [0, 9],
                      [0, 3],
                      [4, 6],
                  ]
              else:
                  seg_boundaries_values = []
                  for i in range(10):
                      seg_boundary_count = max(
                          2, min(100, randrange(0, int(0.4 * n)))
                      )
                      seg_boundaries = [
                          randrange(n)
                          for i in range(seg_boundary_count)
                      ]
                      if n >= 1029:
                          seg_boundaries.insert(0, 1028)
                      seg_boundaries.sort()
                      seg_boundaries_values.append(seg_boundaries)
  
              for seg_boundaries in seg_boundaries_values:
                  # print "BOUNDARIES", seg_boundaries
                  # print a
  
                  seg_boundary_flags = np.zeros(n, dtype=np.uint8)
                  seg_boundary_flags[seg_boundaries] = 1
                  seg_boundary_flags_dev = cl.array.to_device(
                      queue, seg_boundary_flags
                  )
  
                  seg_boundaries.insert(0, 0)
  
                  result_host = a.copy()
                  for i, seg_start in enumerate(seg_boundaries):
                      if i + 1 < len(seg_boundaries):
                          seg_end = seg_boundaries[i + 1]
                      else:
                          seg_end = None
  
                      if is_exclusive:
                          result_host[
                              seg_start + 1 : seg_end
                          ] = np.cumsum(a[seg_start:seg_end][:-1])
                          result_host[seg_start] = 0
                      else:
                          result_host[seg_start:seg_end] = np.cumsum(
                              a[seg_start:seg_end]
                          )
  
                  # print "REF", result_host
  
                  result_dev = cl.array.empty_like(a_dev)
                  knl(a_dev, seg_boundary_flags_dev, result_dev)
  
                  # print "RES", result_dev
                  is_correct = (result_dev.get() == result_host).all()
                  if not is_correct:
                      diff = result_dev.get() - result_host
                      print("RES-REF", diff)
                      print("ERRWHERE", np.where(diff))
                      print(n, list(seg_boundaries))
  
                  assert is_correct
                  from gc import collect
  
                  collect()
  
              print("%d excl:%s done" % (n, is_exclusive))
  
imports: import numpy
  import numpy.linalg
  import sys
  from pytools import memoize
  from test_array import general_clrand
  import pytest
  import pyopencl
  import pyopencl.array
  from pyopencl.tools import pytest_generate_tests_for_pyopencl
  from pyopencl.characterize import has_double_support, has_struct_arg_count_bug
  from pyopencl.scan import InclusiveScanKernel, ExclusiveScanKernel, GenericScanKernel, GenericDebugScanKernel
  from pyopencl.characterize import get_pocl_version
label_desc: Use image node_id 25 for calling a global function with example usage: test_segmented_scan(ctx_factory) without return types