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

add nospecialize #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion src/ApproxFunBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import BlockBandedMatrices: blockbandwidth, blockbandwidths, blockcolstop, block
subblockbandwidth, subblockbandwidths, _BlockBandedMatrix,
_BandedBlockBandedMatrix, BandedBlockBandedMatrix, BlockBandedMatrix,
isblockbanded, isbandedblockbanded, bb_numentries, BlockBandedSizes,
BandedBlockBandedSizes
BandedBlockBandedSizes, DefaultBandedBlockBandedMatrix

import FastTransforms: ChebyshevTransformPlan, IChebyshevTransformPlan, plan_chebyshevtransform,
plan_chebyshevtransform!, plan_ichebyshevtransform, plan_ichebyshevtransform!
Expand Down
71 changes: 35 additions & 36 deletions src/Caching/almostbanded.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
## Caches

function CachedOperator(io::InterlaceOperator{T,1};padding::Bool=false) where T
ds=domainspace(io)
rs=rangespace(io)
CachedOperator(io::InterlaceOperator{T,1};padding::Bool=false) where T =
_interlace_CachedOperator(io, padding)

ind=findall(op->isinf(size(op,1)), io.ops)
if length(ind) ≠ 1 || !isbanded(io.ops[ind[1]]) # is almost banded
function _interlace_CachedOperator(io::InterlaceOperator{T,1}, padding) where T
ds = domainspace(io)
rs = rangespace(io)

ind = findall(op->isinf(size(op,1)), io.ops)
if length(ind) ≠ 1 || !(isbanded(io.ops[ind[1]])::Bool) # is almost banded
return default_CachedOperator(io;padding=padding)
end
i=ind[1]
bo=io.ops[i]
lin,uin=bandwidths(bo)

lin,uin=bandwidths(bo)::NTuple{2,Int}


# calculate number of rows interlaced
Expand All @@ -20,24 +22,23 @@ function CachedOperator(io::InterlaceOperator{T,1};padding::Bool=false) where T
md=0
for k=1:length(io.ops)
if k ≠ i
d=dimension(rs[k])
d=dimension(rs[k])::Int
nds+=d
md=max(md,d)
end
end


isend=true
for k=i+1:length(io.ops)
if dimension(rs[k]) == md
isend=false
end
end

numoprows=isend ? md-1 : md
n=nds+numoprows
numoprows=(isend ? md-1 : md) ::Int
n=(nds+numoprows) ::Int

(l,u) = (max(lin+nds,n-1),max(0,uin+1-ind[1]))
(l,u) = (max(lin+nds,n-1),max(0,uin+1-ind[1]))::Tuple{Int,Int}

# add extra rows for QR
if padding
Expand All @@ -49,13 +50,13 @@ function CachedOperator(io::InterlaceOperator{T,1};padding::Bool=false) where T

# populate the finite rows
jr=1:n+u
ioM=io[1:n,jr]
ioM=io[1:n,jr]::RaggedMatrix{T}


bcrow=1
oprow=0
for k=1:n
K,J=io.rangeinterlacer[k]
K,J= io.rangeinterlacer[k]

if K ≠ i
# fill the fill matrix
Expand Down Expand Up @@ -173,12 +174,15 @@ function CachedOperator(io::InterlaceOperator{T,2};padding::Bool=false) where T
end


resizedata!(co::CachedOperator{T,AlmostBandedMatrix{T}}, row::Integer, col::Integer) where {T} =
_almostbanded_resizedata!(co, co.op, row, col)

resizedata!(co::CachedOperator{T,AlmostBandedMatrix{T}}, row::Integer, ::Colon) where {T} =
_almostbanded_resizedata!(co, co.op, row, :)

# Grow cached interlace operator

