Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poor performance using one(T) inside Polyester.@batch loop #116

Open
ajshephard opened this issue Aug 5, 2023 · 1 comment
Open

Poor performance using one(T) inside Polyester.@batch loop #116

ajshephard opened this issue Aug 5, 2023 · 1 comment

Comments

@ajshephard
Copy link

I am running Julia 1.9 with two threads. If I include one(T) inside a Polyester.@batch loop, where T is a type known to the function, I obtain slow performance with a lot of allocations. Consider the following example

using Polyester
using BenchmarkTools

function test_no_poly!(y::Vector{T}, x::Vector{T}) where{T}
    for i in 1:length(x)
        y[i] = exp(x[i] + one(T))
    end
end

function test_poly_one!(y::Vector{T}, x::Vector{T}) where{T}
    Polyester.@batch for i in 1:length(x)
        y[i] = exp(x[i] + one(T))
    end
end

function test_poly_1!(y::Vector{T}, x::Vector{T}) where{T}
    Polyester.@batch for i in 1:length(x)
        y[i] = exp(x[i] + 1.0)
    end
end

x = rand(1_000_000)
y = similar(x)

@btime test_no_poly!($y, $x)
@btime test_poly_one!($y, $x)
@btime test_poly_1!($y, $x)

This gives me

  4.411 ms (0 allocations: 0 bytes)
  84.761 ms (2000001 allocations: 30.52 MiB)
  2.413 ms (0 allocations: 0 bytes)
@chriselrod
Copy link
Member

chriselrod commented Aug 8, 2023

It's because of Julia's not-specializing-on-type heuristics.

You could make a PR that has the macro call wrap all arguments to batch, and then unwrap them at the start of the loopbody

struct WrapType{T} end
wrap_type(@nospecialize(x)) = x
wrap_type(::Type{T}) where {T} = WrapType{T}()

unwrap_type(@nospecialize(x)) = x
unwrap_type(::WrapType{T}) where {T} = T

This lets you work around this particular one of Julia's performance-killing heuristics.

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

No branches or pull requests

2 participants