From 1a9ad22a8b39391ee73066b6b1445401e329503f Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Mon, 8 Jan 2024 02:00:31 +0300 Subject: [PATCH] add example --- Project.toml | 2 ++ experimental/bumper.jl | 53 ++++++++++++++++++++++++++++++++++++++++++ src/Groebner.jl | 6 ++++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 experimental/bumper.jl diff --git a/Project.toml b/Project.toml index 6957e777..3e308706 100644 --- a/Project.toml +++ b/Project.toml @@ -6,6 +6,7 @@ version = "0.6.0" [deps] AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d" Atomix = "a9b6321e-bd34-4604-b9c9-b65b8de01458" +Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e" Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" @@ -16,6 +17,7 @@ PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d" Primes = "27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +StrideArraysCore = "7792a7ef-975c-4747-a70f-980b88e8d1da" TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" [compat] diff --git a/experimental/bumper.jl b/experimental/bumper.jl new file mode 100644 index 00000000..8dab93af --- /dev/null +++ b/experimental/bumper.jl @@ -0,0 +1,53 @@ +using BenchmarkTools, Bumper + +function work0(polys; use_custom_allocator=false) + if use_custom_allocator + custom_allocator = Bumper.SlabBuffer() + @no_escape custom_allocator begin + work1(polys, custom_allocator) + end + else + work1(polys) + end +end + +# Very important work +function work1(polys, custom_allocator=nothing) + res = 0 + for poly in polys + new_poly = work2(poly, custom_allocator) + res += sum(new_poly) + end + res +end + +function work2(poly::Vector{T}, ::Nothing) where {T} + new_poly = Vector{T}(undef, length(poly)) + work3!(new_poly) +end + +function work2(poly::Vector{T}, custom_allocator) where {T} + new_poly = Bumper.alloc!(custom_allocator, T, length(poly)) + work3!(new_poly) +end + +function work3!(poly::AbstractVector{T}) where {T} + poly[1] = one(T) + for i in 2:length(poly) + poly[i] = convert(T, i)^3 - poly[i - 1] + end + poly +end + +### + +m, n = 100, 10_000 +polys = [rand(UInt32, rand(1:m)) for _ in 1:n]; + +@btime work0(polys, use_custom_allocator=false) +# 956.800 μs (10001 allocations: 2.55 MiB) +# 0x000000064889383b + +@btime work0(polys, use_custom_allocator=true) +# 1.326 ms (5 allocations: 272 bytes) +# 0x000000064889383b diff --git a/src/Groebner.jl b/src/Groebner.jl index 80a20d28..1bec8c3a 100644 --- a/src/Groebner.jl +++ b/src/Groebner.jl @@ -61,11 +61,15 @@ import AbstractAlgebra: base_ring, elem_type import Atomix -import Base: * import Base.Threads import Base.Threads: nthreads, threadid import Base.MultiplicativeInverses: UnsignedMultiplicativeInverse +import Base: * + +# Custom allocators +import Bumper + import Combinatorics using ExprTools