
Every Computer System Is a State Machine - aptxkid
https://blog.the-pans.com/state-machine/
======
epaulson
Margo Seltzer and folks wrote a fun paper about this a few years back at
ASPLOS:

"We present an architecture designed to transparently and automatically scale
the performance of sequential programs as a function of the hardware resources
available. The architecture is predicated on a model of computation that views
program execution as a walk through the enormous state space composed of the
memory and registers of a singlethreaded processor. Each instruction execution
in this model moves the system from its current point in state space to a
deterministic subsequent point. We can parallelize such execution by
predictively partitioning the complete path and speculatively executing each
partition in parallel. Accurately partitioning the path is a challenging
prediction problem. We have implemented our system using a functional
simulator that emulates the x86 instruction set, including a collection of
state predictors and a mechanism for speculatively executing threads that
explore potential states along the execution path. While the overhead of our
simulation makes it impractical to measure speedup relative to native x86
execution, experiments on three benchmarks show scalability of up to a factor
of 256 on a 1024 core machine when executing unmodified sequential programs."

[https://collaborate.princeton.edu/en/publications/asc-
automa...](https://collaborate.princeton.edu/en/publications/asc-
automatically-scalable-computation) and a talk:
[https://www.youtube.com/watch?v=MHZDXC4zJ0c](https://www.youtube.com/watch?v=MHZDXC4zJ0c)

~~~
hbogert
speculative memoization on execution states? Wow, that can never be energy
efficient, can it?

/update she actually says the word memoization in 22:15, so much for original
thought.

------
pedrocr
This is using the terminology a bit strangely. Usually a Turing machine is
something that's in a complexity class above a stack automaton which is in a
complexity class above a finite state machine. And then we think of computers
as Turing machines because that's helpful but they are in practice finite
state machines because they have a limited amount of state. But the
equivalence between a Turing machine and a state machine in the article is
only true if the state in the state machine can be infinite.

------
rantwasp
i can extrapolate even further and say that everything is a state machine.
humans are a state machine, plants are a state machine, reality itself is a
state machine.

we don’t see it like this because of the sheer complexity, but in the end we
are a continuous computation that occurs at every moment in time.

~~~
aptxkid
I don't think this is true. Continuous transition can't be modeled as a state
machine as far as I know.

~~~
jhrmnn
Not in the standard regime of finite state machines. If you allow for infinite
continuous state machines and infinitesimal steps, then it’s no problem. Every
classical mechanical system is fully specified by positions and velocities.
Transitions follow Newtonian mechanics. Every non-relativistic isolated
quantum system is specified by the wave function and the transitions follow
the Schrödinger equation.

~~~
rantwasp
for my own clarification purposes: why is finite vs infinite an issue? IMHO, i
don’t believe we have truly infinite state unless we’re talking about
mathematical abstraction.

~~~
jhrmnn
Don’t know if it is. But in computer science state machines are usually finite
or at least discrete. Time evolution in quantum mechanics is certainly
continuous, not discrete. Discrete are only some observables.

~~~
rantwasp
i think we maybe are mixing measuring with the actual evolution of the state.
you cannot observe a quantum system without altering its state. also, there is
no such thing as an isolated quantum system. this is true even for classic
mechanics but we are cool with it since the measurement error is insignificant
compared to what we measure.

i believe that the number of states a quantum system goes through is finite,
albeit maybe very very large and the way we are treating it is based mostly on
the tools we have not what is happening in reality.

------
diegocg
"A Computer is a state machine. Threads are for people who can't program state
machines" (Alan Cox)

~~~
barbegal
What does this mean in practise? I think this comes from the days of single
core machines where going to multiple threads can't improve performance. But
even on a single core device, threading is routinely used because a single
state machine becomes to big to easily program

------
noodlesUK
Surely it isn’t though? A state machine is generally considered to be a finite
state automaton, isn’t it? Turing machines are substantially higher up the
Chomsky hierarchy than finite state automata.

Edit: take finite out of it, and then I suppose it’s equivalent to a Turing
machine, but it’s a weird use of terminology

~~~
esmi
A digital computer is definitely a FSM. In fact, creating the state diagram of
a simple calculator, and mapping that to digital logic by hand, and then
building it on a breadboard used to be a staple of most introductory computer
architecture courses. Now they just simulate everything. :)

