Example
The program below expressed in pseudocode—which applies some arbitrary operation, foo
, on every element in the array d
—illustrates data parallelism:
If the above example program is executed on a 2-processor system the runtime environment may execute it as follows:
- In an SPMD system, both CPUs will execute the code.
- In a parallel environment, both will have access to
d
. - A mechanism is presumed to be in place whereby each CPU will create its own copy of
lower_limit
andupper_limit
that is independent of the other. - The
if
clause differentiates between the CPUs. CPU"a"
will read true on theif
; and CPU"b"
will read true on theelse if
, thus having their own values oflower_limit
andupper_limit
. - Now, both CPUs execute
foo(d)
, but since each CPU has different values of the limits, they operate on different parts ofd
simultaneously, thereby distributing the task among themselves. Obviously, this will be faster than doing it on a single CPU.
This concept can be generalized to any number of processors. However, when the number of processors increases, it may be helpful to restructure the program in a similar way (where cpuid
is an integer between 1 and the number of CPUs, and acts as a unique identifier for every CPU):
For example, on a 2-processor system CPU A (cpuid
1) will operate on odd entries and CPU B (cpuid
2) will operate on even entries.
Read more about this topic: Data Parallelism
Famous quotes containing the word example:
“Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.”
—Marcel Proust (18711922)