Skip to content

Commit

Permalink
add sweep keyword argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sumiya11 committed Jul 13, 2023
1 parent 33f2d5a commit 21f3883
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 92 deletions.
40 changes: 7 additions & 33 deletions benchmark/experiments/standalone_f4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ using Logging
global_logger(ConsoleLogger(stderr, Logging.Error))

# BenchmarkTools.DEFAULT_PARAMETERS.seconds = 6
BenchmarkTools.DEFAULT_PARAMETERS.samples = 4
BenchmarkTools.DEFAULT_PARAMETERS.samples = 3

Groebner.logging_enabled() = false
Groebner.invariants_enabled() = false

function benchmark_system_my(system)
system = Groebner.change_ordering(system, :degrevlex)
Expand All @@ -21,7 +24,7 @@ function benchmark_system_my(system)
# println("length = $(length(gb))")
# println("degree = $(maximum(AbstractAlgebra.total_degree, gb))")

@btime Groebner.groebner($system, reduced=false, linalg=:prob)
@btime Groebner.groebner($system, loglevel=2^20)
end

function run_f4_ff_degrevlex_benchmarks(ground)
Expand All @@ -33,7 +36,7 @@ function run_f4_ff_degrevlex_benchmarks(ground)
("katsura 11", reverse(Groebner.katsuran(11, ground=ground))),
("noon 7", Groebner.noonn(7, ground=ground)),
("noon 8", Groebner.noonn(8, ground=ground)),
("noon 9", Groebner.noonn(9, ground=ground)),
# ("noon 9", Groebner.noonn(9, ground=ground)),
("eco 10", Groebner.eco10(ground=ground)),
("eco 11", Groebner.eco11(ground=ground))
]
Expand All @@ -45,38 +48,11 @@ function run_f4_ff_degrevlex_benchmarks(ground)
end

println()
ground = AbstractAlgebra.GF(2^50 + 55)
ground = AbstractAlgebra.GF(2^31 - 1)
run_f4_ff_degrevlex_benchmarks(ground)

#=
qq:
cyclic 6
16.083 ms (41950 allocations: 7.84 MiB)
cyclic 7
4.348 s (3224917 allocations: 778.54 MiB)
katsura 7
300.875 ms (395295 allocations: 67.01 MiB)
katsura 8
2.269 s (2073004 allocations: 384.21 MiB)
katsura 9
10.807 s (6798833 allocations: 1.07 GiB)
noon 6
99.882 ms (293406 allocations: 42.26 MiB)
noon 7
683.802 ms (1502368 allocations: 199.92 MiB)
noon 8
7.004 s (9184113 allocations: 1.24 GiB)
eco 10
651.303 ms (1018972 allocations: 208.98 MiB)
eco 11
7.053 s (6136001 allocations: 1.22 GiB)
=#

