Skip to content

mapreducedim! is super slow #352

Closed

Description

@simeonschaub

Reductions on CLArrays seem to be almost 100x slower than Base (This is with the pocl CPU backend):

julia> using OpenCL, pocl_jll

julia> X = rand(Float32, 1000, 1000);

julia> X′ = CLArray(X);

julia> @benchmark sum(X; dims = 1)
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (min … max):   58.380 μs … 922.617 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):      90.710 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   103.658 μs ±  35.143 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

        ▄▃▁ ▃▇█▃                                                 
  ▁▁▁▂▄█████████▆▆▆▅▅▄▄▃▂▃▃▃▃▄▃▃▂▂▂▂▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁ ▃
  58.4 μs          Histogram: frequency by time          212 μs <

 Memory estimate: 4.02 KiB, allocs estimate: 3.

julia> @benchmark OpenCL.synchronize(sum(X′; dims = 1))
BenchmarkTools.Trial: 653 samples with 1 evaluation per sample.
 Range (min … max):  5.585 ms … 12.908 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     7.424 ms              ┊ GC (median):    0.00%
 Time  (mean ± σ):   7.604 ms ±  1.126 ms  ┊ GC (mean ± σ):  0.00% ± 0.00%

       ▁▁▁▂▃▁▄█ ▁▇▇▃▄▄▄▃▄▂▃▂▃▁▁▃▁                             
  ▃▂▃▅▆██████████████████████████▇▆█▇▄▇▇█▆▅▇▄▁▄▅▄▄▃▃▅▄▃▂▂▂▂▃ ▅
  5.59 ms        Histogram: frequency by time        10.6 ms <

 Memory estimate: 22.52 KiB, allocs estimate: 247.

Is there any low-hanging fruit in terms of optimizations here?