This is the technique
[http://faculty.etsu.edu/tarnoff/ntes2150/statemac/statemac.h...](http://faculty.etsu.edu/tarnoff/ntes2150/statemac/statemac.htm)

------
nickpsecurity
More like a composition of interacting, state machines. Far as computers, one
model applied well to many things is Abstract, State Machines. They're like
Turing Machines for structures vs tape or whatever. They've been used in
verification of hardware and software, too. Asmeta is an example tool.

------
PeterStuer
A _real_ computer is not a state machine nor a Turing machine nor an abstract
closed discrete model. It isn't a closed system. It is connected to the rest
of the universe through IO, noise sources, and time has meaning there

~~~
danbruc
State machines can have inputs and outputs - the new state is a function of
the current state and the current input, the output is a function of the
current state and optionally the current input. Noise is just part of the
input and time obviously has meaning for state machines, too, as state
transitions happen as time progresses.

~~~
PeterStuer
I'm not saying state machines don't have changing state. I'm referring to the
closed world assumption in the model. Also, I'm not saying state machines are
not useful models for some types of computer use as in 'all models are wrong,
some models are useful'. I use state machines in solutions all the time. It is
a very useful pattern.

However, a statemachine model will not let me predict whether my computer
driving an actuator will dribble this basketball or just randomly slap it. It
is not going to help me predicting the size of a buffer needed to losslesly
accept a certain rate of incoming packets.

Now in each of these cases I could extend my initial computer model to capture
the relevant information. I could add a clock running 'ticks' for my
'computer', I could add a second clock running 'ticks' for the universe (i
know, but let's keep it simple, remember, _all_ models are wrong) and model
the external system with wich my 'computer' interacts inside my new model and
the above questions could be answered. But now I have no longer modeled a just
a computer. You have modeled a closed world universe as a statemachine.

There is a huge difference for me in saying 'some computations can usefully be
modeled as a state machine' and, to quote from the article, '[A] Computer,
physically, is nothing more than a storage of various states, and
combinational logic based on all the states (Program Counter, register values,
RAM, Carry Flag, etc.) for state transition.'. Because even though usefull,
like Newtinion Physics, or atomic models that look like small planets orbiting
a sun can be usefull models, it is not complete. It will never tell me how my
linear algebra library needs to be optimized for specific processor dies to
minimize thermal throttling.

it is not just an incomplete but a leaky abstraction.

As an aside remember 'Row Hammer' [1]? That was sheer poetry in this regard.
Using physical properties of a computer system to influence computation in a
virtual computer from a different virtual computer just because they run on
the same physical underlying hardware. The computer memory hardware itself was
supposed to have abstracted this, the hypervisor was supposed to ahve
abstracted the computer, and the VM was supposed to be a computer abstraction
on top of the hypervisor.

~~~
danbruc
I see what you mean but I think it is not totally correct to attribute this to
a closed world assumption, after all state machines can have inputs and
outputs. I would say the important thing is that in most of those scenarios
there is an external feedback loop, the output affects future inputs and this
makes the behavior unpredictable as you do not know the sequence of inputs
unless you can accurately model the effect of the output on the input.

On the other hand that is not really different from a scenario without
external feedback where you just do not know the input - if you do not know
the sensor input, you can not predict the movement of the actuator but it does
not really make a difference whether you just do not know the sensor input for
arbitrary reasons or because of unknown effects of the outputs via the
actuator on the sensor inputs.

Thermal throttling is a similar example, there is a feedback loop between the
computations you perform, the heat this generates, and how the processor
reacts to the resulting temperature sensor inputs. The unknown variable is
again the sensor input, whether due to heat from the die are me removing the
cooling fins.

Row Hammer is an example where the physical implementation has additional
state changes not intended by the designer and which are even subject to
variation in the production process. If you are just modelling the intended
behavior than the model we obviously not be correct in cases were the
unintended behavior is relevant.

In the end I would say that a computer is just a state machine as long as you
stay in a regime where the physical implementation is not relevant but at some
point this will break. But I also think that this is pretty academic, modern
computers are so complex that thinking about it as a state machine will almost
always not be useful at all, even in the most well behaved scenarios without
the physical implementation becoming relevant or complex external feedback
loops.

------
aphextron
Wouldn't that imply that P/NP is not a problem? All turing machines are
capable of processing non-deterministic instructions, making them nothing like
a state machine.

~~~
jerf
You're on the right track, but the wrong problem. What this _theoretically_
renders irrelevant is the halting problem. Ignoring IO (which allows the
importing of arbitrary amounts of state from the exterior world), it is in
fact possible to take a real computer and determine if a program will halt.
It's easy. You just run it and record every state it passes through. It will
either return to a previously-existing state in a finite period of time, in
which case the program does not terminate, or it will halt in a finite period
of time.

The problem is that it doesn't take much computer before the fact that the
halting problem is theoretically solvable doesn't matter much. The Commodore
64 had 524,288 bits, which means even ignoring the other hardware that could
have its own states it has 2^524288 states, approx. equal 10^157,828 states.
You can't fit a record of all of the states that it might pass through in our
universe. And it gets exponentially worse with every bit you add. An
impoverished computer with a mere gigabyte of RAM would be 10^2,585,827,973
states.

So while in theory our computers are state machines, in practice we are much
better suited to using the tools of Turing machines to analyze their behavior.

(I'm pretty sure you could construct an argument using the usual formulation
of the halting problem to prove there is no practical easier way to tell that
a computer will halt in general, but it would be more involved than I can
sketch out. There is more to it than just swapping out "Turing machine" for
"Turing machine limited to a tape of size X" everywhere.)

(Edit: Incidentally, I skimmed over the article the first time, assuming it
was based on this observation. Deeper reading shows that it doesn't mean this,
and in fact I don't actually know what it is intending to say, honestly. But
the above still holds. Technically, all computers are state machines, not
Turing machines, as Turing machines don't fit in our universe.)

~~~
sibrahim
There's no need to store all previous states to detect a cycle: it can be done
using just twice the original memory.

You can model the state transitions as a linked list and use Floyd's classical
tortoise and hare algorithm to (eventually) determine a cycle exists.
Initialize "tortoise" and "hare" as two copies of initial state and advance
hare by two instructions and tortoise by one each iteration. A cycle is
reported if the two states become equal again.

~~~
jwalton
It's true your solution will work, but you're essentially trading space
complexity for time complexity. With 2^524288 states, instead of requiring
more RAM than there are atoms in the universe worth of RAM, you'll now need
more seconds of compute time than we have left before the heat death of the
universe.

~~~
sibrahim
Note that the time taken is linear with respect to the original execution.

A cycle of n instructions starting at instruction n0 will be detected in
between n0 and n0+n iterations (i.e. <= 3*(n0+n) underlying instructions)
since n0 iterations gets both into the cycle and the offset between them will
become 0 some time in the next n iterations.

n could be very large, of course (e.g, using all of memory as a giant counter
so the cycle length is huge), but the cycle detection is not really making
your problem worse.

