Tuesday, November 20, 2018

Concurrency VS Parallelism.

Actual Parallelism VS feel of Parallelism

Why a feel of parallelism and why not actual?

Concurrency is about design, parallelism is about hardware’s

Concluding

Further readings


Actual Parallelism VS feel of Parallelism

Technical vocabulary in IT industry are sometimes very confusing and “Concurrency” and “Parallelism” is one of them. Many developers think “Concurrency and parallelism means executing at the same time” which is right 50% but with one big difference:-

  • Concurrency gives you a feel of parallelism and parallelism as the name implies is actual parallelism.

Feel of parallelism means you execute multiple tasks on the same core and the core switches context between tasks and serves them. You can also term this has time slicing / over lapping time period because your single core is just dedicating some time to one task and then some time to other.

Actual parallelism means you execute multiple task on multiple cores parallely.

Note: - “Concurrency is a broader term and Parallelism is subset of it”.

Mapping to the real world the left image depicts parallelism the right image depicts concurrency.

Why a feel of parallelism and why not actual?

In order to achieve actual parallelism we need dedicated cores, separate memory and so on.
We need MORE RESOURCES.

Let’s say we want to show a progress bar for some task completed. Now we really do not want to have separate cores allocated to display the progress.

We do not want PERFORMANCE here we want that physiologically the end user feels both tasks are happening simultaneously.

We want to just beat the human eye capability of 100 FPS and give an illusion of parallelism without stressing our computer resources. But let’s say we want to process big excel files with million records then yes we would love to have actual parallelism to achieve performance.

Concurrency is about design, parallelism is about hardware’s

In order to achieve concurrency we need to compose our application logic independently. For instance let’s say you want to process employee data where you want to increment the salary by x% and bonus by x%.

So you can decompose the application in to logical units by following different designs:-

Design 1

  • Divide data in to 50% size each.
  • Process each 50% as separate unit.

Design 2

  • Process bonus calculation as separate unit.
  • Process salary calculation as separate unit.

Design 3

  • Divide data in to 50% size each.
  • For every 50% data process bonus calculation separately and salary calculation separately.

There can be many such designs and combination. So when you say your application is supporting concurrency your application should be composed in to small independent units.

Now you take these units and run on one core (Concurrency) or you run on multiple cores (Parallelism). So concurrency is about design while on parallelism we talk more from hardware perspective, 2 core, 3 cores and so on.

If you try to run every concurrent code as parallel you have resource starvation unnecessarily. So ask yourself do you want an illusion (concurrent) or do you performance (parallel).

Concluding

Concurrency Parallelism
Basic definition Executing multiple task on same core using overlapping or time slicing. Executing multiple tasks on different core.
Goal Feeling of parallelism without stressing out resources. Actual parallelism for performance.
Perspective Software design: - Composition of independently executing computations in a co-operative fashion. Hardware: - Executing computation parallel.
Resource utilization Light Heavy
  • Parallelism is a subset of concurrency.
  • Concurrency enables parallelism.
  • Concurrency is more about software design while parallelism is more about hardware’s.
  • Concurrency gives an illusion of parallelism while parallelism is about performance.
  • Concurrency just needs one core while parallelism needs at least 2 cores.

Further readings

Concurrency is not parallelism by Rob pike https://www.youtube.com/watch?v=cN_DpYBzKso

Nice discussion on concurrency and parallelism

https://stackoverflow.com/questions/1897993/what-is-the-difference-between-concurrent-programming-and-parallel-programming