Activity

  1. maleadt commented on Aug 5, 2025

    @maleadt
    Member

    That's curious, I've seen it outperform Base when doing some initial testing last year.

    The mapreduce implementation is taken from CUDA.jl, so given PoCL's 'portable performance' promise we shouldn't be seeing those kinds of slowdowns. If anybody wants to look into this, I'd try and see which parallelization method PoCL is using here (try forcing one using POCL_WORK_GROUP_METHOD) and see if the performance changes. There's definitely improvements to be made to our mapreduce implementation, but nothing that should yield a 100x speed-up.

  2. added
    performance
    on Aug 5, 2025
  3. simeonschaub commented on Aug 5, 2025

    @simeonschaub
    MemberAuthor

    Here are some results with different POCL_WORK_GROUP_METHODs:

    Default method (loopvec)

    julia> @benchmark OpenCL.synchronize(sum(X′; dims = 1))
    BenchmarkTools.Trial: 1298 samples with 1 evaluation per sample.
     Range (min … max):  2.214 ms …   5.297 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     3.794 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):   3.841 ms ± 295.490 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
                                     ▁▄▆▇██▆▄▂                     
      ▂▁▁▁▁▂▂▂▂▂▂▁▁▁▁▁▁▂▁▁▁▁▁▁▂▂▂▃▄▅▇██████████▆▆▅▅▅▄▄▄▃▄▃▃▃▃▃▂▂▂ ▃
      2.21 ms         Histogram: frequency by time        4.77 ms <
    
     Memory estimate: 22.19 KiB, allocs estimate: 248.

    cbs

    BenchmarkTools.Trial: 661 samples with 1 evaluation per sample.
     Range (min … max):  4.527 ms …  10.373 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     7.426 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):   7.548 ms ± 610.995 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
                                  ▁▁▅▄▇▇█▇▄▅▁▂                     
      ▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▂▃▁▅▅████████████▇▆█▆▇▅▆▅▄▄▄▃▄▃▄▃▂▂▃ ▄
      4.53 ms         Histogram: frequency by time        9.33 ms <
    
     Memory estimate: 22.19 KiB, allocs estimate: 248.
    

    loops

    BenchmarkTools.Trial: 1846 samples with 1 evaluation per sample.
     Range (min … max):  1.490 ms …   5.119 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     2.652 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):   2.698 ms ± 320.992 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
                             ▂▂▆▇█▇▇▅▅▅▃▃▂                         
      ▂▂▂▂▂▃▂▂▁▁▁▂▂▂▂▂▁▃▂▄▄▆▇██████████████▇▅▅▅▅▆▄▄▄▄▃▃▂▃▂▂▂▂▁▂▁▂ ▄
      1.49 ms         Histogram: frequency by time        3.76 ms <
    
     Memory estimate: 22.19 KiB, allocs estimate: 248.
    

    I also tried it with the cpu-minimal device

    cpu-minimal

    BenchmarkTools.Trial: 386 samples with 1 evaluation per sample.
     Range (min … max):  11.950 ms … 26.817 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     12.595 ms              ┊ GC (median):    0.00%
     Time  (mean ± σ):   12.971 ms ±  1.400 ms  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
      ▄▁ █▆▂▂▃▄▂▂▁                                                 
      ██▇██████████▇▅▆▇▁▄▅▄▁▁▅▁▁▁▁▁▁▁▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▁▁▁▁▁▁▄ ▆
      12 ms        Histogram: log(frequency) by time      20.9 ms <
    
     Memory estimate: 22.19 KiB, allocs estimate: 248.
    

    And just for comparison:

    Base.mapreducedim!

    julia> @benchmark sum(X; dims = 1)
    BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
     Range (min … max):  33.613 μs … 334.269 μs  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     42.350 μs               ┊ GC (median):    0.00%
     Time  (mean ± σ):   42.562 μs ±   6.059 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
                             ▅█▃▃▅▁                                 
      ▂▂▂▁▂▂▃▆▆▄▅▇▆▅▆▅▄▄▄▆█▆▇████████▇▆▅▅▄▄▄▄▄▃▃▃▃▃▃▂▃▂▂▂▂▂▂▂▂▂▂▂▂ ▄
      33.6 μs         Histogram: frequency by time         54.7 μs <
    
     Memory estimate: 4.02 KiB, allocs estimate: 3.

    So overall, the loops method seems to be a little bit faster than the default, but it's still orders of magnitude slower than the method from Base. Do note that this is on my laptop (Ryzen AI 7 PRO 360), so timings may vary. Could this be something device specific, or can others reproduce this as well? Is there anything else I could look at in pocl?

  4. maleadt commented on Aug 5, 2025

    @maleadt
    Member

    Interesting. Maybe @vchuravy can shed some light on the benchmarks he ran a while ago.

    Sadly, I don't have the time to look into this right now. Maybe you can vibe-port a known-good OpenCL sum implementation to see how that fares? Our much more generic mapreduce (N-dimensional, using Cartesian indices necessitating expensive integer divisions at run time now that #348 is reverted) is known to perform badly on certain GPUs, maybe the same applies to CPUs.

    Also consider that there may be an API-level issue, so it may be worth to get PoCL to dump kernel times as well, just to be sure (e.g. by using POCL_TRACING).

  5. simeonschaub commented on Aug 6, 2025

    @simeonschaub
    MemberAuthor

    It doesn't seem like it's an API-level issue, in this instance the kernel times were over 3 ms:

    Image

    Let me try with a different sum implementation

  6. simeonschaub commented on Aug 6, 2025

    @simeonschaub
    MemberAuthor

    The following implementation is really fast:

    using OpenCL, pocl_jll, BenchmarkTools
    
    const source = """
    __kernel void sum_columns_subgroup(
        __global const float* X,     // Input matrix (column-major)
        __global float* result,      // Output (length N)
        const int M,                 // Rows
        const int N                  // Columns
    ) {
        int col = get_global_id(0);
        int row_thread = get_global_id(1);
        int row_stride = get_global_size(1);
    
        if (col >= N)
            return;
    
        float partial = 0.0f;
    
        // Column-major access: X[i + j*M]
        for (int row = row_thread; row < M; row += row_stride) {
            int idx = col * M + row;
            partial += X[idx];
        }
    
        // Subgroup-level reduction
        partial = sub_group_reduce_add(partial);
    
        // Only one thread per subgroup writes to result
        if (get_sub_group_local_id() == 0) {
            __atomic_add_fetch(&result[col], partial, __ATOMIC_RELAXED);
        }
    }
    """
    p = cl.Program(; source) |> cl.build!
    k = cl.Kernel(p, "sum_columns_subgroup")
    
    X = OpenCL.rand(Float32, 1000, 1000)
    out = OpenCL.zeros(Float32, 1000)
    @benchmark begin
        clcall(k, Tuple{CLPtr{Float32}, CLPtr{Float32}, Cint, Cint}, X, out, 1000, 1000; global_size=(1000,64), local_size=(1,64))
        OpenCL.synchronize(out)
    end

    With results:

    BenchmarkTools.Trial: 10000 samples with 9 evaluations per sample.
     Range (min … max):  2.392 μs … 34.693 μs  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     4.447 μs              ┊ GC (median):    0.00%
     Time  (mean ± σ):   4.452 μs ±  1.086 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
                         ▁▄▅███▆▅▄▂                               
      ▁▁▂▄▅▆▆▅▅▄▃▃▃▄▃▃▄▅▆███████████▆▅▅▄▃▃▃▃▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁ ▃
      2.39 μs        Histogram: frequency by time        7.43 μs <
    
     Memory estimate: 1.16 KiB, allocs estimate: 33.
    

    so it's probably not a general pocl issue. I wasn't able to translate this into a native Julia kernel though since we seem to be missing a sub_group_reduce_add intrinsic.

    The actual example I care about is also a more complex reduction (logsumexp! from LogExpFunctions), so it would be nice if we could speed up the generic mapreduce as well

  7. maleadt commented on Aug 6, 2025

    @maleadt
    Member

    We do support subgroup intrinsics:

    # indices
    :get_global_linear_id => (:BuiltInGlobalLinearId, Csize_t, 1),
    :get_local_linear_id => (:BuiltInLocalInvocationIndex, Csize_t, 1),
    :get_sub_group_id => (:BuiltInSubgroupId, UInt32, 1),
    :get_sub_group_local_id => (:BuiltInSubgroupLocalInvocationId, UInt32, 1),
    # sizes
    :get_work_dim => (:BuiltInWorkDim, UInt32, 0),
    :get_sub_group_size => (:BuiltInSubgroupSize, UInt32, 0),
    :get_max_sub_group_size => (:BuiltInSubgroupMaxSize, UInt32, 0),
    :get_num_sub_groups => (:BuiltInNumSubgroups, UInt32, 0),
    :get_enqueued_num_sub_groups => (:BuiltInNumEnqueuedSubgroups, UInt32, 0)]

    But sub_group_reduce_add is a high level one that doesn't work generally for Julia anyway (since operator and type semantics do not match), so it'd be better to compare against a kernel that doesn't simply call a subgroup reductor.

  8. simeonschaub commented on Aug 7, 2025

    @simeonschaub
    MemberAuthor

    so it'd be better to compare against a kernel that doesn't simply call a subgroup reductor.

    What kind of strategy are you proposing? Do you mean something similar to the builtin mapreduce in https://github.com/JuliaGPU/OpenCL.jl/blob/master/src/mapreduce.jl and then try to optimize that?

    I am also not really sure what you mean by non-matching operator and type semantics, is it just the lack of genericness?

  9. maleadt commented on Aug 7, 2025

    @maleadt
    Member

    I am also not really sure what you mean by non-matching operator and type semantics, is it just the lack of genericness?

    Yes, the semantics of add often don't match Julia's (notably wrt. NaNs), and there's of course the limited type support.

    What kind of strategy are you proposing? Do you mean something similar to the builtin mapreduce in https://github.com/JuliaGPU/OpenCL.jl/blob/master/src/mapreduce.jl and then try to optimize that?

    It's fine keeping it in OpenCL C for now, which would be useful to determine if it's our SPIR-V codegen that introduces a performance penalty, but instead of calling sub_group_reduce_add you could use shuffle intrinsics (sub_group_shuffle I presume, but I haven't looked closely) or a tree-style reduction (such as used in our current implementation).

  10. simeonschaub commented on Aug 7, 2025

    @simeonschaub
    MemberAuthor

    Here's a sub_group_shuffle implementation:

    using OpenCL, pocl_jll, BenchmarkTools
    
    const source = """
    #pragma OPENCL EXTENSION cl_khr_subgroups : enable
    
    float warp_reduce(float val) {
        uint lane = get_sub_group_local_id();
        uint width = get_sub_group_size();
    
        for (uint offset = 1; offset < width; offset <<= 1) {
            if (lane >= offset) {
                float other = sub_group_shuffle(val, lane - offset);
                val += other;
            }
        }
        return val;
    }
    
    __kernel void sum_columns_subgroup(
        __global const float* X,     // Input matrix (column-major)
        __global float* result,      // Output (length N)
        const int M,                 // Rows
        const int N                  // Columns
    ) {
        int col = get_global_id(0);
        int row_thread = get_global_id(1);
        int row_stride = get_global_size(1);
    
        if (col >= N)
            return;
    
        float partial = 0.0f;
    
        // Column-major indexing: X[i + j*M]
        for (int row = row_thread; row < M; row += row_stride) {
            int idx = col * M + row;
            partial += X[idx];
        }
    
        // Subgroup reduction using shuffle
        partial = warp_reduce(partial);
    
        // Only the first lane writes result
        if (get_sub_group_local_id() == 0) {
            __atomic_add_fetch(&result[col], partial, __ATOMIC_RELAXED);
        }
    }
    """
    p = cl.Program(; source) |> cl.build!
    k = cl.Kernel(p, "sum_columns_subgroup")
    
    X = OpenCL.rand(Float32, 1000, 1000)
    out = OpenCL.zeros(Float32, 1000)
    @benchmark begin
        clcall(k, Tuple{CLPtr{Float32}, CLPtr{Float32}, Cint, Cint}, X, out, 1000, 1000; global_size=(1000,64), local_size=(1,64))
        OpenCL.synchronize(out)
    end

    This is now 4x slower than Base, but still a lot faster than the current mapreduce implementation:

    BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
     Range (min … max):  207.792 μs …  2.056 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     263.337 μs              ┊ GC (median):    0.00%
     Time  (mean ± σ):   272.909 μs ± 57.939 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
              ▁▃▄▆██▆▅▅▅▃▃▂                                         
      ▁▁▁▁▂▃▅▇██████████████▇▇▆▅▄▄▄▄▃▃▃▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▃
      208 μs          Histogram: frequency by time          414 μs <
    
     Memory estimate: 1.16 KiB, allocs estimate: 33.
    

    A tree-style reduction is quite a bit slower yet:

    const source2 = """
    __kernel void sum_columns_mapreduce(
        __global const float* X,     // Input matrix in column-major layout
        __global float* result,      // Output sums (length N)
        const int M,                 // Number of rows
        const int N                  // Number of columns
    ) {
        // Which column is this workgroup working on?
        int col = get_group_id(0);
        if (col >= N) return;
    
        int local_id = get_local_id(0);
        int local_size = get_local_size(0);
        int global_id = get_global_id(0);
    
        // Allocate local memory for partial sums per work-item
        __local float shared[64];
    
        // Each work-item sums multiple rows (group-stride loop)
        float partial = 0.0f;
        for (int row = local_id; row < M; row += local_size) {
            int idx = col * M + row;
            partial += X[idx];
        }
    
        // Store partial sum in local memory
        shared[local_id] = partial;
        barrier(CLK_LOCAL_MEM_FENCE);
    
        // Perform reduction in local memory (tree reduction)
        for (int offset = local_size >> 1; offset > 0; offset >>= 1) {
            if (local_id < offset) {
                shared[local_id] += shared[local_id + offset];
            }
            barrier(CLK_LOCAL_MEM_FENCE);
        }
    
        // Work-item 0 writes final sum for this column
        if (local_id == 0) {
            result[col] = shared[0];
        }
    }
    """
    p2 = cl.Program(; source = source2) |> cl.build!
    k2 = cl.Kernel(p, "sum_columns_subgroup")
    
    X = OpenCL.rand(Float32, 1000, 1000)
    out = OpenCL.zeros(Float32, 1000)
    local_size = 64
    global_size = (1000 * local_size,)  # Launch one workgroup per column
    @benchmark begin
        clcall(k2, Tuple{CLPtr{Float32}, CLPtr{Float32}, Cint, Cint}, X, out, 1000, 1000; global_size, local_size=(local_size,))
        OpenCL.synchronize(out)
    end
    BenchmarkTools.Trial: 4320 samples with 1 evaluation per sample.
     Range (min … max):  768.730 μs …   3.685 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):       1.197 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):     1.153 ms ± 298.562 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
       ▂▅▇▇▇▆▆▅▄▄▂▂▁                                 ▂▅▇█▇▆▅▄▂▁     ▂
      ▇██████████████▇▇▇▇▇▄▇▅▇▆▇▆▆▅▅▇▅▆▆▇▆▆▄▄▄▄▇▅▆▆▇▆███████████▇▇▅ █
      769 μs        Histogram: log(frequency) by time       1.57 ms <
    
     Memory estimate: 1.17 KiB, allocs estimate: 36.
    

    But still faster than the current Julia implementation. So it might be a combination of factors?

  11. maleadt commented on Aug 7, 2025

    @maleadt
    Member

    I wouldn't expect such drastic slowdowns, recuce_add -> shuffle -> tree going from 2us -> 200us -> 750us is much worse than I've seen on any GPU platform. The additional 2x slowdown to the current mapreducedim!'s 1.5ms doesn't seem that outrageous anymore then, so yeah this is probably a combination of factors.

    I wonder if the reduce_add results in SIMD CPU code where the shuffle / tree operations don't?
    We could probably consider having optimized kernels for known types / reduce operators.

  12. VarLad commented on Nov 5, 2025

    @VarLad
    Contributor

    @simeonschaub apologies to add noise here.

    I was trying to do my benchmarks for mapreduce.
    Here are some of my results.

    julia> let
    		(OpenCL.cl.platform!("Portable Computing Language") |>
    		OpenCL.cl.devices)[1] |>
    		OpenCL.cl.device!
    
    		X = rand(Float32, 1000, 1000);
    
    		Y = CLArray(X);
    
    		@benchmark OpenCL.synchronize(sum($Y; dims = 1))
    end
    
    BenchmarkTools.Trial: 689 samples with 1 evaluation per sample.
     Range (min … max):  5.550 ms …  11.266 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     7.034 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):   7.184 ms ± 939.385 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
           ▁ ▃▄█▃▅ ▂▅▃▃  ▁▄▃ ▃ ▁ ▂▁▁                               
      ▂▂▁▃▄█████████████▆███▆███▇███▆▇▆█▇▆▆▄▄▃▇▅▄▄▃▃▃▃▄▅▃▃▄▄▃▃▁▁▂ ▄
      5.55 ms         Histogram: frequency by time        9.84 ms <
    
     Memory estimate: 23.23 KiB, allocs estimate: 266.
    
    julia> let
    		(OpenCL.cl.platform!("Portable Computing Language") |>
    		OpenCL.cl.devices)[1] |>
    		OpenCL.cl.device!
    
    		X = CLArray(rand(Float32, 1000, 1000));
    
    		@benchmark OpenCL.synchronize(AcceleratedKernels.sum($X; dims = 1))
    end
    
    BenchmarkTools.Trial: 7219 samples with 1 evaluation per sample.
     Range (min … max):  395.348 μs …   2.523 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     619.647 μs               ┊ GC (median):    0.00%
     Time  (mean ± σ):   678.071 μs ± 205.095 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
           ▃▄▅▆▇█▇▄▅▅▄▄▂▁                                            
      ▂▃▄▅███████████████▇▇▆▆▆▅▅▄▄▄▃▃▄▃▃▃▃▂▃▂▃▂▂▂▂▂▂▂▁▂▂▂▂▂▂▂▂▂▁▁▂▁ ▄
      395 μs           Histogram: frequency by time         1.36 ms <
    
     Memory estimate: 12.84 KiB, allocs estimate: 166.
    
    julia> let
    	X = rand(Float32, 1000, 1000)
    
    	@benchmark sum($X; dims = 1)
    end
    BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
     Range (min … max):   67.781 μs … 926.996 μs  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):      84.963 μs               ┊ GC (median):    0.00%
     Time  (mean ± σ):   124.444 μs ±  94.425 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
      ▇█▆▅▄▄▄▄▃▂▂▂▂▁▂▁▁▁ ▁▁ ▁▁                                      ▂
      ██████████████████████████████▇▆▇█▇▇▆▇▆▆▆▆▅▆▆▅▅▆▅▅▅▆▅▅▅▅▅▄▄▅▅ █
      67.8 μs       Histogram: log(frequency) by time        554 μs <
    
     Memory estimate: 4.02 KiB, allocs estimate: 3.
    

    I was trying to run the mapreduce pr and ran into:

    InvalidIRError: compiling MethodInstance for OpenCL.partial_mapreduce_device(::typeof(identity), ::typeof(Base.add_sum), ::Float32, ::Val{1000}, ::CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, ::CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, ::OpenCL.CLDeviceArray{Float32, 3, 1}, ::OpenCL.CLDeviceMatrix{Float32, 1}) resulted in invalid LLVM IR
    
    Reason: unsupported dynamic function invocation (call to reduce_group(op, val::T, neutral, ::Val{maxitems}) where {T, maxitems} @ OpenCL none:0)
    
    Stacktrace:
    
     [1] partial_mapreduce_device
    
       @ ~/.julia/packages/OpenCL/unQqY/src/mapreduce.jl:152
    
    Reason: unsupported dynamic function invocation (call to convert)
    
    Stacktrace:
    
     [1] setindex!
    
       @ ~/.julia/packages/OpenCL/unQqY/src/device/array.jl:180
    
     [2] setindex!
    
       @ ~/.julia/packages/OpenCL/unQqY/src/device/array.jl:192
    
     [3] partial_mapreduce_device
    
       @ ~/.julia/packages/OpenCL/unQqY/src/mapreduce.jl:156
    

    and I can't try Rusticl + llvmpipe because using local Rusticl installation still makes julia crash for some reason. I'll try again once there's updated SPIRV-Tools for Fedora. 🙂

    AcceleratedKernels is quite a lot faster than current implementation with pocl.

    By the way, I wonder if for our mapreduce implementation, can (or should) we co-ordinate with JuliaGPU/KernelAbstractions.jl#559 ?

  13. pjaaskel commented on Nov 5, 2025

    @pjaaskel

    PoCL-CUDA is still an experimental target (far from conformance!) not actively worked on by the core/original developers. I suppose we should state that out more loudly in the cmake or similar.

    For targeting NVIDIA with SPIR-V inputs, perhaps you want to also try out the recent CLUDA backend for Rusticl as well or contribute back to PoCL-CUDA. Unfortunately the core devels can work on it only a very little, "lazily" as needed by the goals of the research projects that fund the team. There's a recent PR that might affect this.

    What comes to the "performance portability promises/goals of PoCL", it is currently realized via the kernel compiler which modularizes the vectorization performing (micro)architecture-specific optimizations/vectorization on the lower layers of the compilation chain. Thus, it "holds" only on CPU and will be realized better in the new work-group compiler which we are currently stabilizing.

  14. pjaaskel commented on Nov 5, 2025

    @pjaaskel

    OK, reading the whole thread it actually is about running on CPU not on CUDA -- sorry about that. Then my guess is the lack of vectorization is (partially) a result from 'unreachables', which is still to resolve. An efficient and robust handling for "aborting" kernels is still to figure out and implement. Meanwhile, I suggest try out with the new compiler we work in the private branch, I can hand out access to that per request.

  15. VarLad commented on Nov 5, 2025

    @VarLad
    Contributor

    Interestingly, on a 16-thread Intel CPU, for Float32 matrix size 10000 x 10000:

    julia> using OpenCL, AcceleratedKernels, pocl_jll, BenchmarkTools
    julia> X_cpu = rand(Float32, 10000, 10000)
    julia> X = CLArray(X_cpu)
    julia> X_temp = CLArray(zeros(Float32, 1, 10000))
    
    julia> @benchmark OpenCL.synchronize(AcceleratedKernels.sum($X, dims=1, block_size=1))
    BenchmarkTools.Trial: 741 samples with 1 evaluation per sample.
     Range (min … max):  5.855 ms …  10.501 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     6.620 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):   6.731 ms ± 629.302 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
        ▄█▃▃▂▁▁ ▄▃▂▅▂ ▄▂▁ ▁▃ ▁                                     
      ▃▇███████▇█████████▇██▆█▇▇▅▆▅▅▄▄▃▄▁▃▃▃▃▁▃▃▃▁▃▂▁▁▃▁▁▂▂▁▁▁▁▃▃ ▄
      5.86 ms         Histogram: frequency by time        9.05 ms <
    
     Memory estimate: 12.84 KiB, allocs estimate: 166.
    
    julia> @benchmark OpenCL.synchronize(AcceleratedKernels.sum($X, dims=1, temp=X_temp, block_size=1))
    BenchmarkTools.Trial: 747 samples with 1 evaluation per sample.
     Range (min … max):  5.855 ms …   9.356 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     6.603 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):   6.676 ms ± 574.644 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
        ▂█▃        ▃▁▂ ▂▂   ▃                                      
      ▄▆███▇█▇▇▆█▇████████▇██▆▇▅▆▅▅▆▄▅▄▄▄▄▃▃▅▃▃▃▃▁▃▃▃▃▁▃▃▂▃▂▃▁▁▁▂ ▄
      5.86 ms         Histogram: frequency by time        8.56 ms <
    
     Memory estimate: 3.53 KiB, allocs estimate: 82.
    
    julia> @benchmark sum($X_cpu, dims=1)
    BenchmarkTools.Trial: 259 samples with 1 evaluation per sample.
     Range (min … max):  18.867 ms …  21.760 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     19.313 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):   19.298 ms ± 232.038 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
                  ▂ ▁    ▁      ▆▂▇▂▆▆█▁▄▇▇▁▇█▁▆ ▃                  
      ▇▁▃▁▃▃▄▄▆█▁▇███▆▇▇▃█▄█▄▄▇▇████████████████▆██▆▃▄▃▃▁▁▁▁▁▁▄▃▁▄ ▄
      18.9 ms         Histogram: frequency by time         19.7 ms <
    
     Memory estimate: 39.15 KiB, allocs estimate: 3.

    As for 1000 x 1000, it seems to be on par or slightly slower than Base with +10μs or -2μs sometimes.

  16. simeonschaub commented on Jul 29, 2026

    @simeonschaub
    MemberAuthor

    Revisiting this with pocl_next_jll, I get:

    julia> using OpenCL, pocl_next_jll
    
    julia> X = rand(Float32, 10000, 10000);
    
    julia> X′ = CLArray(X);
    
    julia> @benchmark sum(X; dims = 1)
    BenchmarkTools.Trial: 313 samples with 1 evaluation per sample.
     Range (min … max):  15.730 ms …  17.680 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     15.882 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):   16.014 ms ± 334.560 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
       ▁▅█▆▅▂▁▁                                                     
      ▄█████████▇▆▃▄▄▂▃▃▃▂▄▄▁▂▃▃▃▂▁▂▃▂▃▄▃▁▃▁▃▂▃▁▁▁▂▂▁▃▁▁▃▁▂▂▃▃▂▃▁▂ ▃
      15.7 ms         Histogram: frequency by time         17.1 ms <
    
     Memory estimate: 39.15 KiB, allocs estimate: 3.
    
    julia> @benchmark OpenCL.synchronize(sum(X′; dims = 1))
    BenchmarkTools.Trial: 21 samples with 1 evaluation per sample.
     Range (min … max):  237.737 ms … 250.157 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     245.770 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):   245.641 ms ±   3.174 ms  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
                               ▃ ▃           ▃       █    █          
      ▇▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▇▁▁▁▁▇█▇█▁▁▁▁▁▁▁▇▁▁▁█▁▁▁▁▁▁▁█▁▁▁▇█▇▁▁▁▁▇▁▇ ▁
      238 ms           Histogram: frequency by time          250 ms <
    
     Memory estimate: 79.20 KiB, allocs estimate: 593.
    
    julia> import AcceleratedKernels as AK
    
    julia> @benchmark OpenCL.synchronize(AK.sum(X′; dims = 1))
    BenchmarkTools.Trial: 533 samples with 1 evaluation per sample.
     Range (min … max):  8.357 ms …  14.703 ms  ┊ GC (min … max): 0.00% … 0.00%
     Time  (median):     9.201 ms               ┊ GC (median):    0.00%
     Time  (mean ± σ):   9.383 ms ± 661.030 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%
    
              ▂ ██▇▆█▄                                             
      ▂▁▁▁▃▄▅█████████▆▆▃▄▅▅▆▆▄▅▃▂▃▂▅▄▂▂▃▃▂▂▂▂▂▁▃▂▂▃▁▃▁▃▂▁▁▁▁▁▁▂▂ ▃
      8.36 ms         Histogram: frequency by time          12 ms <
    
     Memory estimate: 20.84 KiB, allocs estimate: 181.

    If we decide to go with #423, we might as well use AK's mapreduce implementation as well, I think that's the way forward here.

  17. added
    arrays
    on Oct 19, 2026
  18. maleadt commented on Oct 19, 2026

    @maleadt
    Member

    Reductions now use GPUArrays 12's AcceleratedKernels-based implementation (#541). On PoCL, sum(X; dims=1) of a 1000x1000 Float32 matrix went from several ms to ~120 µs (Base: ~42 µs single-threaded), most of which is launch overhead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arraysperformance

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions