Skip to content

Use multi-threading when calling mapreduce with multiple sources on CPU - #144

Merged
maleadt merged 2 commits into
JuliaGPU:mainfrom
utkinis:iu/mapreduce-cpu
Oct 27, 2026
Merged

maleadt merged 2 commits into
JuliaGPU:mainfrom
utkinis:iu/mapreduce-cpu

Conversation

@utkinis

@utkinis utkinis commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

mapreduce over several arrays (AK.mapreduce(f, op, a, b, c)) reduces a Broadcasted object. The threaded host reduction can't take views of a Broadcasted, so it ran these reductions on a single thread, whatever -t was.

This indexes the Broadcasted as a LinearBroadcast (from #185) instead. That's a vector, so it gets chunked across tasks like any other array. Each argument is indexed with its own linear index, so no Cartesian index is decoded per element. Indexing a 2-D Broadcasted with an Int is about 10× slower per element. A broadcast too long for LinearBroadcast's 32-bit index (over 2^32 elements) still runs serially.

Timings, before (a serial mapreduce over the Broadcasted) and after, measured in the same process. Ryzen 9 9950X, Julia 1.12, KA 0.10.3; minimum of 30 runs, on a shared machine:

threads before after
x * (y - z), 3 × 2^23 Float64 1 6.8 ms 5.1 ms
4 6.6 ms 4.2 ms
16 6.6 ms 4.0 ms
x * y, 2 × 2048×4096 Float64 1 8.4 ms 3.5 ms
4 8.4 ms 2.7 ms
16 8.3 ms 2.6 ms
sin(x) * cos(y), 2 × 2^23 Float64 1 71 ms 73 ms
4 68 ms 18 ms
16 70 ms 6.7 ms

The first two cases are limited by memory bandwidth from about two threads up; the third shows the scaling.

The PR also adds a test that runs a 3-D multi-source reduction across several tasks, and a multi-source case to benchmark/mapreduce_1d.jl.

utkinis and others added 2 commits October 26, 2026 20:19
A reduction over several arrays reduces a `Broadcasted` object, which has no
views, so the threaded host reduction ran it on a single thread. Index it as a
`LinearBroadcast` instead, which is a vector and gets chunked like any array,
without decoding a Cartesian index per element.

Co-authored-by: Tim Besard <tim.besard@gmail.com>
@maleadt
maleadt merged commit db2746b into JuliaGPU:main Oct 27, 2026
34 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants