Skip to content

Commit

Permalink
Optimize mat_entry_ptr methods (#1871)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Sep 30, 2024
1 parent 2ac41c7 commit fbef9d1
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 30 deletions.
28 changes: 14 additions & 14 deletions src/flint/FlintTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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!!
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions src/flint/fmpq_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

################################################################################
#
Expand Down
2 changes: 1 addition & 1 deletion src/flint/fmpz_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 1 addition & 3 deletions src/flint/fmpz_mod_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 7 additions & 3 deletions src/flint/fq_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
13 changes: 10 additions & 3 deletions src/flint/fq_nmod_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
4 changes: 1 addition & 3 deletions src/flint/gfp_fmpz_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 8 additions & 0 deletions src/flint/nmod_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit fbef9d1

Please sign in to comment.