#=
gf:
cyclic 7
Expand All @@ -93,8 +69,6 @@ noon 7
194.497 ms (209483 allocations: 74.25 MiB)
noon 8
1.327 s (950722 allocations: 378.90 MiB)
noon 9
12.392 s (4306578 allocations: 1.98 GiB)
eco 10
66.734 ms (72768 allocations: 38.28 MiB)
eco 11
Expand Down
20 changes: 19 additions & 1 deletion src/f4/basis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,27 @@ function fill_data!(
basis.nfilled = ngens
end

function sweep_redundant!(basis::Basis, hashtable)
# here -- assert that basis is in fact a Groebner basis.
# NOTE: maybe sort generators for more effective sweeping?
@inbounds for i in 1:(basis.nprocessed)
for j in (i + 1):(basis.nprocessed)
basis.isredundant[i] && continue
basis.isredundant[j] && continue
lead_i = basis.monoms[i][1]
lead_j = basis.monoms[j][1]
if is_monom_divisible(lead_i, lead_j, hashtable)
# mark redundant
basis.isredundant[i] = true
end
end
end
nothing
end

# Remove redundant elements from the basis
# by moving all non-redundant up front
function filter_redundant!(basis::Basis)
function mark_redundant!(basis::Basis)
j = 1
@inbounds for i in 1:(basis.nnonredundant)
if !basis.isredundant[basis.nonredundant[i]]
Expand Down
9 changes: 7 additions & 2 deletions src/f4/f4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,13 @@ function f4!(
set_ready!(tracer)
set_final_basis!(tracer, basis.nfilled)

# remove redundant elements
filter_redundant!(basis)
if params.sweep
@log level = -3 "Sweeping redundant elements in the basis"
sweep_redundant!(basis, hashtable)
end

# mark redundant elements
mark_redundant!(basis)
@log level = -2 "Filtered elements marked redundant"

if params.reduced
Expand Down
29 changes: 25 additions & 4 deletions src/f4/learn-apply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function f4_apply!(graph, ring, basis::Basis{C}, params) where {C <: Coeff}

@log level = -6 "Before reduction" basis

# filter_redundant!(basis)
# mark_redundant!(basis)

if params.reduced
@log level = -6 "Autoreducing the final basis.."
Expand Down Expand Up @@ -586,11 +586,32 @@ function f4_learn!(
end
end

# remove redundant elements
filter_redundant!(basis)
@log level = -6 "Before filter redundant" basis

# TODOTODO
for i in 1:(basis.nfilled)
for j in (i + 1):(basis.nfilled)
basis.isredundant[i] && continue
basis.isredundant[j] && continue
lead_i = basis.monoms[i][1]
lead_j = basis.monoms[j][1]
if is_monom_divisible(lead_j, lead_i, hashtable)
# mark redundant
basis.isredundant[j] = true
end
end
end

if params.sweep
@log level = -3 "Sweeping redundant elements in the basis"
sweep_redundant!(basis, hashtable)
end

# mark redundant elements
mark_redundant!(basis)
@log level = -3 "Filtered elements marked redundant"

@log level = -6 "Before reduction" basis
@log level = -6 "Before autoreduction" basis

if params.reduced
@log level = -3 "Autoreducing the final basis.."
Expand Down
1 change: 0 additions & 1 deletion src/f4/matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ function interreduce_lower_part_learn!(
end

useful_reducers_sorted = sort(collect(useful_reducers))
@log level = 0 "" length(useful_reducers_sorted) matrix.nup newpivs matrix.nright
push!(graph.matrix_infos, (nup=matrix.nup, nlow=matrix.nlow, ncols=matrix.ncols))
push!(graph.matrix_nonzeroed_rows, not_reduced_to_zero)
# push!(graph.matrix_upper_rows, (matrix.up2coef, matrix.up2mult))
Expand Down
17 changes: 12 additions & 5 deletions src/utils/keywords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const _supported_kw_args = (
seed = 42,
loglevel = _default_loglevel,
maxpairs = typemax(Int), # TODO,
strategy = :classic_modular
strategy = :classic_modular,
sweep = false,
),
normalform = (
check = true,
Expand All @@ -41,12 +42,14 @@ const _supported_kw_args = (
groebner_learn = (
seed = 42,
monoms = :packed,
loglevel = _default_loglevel
loglevel = _default_loglevel,
sweep = false,
),
groebner_apply! = (
seed = 42,
monoms = :packed,
loglevel = _default_loglevel
loglevel = _default_loglevel,
sweep = false,
)
)
#! format: on
Expand All @@ -70,6 +73,7 @@ struct KeywordsHandler{Ord}
maxpairs::Int
strategy::Symbol
check::Bool
sweep::Bool

function KeywordsHandler(function_key, kws)
@assert haskey(_supported_kw_args, function_key)
Expand All @@ -92,6 +96,7 @@ struct KeywordsHandler{Ord}
strategy = get(kws, :strategy, get(default_kw_args, :strategy, :classic_modular))
@assert strategy in (:classic_modular, :learn_and_apply) "Not recognized strategy: $strategy"
check = get(kws, :check, get(default_kw_args, :check, true))
sweep = get(kws, :sweep, get(default_kw_args, :sweep, false))
@log level = -1 """
Using keywords:
reduced = $reduced,
Expand All @@ -103,7 +108,8 @@ struct KeywordsHandler{Ord}
loglevel = $loglevel,
maxpairs = $maxpairs,
strategy = $strategy,
check = $check"""
check = $check,
sweep = $sweep"""
new{typeof(ordering)}(
reduced,
ordering,
Expand All @@ -114,7 +120,8 @@ struct KeywordsHandler{Ord}
loglevel,
maxpairs,
strategy,
check
check,
sweep
)
end
end
10 changes: 8 additions & 2 deletions src/utils/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ struct AlgorithmParameters{Ord1, Ord2}
# Random number generator seed
seed::UInt64
rng::_default_rng_type

sweep::Bool
end

function AlgorithmParameters(ring, kwargs::KeywordsHandler)
Expand Down Expand Up @@ -83,6 +85,8 @@ function AlgorithmParameters(ring, kwargs::KeywordsHandler)

useed = UInt64(seed)

sweep = kwargs.sweep

@log level = -1 """
Selected parameters:
target_ord = $target_ord
Expand All @@ -100,7 +104,8 @@ function AlgorithmParameters(ring, kwargs::KeywordsHandler)
emit_computation_graph = $emit_computation_graph
threading = $threading
seed = $seed
rng = $rng"""
rng = $rng
sweep = $sweep"""

AlgorithmParameters(
target_ord,
Expand All @@ -118,6 +123,7 @@ function AlgorithmParameters(ring, kwargs::KeywordsHandler)
emit_computation_graph,
threading,
useed,
rng
rng,
sweep
)
end
42 changes: 21 additions & 21 deletions test/groebner/groebner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,33 @@ using AbstractAlgebra
@test gb [x1 * x2]
end

@testset "groebner noreduce" begin
R, (x, y) = PolynomialRing(GF(2^31 - 1), ["x", "y"], ordering=:lex)
# @testset "groebner noreduce" begin
# R, (x, y) = PolynomialRing(GF(2^31 - 1), ["x", "y"], ordering=:lex)

fs = [x + y^2, x * y - y^2]
gb = Groebner.groebner(fs, reduced=false)
@test gb [x + y^2, x * y + 2147483646 * y^2, y^3 + y^2]
# fs = [x + y^2, x * y - y^2]
# gb = Groebner.groebner(fs, reduced=false)
# @test gb ≂ [x + y^2, x * y + 2147483646 * y^2, y^3 + y^2]

fs = [x + y, x^2 + y]
gb = Groebner.groebner(fs, reduced=false)
@test gb [x + y, x^2 + y, y^2 + y]
# fs = [x + y, x^2 + y]
# gb = Groebner.groebner(fs, reduced=false)
# @test gb ≂ [x + y, x^2 + y, y^2 + y]

fs = [y, x * y + x]
gb = Groebner.groebner(fs, reduced=false)
@test gb [x, y]
# fs = [y, x * y + x]
# gb = Groebner.groebner(fs, reduced=false)
# @test gb ≂ [x, y]

fs = [x^2 + 5, 2y^2 + 3]
gb = Groebner.groebner(fs, reduced=false)
@test gb [y^2 + 1073741825, x^2 + 5]
# fs = [x^2 + 5, 2y^2 + 3]
# gb = Groebner.groebner(fs, reduced=false)
# @test gb ≂ [y^2 + 1073741825, x^2 + 5]

fs = [y^2 + x, x^2 * y + y, y + 1]
gb = Groebner.groebner(fs, reduced=false)
@test gb == [R(1)]
# fs = [y^2 + x, x^2 * y + y, y + 1]
# gb = Groebner.groebner(fs, reduced=false)
# @test gb == [R(1)]

fs = [x^2 * y^2, 2 * x * y^2 + 3 * x * y]
gb = Groebner.groebner(fs, reduced=false)
@test gb [x * y^2 + 1073741825 * x * y, x^2 * y]
end
# fs = [x^2 * y^2, 2 * x * y^2 + 3 * x * y]
# gb = Groebner.groebner(fs, reduced=false)
# @test gb ≂ [x * y^2 + 1073741825 * x * y, x^2 * y]
# end

@testset "groebner modular" begin
R, (x,) = PolynomialRing(QQ, ["x"], ordering=:degrevlex)
Expand Down
Loading

0 comments on commit 21f3883

Please sign in to comment.