This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrhsnode.jl
165 lines (139 loc) · 4.96 KB
/
rhsnode.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
type RHSNode{F}
T::Array{F,2}
B::Array{F,2}
children::Array{RHSNode{F}}
has_parent::Bool
parent::RHSNode{F}
function RHSNode(X,snode::Supernode,parent=nothing)
this = new()
this.has_parent = (parent != nothing)
if this.has_parent
this.parent = parent
end
n,numRHS = size(X)
this.T = copy(X[snode.start:snode.start+snode.size-1,:])
this.B = zeros(F,0,0)
num_children = length(snode.children)
this.children = Array(RHSNode{F},num_children)
for c=1:num_children
this.children[c] = RHSNode{F}(X,snode.children[c],this)
end
this
end
end
function Unpack!{F}(XNode::RHSNode{F},snode::Supernode,X::StridedMatrix{F})
ind = snode.start:snode.start+snode.size-1
X[ind,:] = copy(XNode.T)
num_children = length(XNode.children)
for c=1:num_children
Unpack!(XNode.children[c],snode.children[c],X)
end
end
function Unpack{F}(XNode::RHSNode{F},snode::Supernode)
n = snode.start+snode.size-1
rootSize, numRHS = size(XNode.T)
X = zeros(F,n,numRHS)
Unpack!(XNode,snode,X)
X
end
function ForwardSolve!{F}(front::Front{F},snode::Supernode,XNode::RHSNode{F})
# Recurse on the children
num_children = length(front.children)
for c=1:num_children
ForwardSolve!(front.children[c],snode.children[c],XNode.children[c])
end
# Accumulate the updates from the children
totalSize, nodeSize = size(front.L)
nodeSize, numRHS = size(XNode.T)
structSize = totalSize - nodeSize
XNode.B = zeros(structSize,numRHS)
for c=1:num_children
childStructSize = length(front.child_maps[c]);
for iChild=1:childStructSize
iSub = front.child_maps[c][iChild];
for j=1:numRHS
value = XNode.children[c].B[iChild,j];
if iSub <= nodeSize
XNode.T[iSub,j] += value;
else
XNode.B[iSub-nodeSize,j] += value;
end
end
end
# TODO: Clear XNode.children[c].B
end
LT = sub(front.L,1:nodeSize,1:nodeSize)
LB = sub(front.L,nodeSize+1:totalSize,1:nodeSize)
# BLAS.trsm!('L','L','N','N',1.,LT,XNode.T)
# BLAS.gemm!('N','N',-1.,LB,XNode.T,1.,XNode.B)
BLAS.trsm!('L','L','N','U',1.,LT,XNode.T) # 'U' indicates ones on the diagonal (-> skip diag filled with D)
BLAS.gemm!('N','N',-1.,LB,XNode.T,1.,XNode.B)
end
function BackwardSolve!{F}(front::Front{F},snode::Supernode,XNode::RHSNode{F})
totalSize, nodeSize = size(front.L)
nodeSize, numRHS = size(XNode.T)
structSize = totalSize - nodeSize
XNode.B = zeros(structSize,numRHS)
# Pull updates down from the parent
if front.has_parent
parentNodeSize = snode.parent.size
num_siblings = length(front.parent.children)
# Determine which sibling we are
whichSibling = -1
for s=1:num_siblings
if front === front.parent.children[s]
whichSibling = s
end
end
if whichSibling == -1
error("This front was not a child of the parent")
end
for iSub=1:structSize
iParent = front.parent.child_maps[whichSibling][iSub];
for j=1:numRHS
value = XNode.B[iSub,j];
if iParent <= parentNodeSize
XNode.B[iSub,j] += XNode.parent.T[iParent,j]
else
XNode.B[iSub,j] += XNode.parent.B[iParent-parentNodeSize,j]
end
end
end
# TODO: Clear XNode.parent.B
end
LT = sub(front.L,1:nodeSize,1:nodeSize)
LB = sub(front.L,nodeSize+1:totalSize,1:nodeSize)
# BLAS.gemm!('T','N',-1.,LB,XNode.B,1.,XNode.T)
# BLAS.trsm!('L','L','T','N',1.,LT,XNode.T)
BLAS.gemm!('T','N',-1.,LB,XNode.B,1.,XNode.T)
BLAS.trsm!('L','L','T','U',1.,LT,XNode.T) # 'U' indicates ones on the diagonal -> skip
# Recurse on the children
num_children = length(front.children)
for c=1:num_children
BackwardSolve!(front.children[c],snode.children[c],XNode.children[c])
end
end
function DiagSolve!{F}(front::Front{F},snode::Supernode,XNode::RHSNode{F})
nodeSize, numRHS = size(XNode.T)
num_children = length(front.children)
for c=1:num_children
DiagSolve!(front.children[c],snode.children[c],XNode.children[c])
end
LT = sub(front.L,1:nodeSize,1:nodeSize)
for row=1:nodeSize
XNode.T[row,:] = XNode.T[row,:] / LT[row,row]
end
end
function Solve{F}(front::Front{F},root::Supernode,p::Array{Int},B::StridedMatrix{F})
# P A P^T = L L^H and A x = b imply
#
# x = inv(P) (L^H \ (L \ (P b)))
#
XNodal = RHSNode{F}(B[p,:],root)
ForwardSolve!(front,root,XNodal)
DiagSolve!(front,root,XNodal)
BackwardSolve!(front,root,XNodal)
XPerm = Unpack(XNodal,root)
X = XPerm[invperm(p),:]
X
end