From fbef9d1fea5be38f4128aadf39950da7e5f4bf06 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Mon, 30 Sep 2024 22:05:12 +0200 Subject: [PATCH] Optimize mat_entry_ptr methods (#1871) --- src/flint/FlintTypes.jl | 28 ++++++++++++++-------------- src/flint/fmpq_mat.jl | 4 +--- src/flint/fmpz_mat.jl | 2 +- src/flint/fmpz_mod_mat.jl | 4 +--- src/flint/fq_mat.jl | 10 +++++++--- src/flint/fq_nmod_mat.jl | 13 ++++++++++--- src/flint/gfp_fmpz_mat.jl | 4 +--- src/flint/nmod_mat.jl | 8 ++++++++ 8 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/flint/FlintTypes.jl b/src/flint/FlintTypes.jl index d9b03b320..c4bb4ad1b 100644 --- a/src/flint/FlintTypes.jl +++ b/src/flint/FlintTypes.jl @@ -4696,10 +4696,10 @@ const QQMatrixSpace = AbstractAlgebra.Generic.MatSpace{QQFieldElem} QQMatrixSpace(r::Int, c::Int) = QQMatrixSpace(QQ, r, c) mutable struct QQMatrix <: MatElem{QQFieldElem} - entries::Ptr{Nothing} + entries::Ptr{QQFieldElem} r::Int c::Int - rows::Ptr{Nothing} + rows::Ptr{Ptr{QQFieldElem}} view_parent # used by windows, not finalised!! @@ -4912,10 +4912,10 @@ end const zzModMatrixSpace = AbstractAlgebra.Generic.MatSpace{zzModRingElem} mutable struct zzModMatrix <: MatElem{zzModRingElem} - entries::Ptr{Nothing} + entries::Ptr{UInt} r::Int # Int c::Int # Int - rows::Ptr{Nothing} + rows::Ptr{Ptr{UInt}} n::UInt # mp_limb_t / Culong ninv::UInt # mp_limb_t / Culong norm::UInt # mp_limb_t / Culong @@ -5057,10 +5057,10 @@ end const ZZModMatrixSpace = AbstractAlgebra.Generic.MatSpace{ZZModRingElem} mutable struct ZZModMatrix <: MatElem{ZZModRingElem} - entries::Ptr{Nothing} + entries::Ptr{ZZRingElem} r::Int c::Int - rows::Ptr{Nothing} + rows::Ptr{Ptr{ZZRingElem}} # end flint struct base_ring::ZZModRing @@ -5215,10 +5215,10 @@ end const FpMatrixSpace = AbstractAlgebra.Generic.MatSpace{FpFieldElem} mutable struct FpMatrix <: MatElem{FpFieldElem} - entries::Ptr{Nothing} + entries::Ptr{ZZRingElem} r::Int c::Int - rows::Ptr{Nothing} + rows::Ptr{Ptr{ZZRingElem}} # end flint struct base_ring::FpField @@ -5324,10 +5324,10 @@ end const fpMatrixSpace = AbstractAlgebra.Generic.MatSpace{fpFieldElem} mutable struct fpMatrix <: MatElem{fpFieldElem} - entries::Ptr{Nothing} + entries::Ptr{UInt} r::Int # Int c::Int # Int - rows::Ptr{Nothing} + rows::Ptr{Ptr{UInt}} n::UInt # mp_limb_t / Culong ninv::UInt # mp_limb_t / Culong norm::UInt # mp_limb_t / Culong @@ -6071,10 +6071,10 @@ end const FqPolyRepMatrixSpace = AbstractAlgebra.Generic.MatSpace{FqPolyRepFieldElem} mutable struct FqPolyRepMatrix <: MatElem{FqPolyRepFieldElem} - entries::Ptr{Nothing} + entries::Ptr{FqPolyRepFieldElem} r::Int c::Int - rows::Ptr{Nothing} + rows::Ptr{Ptr{FqPolyRepFieldElem}} base_ring::FqPolyRepField view_parent @@ -6204,10 +6204,10 @@ end const fqPolyRepMatrixSpace = AbstractAlgebra.Generic.MatSpace{fqPolyRepFieldElem} mutable struct fqPolyRepMatrix <: MatElem{fqPolyRepFieldElem} - entries::Ptr{Nothing} + entries::Ptr{fqPolyRepFieldElem} r::Int c::Int - rows::Ptr{Nothing} + rows::Ptr{Ptr{fqPolyRepFieldElem}} base_ring::fqPolyRepField view_parent diff --git a/src/flint/fmpq_mat.jl b/src/flint/fmpq_mat.jl index a552479dc..918e39918 100644 --- a/src/flint/fmpq_mat.jl +++ b/src/flint/fmpq_mat.jl @@ -984,9 +984,7 @@ end # ################################################################################ -@inline mat_entry_ptr(A::QQMatrix, i::Int, j::Int) = -ccall((:fmpq_mat_entry, libflint), - Ptr{QQFieldElem}, (Ref{QQMatrix}, Int, Int), A, i-1, j-1) +mat_entry_ptr(A::QQMatrix, i::Int, j::Int) = unsafe_load(A.rows, i) + (j-1)*sizeof(QQFieldElem) ################################################################################ # diff --git a/src/flint/fmpz_mat.jl b/src/flint/fmpz_mat.jl index 33e53509c..ba1b96177 100644 --- a/src/flint/fmpz_mat.jl +++ b/src/flint/fmpz_mat.jl @@ -2097,4 +2097,4 @@ end # ################################################################################ -@inline mat_entry_ptr(A::ZZMatrix, i::Int, j::Int) = unsafe_load(A.rows, i) + (j-1)*sizeof(UInt) +mat_entry_ptr(A::ZZMatrix, i::Int, j::Int) = unsafe_load(A.rows, i) + (j-1)*sizeof(ZZRingElem) diff --git a/src/flint/fmpz_mod_mat.jl b/src/flint/fmpz_mod_mat.jl index 15423cc67..a36bf5679 100644 --- a/src/flint/fmpz_mod_mat.jl +++ b/src/flint/fmpz_mod_mat.jl @@ -900,6 +900,4 @@ end # ################################################################################ -@inline mat_entry_ptr(A::ZZModMatrix, i::Int, j::Int) = -ccall((:fmpz_mod_mat_entry, libflint), Ptr{ZZRingElem}, - (Ref{ZZModMatrix}, Int, Int), A, i - 1, j - 1) +mat_entry_ptr(A::ZZModMatrix, i::Int, j::Int) = unsafe_load(A.rows, i) + (j-1)*sizeof(ZZRingElem) diff --git a/src/flint/fq_mat.jl b/src/flint/fq_mat.jl index bfdd0c84e..1dd74e5bf 100644 --- a/src/flint/fq_mat.jl +++ b/src/flint/fq_mat.jl @@ -764,6 +764,10 @@ end # ################################################################################ -@inline mat_entry_ptr(A::FqPolyRepMatrix, i::Int, j::Int) = -ccall((:fq_mat_entry, libflint), Ptr{FqPolyRepFieldElem}, - (Ref{FqPolyRepMatrix}, Int, Int), A, i - 1, j - 1) +# each matrix entry consists of +# coeffs :: Ptr{Nothing} +# alloc :: Int +# length :: Int +# The `parent` member of struct FqPolyRepFieldElem is not replicated in each +# struct member, so we cannot use `sizeof(FqPolyRepFieldElem)`. +mat_entry_ptr(A::FqPolyRepMatrix, i::Int, j::Int) = unsafe_load(A.rows, i) + (j-1)*(sizeof(Ptr)+2*sizeof(Int)) diff --git a/src/flint/fq_nmod_mat.jl b/src/flint/fq_nmod_mat.jl index 0c6843809..515a17a76 100644 --- a/src/flint/fq_nmod_mat.jl +++ b/src/flint/fq_nmod_mat.jl @@ -753,6 +753,13 @@ end # ################################################################################ -@inline mat_entry_ptr(A::fqPolyRepMatrix, i::Int, j::Int) = -ccall((:fq_nmod_mat_entry, libflint), Ptr{fqPolyRepFieldElem}, - (Ref{fqPolyRepMatrix}, Int, Int), A, i - 1, j - 1) +# each matrix entry consists of +# coeffs :: Ptr{Nothing} +# alloc :: Int +# length :: Int +# n :: Int +# ninv :: Int +# norm :: Int +# The `parent` member of struct fqPolyRepFieldElem is not replicated in each +# struct member, so we cannot simply use `sizeof(fqPolyRepFieldElem)`. +mat_entry_ptr(A::fqPolyRepMatrix, i::Int, j::Int) = unsafe_load(A.rows, i) + (j-1)*(sizeof(Ptr)+5*sizeof(Int)) diff --git a/src/flint/gfp_fmpz_mat.jl b/src/flint/gfp_fmpz_mat.jl index 41cc7060b..cdfa893cf 100644 --- a/src/flint/gfp_fmpz_mat.jl +++ b/src/flint/gfp_fmpz_mat.jl @@ -457,6 +457,4 @@ end # ################################################################################ -@inline mat_entry_ptr(A::FpMatrix, i::Int, j::Int) = -ccall((:fmpz_mod_mat_entry, libflint), Ptr{ZZRingElem}, - (Ref{FpMatrix}, Int, Int), A, i - 1, j - 1) +mat_entry_ptr(A::FpMatrix, i::Int, j::Int) = unsafe_load(A.rows, i) + (j-1)*sizeof(ZZRingElem) diff --git a/src/flint/nmod_mat.jl b/src/flint/nmod_mat.jl index e47300de9..68c429a19 100644 --- a/src/flint/nmod_mat.jl +++ b/src/flint/nmod_mat.jl @@ -898,3 +898,11 @@ function kernel(::Solve.RREFTrait, M::Union{zzModMatrix, ZZModMatrix}; side::Sym end # For kernel(::HowellFormTrait, ...) we use generic AbstractAlgebra code + +################################################################################ +# +# Entry pointers +# +################################################################################ + +mat_entry_ptr(A::Zmodn_mat, i::Int, j::Int) = unsafe_load(A.rows, i) + (j-1)*sizeof(UInt)