Download How To Think Like A Computer Scientist

Document related concepts
no text concepts found
Transcript
Building a Computer
From Sand
•Layers of Abstraction
•Building an AND Gate
•AND/OR/NOT Circuits
•Building a CPU
•Parsing/Compiling
•Abstract Data Types
•Algorithms
•AI
Jeff Edmonds
York University
Do you know the basics
of how the things in your world
work?
GCD(a,b) = GCD(x,y)
<x,y>  <y,x mod y>
Layers of Abstraction
Low level
instructions
Emergent
Complexity
Layers of Abstraction
Low level
Emergent
instructions
Complexity
The psychological profiling of a successful person is
mostly the ability to shift levels of abstraction, from
low level to high level.
• To understand the detailed workings.
Donald Kunth
• To understand the big picture.
Layers of Abstraction
Low level
Emergent
instructions
Complexity
The psychological profiling of a successful person is
mostly the ability to shift levels of abstraction, from
low level to high level.
• To understand the detailed workings.
Donald Kunth
• To understand the big picture.
• To understand complex things
in simple ways.
=
Roumani-CSE
6
SEMICONDUCTOR
Roumani-CSE
7
Roumani-CSE
8
Select between two alternatives A and B
Roumani-CSE
9
Roumani-CSE
10
I/O
CPU
DRAM
Roumani-CSE
11
boolean found = list.contains(target);
Vision | Robotics | AI | HCI | DB | Sim | Bio | DC | QC
la
$a0, yes
addi $s0, $0, 550
add $t0, $0, $0
add $t1, $0, $0
lbl: lw
$t2, list($t1)
beq $t2, $s0, ok
addi $t1, $t1, 4
slti $t2, $t1, 40
bne $t2, $0, lbl
la
$a0, no
boolean
found
false;
ok:
addi
$v0,=$0,
4
for (int
i = 0; i < 10 && !found; i++)
syscall
{
jr
$ra
found = (target == list[i]);
}
Roumani-CSE
0x3c011001
0x34240028
Loader
0x20100226
0x00004020
Linker
0x00004820
Memory
Manager
0x3c011001
0x00290821
I/O
Controller
0x8c2a0000
Process
Manager
0x51500006
0x21290004
0x292a0028
0x1540fffa
0x3c011001
0x34240031
0x20020004
0x0000000c
0x03e00008 12
Lets do it again more slowly.
Layers of Abstraction
Building an AND Gate
Building an AND Gate Same as Carbon
Four
electrons
in the
outer ring.
Four
holes
in the
outer ring.
Building an AND Gate Same as Carbon
Each Carbon/Silicon
bonds with four others.
Building an AND Gate Same as Carbon
Building an AND Gate
Electricity does not flow through it
because all the electrons are happy.
Building an AND Gate
Called N
Dope it with what is one to
the right in the periotic table,
i.e. one extra electron.
Electricity flows through it
because this extra electron
moves.
Building an AND Gate
Called P
Dope it with what is one to
the left in the periotic table,
i.e. one extra hole.
Electricity flows through it
because this extra hole
moves.
Building an AND Gate
N
P
Electricity flows from N to P
extra electron to extra hole.
Electricity does not flow from P to N
extra hole to extra electron
Building an AND Gate
N
N
P
Once the electricity starts
the electrons keep tunneling through!!!
Building an AND Gate
N
P
N
Tunneling happens
iff power
across PN
AND
across PNP
Building an AND Gate
y
x
AND
z
Tunneling happens
iff power
across PN
AND
across PNP
Building an AND Gate
y
x
AND
z
Building an AND Gate
y
x
AND
z
And/Or/Not Circuits (acyclic)
And/Or/Not Circuits (acyclic)
•A circuit is a directed acyclic graph of and/or/not gates
•An input X assigns a bit to each incoming wire.
x1 0
x2 1
AND
x30
AND
0
OR
0
1
NOT
OR
0
0
OR
0
The bits percolate
down to the output wires.
And/Or/Not Circuits (acyclic)
5 volts 0 volts
open closed
true
false
yes
no
1
0
And/Or/Not Circuits (acyclic)
Decimal
927310
=
3 100
+ 7 101
+ 2 102
+9
103
Binary
10112
=
1  20
+ 1  21
+ 0  22
+1
 23
