# CUDA.jl: Unexpected \`mapreduce\` error: threads per block exceed GPU limit (640 \> 512

**URL:** https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778
**Category:** GPU
**Tags:** question
**Created:** [August 22, 2025, 3:11pm UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778 "2025-08-22T15:11:00Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![ykkan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykkan/32/14541_2.png) [@ykkan](https://discourse.julialang.org/u/ykkan)
#### Post date: [August 22, 2025, 3:11pm UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778/1 "2025-08-22T15:11:00Z")

</div>

Hello everyone,

I am using `mapreduce` from CUDA.jl to compute the sum of function evaluations over an array of `SVector{7,T}` data.  
The code looks roughly like this:

```julia
function func(x1, x2, x3, x4, x5, x6, x7, x8, x9)
    # very complicated calculation
    out1 = ...
    out2 = ...
    out3 = ...
    return SVector{3, Float64}(out1, out2, out3)
end

data = CuArray(zeros(SVector{7, Float64}, 2048))

mapreduce(vec -> func(2.0, 3.0, vec...), +, data)

```

However, I get the following error:  
`ERROR: LoadError: Number of threads per block exceeds kernel limit (640 > 512).`

I am running this on our lab-maintained cluster. Interestingly, the same solver worked perfectly about a month ago, but now it suddenly fails with this error.

Does anyone know what might be causing this issue? Thanks.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [August 22, 2025, 6:29pm UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778/2 "2025-08-22T18:29:12Z")

</div>

are you on CUDA 13.0? maybe downgrade it?

---

<div class="post-metadata">

### Author: ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)
#### Post date: [August 25, 2025, 8:11am UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778/3 "2025-08-25T08:11:36Z")

</div>

Without being an expert, I’d guess your kernel uses too many GPU registers, which limits the maximum number of threads per block.

A quick suggestion would be to replace your `mapreduce` call with AcceleratedKernel’s [`mapreduce`](https://juliagpu.github.io/AcceleratedKernels.jl/stable/api/mapreduce/), which allows you to tune the number of threads per block (via the `block_size` parameter).

---

<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: [September 1, 2025, 11:14am UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778/4 "2025-09-01T11:14:05Z")

</div>

This looks like [Invalid kernel config generated by `mapreducedim!` with `SubArray` input and output · Issue #2863 · JuliaGPU/CUDA.jl · GitHub](https://github.com/JuliaGPU/CUDA.jl/issues/2863). I’ll take a look.

---

<div class="post-metadata">

### Author: ![ykkan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykkan/32/14541_2.png) [@ykkan](https://discourse.julialang.org/u/ykkan)
#### Post date: [September 5, 2025, 2:27pm UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778/5 "2025-09-05T14:27:07Z")

</div>

I am using CUDA 12.4.

---

<div class="post-metadata">

### Author: ![ykkan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykkan/32/14541_2.png) [@ykkan](https://discourse.julialang.org/u/ykkan)
#### Post date: [September 5, 2025, 2:30pm UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778/6 "2025-09-05T14:30:44Z")

</div>

That’s what I suspected as well. In the end, I ended up writing my own kernel function. Thanks for pointing out _AcceleratedKernel_. I wasn’t aware of it, and I’ll definitely take a look.

---

<div class="post-metadata">

### Author: ![ykkan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykkan/32/14541_2.png) [@ykkan](https://discourse.julialang.org/u/ykkan)
#### Post date: [September 5, 2025, 5:28pm UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778/7 "2025-09-05T17:28:11Z")

</div>

Thank you for taking a look. I saw the new commit. With the latest version (v5.9.0), `mapreduce` works for my case. However, I noticed it is significantly slower than before.

I also tested my own kernel, which uses `CUDA.reduce_block` at its core. The elapsed times were **780 μs** on v5.8.3 and **19.5 ms** on v5.9.0. Is this slowdown expected?

---

<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: [September 9, 2025, 10:54am UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778/8 "2025-09-09T10:54:10Z")

</div>

Some slowdown was expected, but that’s much too large. Can you open an issue?

---

<div class="post-metadata">

### Author: ![ykkan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ykkan/32/14541_2.png) [@ykkan](https://discourse.julialang.org/u/ykkan)
#### Post date: [September 16, 2025, 11:57am UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778/9 "2025-09-16T11:57:51Z")

</div>

Sorry for the late follow-up.

You suggested I open an issue last week, but it looks like this was already resolved at (if i understand it correct)  
[mapreduce: reinstate and fix block optimization (#2880)](https://github.com/JuliaGPU/CUDA.jl/commit/c929405e57acc1a31ed005958403f342185d4273).

I re-ran my script on different versions, and the elapsed times are now much closer:  
**780 μs** on v5.8.3 and **1.089 ms** on v5.9.0.

Do you still recommend that I open an issue, or can we consider this closed?

---

<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: [September 18, 2025, 11:27am UTC](https://discourse.julialang.org/t/cuda-jl-unexpected-mapreduce-error-threads-per-block-exceed-gpu-limit-640-512/131778/10 "2025-09-18T11:27:07Z")

</div>

A 25% regression is still problematic, but this may be a measuring artifact, as our CI benchmarks indicate no issue. If you have the time, please take a look with NSight Compute and if that does indeed show an issue (e.g. a suboptimal launch configuration), please file an issue.
