0% found this document useful (0 votes)
41 views2 pages

Deadlock Free Code

The document discusses deadlocks and how to avoid them in multithreaded programs using OpenMP. It provides an example of deadlock that can occur if a thread is waiting to acquire a lock that another thread holds. The solution is to inline a function call that acquires the lock to avoid the deadlock situation. It also discusses how to ensure atomic updates to shared memory locations using the OpenMP atomic pragma. Finally, it lists some key factors that affect the performance of OpenMP programs, such as the performance of single-threaded code, how much of the program can run in parallel, data locality, load balancing, and thread utilization.

Uploaded by

bsgindia82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Deadlock Free Code

The document discusses deadlocks and how to avoid them in multithreaded programs using OpenMP. It provides an example of deadlock that can occur if a thread is waiting to acquire a lock that another thread holds. The solution is to inline a function call that acquires the lock to avoid the deadlock situation. It also discusses how to ensure atomic updates to shared memory locations using the OpenMP atomic pragma. Finally, it lists some key factors that affect the performance of OpenMP programs, such as the performance of single-threaded code, how much of the program can run in parallel, data locality, load balancing, and thread utilization.

Uploaded by

bsgindia82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Thus, the do_work function could never complete. This is a deadlock situation.

The simple way to fix the problem in the previous code is to do the inlining of the dequeue function in the do_work function as follows: Deadlock free code:
void do_work(NODE *node) { #pragma omp critical (x) { node->next->data = n!(node->data)" node = node->next" # #

It is sometimes you may require that a statement in a high level language complete in its entirety before a thread is suspended. !or example, a statement x"" is translated into a sequence of machine instructions such as: load reg, [x]; add reg 1; store [x], reg; It is possible that the thread is swapped out between two of these machine instructions. The atomic pragma directs the compiler to generate code toensure that the specific memory storage is updated atomically. The following code example shows a usage of the atomic pragma. int main#$ % float y&'((()* int k, idx&'((()* +pragma omp parallel for shared#y, idx$ for # k , (* k - .(((* k""$ % idx&k) , k / '(((* y&idx&k)) , ..(* 0 +pragma omp parallel for shared#y, idx$ for # k , (* k - .(((* k""$ % +pragma omp atomic y&idx&k)) ", ..( 1 #k / '((($* 0 return (* 0 2n expression statement that is allowed to use the atomic pragma must be with one of the following forms: x binop , exp x"" ""x x3 x

Department CSE, SCAD CET

In the preeding expressions, x is an lvalue expression with scalar type* expr is an expression with scalar type and does not reference the ob4ect designed by x* binop is not an overloaded operator and is one of ", 1, , 5, 6, 7 , 8 , --, or 99 for the :5:"" language. Factors Affecting Performance of OpenMp Program: ;pen <p provides a simple and portable way for paralleli=e the applications to develop threaded applications. Threaded application performance with ;pen <p is largely dependent on the following program. The >nderlying performance of single threaded core. The percentage of program #i.e$run in parallel 6 its scalability. :?> utili=ation datalocality#availability of data$ 6 load balancing.#each thread

Department CSE, SCAD CET

You might also like