2. SJ Campus Model
Speed up from 4 minutes to 40 seconds (6 times faster)
If also include Optimisation time, we speed up around 12 times.
3. Concurrency vs Parallelism
• Rob Pike, the inventor of Go and member of Unix team
• Gave a talk in 2013, when introducing the Go concurrency primitives
• “Concurrency is NOT parallelism”
• https://blog.golang.org/waza-talk
4. Concurrency vs Parallelism
• The modern world is parallel
• CPU, Network, Users, Workers, etc
• A lot of programming language provides concurrency primitives
• Concurrent execution
• Synchronisation and messaging
5. Concurrency vs Parallelism
• Concurrency
• Programming as the composition of independently executing
processes.
• Parallelism
• Programming as the simultaneous execution of (possibly related)
computations.
6. Concurrency vs Parallelism
• Concurrency is about dealing with lots of things at once.
• Parallelism is about doing lots of things at once.
• Concurrency is about structure, parallelism is about execution.
• Concurrency provides a way to structure a solution to solve a problem
that may (but not necessarily) be parallelizable.
7. Concurrency plus communication
• Concurrency is a way to structure a program by breaking it into pieces
that can be executed independently.
• Communication is the means to coordinate the independent executions.
• CSP (Communicating Sequential Processes), 1978
• https://www.cs.cmu.edu/~crary/819-f09/Hoare78.pdf
9. Parallelism example
Concurrent Dictionary (Meshes, …)
Thread 1: IO, Read and parse data from disk.
Thread 2: loop forever and
if the mesh does not have
collider, attach it.
Thread 3: loop forever and if there
is new mesh instances, merge it.
4 threads used
including main thread.
10. Problem?
Concurrent Dictionary (Meshes, …)
Thread 1: IO, Read and parse data from disk.
Thread 2: loop forever and
if the mesh does not have
collider, attach it.
Thread 3: loop forever and if there
is new mesh instances, merge it.
Workload: High
Workload: Medium, but
need wait for main thread
Workload: low
Low-level concurrency model is
NOT able to utilise the
computational resources
11. Problem?
Concurrent Dictionary (Meshes, …)
Thread 1: IO, Read and parse data from disk.
Thread 2: loop forever and
if the mesh does not have
collider, attach it.
Thread 3: loop forever and if there
is new mesh instances, merge it.
Loop forever, busy wait
Workload: low
Loop forever, busy wait
Low-level concurrency model
creates OVERHEAD
12. Modern Concurrency Model
• Task (promise, coroutine, worker) model
• Thread pool model
• Be able to utilise all computational resources
• Be able to write code in sequential way
13. Task Model
Concurrent Queue (Tasks…) Push task to the queue
Thread 1: loop forever and
try dequeue, if there is a
task, executes it.
Thread 2: loop forever and
try dequeue, if there is a
task, executes it.
Thread 3: loop forever and
try dequeue, if there is a
task, executes it.
Worker Threads
Thread Pool
14. Problem?
Concurrent Queue (Tasks…) Push task to the queue
Thread 1: loop forever and
try dequeue, if there is a
task, executes it.
Thread 2: loop forever and
try dequeue, if there is a
task, executes it.
Thread 3: loop forever and
try dequeue, if there is a
task, executes it.
Worker Threads
When thread 1 is busy, thread 2 can
take the task and execute
Be able to utilise all resources (if
there are tasks in the queue)
15. Problem?
Concurrent Queue (Tasks…) Push task to the queue
Thread 1: loop forever and
try dequeue, if there is a
task, executes it.
Thread 2: loop forever and
try dequeue, if there is a
task, executes it.
Thread 3: loop forever and
try dequeue, if there is a
task, executes it.
Worker Threads
If there is no tasks or just a few
tasks in the queue, we can reduce
the amount of the worker threads.
And increase them when more
tasks come in. (Less overhead)
16. Dependency?
Concurrent Queue (Tasks…) Push task to the queue
Thread 1: loop forever and
try dequeue, if there is a
task, executes it.
Thread 2: loop forever and
try dequeue, if there is a
task, executes it.
Thread 3: loop forever and
try dequeue, if there is a
task, executes it.
Worker Threads
What if tasks have dependency? i.e.
task 2 must execute after task 1.
We can enqueue task 2 after task 1
finishes.
17. Priority?
Concurrent Queue (Tasks…) Push task to the queue
Thread 1: loop forever and
try dequeue, if there is a
task, executes it.
Thread 2: loop forever and
try dequeue, if there is a
task, executes it.
Thread 3: loop forever and
try dequeue, if there is a
task, executes it.
Worker Threads
We can use multiple task queues if
task has priority. Or we can create a
special task queue for main thread.
Concurrent Queue (Tasks…)
Concurrent Queue (Tasks…)
18. Question
• Unity StartCoroutine
• Is it a concurrency model?
• Does it run parallel?
• How does Unity implement the coroutine?
• What is the potential issue of the Task model?