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

Possibility of tranforming existing functions to use Bumper.jl #22

Open
artemsolod opened this issue Oct 26, 2023 · 3 comments
Open

Possibility of tranforming existing functions to use Bumper.jl #22

artemsolod opened this issue Oct 26, 2023 · 3 comments

Comments

@artemsolod
Copy link

Is it in principle possible to have some sort of a macro that would take a function and replace its inner calls to Vector{T}(undef, n) (and similar) with @alloc(T, n)? To me this appears possible after the calls are inlined and possibly some escape analysis applied but I have a very limited understanding of the problem.
So the macro could do something like:

function mapsum(f, x)
    arr = Vector{Float64}(undef, length(x))
    arr .= f.(x)
    return sum(arr)
end

transforms into

function mapsum_bumpered(f, x)
   @no_escape begin
        arr = @alloc(Float64, length(x))
        arr .= f.(x)
        ans = sum(arr)
    end
   return ans
end

Thanks!

@MasonProtter
Copy link
Owner

This isn't quite the same, but there is the package https://github.com/ericphanson/AllocArrays.jl which does something similar. I must say though, the idea makes me a bit uncomfortable. Without escape analysis available at code generation time, we can't really know if it's safe to do this transformation or not.

@artemsolod
Copy link
Author

Thank you for the answer! AllocArrays.jl looks interesting, will try it out.

I agree the approach looks unsafe but I find it would help my workflow and speed up running a bunch of parallel simulations.

It's interesting also if some approaches can be done without full escape analysis. Like considering a function safe for Bumper transformation if it only returns isbits types. Or maybe inlining and then wrapping all remaining return statements in copy_if_bumpallocated that could tell if something is bump-allocated and copy to memory when it is.

Meanwhile, feel free to close the issue if that is something not relevant for Bumper.jl atm.

@MasonProtter
Copy link
Owner

I'll leave this open as a somewhat far-off speculative potential feature.

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