Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ACEsuit committed May 31, 2024
1 parent 9305f03 commit ef0a0c5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
51 changes: 45 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,54 @@ end
end

# ------------------------------------------------------------------------
# Bonus: does this become non-allocating ... we can quickly check ...
function alloctest(B, C)

# Multiple arrays is handled via tuples:

B = randn(5,10)
C = randn(10, 3)
D = randn(10, 5)
A1 = B * C
A2 = B * D

mymul2!(A1, A2, B, C, D) = mul!(A1, B, C), mul!(A2, B, D)

function WithAlloc.whatalloc(::typeof(mymul2!), B, C, D)
T1 = promote_type(eltype(B), eltype(C))
T2 = promote_type(eltype(B), eltype(D))
return ( (T1, size(B, 1), size(C, 2)),
(T2, size(B, 1), size(D, 2)) )
end

@no_escape begin
A1b, A2b = WithAlloc.@withalloc mymul2!(B, C, D)
@show A1 A1b, A2 A2b # true, true
end
```

This approach should become non-allocating, which we can quickly check.
There currently seems to be a bug in `@withalloc`, which is not occurring in
`@withalloc1`; cf Issue #1.
```julia
using WithAlloc, LinearAlgebra, Bumper, BenchmarkTools

mymul!(A, B, C) = mul!(A, B, C)

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

function alloctest1(B, C)
@no_escape begin
s3 = sum( @withalloc1 mymul!(B, C) )
end
return s3
end

using BenchmarkTools
@btime alloctest($B, $C)
# 125.284 ns (0 allocations: 0 bytes)
function alloctest(B, C)
@no_escape begin
s3 = sum( @withalloc mymul!(B, C) )
end
end

B = randn(5,10); C = randn(10, 3)
@btime alloctest($B, $C) # 243.056 ns (2 allocations: 64 bytes)
@btime alloctest1($B, $C) # 106.551 ns (0 allocations: 0 bytes)
```
41 changes: 29 additions & 12 deletions test/_readme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@ end
@show A3 A1
end

# ------------------------------------------------------------------------
# Bonus: does this become non-allocating ... we can quickly check ...
function alloctest(B, C)
@no_escape begin
s3 = sum( @withalloc1 mymul!(B, C) )
end
return s3
end

using BenchmarkTools
@btime alloctest($B, $C)
# 125.284 ns (0 allocations: 0 bytes)
# ------------------------------------------------------------------------

# Multiple arrays is handled via tuples:
Expand All @@ -64,3 +52,32 @@ end
A1b, A2b = WithAlloc.@withalloc mymul2!(B, C, D)
@show A1 A1b, A2 A2b # true, true
end


# ------------------------------------------------------------------------
# Bonus: does this become non-allocating ... we can quickly check ...
# there currently seems to be a bug in @withalloc, which is
# not occurring in @withalloc1.

using WithAlloc, LinearAlgebra, Bumper, BenchmarkTools

mymul!(A, B, C) = mul!(A, B, C)

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

function alloctest1(B, C)
@no_escape begin
s3 = sum( @withalloc1 mymul!(B, C) )
end
end

function alloctest(B, C)
@no_escape begin
s3 = sum( @withalloc mymul!(B, C) )
end
end

B = randn(5,10); C = randn(10, 3)
@btime alloctest($B, $C) # 243.056 ns (2 allocations: 64 bytes)
@btime alloctest1($B, $C) # 106.551 ns (0 allocations: 0 bytes)

0 comments on commit ef0a0c5

Please sign in to comment.