# Strange behavior of \`mapreduce\`

**URL:** <https://discourse.julialang.org/t/strange-behavior-of-mapreduce/17588>\
**Category:** GPU\
**Created:** [November 16, 2018, 7:20am UTC](https://discourse.julialang.org/t/strange-behavior-of-mapreduce/17588 "2018-11-16T07:20:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [November 16, 2018, 7:20am UTC](https://discourse.julialang.org/t/strange-behavior-of-mapreduce/17588/1 "2018-11-16T07:20:01Z")

</div>

I have a kernel, which uses the `exp` operation, it breaks down in a strange way

Minimum working example:

```julia
using CuArrays, CUDAnative
CuArrays.allowscalar(false)

x = randn(1000) |> cu
CUDAnative.map(CUDAnative.exp, x)
CUDAnative.reduce(+, x)

# works, but no kernel is evaluated
@device_code_warntype CUDAnative.mapreduce(exp, +, x) # no kernel evaluated

# the following code will cause Julia breaking down
@device_code_warntype CUDAnative.mapreduce(CUDAnative.exp, +, x)

```

Questions:

1. Why `mapreduce` can run without calling a kernel and `map` calls the kernel?
2. Is it possible to prevent Julia from breaking down when calling `CUDAnative.exp` directly (not on GPU) and other CUDA intrinsics? Relaunching Julia REPL or Atom can be frustrating.
3. How to make the kernel function reusable, i.e. switch between `CUDAnative.exp` and `Base.exp` without changing the kernel function?

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [November 16, 2018, 7:38am UTC](https://discourse.julialang.org/t/strange-behavior-of-mapreduce/17588/2 "2018-11-16T07:38:06Z")

</div>

```julia
julia> @device_code_warntype CUDAnative.mapreduce(CUDAnative.exp, +, x)
Invalid instruction at 0x7fcb18a3707c: 0x06, 0x00, 0x00, 0x00, 0xd0, 0x8b, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x8b, 0x27

signal (4): Illegal instruction

```

That indicates GPU code is being executed on the CPU, which means `mapreduce` for CuArrays triggers a CPU fallback (but without scalar indexing, so [https://github.com/JuliaGPU/CuArrays.jl/issues/180](https://github.com/JuliaGPU/CuArrays.jl/issues/180) would have been useful there).

Please open an issue on CuArrays. The entire CuArrays/GPUArrays `mapreduce` implementation needs a clean-up.

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [November 16, 2018, 7:49am UTC](https://discourse.julialang.org/t/strange-behavior-of-mapreduce/17588/3 "2018-11-16T07:49:11Z")

</div>

Thanks a lot for your explaination, may I also merge this issue  
[https://github.com/JuliaGPU/CuArrays.jl/issues/199](https://github.com/JuliaGPU/CuArrays.jl/issues/199)

into a list of improvements for `mapreduce`?
