Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ACEsuit committed May 31, 2024
1 parent bd94db0 commit 3eb1386
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 39 deletions.
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ version = "0.0.1-dev"
Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e"

[compat]
julia = "1.6.7"
julia = "1.9, 1.10"
Bumper = "0.6.0"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[targets]
test = ["Test"]
test = ["Test", "LinearAlgebra"]
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,42 @@ end
After writing the same pattern 10 times, we wondered whether there is an easy way to wrap this and the result is the present package. It allows us to replace the above 3 lines with
```julia
@no_escape begin
A = @with_alloc calculate_something!(more, args)
A = @withalloc calculate_something!(more, args)
end
```

### Documentation

For now, there is just a simple example. More soon ...
```julia
using WithAlloc, LinearAlgebra, Bumper

# simple allocating operation
B = randn(5,10)
C = randn(10, 3)
A1 = B * C
s1 = sum(A1)

# we wrap mul! into a new function so we don't become pirates...
mymul!(A, B, C) = mul!(A, B, C)

# tell `WithAlloc` how to allocate memory for `mymul!`
WithAlloc.whatalloc(::typeof(mymul!), B, C) =
(promote_type(eltype(B), eltype(C)), size(B, 1), size(C, 2))

# the "naive use" of automated pre-allocation could look like this:
@no_escape begin
A2_alloc_info = WithAlloc.whatalloc(mymul!, B, C)
A2 = @alloc(A2_alloc_info...)
mymul!(A2, B, C)

@show A2 A1
end

# but the same pattern will be repreated over and over so ...
@no_escape begin
A3 = @withalloc1 mymul!(B, C)

@show A3 A1
end
```
2 changes: 1 addition & 1 deletion src/WithAlloc.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module WithAlloc

using Bumper
export whatalloc, @withalloc
export whatalloc, @withalloc1

function whatalloc end

Expand Down
30 changes: 30 additions & 0 deletions test/_readme.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using WithAlloc, LinearAlgebra, Bumper

# simple allocating operation
B = randn(5,10)
C = randn(10, 3)
A1 = B * C
s1 = sum(A1)

# we wrap mul! into a new function so we don't become pirates...
mymul!(A, B, C) = mul!(A, B, C)

# tell `WithAlloc` how to allocate memory for `mymul!`
WithAlloc.whatalloc(::typeof(mymul!), B, C) =
(promote_type(eltype(B), eltype(C)), size(B, 1), size(C, 2))

# the "naive use" of automated pre-allocation could look like this:
@no_escape begin
A2_alloc_info = WithAlloc.whatalloc(mymul!, B, C)
A2 = @alloc(A2_alloc_info...)
mymul!(A2, B, C)

@show A2 A1
end

# but the same pattern will be repreated over and over so ...
@no_escape begin
A3 = @withalloc1 mymul!(B, C)

@show A3 A1
end
36 changes: 1 addition & 35 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,37 +1,3 @@
using WithAlloc, Bumper, Test, LinearAlgebra

# @testset "WithAlloc.jl" begin
# end


function mymul!(A, B, C)
mul!(A, B, C)
end

function WithAlloc.whatalloc(::typeof(mymul!), B, C)
T = promote_type(eltype(B), eltype(C))
return (T, size(B, 1), size(C, 2))
end

B = randn(5,10)
C = randn(10, 3)
A1 = B * C
s1 = sum(A1)

@no_escape begin
A2_alloc_info = WithAlloc.whatalloc(mymul!, B, C)
A2 = @alloc(A2_alloc_info...)
mymul!(A2, B, C)
@show A2 A1
s2 = sum(A2)
end
@show s2 s1

@no_escape begin
A3 = WithAlloc.@withalloc_let mymul!(B, C)

@show A3 A1
s3 = sum(A3)
end
@show s3 s1

@testset "WithAlloc.jl" begin include("test1.jl") end
36 changes: 36 additions & 0 deletions test/test1.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using WithAlloc, Bumper, Test, LinearAlgebra


function mymul!(A, B, C)
mul!(A, B, C)
end

function WithAlloc.whatalloc(::typeof(mymul!), B, C)
T = promote_type(eltype(B), eltype(C))
return (T, size(B, 1), size(C, 2))
end

B = randn(5,10)
C = randn(10, 3)
A1 = B * C
s1 = sum(A1)

@no_escape begin
A2_alloc_info = WithAlloc.whatalloc(mymul!, B, C)
A2 = @alloc(A2_alloc_info...)
mymul!(A2, B, C)
@show A2 A1
s2 = sum(A2)
end
@test s2 s1

@no_escape begin
A3 = WithAlloc.@withalloc1 mymul!(B, C)

@show A3 A1
s3 = sum(A3)
end
@test s3 s1



0 comments on commit 3eb1386

Please sign in to comment.