And/Or/Not Circuits (acyclic)
Add 2 n-bit numbers
***********
+ ***********
***********
x0 y0 c0
x1 y1 c1 +
z0
x2 y2 c2 +
z1
* * * * * * * * * * * * x3 y3 c3 +
z2
x4 y4 c4 +
z3
x5 y5 c5 +
z4
n size and n and depth.
+
x5 y5 c5
With more care
z5
+
can be done in log n depth.
z z
Multiplexor
And/Or/Not Circuits (acyclic)
log r bits x1 xlogr
forms address x
y1
Multiplexor
r bits to be addressed
yr
yx The addressed bit
Multiplexor
And/Or/Not Circuits (acyclic)
¬x1¬x2 ¬x3 …xlogr¬x1 ¬x2 ¬x3 x4 … ¬xlogr
AND
AND
y1
yi
AND
AND
All r value that
might get addressed.
Get the bit addressed.
…
x1 x2 ¬x3 … ¬xlogr
AND
yr
AND
OR
Multiplexor outputs addressed bit yx.
Circuit Size ≈ 2logr = r = size of table.
Building a CPU
Building a CPU
Clock: 2.5 GHz = 2,500,000,000 cycles/sec
Clock
Building a CPU
• When clock is low, it remembers previous value.
xt+1
• When clock is high, new value is stored.
c
¬c
zt =
xt
yt
AND
if clock=1
if clock=0
cycle
xt
Memory yxtt-1
xztt-1
AND
OR
yt
Clock
Building a CPU
State of CPU & Computer at time t.
accumulator
register
A
B
program counter
RAM
PC
10
Add *B
to A
38
Building a CPU
State of CPU & Computer at time t.
accumulator
register
A
B
RAM
program counter
PC
10
Add *B
to A
Multiplexor
Command
Add *B
to A
38
Building a CPU
State of CPU & Computer at time t.
accumulator
register
A
B
RAM
program counter
PC
10
38
Command
Add *B
to A
Multiplexor
*B
Add *B
to A
38
Building a CPU
State of CPU & Computer at time t.
accumulator
register
A
B
program counter
RAM
PC
10
Add *B
to A
*B
38
Command
Add *B
to A
Multiplexor
10
38
Add
New A
48
38
Building a CPU
State of CPU & Computer at time t.
accumulator
register
A
B
program counter
RAM
PC
10
Add *B
to A
Save A
to *B
38
Inc
New A
48
New B
New PC
Building a CPU
State of CPU & Computer at time t.
accumulator
register
A
B
program counter
PC
10
RAM
Clock
Add *B
to A
Save A
to *B
38
New A
48
New B
New PC
boolean found = false;
for (int i = 0; i < 10 && !found; i++)
{
found = (target == list[i]);
la
$a0,} yes
addi $s0, $0, 550
0x3c011001
0x34240028
add $t0, $0, $0
0x20100226
add $t1, $0, $0
0x00004020
lbl: lw
$t2, list($t1)
0x00004820
beq $t2, $s0, ok
0x3c011001
addi $t1, $t1, 4
0x00290821
slti $t2, $t1, 40
0x8c2a0000
bne $t2, $0, lbl
0x51500006
0x21290004
la
$a0, no
0x292a0028
ok: addi $v0, $0, 4
0x1540fffa
syscall
0x3c011001
jr
$ra
0x34240031
0x20020004
0x0000000c
0x03e00008
Roumani-CSE
44
Compiling Java into Machine Code
Java Model:
• fancy data structures
• loops
• recursions
• object oriented
Machine Model:
• one line of simple
instructions.
Compiling Java into Machine Code
(3+4)7
exp
term
fact  fact
(
exp
)
term + term
fact
fact
3
4
7
Algorithm: GetExp( s, i )
Exp
p<term,1>
+
p<term,2>
A simple
recursive
algorithm!
+ …+
p<term,k>
Abstract Data Types
boolean found = list.contains(target);
Roumani-CSE
48
Abstract Data Types
Roumani-CSE
49
Abstract Data Types
Set: Collects a bunch of elements together.
List: A set with an order on the elements
Stack: A list, but elements can only be
pushed onto and popped from the top.
Queue: A list, but elements can only be
added at the end and removed from
the front.
Priority Queue: The “highest priority” element
is handled next.
Tree: A hierarchal structure
Graph: Edges between
the elements/nodes
Abstract Data Types
Pointers: Store the address of more data.
tree
Thinking Abstractly About Algorithms
Roumani-CSE
52
<preCond>
codeA
loop
<loop-invariant>
exit when <exit Cond>
codeB
codeC
<postCond>
Iterative Algorithms
I implored you to not worry
about the entire computation.
5 km
9 km
A loop invariant is a
statement/picture
about the state of your computation
to make sure it does not get lost.
Your algorithm must only maintain it
while making progress
Iterative Algorithms
Graph Search
Recursive Algorithms
A recursive algorithm is one that calls itself.
MULT(X,Y):
If |X| = |Y| = 1 then return( XY )
Break X into a,b and Y into c,d
e = MULT(a,c) and f =MULT(b,d)
return( e 10n + (MULT(a+b, c+d)
– e - f) 10n/2 + f )
X = 21
Y = 23
ac = 4
bd = 3
(a+b)(c+d) = 15
XY = 483
X=2
Y=2
XY=4
X=1
Y=3
XY=3
X=3
Y=5
XY=15
X = 2133
Y = 2312
ac = 483
bd = 396
(a+b)(c+d) = 1890
XY = 4931496
I implored you to not worry
about the entire computation.
X = 33
Y = 12
ac = 3
bd = 6
(a+b)(c+d) = 18
XY = 396
X=3
Y=1
XY=3
X=3
Y=2
XY=6
X = 54
Y = 35
ac = 15
bd = 20
(a+b)(c+d) = 72
XY = 1890
X=6
Y=3
XY=18
X=5
Y=3
XY=15
X=4
Y=5
XY=20
X=9
Y=8
XY=72
Recursive Algorithms
A recursive algorithm is one that calls itself.
MULT(X,Y):
If |X| = |Y| = 1 then return( XY )
Break X into a,b and Y into c,d
e = MULT(a,c) and f =MULT(b,d)
return( e 10n + (MULT(a+b, c+d)
– e - f) 10n/2 + f )
X = 2133
Y = 2312
ac = 483
bd = 396
(a+b)(c+d) = 1890
XY = 4931496
You can give your “friend”
any instance that is
– smaller
– meets the precondition
Trust her to give you
the answer.
Recursive Algorithms
A recursive algorithm is one that calls itself.
Recursive Algorithms
Fourier Transformations
O(nlogn) time!
58
Artificial Intelligence
Roumani-CSE
59
Neural Nets
Neural Nets
x1 x2 x3 … xn
w1 w2 w3 … wn
Threshold Gate
Binary Inputs
Real Weights
possibly negative
Threshold
T
y
Real Threashold
Binary Output
y = 1 iff Σi wi×xi ≥ T
Neural Nets
Bicycle
Face
The neural net learns
by adjusting weights wi.
Wrong
Right
Genetic Algorithms
Loop
Given a population of solutions.
Randomly cross two.
Keep best.
Quantum Machines
What about Quantum Machines?
•A quantum TM is in the super-position of states.
•Factoring can be done in poly-time, ie 62×3.
•Not that much more.
Human
What about the human brain?
•Science:
•The brain is just an elaborate machine (neural net).
•New Age:
•The brain has quantum mechanic and vibrations
> machine
•Religion:
•The human has a soul
>> machine
Emergent
Intelligence
Do
you know
the basics
of how the things in your world
work?
GCD(a,b) = GCD(x,y)
<x,y>  <y,x mod y>
Areas of Study
Vision | Robotics | AI | HCI | DB | Sim | Bio | DC | QC
Roumani-CSE
67
End
Building a Computer from Sand
I will give a brief history of computing; outline
the layers of abstraction within which one
thinks about computing; discuss how to build
an AND gate from silicon, circuits from AND
gates, and a CPU from circuits; sketch how to
compile a high level program into machine
code; mention the basic data structures; and the
paradigms of algorithms; closing with some
thoughts on artificial intelligence.
I don't know if there is too much here for an
hour. But can stop when an hour is up.