-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ACEsuit
committed
May 31, 2024
1 parent
bd94db0
commit 3eb1386
Showing
6 changed files
with
106 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|