-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit.t
795 lines (718 loc) · 24.6 KB
/
circuit.t
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
require("tablehelpers")
require("util")
NodeType = {INTERNAL={},INPUT={},OUTPUT={}}
local terra max(a : int32, b : int32)
return terralib.select( a > b, a, b)
end
local Cir = {}
function Cir.nodeTypeString(typ)
if typ == NodeType.INTERNAL then
return "INTERNAL"
end
if typ == NodeType.INPUT then
return "INPUT"
end
if typ == NodeType.OUTPUT then
return "OUTPUT"
end
end
function Cir.printNode(node)
print("BEGIN_NODE")
print(" type = "..Cir.nodeTypeString(node.type))
if node.inputs and #node.inputs > 0 then
io.write(" inputs[", tostring(#node.inputs), "] = {")
for i,input in ipairs(node.inputs) do
io.write(Cir.nodeTypeString(input.type), ", ")
end
io.write("}\n")
end
if node.lutValue then
print(" lutValue = "..string.format("%x", node.lutValue))
end
print("END_NODE")
end
function Cir.print(circuit)
print("inputs")
for i,input in ipairs(circuit.inputs) do
printNode(input)
end
print("internal")
for i,node in ipairs(circuit.internalNodes) do
printNode(node)
end
print("outputs")
for i,output in ipairs(circuit.outputs) do
printNode(output)
end
end
local function Wire(input, output, indexInOut)
local wire = {}
wire.input = input
wire.output = output
wire.indexInOut = indexInOut
return wire
end
function Cir.getInputWires(node)
local result = {}
for i,input in ipairs(node.inputs) do
log.trace(tostring(node.inputs[i])," -> ",tostring(node))
result[i] = Wire(node.inputs[i],node, i)
end
return result
end
local function visit(node, newNodes)
node.marked = true
for i,m in ipairs(node.inputs) do
if m.type == NodeType.INTERNAL and (not m.marked) then
visit(m, newNodes)
end
end
newNodes[#newNodes+1] = node
end
local function topologicalSort(nodes)
for i,v in ipairs(nodes) do
v.marked = false
end
local newNodes = {}
for i,v in ipairs(nodes) do
if not v.marked then
visit(v, newNodes)
end
end
for i,v in ipairs(newNodes) do
-- inline reverse
--nodes[i] = newNodes[#newNodes-i+1]
nodes[i] = v
end
end
function Cir.getWireArray(circuit)
local wires = {}
for i,output in ipairs(circuit.outputs) do
wires = table.concattables(wires, Cir.getInputWires(output))
end
for i,node in ipairs(circuit.internalNodes) do
wires = table.concattables(wires, Cir.getInputWires(node))
end
log.debug("wireArray size: "..#wires)
return wires
end
function Cir.makeConsistent(circuit)
circuit.wires = Cir.getWireArray(circuit)
topologicalSort(circuit.internalNodes)
end
function Cir.deleteNode(circuit, node)
log.trace("deleteNode")
log.trace("patching inputs")
for i,wire in ipairs(circuit.wires) do
if wire.input == node then
--TODO: randomize?
log.debug("Patching wire at index "..wire.indexInOut)
wire.output.inputs[wire.indexInOut] = node.inputs[1]
end
end
log.trace("Actual removal")
local oldIndex = table.invert(circuit.internalNodes)[node]
table.remove(circuit.internalNodes, oldIndex)
Cir.makeConsistent(circuit)
end
function Cir.selectInternalNode(circuit, internalNodeIndex)
log.trace("selectInternalNode("..internalNodeIndex..")")
return circuit.internalNodes[internalNodeIndex]
end
function Cir.nonInputNodeCount(circuit)
return #circuit.internalNodes + #circuit.outputs
end
--TODO: check if downstream nodes are really downstream?
function Cir.upstreamNodes(circuit, node)
local upNodes = {}
for i,v in ipairs(circuit.internalNodes) do
if node == v then
break
end
upNodes[#upNodes+1] = v
end
for i,v in ipairs(circuit.inputs) do
upNodes[#upNodes+1] = v
end
return upNodes
end
function Cir.setInputOfNode(circuit,node,index,inputNode)
node.inputs[index] = inputNode
log.trace("Setting index "..index.." to node type "..Cir.nodeTypeString(inputNode.type))
log.trace("Input set. Making consistent")
Cir.makeConsistent(circuit)
end
function Cir.selectNonInputNode(circuit, nonInputNodeIndex)
log.trace("selectNonInputNode")
if nonInputNodeIndex > #circuit.internalNodes then
local i = nonInputNodeIndex - #circuit.internalNodes
return circuit.outputs[i], true
else
return circuit.internalNodes[nonInputNodeIndex], false
end
assert(false,"selectNonInputNode index too large")
end
function Cir.wireCount(circuit)
return #circuit.wires
end
function Cir.selectWire(circuit, wireIndex)
return circuit.wires[wireIndex]
end
local function twoLevelCopyNode(node)
local copiedNode = table.shallowcopy(node)
if copiedNode.inputs then
copiedNode.inputs = table.shallowcopy(node.inputs)
end
return copiedNode
end
function Cir.deepCopy(circuit)
log.trace("Deep copy")
local newCircuit = {}
newCircuit.inputs = {}
newCircuit.internalNodes = {}
newCircuit.outputs = {}
local oldToNew = {}
log.info("deepCopy of circuit with Inp/Int/Out: "..#circuit.inputs.."/"..#circuit.internalNodes.."/"..#circuit.outputs)
for i=1,#circuit.inputs do
newCircuit.inputs[i] = twoLevelCopyNode(circuit.inputs[i])
if oldToNew[circuit.inputs[i]] then
log.error("==== DUPLICATE CIRCUIT IN DEEP COPY (INPUTS): "..tostring(oldToNew[circuit.inputs[i]]))
Cir.toGraphviz(circuit, "out/error")
assert(false)
end
oldToNew[circuit.inputs[i]] = newCircuit.inputs[i]
end
for i=1,#circuit.outputs do
newCircuit.outputs[i] = twoLevelCopyNode(circuit.outputs[i])
if oldToNew[circuit.outputs[i]] then
log.error("==== DUPLICATE CIRCUIT IN DEEP COPY (OUTPUTS): "..tostring(oldToNew[circuit.outputs[i]]))
Cir.toGraphviz(circuit, "out/error")
assert(false)
end
oldToNew[circuit.outputs[i]] = newCircuit.outputs[i]
end
for i=1,#circuit.internalNodes do
newCircuit.internalNodes[i] = twoLevelCopyNode(circuit.internalNodes[i])
if oldToNew[circuit.internalNodes[i]] then
log.error("==== DUPLICATE CIRCUIT IN DEEP COPY (INTERNAL): "..tostring(oldToNew[circuit.internalNodes[i]]))
Cir.toGraphviz(circuit, "out/error")
assert(false)
end
oldToNew[circuit.internalNodes[i]] = newCircuit.internalNodes[i]
end
-- Now node references at top level are fixed, need to fix all internal references
for oldN,newN in pairs(oldToNew) do
log.trace("old: "..tostring(oldN))
log.trace("new: "..tostring(newN))
if newN.inputs and #newN.inputs > 0 then
for i,v in ipairs(newN.inputs) do
newN.inputs[i] = oldToNew[v]
end
end
end
newCircuit.wires = Cir.getWireArray(newCircuit)
-- Topo sorting is maintained during deep copy
log.trace("Deep copy done")
return newCircuit
end
function Cir.internalNodeCount(circuit)
return #circuit.internalNodes
end
local function InputNode()
local node = {}
node.type = NodeType.INPUT
return node
end
local function ConstNode(val)
local node = InputNode()
node.val = val
return node
end
local function OutputNode(inputNode)
local node = {}
node.type = NodeType.OUTPUT
node.inputs = {inputNode}
return node
end
local function LUTNode(inputs,lutValue)
local node = {}
node.inputs = table.shallowcopy(inputs)
node.lutValue = lutValue
node.type = NodeType.INTERNAL
return node
end
function Cir.setLUTValue(node, lutValue)
assert(node.lutValue, "Tried to assign lut value to non-lut node")
node.lutValue = lutValue
end
function Cir.nodeSanityCheck(circuit)
log.debug("NODE SANITY CHECK")
local validNodes = {}
for i,v in ipairs(circuit.inputs) do validNodes[v] = true end
for i,v in ipairs(circuit.outputs) do validNodes[v] = true end
for i,v in ipairs(circuit.internalNodes) do validNodes[v] = true end
log.debug("SETUP DONE")
for k,v in pairs(validNodes) do
if (k.inputs) and #k.inputs > 0 then
for i,node in ipairs(k.inputs) do
if not validNodes[node] then
log.error("INVALID NODE FOUND IN INPUT ", i, " OF ", Cir.nodeTypeString(k.type))
log.error("Claims to be ", Cir.nodeTypeString(node.type))
end
end
elseif k.type ~= NodeType.INPUT then
log.error("NON-INPUT NODE WITHOUT INPUTS: ", Cir.nodeTypeString(k.type))
end
end
log.debug("NODE SANITY CHECK DONE")
end
local function isNode(circuit, node)
local validNodes = {}
for i,v in ipairs(circuit.inputs) do validNodes[v] = true end
for i,v in ipairs(circuit.outputs) do validNodes[v] = true end
for i,v in ipairs(circuit.internalNodes) do validNodes[v] = true end
return validNodes[node] ~= nil
end
function Cir.addLUTNode(circuit, inputs, wire, lutValue)
for i,v in ipairs(inputs) do
log.trace(isNode(circuit,v))
end
log.trace(isNode(circuit,wire.input))
log.trace(isNode(circuit,wire.output))
local node = LUTNode(inputs,lutValue)
wire.output.inputs[wire.indexInOut] = node
circuit.internalNodes[#circuit.internalNodes+1] = node
Cir.makeConsistent(circuit)
log.debug("LUTNODE added")
return node
end
function Cir.getGround(circuit)
return circuit.inputs[#circuit.inputs-1]
end
function Cir.getPower(circuit)
return circuit.inputs[#circuit.inputs]
end
function Cir.emptyCircuit(inputsCount, outputCount)
local circuit = {}
circuit.inputs = {}
for i=1,inputsCount do
circuit.inputs[i] = InputNode()
end
local groundNode = ConstNode(false)
local powerNode = ConstNode(true)
circuit.inputs[inputsCount+1] = groundNode
circuit.inputs[inputsCount+2] = powerNode
circuit.outputs = {}
for i=1,outputCount do
circuit.outputs[i] = OutputNode(groundNode)
end
circuit.internalNodes = {}
circuit.wires = Cir.getWireArray(circuit)
return circuit
end
local function uniqueNodeName(node)
return "node"..string.sub(tostring(node), 10)
end
local struct TestSet {
inputs : &uint64
outputs : &uint64
N : uint64
}
terra TestSet:set(idx : uint64, inp : uint64, out : uint64)
self.inputs[idx] = inp
self.outputs[idx] = out
end
terra TestSet:init(size : uint64)
self.N = size
self.inputs = [&uint64](C.malloc(sizeof(uint64) * size))
self.outputs = [&uint64](C.malloc(sizeof(uint64) * size))
end
terra TestSet:free()
C.free(self.inputs)
C.free(self.outputs)
end
Cir.TestSet = TestSet
local struct TerraInputNode {
val : uint32
}
local struct TerraOutputNode {
inputIndex : int32
}
local struct TerraLUTNode {
inputs : int32[4]
lutValue : uint32
val : uint32
}
local makeTerraCircuit = terralib.memoize(function(numInputs, numOutputs, maxInternalNodes)
local struct TerraCircuit {
inputs : TerraInputNode[numInputs]
luts : BoundedArray(TerraLUTNode, maxInternalNodes) -- TODO:Abstract
outputs : TerraOutputNode[numOutputs]
}
terra TerraCircuit:deleteLUT(nodeIndex : int32)
self.luts:delete(nodeIndex)
for i=nodeIndex,self.luts.N do
for j=0,4 do
var currIndex = self.luts.data[i].inputs[j]
if currIndex > nodeIndex + numInputs then
self.luts.data[i].inputs[j] = currIndex-1
end
end
end
for i=0,numOutputs do
var currIndex = self.outputs[i].inputIndex
if currIndex > nodeIndex + numInputs then
self.outputs[i].inputIndex = currIndex-1
end
end
end
terra TerraCircuit:isInputIndex( index : int32)
return index < numInputs
end
terra TerraCircuit:packLUTs()
C.printf("For now TerraCircuit:packLUTs assumes all gates start as 2 input, I0 I1")
var maxInd = self.luts.N
for k=0,maxInd do
var i = maxInd - k - 1
var lut = self.luts.data[i]
var inputs : int32[2] = array(lut.inputs[0], lut.inputs[1])
if (not self:isInputIndex(inputs[0])) and (not self:isInputIndex(inputs[1])) then
-- Trivial rewiring
var lut0 = self.luts.data[inputs[0]-numInputs]
var lut1 = self.luts.data[inputs[1]-numInputs]
self.luts.data[i].inputs[0] = lut0.inputs[0]
self.luts.data[i].inputs[1] = lut0.inputs[1]
self.luts.data[i].inputs[2] = lut1.inputs[0]
self.luts.data[i].inputs[3] = lut1.inputs[1]
var oldLutVal = self.luts.data[i].lutValue
self.luts.data[i].lutValue = 0
--[[for j=0,16 do
lut0.lutValue[]
end--]]
end
end
end
terra TerraCircuit:pruneUnusedLUTs()
var used : BoundedArray(bool, maxInternalNodes)
used.N = self.luts.N
for i=0,used.N do
used.data[i] = false
end
for i=0,numOutputs do
var index = self.outputs[i].inputIndex
if index >= numInputs then
used.data[index-numInputs] = true
end
end
var maxInd = self.luts.N
for k=0,maxInd do
var i = maxInd - k - 1
if used.data[i] then
for j=0,4 do
var index = self.luts.data[i].inputs[j]
if index >= numInputs then
used.data[index-numInputs] = true
end
end
else
self:deleteLUT(i)
end
end
end
terra TerraCircuit:simulate()
--C.printf("self.luts.N, %u\n", self.luts.N)
for i=0,self.luts.N do
var bitindex = 0
for j=0,4 do
var nodeindex = self.luts.data[i].inputs[j]
if nodeindex < [numInputs] then
bitindex = bitindex or (self.inputs[nodeindex].val << j)
else
nodeindex = nodeindex - [numInputs]
bitindex = bitindex or (self.luts.data[nodeindex].val << j)
end
end
self.luts.data[i].val = 1 and (self.luts.data[i].lutValue >> bitindex)
end
--C.printf("numOutputs, %u\n", numOutputs)
var result : uint32 = 0 --TODO: get rid of 32-bit limitation
for i=0,numOutputs do
var nodeindex = self.outputs[i].inputIndex
if nodeindex < [uint32]([numInputs]) then
result = result or (self.inputs[nodeindex].val << i)
else
nodeindex = nodeindex - [int32]([numInputs])
result = result or (self.luts.data[nodeindex].val << i)
end
end
return result
end
terra TerraCircuit:setInputs(inp : uint64)
for i=0,numInputs do
self.inputs[i].val = [uint32]((inp >> i) and 1)
end
end
local showConstantInputs = false
terra TerraCircuit:toGraphviz(filename : rawstring)
var inputCount : uint32 = [numInputs]
if not [showConstantInputs] then
inputCount = inputCount - 2
end
var fp = C.fopen(filename, "w")
--Header
C.fprintf(fp, "digraph {\nrankdir = TB;\nsubgraph {\n")
--Input Nodes
for i=0,inputCount do
C.fprintf(fp, " node%d [label=\"input%d\"]\n", i, i)
end
--Internal Nodes
C.fprintf(fp, "node[shape=box]\n")
for i=0,self.luts.N do
C.fprintf(fp, " node%d [label=\"%04x\"]\n", [numInputs] + i, self.luts.data[i].lutValue)
end
--Output Nodes
C.fprintf(fp, "node[shape=oval]\n")
for i=0,[numOutputs] do
C.fprintf(fp, " output%d [label=\"output%d\"]\n", i, i)
end
-- Edges
for i=0,self.luts.N do
for j=0,4 do
var inp = self.luts.data[i].inputs[j]
if [showConstantInputs] or (inp >= [numInputs]) or (inp < inputCount) then
C.fprintf(fp, " node%d -> node%d\n", inp, [numInputs] + i)
end
end
end
for i=0,[numOutputs] do
var inp = self.outputs[i].inputIndex
if [showConstantInputs] or (inp >= [numInputs]) or (inp < inputCount) then
C.fprintf(fp, " node%d -> output%d\n", inp, i)
end
end
-- Fake edges to sort inputs
for i=0,inputCount-1 do
C.fprintf(fp, " node%d -> node%d [style=invis]\n", i, i+1)
end
-- Fake edges to sort outputs
for i=0,[numOutputs]-1 do
C.fprintf(fp, " output%d -> output%d [style=invis]\n", i, i+1)
end
-- Put inputs at the top
C.fprintf(fp, " {rank = same; ")
for i=0,inputCount do
C.fprintf(fp, "node%d;", i)
end
C.fprintf(fp, "}\n")
-- Put outputs at the bottom
C.fprintf(fp, " {rank = same; ")
for i=0,[numOutputs] do
C.fprintf(fp, "output%d;", i)
end
C.fprintf(fp, "}\n")
--Footer
C.fprintf(fp, "} /* closing subgraph */\n")
--if useCostLabel then
-- C.fprintf(fp, "label=\"Cost %f; Error Cost %f\"\nlabelloc=top;\nlabeljust=left;\n", cost, errorCost)
--end
C.fprintf(fp, "}\n")
C.fclose(fp)
end
terra TerraCircuit:addLUTPreservingTopology(newLUTIndex : int32, lutValue : int32, inputs : int32[4])
var newLUTIndex = newLUTIndex
--C.printf("newLUTIndex: %d\n", newLUTIndex)
--C.printf("self.luts.N: %d\n", self.luts.N)
for i=0,self.luts.N-newLUTIndex do
var idx = self.luts.N-i
--C.printf("moving : %d to %d\n", idx-1, idx)
self.luts.data[idx] = self.luts.data[idx-1]
end
self.luts.N = self.luts.N+1
self.luts.data[newLUTIndex].inputs = inputs
self.luts.data[newLUTIndex].lutValue = lutValue
for i=newLUTIndex+1,self.luts.N do
--C.printf("Rewriting inputs of : %d\n", i)
for j=0,4 do
var idx = self.luts.data[i].inputs[j]
if idx >= (newLUTIndex + numInputs) then
idx = idx + 1
end
--C.printf("%d -> %d\n", self.luts.data[i].inputs[j], idx)
self.luts.data[i].inputs[j] = idx
end
end
for i=0,numOutputs do
--C.printf("Rewriting input of output %d\n", i)
var idx = self.outputs[i].inputIndex
if idx >= (newLUTIndex + numInputs) then
idx = idx + 1
end
--C.printf("%d -> %d\n", self.outputs[i].inputIndex, idx)
self.outputs[i].inputIndex = idx
end
end
terra TerraCircuit:setLUTInputs(lutIndex : int32, inputs : int32[4])
self.luts.data[lutIndex].inputs = inputs
end
terra TerraCircuit:setOutInput(outIndex : int32, inIndex : int32)
self.outputs[outIndex].inputIndex = inIndex
end
terra TerraCircuit:hammingDist(inp : uint64, out : uint64)
--C.printf("About to set Inputs\n")
self:setInputs(inp)
--C.printf("About to simulate\n")
var result = self:simulate()
--C.printf("About to popcount\n")
return popcount(result ^ out)
end
terra TerraCircuit:hammingErrorOnTestSet(testSet : TestSet)
var dist : int32 = 0
--C.printf("hammingErrorOnTestSet: size(%d)\n",testSet.N)
for i=0,testSet.N do
dist = dist + self:hammingDist(testSet.inputs[i], testSet.outputs[i])
end
--C.printf("error %d\n",dist)
return dist
end
return TerraCircuit
end)
function Cir.terraCircuitToLuaCircuit(terraCircuit)
local inputCount = terralib.typeof(terraCircuit.inputs).N-2
local outputCount = terralib.typeof(terraCircuit.outputs).N
local luaCircuit = Cir.emptyCircuit(inputCount, outputCount)
local internalNodeCount = terraCircuit.luts.N
for i=1,internalNodeCount do
local lutValue = terraCircuit.luts.data[i-1].lutValue
local inputs = {}
for j=0,3 do
local index = terraCircuit.luts.data[i-1].inputs[j]
index = index + 1
if index <= #luaCircuit.inputs then
inputs[#inputs+1] = luaCircuit.inputs[index]
else
index = index - #luaCircuit.inputs
inputs[#inputs+1] = luaCircuit.internalNodes[index]
end
end
local newNode = LUTNode(inputs,lutValue)
luaCircuit.internalNodes[#luaCircuit.internalNodes+1] = newNode
end
for i=1,outputCount do
local index = terraCircuit.outputs[i-1].inputIndex
index = index + 1
if index <= #luaCircuit.inputs then
luaCircuit.outputs[i].inputs[1] = luaCircuit.inputs[index]
else
index = index - #luaCircuit.inputs
luaCircuit.outputs[i].inputs[1] = luaCircuit.internalNodes[index]
end
end
Cir.makeConsistent(luaCircuit)
return luaCircuit
end
function Cir.createTerraCircuit(circuit, maxInternalNodes)
local nodesToIndices = {}
for i,v in ipairs(circuit.inputs) do
nodesToIndices[v] = i-1
end
for i,v in ipairs(circuit.internalNodes) do
nodesToIndices[v] = i-1+(#circuit.inputs)
end
local tCircType = makeTerraCircuit(#circuit.inputs, #circuit.outputs, maxInternalNodes)
print("Creating Terra Circuit")
local terra createCircuit()
var circ : tCircType
circ.luts:resize([#circuit.internalNodes])
escape
for i,v in ipairs(circuit.internalNodes) do
for j=0,3 do
local nodeIndex = nodesToIndices[v.inputs[j+1]]
emit quote
circ.luts([i-1]).inputs[j] = [int32](nodeIndex)
end
end
emit quote
circ.luts([i-1]).lutValue = uint32(v.lutValue)
end
end
for i,v in ipairs(circuit.outputs) do
local nodeIndex = nodesToIndices[v.inputs[1]]
emit quote
circ.outputs[i-1].inputIndex = [int32](nodeIndex)
end
end
end
return circ
end
return createCircuit, tCircType
end
--[[
function Cir.runCircuitInTerra(circuit, input)
local terra runCircuit()
var circ = [createTerraCircuit(circuit)()]
circ:setInputs([input])
return circ:simulate()
end
return runCircuit()
end
--]]
local function runCreatedCircuitInTerra(terraCircuit, input)
local terra runCircuit()
var circ = [terraCircuit]
var inp : uint64 = [input]
circ:setInputs([input])
return circ:simulate()
end
return runCircuit()
end
function Cir.createTerraTestSet(testSet)
local terra createTestSet()
var tSet : TestSet
tSet:init([#testSet])
return tSet
end
local tSet = createTestSet()
for i,v in ipairs(testSet) do
tSet:set(i-1, v.input, v.output)
end
return tSet
end
function Cir.hammingDistanceOnTestSetTerra(circuit, testSet)
local hammingDist = 0
local tCirc,TerraCircuitType = Cir.createTerraCircuit(circuit)
local terraTestSet = Cir.createTerraTestSet(testSet)
local terra hammingDistOnTest()
var terraCircuit = tCirc()
var tSet : TestSet = terraTestSet
var result = terraCircuit:hammingErrorOnTestSet(tSet)
tSet:free()
return result
end
print("Before terra compilation")
local hammingDist = hammingDistOnTest()
print("Hamming dist "..hammingDist)
return hammingDist
end
function Cir.toGraphviz(circuit, filename)
local graph = graphviz()
for i=1,#circuit.inputs-2 do
graph:node(uniqueNodeName(circuit.inputs[i]), "input"..i-1)
end
graph:node(uniqueNodeName(circuit.inputs[#circuit.inputs-1]), "0")
graph:node(uniqueNodeName(circuit.inputs[#circuit.inputs]), "1")
for i,v in ipairs(circuit.internalNodes) do
graph:node(uniqueNodeName(v), string.format("%x", v.lutValue))
for j,input in ipairs(v.inputs) do
graph:edge(uniqueNodeName(input), uniqueNodeName(v))
end
end
for i=1,#circuit.outputs do
local v = circuit.outputs[i]
graph:node(uniqueNodeName(v), "output"..i-1)
for j,input in ipairs(v.inputs) do
graph:edge(uniqueNodeName(input), uniqueNodeName(v))
end
end
graph:compile(filename)
end
return Cir