function resizedata!(co::CachedOperator{T,AlmostBandedMatrix{T},
InterlaceOperator{T,1,DS,RS,DI,RI,BI}},
n::Integer,::Colon) where {T<:Number,DS,RS,DI,RI,BI}
function _almostbanded_resizedata!(co, op::InterlaceOperator{T,1}, n::Integer,::Colon) where {T<:Number}
if n ≤ co.datasize[1]
return co
end
Expand All @@ -187,12 +191,12 @@ function resizedata!(co::CachedOperator{T,AlmostBandedMatrix{T},
pad!(co.data,n,n+u)

r=rank(co.data.fill)
ind=findfirst(op->isinf(size(op,1)),co.op.ops)
ind=findfirst(op->isinf(size(op,1)),op.ops)

k=1
for (K,J) in co.op.rangeinterlacer
for (K,J) in op.rangeinterlacer
if K ≠ ind
co.data.fill.V[co.datasize[2]:end,k] = co.op.ops[K][J,co.datasize[2]:n+u]
co.data.fill.V[co.datasize[2]:end,k] = op.ops[K][J,co.datasize[2]:n+u]
k += 1
if k > r
break
Expand All @@ -202,7 +206,7 @@ function resizedata!(co::CachedOperator{T,AlmostBandedMatrix{T},

kr=co.datasize[1]+1:n
jr=max(1,kr[1]-l):n+u
BLAS.axpy!(1.0,view(co.op.ops[ind],kr .- r,jr),
BLAS.axpy!(1.0,view(op.ops[ind],kr .- r,jr),
view(co.data.bands,kr,jr))

co.datasize=(n,n+u)
Expand All @@ -211,14 +215,11 @@ end



function resizedata!(co::CachedOperator{T,AlmostBandedMatrix{T},
InterlaceOperator{T,2,DS,RS,DI,RI,BI}},
n::Integer,::Colon) where {T<:Number,DS,RS,DI,RI,BI}
function _almostbanded_resizedata!(co, io::InterlaceOperator{T,2}, n::Integer,::Colon) where T<:Number
if n ≤ co.datasize[1]
return co
end

io=co.op
ds=domainspace(io)
rs=rangespace(io)
di=io.domaininterlacer
Expand All @@ -242,7 +243,7 @@ function resizedata!(co::CachedOperator{T,AlmostBandedMatrix{T},
K=k=1
while k ≤ r
if isfinite(dimension(rs[ri[K][1]]))
co.data.fill.V[co.datasize[2]:end,k] = co.op[K,co.datasize[2]:n+u]
co.data.fill.V[co.datasize[2]:end,k] = io[K,co.datasize[2]:n+u]
k += 1
end
K += 1
Expand All @@ -259,14 +260,12 @@ function resizedata!(co::CachedOperator{T,AlmostBandedMatrix{T},
end


resizedata!(co::CachedOperator{T,AlmostBandedMatrix{T},
InterlaceOperator{T,1,DS,RS,DI,RI,BI}},
n::Integer,m::Integer) where {T<:Number,DS,RS,DI,RI,BI} = resizedata!(co,max(n,m+bandwidth(co.data.bands,1)),:)
_almostbanded_resizedata!(co, ::InterlaceOperator{T,1}, n::Integer,m::Integer) where {T<:Number} =
resizedata!(co,max(n,m+bandwidth(co.data.bands,1)),:)


resizedata!(co::CachedOperator{T,AlmostBandedMatrix{T},
InterlaceOperator{T,2,DS,RS,DI,RI,BI}},
n::Integer,m::Integer) where {T<:Number,DS,RS,DI,RI,BI} = resizedata!(co,max(n,m+bandwidth(co.data.bands,1)),:)
_almostbanded_resizedata!(co, ::InterlaceOperator{T,2}, n::Integer,m::Integer) where {T<:Number} =
resizedata!(co,max(n,m+bandwidth(co.data.bands,1)),:)



Expand All @@ -285,8 +284,8 @@ end


function resizedata!(QR::QROperator{CachedOperator{T,AlmostBandedMatrix{T},
MM,DS,RS,BI}},
::Colon,col) where {T,MM,DS,RS,BI}
DS,RS,BI}},
::Colon,col) where {T,DS,RS,BI}
if col ≤ QR.ncols
return QR
end
Expand Down Expand Up @@ -351,8 +350,8 @@ end
# BLAS versions, requires BlasFloat

function resizedata!(QR::QROperator{CachedOperator{T,AlmostBandedMatrix{T},
MM,DS,RS,BI}},
::Colon,col) where {T<:BlasFloat,MM,DS,RS,BI}
DS,RS,BI}},
::Colon,col) where {T<:BlasFloat,DS,RS,BI}
if col ≤ QR.ncols
return QR
end
Expand Down
8 changes: 4 additions & 4 deletions src/Caching/banded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ end


function resizedata!(QR::QROperator{<:CachedOperator{T,<:BandedMatrix{T},
MM,DS,RS,BI}},
::Colon,col) where {T,MM,DS,RS,BI}
DS,RS,BI}},
::Colon,col) where {T,DS,RS,BI}
if col ≤ QR.ncols
return QR
end
Expand Down Expand Up @@ -94,8 +94,8 @@ end


