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

MPS solve #145

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions lib/mps/MPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ include("linalg.jl")
# decompositions
include("decomposition.jl")

include("solve.jl")

# matrix copy
include("copy.jl")

Expand Down
142 changes: 142 additions & 0 deletions lib/mps/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,5 +274,147 @@ function LinearAlgebra.transpose!(B::MtlMatrix{T}, A::MtlMatrix{T}) where {T}

commit!(cmdbuf)

wait_completed(cmdbuf)

return B
end


function LinearAlgebra.:(\)(A::LU{T,<:MtlMatrix{T},<:MtlVector{UInt32}}, B::MtlVecOrMat{T}) where {T<:MtlFloat}
C = deepcopy(B)
LinearAlgebra.ldiv!(A, C)
return C
end


function LinearAlgebra.ldiv!(A::LU{T,<:MtlMatrix{T},<:MtlVector{UInt32}}, B::MtlVecOrMat{T}) where {T<:MtlFloat}
M, N = size(B, 1), size(B, 2)
dev = current_device()
queue = global_queue(dev)

At = similar(A.factors)
Bt = similar(B, (N, M))
P = reshape((A.ipiv .- UInt32(1)), (1, M))
X = similar(B, (N, M))

transpose!(At, A.factors)
transpose!(Bt, B)

mps_a = MPSMatrix(At)
mps_b = MPSMatrix(Bt)
mps_p = MPSMatrix(P)
mps_x = MPSMatrix(X)

MTLCommandBuffer(queue) do cmdbuf
kernel = MPSMatrixSolveLU(dev, false, M, N)
encode!(cmdbuf, kernel, mps_a, mps_b, mps_p, mps_x)
end

transpose!(B, X)
return B
end


function LinearAlgebra.ldiv!(A::UpperTriangular{T,<:MtlMatrix{T}}, B::MtlVecOrMat{T}) where {T<:MtlFloat}
M, N = size(B, 1), size(B, 2)
dev = current_device()
queue = global_queue(dev)

