--- Apply f on t recursively; use the corresponding a1 and a2 elements
--- (i.e. same keys) as second and third parameters to f when
--- available; return the results from f, organized in a similarly
--- nested table.
-function DAG:nestApply(f, t, a1, a2)
- if torch.type(t) == 'table' then
- local result = {}
- for k, s in pairs(t) do
- result[k] = self:nestApply(f, s, a1 and a1[k], a2 and a2[k])
+function DAG:putInOrder()
+ if self.sorted then
+ return
+ end
+
+ local distance = {}
+ self:nestedApply(function(m) distance[m] = 1 end, self.inputModules)
+
+ local nc
+ repeat
+ nc = 0
+ for nnma, node in pairs(self.node) do
+ for _, nnmb in pairs(node.succ) do
+ if distance[nnma] and (not distance[nnmb] or distance[nnmb] < distance[nnma] + 1) then
+ distance[nnmb] = distance[nnma] + 1
+ nc = nc + 1
+ end
+ end
+ end
+ until nc == 0
+
+ self.sorted = {}
+ for m, d in pairs(distance) do
+ table.insert(self.sorted, { distance = d, nnm = m })
+ end
+
+ table.sort(self.sorted, function(a, b) return a.distance < b.distance end)
+
+ for i, a in ipairs(self.sorted) do self.sorted[i] = a.nnm end
+end
+
+-- This accumulate x in a where they are both nested tables of
+-- tensors. If first is true, set a = x.
+function DAG:nestedAccTensor(a, x, first)
+ if torch.type(x) == 'table' then
+ a = a or {}
+ for i in pairs(x) do
+ a[i] = self:nestedAccTensor(a[i], x[i], first)