<p>Since you know that you must process each element and tasks are independent, i.e., the algorithm is <em>embarrassingly parallel</em>, in the Dynamic Multithreading model you should simply use a <em>parallel for</em> instead of spawning threads as you do, and let the compiler implement this optimally.</p>

<p>The work (sequential time) is $T_1 = O(nk)$ because you need to process each element and each element requires O(k) time. The span is $T_\infty = O(log n + k)$, and the parallelism is $T_1 / T_\infty = nk / (log n + k)$. In general, you know that, regarding $T_p$, we can only say that:</p>

<p>$T_1/p \leq T_p \leq T_1/p +  T_\infty$</p>

<p>$T_\infty \leq T_p \leq T_1/p + T_\infty$</p>

<p>This is because $T_p$ is a function of the work, span, size $n$ and scheduler behavior. And $T_\infty$ actually means for your algorithm $p = n$. </p>

<p>Now, let's analyze what happens to the speedup by using the Generalized Amdahl's law (derived by Lee). This law describes the speedup of parallel applications in which the computation is made of phases and in each phase a different number of processors is actually used. Think about each level of the directed acyclic graph corresponding to the computation as a phase.</p>

<p>Let $q_i$ be the percentage of a parallel application executed simultaneously by $i$ processors; then, we have:</p>

<p>${T_1} = \sum\limits_{i = 1}^p {{q_i}} {T_1}$</p>

<p>${T_p} = \sum\limits_{i = 1}^p {\frac{{{q_i}}}{i}} {T_1}$</p>

<p>Therefore, the speedup is</p>

<p>$\frac{{{T_1}}}{{{T_p}}} = \frac{{\sum\limits_{i = 1}^p {{q_i}} {T_1}}}{{\sum\limits_{i = 1}^p {\frac{{{q_i}}}{i}} {T_1}}} = \frac{{\sum\limits_{i = 1}^p {{q_i}} }}{{\sum\limits_{i = 1}^p {\frac{{{q_i}}}{i}} }} = \frac{1}{{\sum\limits_{i = 1}^p {\frac{{{q_i}}}{i}} }}$</p>

<p>Why is this useful? Because in general it allows you to derive useful bounds. For instance, assuming $q_i = 1/p$ then we can derive an upper bound for the speedup as follows:</p>

<p>$\frac{{{T_1}}}{{{T_p}}} = \frac{1}{{\sum\limits_{i = 1}^p {\frac{{{q_i}}}{i}} }} = \frac{1}{{\frac{1}{p}\sum\limits_{i = 1}^p {\frac{1}{i}} }} = \frac{p}{{\sum\limits_{i = 1}^p {\frac{1}{i}} }} \leqslant \frac{p}{{\log p}}$</p>

<p>In your algorithm, $q_i$ is different from zero only for values of $p$ which are powers of 2; i.e.,  $q_i = 2^i, i = 0,...,\log p$. In the corresponding summation $q_i$ will be divided by $2^i$, giving rise to a summation in which each term is 1. As an example, for $p=16$ the nonzero $q_i$ values are 1,2,4,8,16 and these values must be divided respectively by 1,2,4,8,16 so the sum is 5 and the speedup is 1/5. In general, the speedup of your algorithm is:</p>

<p>$\frac{{{T_1}}}{{{T_p}}} = \frac{1}{{\sum\limits_{i = 1}^p {\frac{{{q_i}}}{i}} }} = \frac{1}{{\sum\limits_{j = 0}^{\log p} {\frac{{{2^i}}}{{{2^i}}}} }} = \frac{1}{{(\log p) + 1}}$</p>

<p>Now compare this with the performances you can obtain in the message passing model, in which your algorithm is executed on $p$ processors. Since the algorithm is embarrassingly parallel, $T_p = O(\frac{n}{p} k)$ (domain decomposition is trivial and requires O(1) time) and the speedup is (as expected for an embarrassingly parallel algorithm) $T_1/T_p = p.$</p>

CC BY-SA 3.0
2013-10-06T15:07:11.970Z
I don't think in my algorithm $q_i = 1/p$. That would mean, for example, that for the first phase we'll have $q_1 = 1$, so $q_1 T_1 = T_1$, which is all the work, when actually the work in that phase is $\frac{T_1 - nk}{n}$ (this is the work of each internal node).
Anyway, I got the idea.  I'll try to work a little with the formula to see if I get what I'm looking for. Thanks!
ees_cu
CC BY-SA 3.0
2013-10-06T16:56:35.473Z
Yes, you are right. I just gave you an example. However, determining $q_i$ in your algorithm should not be too much difficult. I am going to update the answer providing additional details.
Massimo Cafaro
CC BY-SA 3.0
2013-10-05T10:00:48.207Z
2013-10-06T17:19:10.143Z
2013-10-06T17:19:10.143Z
Massimo Cafaro
<p>I would say that the speedup is asymptotic to 1/p because it's only the top levels of the tree which can't use all processors (e.g. where the number of nodes is less than p). As n approaches infinity this becomes an ever smaller proportion of the algorithm.</p>

CC BY-SA 3.0
2013-10-06T15:12:16.507Z
The problem with this is that $p$ may also grow as much as you want, and we have to take it into account.
ees_cu
CC BY-SA 3.0
2013-10-04T23:08:35.513Z
2013-10-04T23:08:35.513Z
David Cummins
<p>Suppose an algorithm that receive an input array of $n$ elements and it performs a task over each element. All tasks are independent and take $O(k)$ each (being $k$ a variable). Since all tasks are independent this is a fully parallelizable problem.</p>

<p>I have a basic algorithm that uses the Divide and Conquer technique. It receive the input array $A$ and if $|A| &gt; 1$ then it <strong>spawn</strong> a new thread to process the first half of $A$, and recursively call itself to process the second half. This recursions stops when the function is called with a single element, in which case it runs a series of steps that takes $O(k)$.</p>

<p>I´m using the <em>Dynamic Multithreading</em> model (<a href="http://rads.stackoverflow.com/amzn/click/0262033844" rel="nofollow">Introduction to Algorithms</a>, chapter 27) to analyze this algorithm. So, I have an execution graph with n "leafs", representing the basic cases ($|A| = 1$), and it needs $O(n)$ "internal nodes" to divide the work until it reaches the leafs. Given that the longest path has length $O(\log n)$ with a constant cost for each internal node, then the total cost will be $T_\infty = O(\log (n) + k)$ (which is the <em>span</em>). I also can get other measures like the total work $T_1 = O(n + kn)$, and the <em>parallelism</em> $\frac{T_1}{T_\infty} = \frac{n + nk}{k + \log n}$.</p>

<p>Another performance measure of interest is the <em>speedup</em>, which is defined as $\frac{T_1}{T_p}$ (where <strong><em>p</em></strong> is the number of processors used to compute $T_p$). </p>

<p>Finally my question is how do I get $T_p$ (analytically) so I can calculate the speedup?
I cannot simply divide $T_1/p$ since in the first levels of the graph/tree I won't be using all the processors.</p>

<p>Is there any more elegant expression for this than simply dividing the height of the tree before and after $\log p$?</p>

<ds.algorithms><time-complexity><dc.parallel-comp>
Task Multithreading analysis for a Divide and Conquer algorithm
ees_cu
# Task Multithreading analysis for a Divide and Conquer algorithm

(Asked by: username_0 on 2013-10-01)

Suppose an algorithm that receive an input array of $n$ elements and it performs a task over each element. All tasks are independent and take $O(k)$ each (being $k$ a variable). Since all tasks are independent this is a fully parallelizable problem.

I have a basic algorithm that uses the Divide and Conquer technique. It receive the input array $A$ and if $|A| > 1$ then it **spawn** a new thread to process the first half of $A$, and recursively call itself to process the second half. This recursions stops when the function is called with a single element, in which case it runs a series of steps that takes $O(k)$.

I´m using the *Dynamic Multithreading* model ([Introduction to Algorithms](http://rads.stackoverflow.com/amzn/click/0262033844), chapter 27) to analyze this algorithm. So, I have an execution graph with n "leafs", representing the basic cases ($|A| = 1$), and it needs $O(n)$ "internal nodes" to divide the work until it reaches the leafs. Given that the longest path has length $O(\log n)$ with a constant cost for each internal node, then the total cost will be $T\_\infty = O(\log (n) + k)$ (which is the *span*). I also can get other measures like the total work $T\_1 = O(n + kn)$, and the *parallelism* $\frac{T\_1}{T\_\infty} = \frac{n + nk}{k + \log n}$.

Another performance measure of interest is the *speedup*, which is defined as $\frac{T\_1}{T\_p}$ (where ***p*** is the number of processors used to compute $T\_p$).

Finally my question is how do I get $T\_p$ (analytically) so I can calculate the speedup?
I cannot simply divide $T\_1/p$ since in the first levels of the graph/tree I won't be using all the processors.

Is there any more elegant expression for this than simply dividing the height of the tree before and after $\log p$?

**Tags:** ds.algorithms, time-complexity, dc.parallel-comp


---

## 2 answers

### Answer by username_1 on 2013-10-05. Score: 3 | Accepted answer

Since you know that you must process each element and tasks are independent, i.e., the algorithm is *embarrassingly parallel*, in the Dynamic Multithreading model you should simply use a *parallel for* instead of spawning threads as you do, and let the compiler implement this optimally.

The work (sequential time) is $T\_1 = O(nk)$ because you need to process each element and each element requires O(k) time. The span is $T\_\infty = O(log n + k)$, and the parallelism is $T\_1 / T\_\infty = nk / (log n + k)$. In general, you know that, regarding $T\_p$, we can only say that:

$T\_1/p \leq T\_p \leq T\_1/p + T\_\infty$

$T\_\infty \leq T\_p \leq T\_1/p + T\_\infty$

This is because $T\_p$ is a function of the work, span, size $n$ and scheduler behavior. And $T\_\infty$ actually means for your algorithm $p = n$.

Now, let's analyze what happens to the speedup by using the Generalized Amdahl's law (derived by Lee). This law describes the speedup of parallel applications in which the computation is made of phases and in each phase a different number of processors is actually used. Think about each level of the directed acyclic graph corresponding to the computation as a phase.

Let $q\_i$ be the percentage of a parallel application executed simultaneously by $i$ processors; then, we have:

${T\_1} = \sum\limits\_{i = 1}^p {{q\_i}} {T\_1}$

${T\_p} = \sum\limits\_{i = 1}^p {\frac{{{q\_i}}}{i}} {T\_1}$

Therefore, the speedup is

$\frac{{{T\_1}}}{{{T\_p}}} = \frac{{\sum\limits\_{i = 1}^p {{q\_i}} {T\_1}}}{{\sum\limits\_{i = 1}^p {\frac{{{q\_i}}}{i}} {T\_1}}} = \frac{{\sum\limits\_{i = 1}^p {{q\_i}} }}{{\sum\limits\_{i = 1}^p {\frac{{{q\_i}}}{i}} }} = \frac{1}{{\sum\limits\_{i = 1}^p {\frac{{{q\_i}}}{i}} }}$

Why is this useful? Because in general it allows you to derive useful bounds. For instance, assuming $q\_i = 1/p$ then we can derive an upper bound for the speedup as follows:

$\frac{{{T\_1}}}{{{T\_p}}} = \frac{1}{{\sum\limits\_{i = 1}^p {\frac{{{q\_i}}}{i}} }} = \frac{1}{{\frac{1}{p}\sum\limits\_{i = 1}^p {\frac{1}{i}} }} = \frac{p}{{\sum\limits\_{i = 1}^p {\frac{1}{i}} }} \leqslant \frac{p}{{\log p}}$

In your algorithm, $q\_i$ is different from zero only for values of $p$ which are powers of 2; i.e., $q\_i = 2^i, i = 0,...,\log p$. In the corresponding summation $q\_i$ will be divided by $2^i$, giving rise to a summation in which each term is 1. As an example, for $p=16$ the nonzero $q\_i$ values are 1,2,4,8,16 and these values must be divided respectively by 1,2,4,8,16 so the sum is 5 and the speedup is 1/5. In general, the speedup of your algorithm is:

$\frac{{{T\_1}}}{{{T\_p}}} = \frac{1}{{\sum\limits\_{i = 1}^p {\frac{{{q\_i}}}{i}} }} = \frac{1}{{\sum\limits\_{j = 0}^{\log p} {\frac{{{2^i}}}{{{2^i}}}} }} = \frac{1}{{(\log p) + 1}}$

Now compare this with the performances you can obtain in the message passing model, in which your algorithm is executed on $p$ processors. Since the algorithm is embarrassingly parallel, $T\_p = O(\frac{n}{p} k)$ (domain decomposition is trivial and requires O(1) time) and the speedup is (as expected for an embarrassingly parallel algorithm) $T\_1/T\_p = p.$

> **Comments:**
> **username_0**: I don't think in my algorithm $q_i = 1/p$. That would mean, for example, that for the first phase we'll have $q_1 = 1$, so $q_1 T_1 = T_1$, which is all the work, when actually the work in that phase is $\frac{T_1 - nk}{n}$ (this is the work of each internal node). Anyway, I got the idea.  I'll try to work a little with the formula to see if I get what I'm looking for. Thanks!  
> **username_1**: Yes, you are right. I just gave you an example. However, determining $q_i$ in your algorithm should not be too much difficult. I am going to update the answer providing additional details.  

---

### Answer by username_2 on 2013-10-04. Score: 0

I would say that the speedup is asymptotic to 1/p because it's only the top levels of the tree which can't use all processors (e.g. where the number of nodes is less than p). As n approaches infinity this becomes an ever smaller proportion of the algorithm.

> **Comments:**
> **username_0**: The problem with this is that $p$ may also grow as much as you want, and we have to take it into account.  

---