Ad = MtlMatrix(A')
Br = similar(B, (M, M))
X = similar(Br)

transpose!(Br, B)

mps_a = MPSMatrix(Ad)
mps_b = MPSMatrix(Br)
mps_x = MPSMatrix(X)

buf = MTLCommandBuffer(queue) do cmdbuf
kernel = MPSMatrixSolveTriangular(dev, false, true, false, false, N, M, 1.0)
encode!(cmdbuf, kernel, mps_a, mps_b, mps_x)
end

wait_completed(buf)

copy!(B, X)
return B
end


function LinearAlgebra.ldiv!(A::UnitUpperTriangular{T,<:MtlMatrix{T}}, B::MtlVecOrMat{T}) where {T<:MtlFloat}
M, N = size(B, 1), size(B, 2)
dev = current_device()
queue = global_queue(dev)

Ad = MtlMatrix(A)
Br = reshape(B, (M, N))
X = similar(Br)

mps_a = MPSMatrix(Ad)
mps_b = MPSMatrix(Br)
mps_x = MPSMatrix(X)


buf = MTLCommandBuffer(queue) do cmdbuf
kernel = MPSMatrixSolveTriangular(dev, true, false, false, true, M, N, 1.0)
encode!(cmdbuf, kernel, mps_a, mps_b, mps_x)
end

wait_completed(buf)

copy!(Br, X)
return B
end


function LinearAlgebra.ldiv!(A::LowerTriangular{T,<:MtlMatrix{T}}, B::MtlVecOrMat{T}) where {T<:MtlFloat}
M, N = size(B, 1), size(B, 2)
dev = current_device()
queue = global_queue(dev)

Ad = MtlMatrix(A)
Br = reshape(B, (M, N))
X = similar(Br)

mps_a = MPSMatrix(Ad)
mps_b = MPSMatrix(Br)
mps_x = MPSMatrix(X)


buf = MTLCommandBuffer(queue) do cmdbuf
kernel = MPSMatrixSolveTriangular(dev, true, true, false, false, M, N, 1.0)
encode!(cmdbuf, kernel, mps_a, mps_b, mps_x)
end

wait_completed(buf)

copy!(Br, X)
return B
end


function LinearAlgebra.ldiv!(A::UnitLowerTriangular{T,<:MtlMatrix{T}}, B::MtlVecOrMat{T}) where {T<:MtlFloat}
M, N = size(B, 1), size(B, 2)
dev = current_device()
queue = global_queue(dev)

Ad = MtlMatrix(A)
Br = reshape(B, (M, N))
X = similar(Br)

mps_a = MPSMatrix(Ad)
mps_b = MPSMatrix(Br)
mps_x = MPSMatrix(X)


buf = MTLCommandBuffer(queue) do cmdbuf
kernel = MPSMatrixSolveTriangular(dev, true, true, false, true, M, N, 1.0)
encode!(cmdbuf, kernel, mps_a, mps_b, mps_x)
end

wait_completed(buf)

copy!(Br, X)
return B
end
77 changes: 77 additions & 0 deletions lib/mps/solve.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

export MPSMatrixSolveTriangular

@objcwrapper immutable=false MPSMatrixSolveTriangular <: MPSMatrixUnaryKernel

function MPSMatrixSolveTriangular(device, right, upper, unit, order, numberOfRightHandSides, alpha)
kernel = @objc [MPSMatrixSolveTriangular alloc]::id{MPSMatrixSolveTriangular}
obj = MPSMatrixSolveTriangular(kernel)
finalizer(release, obj)
@objc [obj::id{MPSMatrixSolveTriangular} initWithDevice:device::id{MTLDevice}
right:right::Bool
upper:upper::Bool
transpose:transpose::Bool
unit:unit::Bool
order:order::NSUInteger
numberOfRightHandSides:numberOfRightHandSides::NSUInteger
alpha:alpha::Float64]::id{MPSMatrixSolveTriangular}
return obj
end

function encode!(cmdbuf::MTLCommandBuffer, kernel::MPSMatrixSolveTriangular, sourceMatrix, resultMatrix, pivotIndices, status)
@objc [kernel::id{MPSMatrixSolveTriangular} encodeToCommandBuffer:cmdbuf::id{MTLCommandBuffer}
sourceMatrix:sourceMatrix::id{MPSMatrix}
resultMatrix:resultMatrix::id{MPSMatrix}
pivotIndices:pivotIndices::id{MPSMatrix}
status:status::id{MPSMatrix}]::Nothing
end


export MPSMatrixSolveLU

@objcwrapper immutable=false MPSMatrixSolveLU <: MPSMatrixUnaryKernel

function MPSMatrixSolveLU(device, transpose, order, numberOfRightHandSides)
kernel = @objc [MPSMatrixSolveLU alloc]::id{MPSMatrixSolveLU}
obj = MPSMatrixSolveLU(kernel)
finalizer(release, obj)
@objc [obj::id{MPSMatrixSolveLU} initWithDevice:device::id{MTLDevice}
transpose:transpose::Bool
order:order::NSUInteger
numberOfRightHandSides:numberOfRightHandSides::NSUInteger]::id{MPSMatrixSolveLU}
return obj
end

function encode!(cmdbuf::MTLCommandBuffer, kernel::MPSMatrixSolveLU, sourceMatrix, rightHandSideMatrix, pivotIndices, solutionMatrix)
@objc [kernel::id{MPSMatrixSolveLU} encodeToCommandBuffer:cmdbuf::id{MTLCommandBuffer}
sourceMatrix:sourceMatrix::id{MPSMatrix}
rightHandSideMatrix:rightHandSideMatrix::id{MPSMatrix}
pivotIndices:pivotIndices::id{MPSMatrix}
solutionMatrix:solutionMatrix::id{MPSMatrix}]::Nothing
end




export MPSMatrixSolveCholesky

@objcwrapper immutable=false MPSMatrixSolveCholesky <: MPSMatrixUnaryKernel

function MPSMatrixSolveCholesky(device, upper, order, numberOfRightHandSides)
kernel = @objc [MPSMatrixSolveCholesky alloc]::id{MPSMatrixSolveCholesky}
obj = MPSMatrixSolveCholesky(kernel)
finalizer(release, obj)
@objc [obj::id{MPSMatrixSolveCholesky} initWithDevice:device::id{MTLDevice}
upper:upper::Bool
order:order::NSUInteger
numberOfRightHandSides:numberOfRightHandSides::NSUInteger]::id{MPSMatrixSolveCholesky}
return obj
end

function encode!(cmdbuf::MTLCommandBuffer, kernel::MPSMatrixSolveCholesky, sourceMatrix, rightHandSideMatrix, solutionMatrix)
@objc [kernel::id{MPSMatrixSolveCholesky} encodeToCommandBuffer:cmdbuf::id{MTLCommandBuffer}
sourceMatrix:sourceMatrix::id{MPSMatrix}
rightHandSideMatrix:rightHandSideMatrix::id{MPSMatrix}
pivotIndices:pivotIndices::id{MPSMatrix}
solutionMatrix:solutionMatrix::id{MPSMatrix}]::Nothing
end
35 changes: 35 additions & 0 deletions test/mps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,39 @@ end
@test_throws SingularException lu(A)
end

@testset "solves" begin
b = MtlVector(rand(Float32, 1024))
B = MtlMatrix(rand(Float32, 1024, 1024))

A = MtlMatrix(rand(Float32, 1024, 512))
x = lu(A) \ b
@test A * x ≈ b
X = lu(A) \ B
@test A * X ≈ B

A = UpperTriangular(MtlMatrix(rand(Float32, 1024, 1024)))
x = A \ b
@test A * x ≈ b
X = A \ B
@test A * X ≈ B

A = UnitUpperTriangular(MtlMatrix(rand(Float32, 1024, 1024)))
x = A \ b
@test A * x ≈ b
X = A \ B
@test A * X ≈ B

A = LowerTriangular(MtlMatrix(rand(Float32, 1024, 1024)))
x = A \ b
@test A * x ≈ b
X = A \ B
@test A * X ≈ B

A = UnitLowerTriangular(MtlMatrix(rand(Float32, 1024, 1024)))
x = A \ b
@test A * x ≈ b
X = A \ B
@test A * X ≈ B
end

end