Skip to content

Commit 783ee2c

Browse files
committed
Parallel: dependencies
1 parent f623e4f commit 783ee2c

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

docs/day4/parallel.rst

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -434,26 +434,30 @@ in Python rather than learning to write those codes.
434434
Optional flags for ``srun`` for writing output and error files are ``-o output_%j.out -e error_%j.err`` instead
435435
of writing on the terminal screen.
436436

437-
**Data dependencies**
437+
.. warning::
438438

439-
In general, one cannot parallelize a serial algorithm; let's take the case of:
439+
**Data dependencies**
440440

441+
In general, one cannot parallelize a serial algorithm; let's take the case of:
441442

442-
.. code-block:: python
443-
444-
a[0] = 0
445-
for i in range(1, N):
446-
a[i] = a[i-1] + i
447-
448-
Let's say we have the elements :code:`a[0], a[1],..., a[5]`
443+
.. code-block:: python
449444
445+
a[0] = 0
446+
for i in range(1, N):
447+
a[i] = a[i-1] + i
450448
451-
Here, the iteration ``i`` depends on the previous iteration ``i-1``. One needs to transform this algorithm:
449+
Let's say we have the elements :code:`a[0], a[1],..., a[5]` and only two processes. Process number one
450+
takes the elements :code:`a[0], a[1], a[2]` and process number two :code:`a[3], a[4], a[5]`.
451+
Because in the loop the iteration ``i`` depends on the previous iteration ``i-1``, process two will have
452+
conflicts computing :code:`a[3]` because it depends on :code:`a[2]` which is owned by process one.
453+
454+
One can avoid data dependencies by transforming the initial algorithm to a more suitable algorithm for
455+
parallelization:
452456

453-
.. code-block:: python
457+
.. code-block:: python
454458
455-
for i in range(N):
456-
a[i] = 0.5 * i * (i + 1)
459+
for i in range(N):
460+
a[i] = 0.5 * i * (i + 1)
457461
458462
459463

0 commit comments

Comments
 (0)