Multi Threading
Multi Threading
Algorithms
Motivatio
n
int main() {
int I , n=5;
int array[5] ={0,1,4,9,16};
#pragma omp parallel for
for (i = 0; i < n; i++) {
printf("Thread processes index %d\n", i);
array[i] = array[i] * 2; // Example operation
printf("array[%d] = %d\n", i, array[i]);
}
return 0;
}
Fibonacci
Numbers
Definitio
n
F1 = 1
Fi = Fi-1 + Fi-2
for i > 1.
Naive
Algorithm
Computing the Fibonacci numbers can be done
with the following algorithm:
Fibonacci(n)
if n < 2 then return
n; x =
Fibonacci(n-1); y
= Fibonacci(n-2) ;
return x + y;
Running
Time
The run time if each vertex of the DAG has its own processor.
• Work W or T1 (n). Total time to execute the entire computation on
one processor. Defined as the number of vertices in the
computation DAG
• Tp(n). Total time to execute entire computation with p processors
2.125.
Consequently, achieving much more than double the
speedup is impossible, no matter how many processors we
employ to execute the computation.
Schedulin
g
The performance depends not just on the work and
span. Additionally, the strands must be scheduled
efficiently onto the processors of the parallel
machines.
The strands must be mapped to static threads, and
the operating system schedules the threads on
the processors themselves.
The scheduler must schedule the computation with
no advance knowledge of when the strands will be
spawned or when they will complete; it must
operate online.
Greedy
Scheduler
We will assume a greedy scheduler in our analysis,
since this keeps things simple. A greedy scheduler
assigns as many strands to processors as possible
in each time step.
On P processors, if at least P strands are ready to
execute during a time step, then we say that the
step is a complete step; otherwise we say that it is
an incomplete step.
Greedy Scheduler
Theorem
TP <= T1 / P + T∞
T1(n)/T∞(n) = θ( ((1+sqrt(5))/2)n / n)
Parallelism:
Acknowledgeme
nts
https://www.youtube.com/watch?v=UaCX8Iy00DA
https://www.youtube.com/watch?v=VD8hY7kWjdc
https://www.youtube.com/watch?v=7T-gjX24FR0
https://www.slideshare.net/AndresMendezVazquez/24-multith
re
aded-algorithms
• https://homes.luddy.indiana.edu/achauhan/Teaching/B403/L
ect ureNotes/11-multithreaded.html
• https://catonmat.net/mit-introduction-to-algorithms-part-thi
rt een
• https://www.youtube.com/watch?v=iFrmLRr9ke0
• https://www.youtube.com/watch?v=GvtgV2NkdVg&t=31s
• https://www.youtube.com/watch?v=_XOZ2IiP2nw
• Analysis of merge sort :
https://www.youtube.com/watch?v=0nlPxaC
2lTw