prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Stream in the context-manager form is not used in `ElementwiseKernel` or `ReductionKernel`
  This is actually a bug reported back in #1695 that unfortunately went unnoticed. 
  
  In `examples/stream/map_reduce.py`, a list of streams was created for executing `cupy.matmul()` in parallel, which is backed by a `ReductionKernel` in this case: https://github.com/cupy/cupy/blob/1af22f57fda92ae35bde806d0c4d110faf4fed52/cupy/core/core.pyx#L2513-L2516
  However, inspecting the implementation I found that `ReductionKernel` only accepts an explicit `stream` argument; it does not pick up any current stream: https://github.com/cupy/cupy/blob/32718607a7808ec6bc3a24cf9231a9351f8fc95e/cupy/core/reduction.pxi#L396
  In other words, that example was misleading because those streams were not used at all and so all executions were serialized, as can be checked from nvprof + nvvp (see the circle in red):
  <img width="972" alt="螢幕快照 2019-10-03 上午11 24 27" src="https://user-images.githubusercontent.com/5534781/66140715-978bf180-e5d0-11e9-8228-f613a3eba6fd.png">
  
  The same bug also appears in `ElementwiseKernel`:
  https://github.com/cupy/cupy/blob/1af22f57fda92ae35bde806d0c4d110faf4fed52/cupy/core/_kernel.pyx#L537
  
  In my opinion, unlike `RawKernel` which is not used by any CuPy core functionalities, `ElementwiseKernel` and `ReductionKernel` should honor the current stream by checking the current stream pointer if no stream argument is explicitly given, since many CuPy functions like `cupy.matmul()` do not support passing in a stream. A similar approach is already adopted in the FFT module, see #2362.
  
  </issue>
  <code>
  [start of examples/stream/map_reduce.py]
  1 import cupy
  2 import time
  3 
  4 device = cupy.cuda.Device()
  5 memory_pool = cupy.cuda.MemoryPool()
  6 cupy.cuda.set_allocator(memory_pool.malloc)
  7 rand = cupy.random.generator.RandomState(seed=1)
  8 
  9 n = 10
  10 zs = []
  11 map_streams = []
  12 stop_events = []
  13 reduce_stream = cupy.cuda.stream.Stream()
  14 for i in range(n):
  15     map_streams.append(cupy.cuda.stream.Stream())
  16 
  17 start_time = time.time()
  18 
  19 # Map
  20 for stream in map_streams:
  21     with stream:
  22         x = rand.normal(size=(1, 1024 * 256))
  23         y = rand.normal(size=(1024 * 256, 1))
  24         z = cupy.matmul(x, y)
  25         zs.append(z)
  26     stop_event = stream.record()
  27     stop_events.append(stop_event)
  28 
  29 # Block the `reduce_stream` until all events occur. This does not block host.
  30 # This is not required when reduction is performed in the default (Stream.null)
  31 # stream unless streams are created with `non_blocking=True` flag.
  32 for i in range(n):
  33     reduce_stream.wait_event(stop_events[i])
  34 
  35 # Reduce
  36 with reduce_stream:
  37     z = sum(zs)
  38 
  39 device.synchronize()
  40 elapsed_time = time.time() - start_time
  41 print('elapsed time', elapsed_time)
  42 print('total bytes', memory_pool.total_bytes())
  43 
  44 # Free all blocks in the memory pool of streams
  45 for stream in map_streams:
  46     memory_pool.free_all_blocks(stream=stream)
  47 print('total bytes', memory_pool.total_bytes())
  48
  [end of examples/stream/map_reduce.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/examples/stream/map_reduce.py b/examples/stream/map_reduce.py
  --- a/examples/stream/map_reduce.py
  +++ b/examples/stream/map_reduce.py
  @@ -19,8 +19,8 @@
   # Map
   for stream in map_streams:
       with stream:
  -        x = rand.normal(size=(1, 1024 * 256))
  -        y = rand.normal(size=(1024 * 256, 1))
  +        x = rand.normal(size=(1, 1024**2))
  +        y = rand.normal(size=(1024**2, 1))
           z = cupy.matmul(x, y)
           zs.append(z)
       stop_event = stream.record()
  
verification_info: {"golden_diff": "diff --git a/examples/stream/map_reduce.py b/examples/stream/map_reduce.py\n--- a/examples/stream/map_reduce.py\n+++ b/examples/stream/map_reduce.py\n@@ -19,8 +19,8 @@\n # Map\n for stream in map_streams:\n     with stream:\n-        x = rand.normal(size=(1, 1024 * 256))\n-        y = rand.normal(size=(1024 * 256, 1))\n+        x = rand.normal(size=(1, 1024**2))\n+        y = rand.normal(size=(1024**2, 1))\n         z = cupy.matmul(x, y)\n         zs.append(z)\n     stop_event = stream.record()\n", "issue": "Stream in the context-manager form is not used in `ElementwiseKernel` or `ReductionKernel`\nThis is actually a bug reported back in #1695 that unfortunately went unnoticed. \r\n\r\nIn `examples/stream/map_reduce.py`, a list of streams was created for executing `cupy.matmul()` in parallel, which is backed by a `ReductionKernel` in this case: https://github.com/cupy/cupy/blob/1af22f57fda92ae35bde806d0c4d110faf4fed52/cupy/core/core.pyx#L2513-L2516\r\nHowever, inspecting the implementation I found that `ReductionKernel` only accepts an explicit `stream` argument; it does not pick up any current stream: https://github.com/cupy/cupy/blob/32718607a7808ec6bc3a24cf9231a9351f8fc95e/cupy/core/reduction.pxi#L396\r\nIn other words, that example was misleading because those streams were not used at all and so all executions were serialized, as can be checked from nvprof + nvvp (see the circle in red):\r\n<img width=\"972\" alt=\"\u87a2\u5e55\u5feb\u7167 2019-10-03 \u4e0a\u534811 24 27\" src=\"https://user-images.githubusercontent.com/5534781/66140715-978bf180-e5d0-11e9-8228-f613a3eba6fd.png\">\r\n\r\nThe same bug also appears in `ElementwiseKernel`:\r\nhttps://github.com/cupy/cupy/blob/1af22f57fda92ae35bde806d0c4d110faf4fed52/cupy/core/_kernel.pyx#L537\r\n\r\nIn my opinion, unlike `RawKernel` which is not used by any CuPy core functionalities, `ElementwiseKernel` and `ReductionKernel` should honor the current stream by checking the current stream pointer if no stream argument is explicitly given, since many CuPy functions like `cupy.matmul()` do not support passing in a stream. A similar approach is already adopted in the FFT module, see #2362.\n", "before_files": [{"content": "import cupy\nimport time\n\ndevice = cupy.cuda.Device()\nmemory_pool = cupy.cuda.MemoryPool()\ncupy.cuda.set_allocator(memory_pool.malloc)\nrand = cupy.random.generator.RandomState(seed=1)\n\nn = 10\nzs = []\nmap_streams = []\nstop_events = []\nreduce_stream = cupy.cuda.stream.Stream()\nfor i in range(n):\n    map_streams.append(cupy.cuda.stream.Stream())\n\nstart_time = time.time()\n\n# Map\nfor stream in map_streams:\n    with stream:\n        x = rand.normal(size=(1, 1024 * 256))\n        y = rand.normal(size=(1024 * 256, 1))\n        z = cupy.matmul(x, y)\n        zs.append(z)\n    stop_event = stream.record()\n    stop_events.append(stop_event)\n\n# Block the `reduce_stream` until all events occur. This does not block host.\n# This is not required when reduction is performed in the default (Stream.null)\n# stream unless streams are created with `non_blocking=True` flag.\nfor i in range(n):\n    reduce_stream.wait_event(stop_events[i])\n\n# Reduce\nwith reduce_stream:\n    z = sum(zs)\n\ndevice.synchronize()\nelapsed_time = time.time() - start_time\nprint('elapsed time', elapsed_time)\nprint('total bytes', memory_pool.total_bytes())\n\n# Free all blocks in the memory pool of streams\nfor stream in map_streams:\n    memory_pool.free_all_blocks(stream=stream)\nprint('total bytes', memory_pool.total_bytes())\n", "path": "examples/stream/map_reduce.py"}]}