Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DualTVDD.jl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stephan Hilb
DualTVDD.jl
Commits
e9420a5b
Commit
e9420a5b
authored
4 years ago
by
Stephan Hilb
Browse files
Options
Downloads
Patches
Plain Diff
avoid nested SArrays
gets rid of annoying broadcasting memory allocations
parent
c33b9e16
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/DualTVDD.jl
+1
-0
1 addition, 0 deletions
src/DualTVDD.jl
src/chambolle.jl
+22
-7
22 additions, 7 deletions
src/chambolle.jl
src/common.jl
+31
-1
31 additions, 1 deletion
src/common.jl
src/dualtvdd.jl
+13
-7
13 additions, 7 deletions
src/dualtvdd.jl
with
67 additions
and
15 deletions
src/DualTVDD.jl
+
1
−
0
View file @
e9420a5b
module
DualTVDD
export
DualTVDDAlgorithm
export
init
,
step!
,
fetch
,
run
include
(
"common.jl"
)
...
...
This diff is collapsed.
Click to expand it.
src/chambolle.jl
+
22
−
7
View file @
e9420a5b
...
...
@@ -49,22 +49,27 @@ struct ChambolleState{A,T,R,S,Sv,K1,K2} <: State
end
projnorm
(
v
)
=
projnorm
(
v
,
false
)
projnorm
(
v
,
anisotropic
)
=
anisotropic
?
abs
.
(
v
)
:
norm
(
vec
(
v
))
const
SVectorNest
=
SArray
{
<:
Any
,
<:
SArray
{
<:
Any
,
Float64
,
1
},
1
}
projnorm
(
v
::
SVector
,
anisotropic
)
=
anisotropic
?
abs
.
(
v
)
:
norm
(
vec
(
v
))
#
const SVectorNest = SArray{<:Any,<:SArray{<:Any,Float64,1},1}
# TODO: respect anisotropic
#@inline projnorm(v::SVectorNest, _) = norm(norm.(v))
@inline
projnorm
(
v
::
SVectorNest
,
_
)
=
sqrt
(
sum
(
sum
(
v
[
i
]
.^
2
)
for
i
in
eachindex
(
v
)))
@inline
projnorm
(
v
,
_
)
=
norm
(
v
)
#@inline projnorm(v::SVectorNest, _) = sqrt(sum(sum(v[i] .^ 2) for i in eachindex(v)))
function
init
(
alg
::
ChambolleAlgorithm
{
<:
DualTVL1ROFOpProblem
})
g
=
alg
.
problem
.
g
λ
=
alg
.
problem
.
λ
d
=
ndims
(
g
)
ax
=
axes
(
g
)
# TODO: generalize to SArray
p1type
(
T
::
Type
{
<:
Real
})
=
SVector
{
d
,
T
}
p1type
(
::
Type
{
SVector
{
m
,
T
}})
where
{
m
,
T
}
=
SMatrix
{
m
,
d
,
T
,
m
*
d
}
pv
=
zeros
(
d
*
length
(
reinterpret
(
Float64
,
g
)))
rv
=
zeros
(
eltype
(
g
),
length
(
g
))
sv
=
zero
(
rv
)
p
=
extend
(
reshape
(
reinterpret
(
SVector
{
d
,
eltype
(
g
)},
pv
),
size
(
g
)
),
StaticKernels
.
ExtensionNothing
())
p
=
extend
(
zeros
(
p1type
(
eltype
(
g
)),
ax
),
StaticKernels
.
ExtensionNothing
())
r
=
reshape
(
rv
,
size
(
g
))
s
=
extend
(
reshape
(
sv
,
size
(
g
)),
StaticKernels
.
ExtensionReplicate
())
...
...
@@ -94,6 +99,11 @@ end
function
step!
(
ctx
::
ChambolleState
)
alg
=
ctx
.
algorithm
d
=
ndims
(
ctx
.
r
)
kgrad
=
Kernel
{
ntuple
(
_
->
0
:
1
,
d
)}(
gradient
)
λ
=
alg
.
problem
.
λ
p
=
ctx
.
p
sv
=
vec
(
parent
(
ctx
.
s
))
rv
=
vec
(
ctx
.
r
)
...
...
@@ -103,7 +113,12 @@ function step!(ctx::ChambolleState)
# s = B * r
mul!
(
sv
,
alg
.
problem
.
B
,
rv
)
# p = (p + τ*grad(s)) / (1 + τ/λ|grad(s)|)
map!
(
ctx
.
k2
,
ctx
.
p
,
ctx
.
s
)
sgrad
=
map
(
kgrad
,
ctx
.
s
)
f
(
p
,
sgrad
,
λ
)
=
iszero
(
λ
)
?
zero
(
p
)
:
(
p
+
alg
.
τ
*
sgrad
)
./
(
1.
.+
projnorm
(
alg
.
τ
*
sgrad
,
alg
.
anisotropic
)
./
λ
)
ctx
.
p
.=
f
.
(
ctx
.
p
,
sgrad
,
λ
)
return
ctx
end
...
...
This diff is collapsed.
Click to expand it.
src/common.jl
+
31
−
1
View file @
e9420a5b
...
...
@@ -56,7 +56,7 @@ fetch_u(st) = recover_u(fetch(st), st.algorithm.problem)
Base
.
intersect
(
a
::
CartesianIndices
{
d
},
b
::
CartesianIndices
{
d
})
where
d
=
CartesianIndices
(
intersect
.
(
a
.
indices
,
b
.
indices
))
@generated
function
gradient
(
w
::
StaticKernels
.
Window
{
<:
Any
,
N
})
where
N
@generated
function
gradient
(
w
::
StaticKernels
.
Window
{
<:
Real
,
N
})
where
N
i0
=
ntuple
(
_
->
0
,
N
)
i1
(
k
)
=
ntuple
(
i
->
Int
(
k
==
i
),
N
)
...
...
@@ -67,6 +67,18 @@ Base.intersect(a::CartesianIndices{d}, b::CartesianIndices{d}) where d =
end
end
@generated
function
gradient
(
w
::
StaticKernels
.
Window
{
S
,
N
})
where
{
S
<:
SArray
,
N
}
i0
=
ntuple
(
_
->
0
,
N
)
i1
(
k
)
=
ntuple
(
i
->
Int
(
k
==
i
),
N
)
wi
=
(
:
(
w
[
$
(
i1
(
k
)
...
)][
$
j
]
-
w
[
$
(
i0
...
)][
$
j
]
)
for
k
in
1
:
N
for
j
in
1
:
length
(
S
))
return
quote
Base
.
@_inline_meta
return
@inbounds
SArray
{
Tuple
{
$
(
size
(
S
)
...
),
N
}}(
$
(
wi
...
))
::
SArray
{
Tuple
{
$
(
size
(
S
)
...
),
N
},
$
(
eltype
(
S
)),
$
(
ndims
(
S
)
+
1
),
$
(
length
(
S
)
*
N
)}
end
end
@generated
function
divergence
(
w
::
StaticKernels
.
Window
{
SVector
{
N
,
T
},
N
})
where
{
N
,
T
}
i0
=
ntuple
(
_
->
0
,
N
)
i1
(
k
)
=
ntuple
(
i
->
Int
(
k
==
i
),
N
)
...
...
@@ -79,6 +91,24 @@ end
end
end
@generated
function
divergence
(
w
::
StaticKernels
.
Window
{
S
,
N
})
where
{
S
<:
SArray
,
N
}
T
=
eltype
(
S
)
sz
=
size
(
S
)
sz
[
end
]
==
N
||
throw
(
ArgumentError
(
"last eltype dimension does not match array dimensionality"
))
i0
=
ntuple
(
_
->
0
,
N
)
i1
(
k
)
=
ntuple
(
i
->
Int
(
k
==
i
),
N
)
slice
(
k
)
=
(
ntuple
(
_
->:
,
ndims
(
S
)
-
1
)
...
,
k
)
wi
=
(
:
((
isnothing
(
w
[
$
(
i0
...
)])
?
zero
(
$
T
)
:
w
[
$
(
i0
...
)][
$
(
slice
(
k
)
...
)])
-
(
isnothing
(
w
[
$
((
.-
i1
(
k
))
...
)])
?
zero
(
$
T
)
:
w
[
$
((
.-
i1
(
k
))
...
)][
$
(
slice
(
k
)
...
)]))
for
k
in
1
:
N
)
return
quote
Base
.
@_inline_meta
return
@inbounds
+
(
$
(
wi
...
))
end
end
function
div_op
(
a
::
AbstractArray
{
<:
StaticVector
{
N
},
N
})
where
N
k
=
Kernel
{
ntuple
(
_
->-
1
:
1
,
ndims
(
a
))}(
k
)
ae
=
extend
(
a
,
StaticKernels
.
ExtensionNothing
())
...
...
This diff is collapsed.
Click to expand it.
src/dualtvdd.jl
+
13
−
7
View file @
e9420a5b
using
Distributed
:
nworkers
,
workers
using
Distributed
:
workers
using
Outsource
:
Connector
,
outsource
#import Serialization
...
...
@@ -27,11 +27,13 @@ struct DualTVDDAlgorithm{P,d} <: Algorithm{P}
ninner
::
Int
"prob -> Algorithm(::Problem, ...)"
subalg
::
Function
function
DualTVDDAlgorithm
(
problem
;
M
,
overlap
,
parallel
=
true
,
σ
=
parallel
?
1
/
4
:
1.
,
ninner
=
10
,
subalg
=
x
->
ProjGradAlgorithm
(
x
))
"worker ids used for distributed execution"
workers
::
Vector
{
Int
}
function
DualTVDDAlgorithm
(
problem
;
M
,
overlap
,
parallel
=
true
,
σ
=
parallel
?
1
/
4
:
1.
,
ninner
=
10
,
subalg
=
x
->
ProjGradAlgorithm
(
x
),
workers
=
workers
())
if
parallel
==
true
&&
σ
>
1
/
4
@warn
"parallel domain decomposition needs σ >= 1/4 for theoretical convergence"
end
return
new
{
typeof
(
problem
),
length
(
M
)}(
problem
,
M
,
overlap
,
parallel
,
σ
,
ninner
,
subalg
)
return
new
{
typeof
(
problem
),
length
(
M
)}(
problem
,
M
,
overlap
,
parallel
,
σ
,
ninner
,
subalg
,
workers
)
end
end
...
...
@@ -59,10 +61,14 @@ function init(alg::DualTVDDAlgorithm{<:DualTVL1ROFOpProblem})
subλ
=
[
alg
.
problem
.
λ
[
subax
[
i
]
...
]
.*
theta
.
(
Ref
(
ax
),
Ref
(
subax
[
i
]),
Ref
(
alg
.
overlap
),
CartesianIndices
(
subax
[
i
]))
for
i
in
CartesianIndices
(
subax
)]
# TODO: generalize to SArray
p1type
(
T
::
Type
{
<:
Real
})
=
SVector
{
d
,
T
}
p1type
(
::
Type
{
SVector
{
m
,
T
}})
where
{
m
,
T
}
=
SMatrix
{
m
,
d
,
T
,
m
*
d
}
# global dual variable
p
=
zeros
(
SVector
{
d
,
eltype
(
g
)
}
,
size
(
g
)
)
p
=
zeros
(
p1type
(
eltype
(
g
)
)
,
ax
)
# local dual variable
subp
=
[
collect
(
reinterpret
(
Float64
,
zeros
(
SVector
{
d
,
eltype
(
g
)
}
,
prod
(
length
.
(
x
))))
)
for
x
in
subax
]
subp
=
[
zeros
(
p1type
(
eltype
(
g
)
)
,
sax
)
for
sa
x
in
subax
]
# create subproblem contexts
cids
=
chessboard_coloring
(
size
(
subax
))
...
...
@@ -73,7 +79,7 @@ function init(alg::DualTVDDAlgorithm{<:DualTVL1ROFOpProblem})
subprob
=
DualTVL1ROFOpProblem
(
subg
[
sidx
],
op_restrict
(
alg
.
problem
.
B
,
ax
,
subax
[
sidx
]),
subλ
[
sidx
])
wf
=
subworker
(
alg
,
alg
.
subalg
(
subprob
))
wid
=
workers
()
[
mod1
(
i
,
n
workers
(
))]
wid
=
alg
.
workers
[
mod1
(
i
,
length
(
alg
.
workers
))]
cons
[
sidx
]
=
outsource
(
wf
,
wid
)
end
end
...
...
@@ -111,7 +117,7 @@ function subworker(alg, subalg)
subg
=
take!
(
con
)
subalg
.
problem
.
g
.=
subg
# run algorithm
for
_
in
1
:
ninner
for
_
in
1
:
1000
step!
(
subst
)
end
# write result
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment