.. _ParVConc:

.. raw:: html

   <script>ODSA.SETTINGS.DISP_MOD_COMP = true;ODSA.SETTINGS.MODULE_NAME = "ParVConc";ODSA.SETTINGS.MODULE_LONG_NAME = "Parallelism vs. Concurrency";ODSA.SETTINGS.MODULE_CHAPTER = "Parallel and Distributed Systems"; ODSA.SETTINGS.BUILD_DATE = "2021-06-01 12:51:47"; ODSA.SETTINGS.BUILD_CMAP = false;JSAV_OPTIONS['lang']='en';JSAV_EXERCISE_OPTIONS['code']='java';</script>


.. |--| unicode:: U+2013   .. en dash
.. |---| unicode:: U+2014  .. em dash, trimming surrounding whitespace
   :trim:


.. This file is part of the OpenCSF eTextbook project. It was
.. auto-generated by scripts from the OpenDSA eTextbook project.
.. See https://OpenCSF.org for more details. OpenCSF is distributed
.. under a Creative Commons Attribution-NonCommercial 4.0 International
.. License (see http://creativecommons.org/licenses/by-nc/4.0/),
.. Copyright (c) 2019-2021 by Michael S. Kirkpatrick. OpenDSA is
.. distributed under an MIT open source license, Copyright (c) 2012-2021
.. by the OpenDSA Project Contributors.

.. avmetadata::
   :author: Michael S. Kirkpatrick
   :requires:
   :satisfies: 
   :topic: 

Parallelism vs. Concurrency
===========================

As a starting point, it is important to emphasize that the terms
:term:`concurrency` and :term:`parallelism` are often used as synonyms, but
there is a distinction. Both terms generally refer to the execution of multiple
tasks within the same time frame. However, concurrency does not necessarily mean
that the tasks are simultaneously running at any given moment. Instead,
concurrency can be achieved on a single-core processor through the use of
:term:`multiprogramming`. In multiprogramming, the OS rapidly switches back and
forth between multiple programs that have been loaded into memory. We can say
that blocks of the two programs' instructions are :term:`interleaved`, meaning
that the processor alternates which program is running. Because this happens so
quickly, the concurrency provided by multiprogramming creates the illusion of
parallel execution. The user believes the two programs are running at the same
time, but they actually are not. Within a single program, concurrency is focused
on the logical structure of the tasks involved. For example, a program with a
graphical user interface employs concurrency by creating separate threads for
handling keyboard input, auto-saving backup copies of modified files, responding
to mouse clicks or screen touches, and so on. 

In contrast, parallelism means that the multiple tasks are simultaneously
executing. The goal in parallelism is focused more on improving the
:term:`throughput` (the amount of work done in a given amount of time) and
:term:`latency` (the time until completion of a task) of the system. In essence,
parallelism is focused on trying to do more work faster. Parallel execution
implies that there is concurrency, but not the other way around. As a starting
point for parallel programming, we often talk about identifying opportunities
for concurrency, then apply techniques to parallelize the concurrent tasks.

Multiprocessing Systems
-----------------------

.. _DualCore:

.. figure:: Images/CSF-Images.9.1.png
   :align: right
   :width: 90%
   :figwidth: 40%
   :alt: A typical cache and core arrangement for a dual-core system

   A typical cache and core arrangement for a dual-core system

Parallelism requires hardware that is capable of :term:`multiprocessing`, which
is the ability to execute multiple processes simultaneously. In the case of a
:term:`multicore` system (which would include a typical modern laptop computer),
the CPU has multiple distinct physical *processing cores* that are all
capable of executing instructions. That is, each core acts as a miniature
version of a CPU, with its own instruction control unit, ALU, and cache memory.
In a multicore system, the OS can arrange to execute the two programs
simultaneously on separate cores. :num:`Figure #DualCore` shows the
logical structure of a typical dual-core system, with three levels of caching.
In this scenario, the L2 and L3 cache levels are *unified*, storing both
data and instructions; the L1 caches are *banked*, with one cache per core
devoted to instructions and another devoted to data.

Another form is :term:`symmetric multiprocessing (SMP) <symmetric
multiprocessing>`, which is the class of systems that contain multiple CPUs
interconnected within a single machine. As all CPUs in an SMP system share the
same memory, the hardware design tends to be complex, making the system
expensive to build and maintain. Cluster systems, on the other hand, use
multiple machines closely connected on a network. :term:`Cluster <cluster system>` systems tend
to use standard hardware, making it possible to drastically increase the number
of processing units relative to a similarly priced SMP system. The tradeoff is
that communication between nodes in a cluster is slower than SMP, because the
communication takes place over a network rather than through shared memory.

The power of multiprocessing is not restricted to kernel designers. Rather, many
types of systems and application software can be built to leverage these
platforms for complex and efficient software. While concurrent program can be
challenging, there are many common parallel design patterns that provide
effective strategies for exploiting parallelism.

.. avembed:: Exercises/ParallelDistributed/ParallelSumm.html ka
   :module: ParVConc
   :long_name: Parallelism vs. concurrency questions
   :threshold: 5