function resizedata!(QR::QROperator{<:CachedOperator{T,<:BandedMatrix{T},
MM,DS,RS,BI}},
::Colon,col) where {T<:BlasFloat,MM,DS,RS,BI}
DS,RS,BI}},
::Colon,col) where {T<:BlasFloat,DS,RS,BI}
if col ≤ QR.ncols
return QR
end
Expand Down
8 changes: 4 additions & 4 deletions src/Caching/blockbanded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ QROperator(R::CachedOperator{T,BlockBandedMatrix{T}}) where {T} =

# always resize by column
resizedata!(QR::QROperator{CachedOperator{T,BlockBandedMatrix{T},
MM,DS,RS,BI}},
::Colon, col::Int) where {T,MM,DS,RS,BI} =
DS,RS,BI}},
::Colon, col::Int) where {T,DS,RS,BI} =
resizedata!(QR, :, block(domainspace(QR.R_cache),col))

function resizedata!(QR::QROperator{CachedOperator{T,BlockBandedMatrix{T},
MM,DS,RS,BI}},
::Colon, COL::Block) where {T<:BlasFloat,MM,DS,RS,BI}
DS,RS,BI}},
::Colon, COL::Block) where {T<:BlasFloat,DS,RS,BI}
MO = QR.R_cache
W = QR.H
R = MO.data
Expand Down
2 changes: 1 addition & 1 deletion src/Caching/matrix.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CachedOperator(::Type{Matrix},op::Operator;padding::Bool=false) =
CachedOperator(op,Array{eltype(op)}(0,0),padding)
CachedOperator(op,Array{eltype(op)}(undef,0,0),padding)


# Grow cached operator
Expand Down
8 changes: 4 additions & 4 deletions src/Caching/ragged.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ QROperator(R::CachedOperator{T,RaggedMatrix{T}}) where {T} =
QROperator(R,RaggedMatrix{T}(undef,0,Int[]),0)

function resizedata!(QR::QROperator{CachedOperator{T,RaggedMatrix{T},
MM,DS,RS,BI}},
::Colon,col) where {T,MM,DS,RS,BI}
DS,RS,BI}},
::Colon,col) where {T,DS,RS,BI}
if col ≤ QR.ncols
return QR
end
Expand Down Expand Up @@ -127,8 +127,8 @@ end


function resizedata!(QR::QROperator{CachedOperator{T,RaggedMatrix{T},
MM,DS,RS,BI}},
::Colon,col) where {T<:BlasFloat,MM,DS,RS,BI}
DS,RS,BI}},
::Colon,col) where {T<:BlasFloat,DS,RS,BI}
if col ≤ QR.ncols
return QR
end
Expand Down
1 change: 0 additions & 1 deletion src/Fun.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ end
const VFun{S,T} = Fun{S,T,Vector{T}}

Fun(sp::Space,coeff::AbstractVector) = Fun{typeof(sp),eltype(coeff),typeof(coeff)}(sp,coeff)
Fun() = Fun(identity)
Fun(d::Domain) = Fun(identity,d)
Fun(d::Space) = Fun(identity,d)

Expand Down
2 changes: 1 addition & 1 deletion src/LinearAlgebra/AlmostBandedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


struct AlmostBandedMatrix{T} <: AbstractMatrix{T}
bands::BandedMatrix{T}
bands::BandedMatrix{T,Matrix{T},Base.OneTo{Int}}
fill::LowRankMatrix{T}
function AlmostBandedMatrix{T}(bands::BandedMatrix{T}, fill::LowRankMatrix{T}) where T
if size(bands) ≠ size(fill)
Expand Down
10 changes: 8 additions & 2 deletions src/LinearAlgebra/RaggedMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ end
RaggedMatrix(dat::Vector,cols::Vector{Int},m::Int) =
RaggedMatrix{eltype(dat)}(dat,cols,m)

