Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make vector times matrix faster #1937

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions src/flint/fmpz_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1784,13 +1784,69 @@
addmul!(z::ZZMatrixOrPtr, a::ZZMatrixOrPtr, b::Integer) = addmul!(z, a, flintify(b))
addmul!(z::ZZMatrixOrPtr, a::IntegerUnionOrPtr, b::ZZMatrixOrPtr) = addmul!(z, b, a)

function _very_unsafe_convert(::Type{ZZMatrix}, a::Vector{ZZRingElem}, row = true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function _very_unsafe_convert(::Type{ZZMatrix}, a::Vector{ZZRingElem}, row = true)
function _very_unsafe_convert(::Type{ZZMatrix}, a::Vector{ZZRingElem}, Val{row} = Val(true)) where {row}

this should push performance ever so slightly further as it eliminates a jump in if row

# a must be GC.@preserved
# row = true -> make it a row
# row = false -> make it a column
M = Nemo.@new_struct(ZZMatrix)
Me = zeros(Int, length(a))
M.entries = reinterpret(Ptr{ZZRingElem}, pointer(Me))
if row
Mep = [pointer(Me)]
M.rows = reinterpret(Ptr{Ptr{ZZRingElem}}, pointer(Mep))
M.r = 1
M.c = length(a)
else
M.r = length(a)
M.c = 1
Mep = [pointer(Me) + 8*(i - 1) for i in 1:length(a)]
M.rows = reinterpret(Ptr{Ptr{ZZRingElem}}, pointer(Mep))
end
for i in 1:length(a)
Me[i] = a[i].d
end
return M, Me, Mep
end

function mul!(z::Vector{ZZRingElem}, a::ZZMatrixOrPtr, b::Vector{ZZRingElem})
@ccall libflint.fmpz_mat_mul_fmpz_vec_ptr(z::Ptr{Ref{ZZRingElem}}, a::Ref{ZZMatrix}, b::Ptr{Ref{ZZRingElem}}, length(b)::Int)::Nothing
GC.@preserve z b begin
bb, dk1, dk2 = _very_unsafe_convert(ZZMatrix, b, false)
zz, dk3, dk4 = _very_unsafe_convert(ZZMatrix, z, false)
GC.@preserve dk1 dk2 dk3 dk4 begin
mul!(zz, a, bb)
for i in 1:length(z)
z[i].d = unsafe_load(zz.entries, i).d
end
end
end
return z
end

function mul!_old(z::Vector{ZZRingElem}, a::ZZMatrixOrPtr, b::Vector{ZZRingElem})
ccall((:fmpz_mat_mul_fmpz_vec_ptr, libflint), Nothing,

Check warning on line 1826 in src/flint/fmpz_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mat.jl#L1825-L1826

Added lines #L1825 - L1826 were not covered by tests
(Ptr{Ref{ZZRingElem}}, Ref{ZZMatrix}, Ptr{Ref{ZZRingElem}}, Int),
z, a, b, length(b))
return z

Check warning on line 1829 in src/flint/fmpz_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mat.jl#L1829

Added line #L1829 was not covered by tests
end

function mul!_old(z::Vector{ZZRingElem}, a::Vector{ZZRingElem}, b::ZZMatrixOrPtr)
ccall((:fmpz_mat_fmpz_vec_mul_ptr, libflint), Nothing,

Check warning on line 1833 in src/flint/fmpz_mat.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mat.jl#L1832-L1833

Added lines #L1832 - L1833 were not covered by tests
(Ptr{Ref{ZZRingElem}}, Ptr{Ref{ZZRingElem}}, Int, Ref{ZZMatrix}),
z, a, length(a), b)
return z
end

function mul!(z::Vector{ZZRingElem}, a::Vector{ZZRingElem}, b::ZZMatrixOrPtr)
@ccall libflint.fmpz_mat_fmpz_vec_mul_ptr(z::Ptr{Ref{ZZRingElem}}, a::Ptr{Ref{ZZRingElem}}, length(a)::Int, b::Ref{ZZMatrix})::Nothing
GC.@preserve z a begin
aa, dk1, dk2 = _very_unsafe_convert(ZZMatrix, a)
zz, dk3, dk4 = _very_unsafe_convert(ZZMatrix, z)
GC.@preserve dk1 dk2 dk3 dk4 begin
mul!(zz, aa, b)
for i in 1:length(z)
z[i].d = unsafe_load(zz.entries, i).d
end
end
end
return z
end

Expand Down
Loading