RaggedMatrix{T}(::UndefInitializer, m::Int, colns::AbstractVector{Int}) where {T} =
RaggedMatrix(Vector{T}(undef, sum(colns)),Int[1;1 .+ cumsum(colns)],m)
function RaggedMatrix{T}(::UndefInitializer, m::Int, colns::AbstractVector{Int}) where {T}
cs = Vector{Int}(undef, length(colns)+1)
cs[1] = 1
for j=2:length(cs)
cs[j] = cs[j-1] + colns[j-1]
end
RaggedMatrix(Vector{T}(undef, cs[end]),cs,m)
end


Base.size(A::RaggedMatrix) = (A.m,length(A.cols)-1)
Expand Down
9 changes: 7 additions & 2 deletions src/LinearAlgebra/helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ end

const TrivialInterlacer{d} = BlockInterlacer{NTuple{d,<:Ones}}

BlockInterlacer(v::AbstractVector) = BlockInterlacer(tuple(v...))
BlockInterlacer(v::AbstractVector) = uninfer(BlockInterlacer(tuple(v...)))

Base.eltype(it::BlockInterlacer) = Tuple{Int,Int}

Expand Down Expand Up @@ -792,4 +792,9 @@ function iterate(it::BlockInterlacer, (N,k,blkst,lngs))
return (N,lngs[N]),(N,k+1,blkst,lngs)
end

cache(Q::BlockInterlacer) = CachedIterator(Q)
cache(Q::BlockInterlacer) = CachedIterator(Q)




@noinline uninfer(@nospecialize(x)) = Ref{Any}(x)[]
16 changes: 10 additions & 6 deletions src/Operators/Operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,13 @@ BlockBandedMatrix(::Type{Zeros}, V::Operator) =
(AbstractVector{Int}(blocklengths(rangespace(V))),
AbstractVector{Int}(blocklengths(domainspace(V)))),
blockbandwidths(V))
RaggedMatrix(::Type{Zeros}, V::Operator) =
RaggedMatrix(Zeros{eltype(V)}(size(V)),
Int[max(0,colstop(V,j)) for j=1:size(V,2)])
function RaggedMatrix(::Type{Zeros}, V::Operator)
cs = Vector{Int}(undef, size(V,2))
for j = 1:length(cs)
cs[j] = max(0,colstop(V,j))
end
RaggedMatrix(Zeros{eltype(V)}(size(V)), cs)
end


convert_axpy!(::Type{MT}, S::Operator) where {MT <: AbstractMatrix} =
Expand Down Expand Up @@ -772,10 +776,10 @@ end

function arraytype(V::SubOperator)
P = parent(V)
isbanded(P) && return BandedMatrix
isbanded(P) && return BandedMatrix |> uninfer
# isbandedblockbanded(P) && return BandedBlockBandedMatrix
isinf(size(P,1)) && israggedbelow(P) && return RaggedMatrix
return Matrix
isinf(size(P,1)) && israggedbelow(P) && return RaggedMatrix|> uninfer
return Matrix|> uninfer
end

AbstractMatrix(V::Operator) = arraytype(V)(V)
Expand Down
6 changes: 3 additions & 3 deletions src/Operators/banded/CalculusOperator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ macro calculus_operator(Op)
space::S # the domain space
order::OT
end
struct $WrappOp{BT<:Operator,S<:Space,OT,T} <: $Op{S,OT,T}
op::BT
struct $WrappOp{S<:Space,OT,T} <: $Op{S,OT,T}
op::Operator{T}
order::OT
end

Expand Down Expand Up @@ -60,7 +60,7 @@ macro calculus_operator(Op)
end

$WrappOp(op::Operator,order) =
$WrappOp{typeof(op),typeof(domainspace(op)),typeof(order),eltype(op)}(op,order)
$WrappOp{typeof(domainspace(op)),typeof(order),eltype(op)}(op,order)
$WrappOp(op::Operator) = $WrappOp(op,1)

function Base.convert(::Type{Operator{T}},D::$WrappOp) where T
Expand Down
Loading