OpenMP Atomics











>> YOUR LINK HERE: ___ http://youtube.com/watch?v=iS6IG7nzCSo

-------------------------------------------------------------------------------------------- • Hey guys! Welcome to HPC Education and today we’ll be looking at the atomic construct in OpenMP. • The atomic directive of openmp Ensures that a specific memory location is updated atomically, which prevents the possibility of multiple, simultaneous reading and writing of threads. A race condition exists when two unsynchronized threads access the same shared variable with at least one thread modifying the variable; this can cause unpredictable results. To avoid race conditions, all accesses of the locations that could potentially occur in parallel must be protected with an atomic construct. So With the help of omp atomic directive, one can write more efficient concurrent algorithms with fewer locks. • This is the syntax for the atomic directive. • There are different clauses for the atomic directive such as update, read, write and capture. • This is the syntax for the update clause. It Updates the value of a variable atomically. Guarantees that only one thread at a time updates the shared variable, avoiding errors from simultaneous writes to the same variable. An omp atomic directive without a clause is equivalent to an omp atomic update. • Let's see an example to see what happens when we don’t use omp atomic update. So In this example I have defined MAX to be 10, initialized the count to 0 and then used the #pragma omp parallel num_threads(MAX) and inside which I have incremented the value of count. Now the expected answer is 10 but as we can see we are getting different output every time like 8,9,10. This is because multiple threads are trying to simultaneously update the same variable. • Thus when we use the omp atomic update construct it ensures that only one thread at a time updates a shared variable avoiding errors from simultaneous writes to the same variable. And as we can see in this example we get a consistent result which is 10 in this case every time we run this program. • This is the syntax for the read clause. Reads the value of a variable atomically. The value of a shared variable can be read safely, avoiding the danger of reading an intermediate value of the variable when it is accessed simultaneously by a concurrent thread. • Lets see an example to see what happens when we don’t use omp atomic read. In this example I have initialized the value of maximum to 0. I have created an array of random integers using the rand function. Then I have displayed the elements of the array. We have then used the #pragma omp parallel for using which we have computed the maximum element of the array. So this for loop iterates through the array and if the element is greater than the maximum then it sets the value of that element to maximum. So at the end of the iteration we obtain the maximum element. But in this case what we observe is that we get inconsistent output ie. 29358 and 26962 in this case • When we use omp atomic read it ensures that it reads the value safely avoiding the danger of reading an intermediate value of a variable and thus we get consistent output which in this case is 29358. • This is the syntax for the write clause. Writes the value of a variable atomically. The value of a shared variable can be written exclusively to avoid errors from simultaneous writes. • Lets see an example to see what happens when we don’t use omp atomic write. Here I have initialized the value of N to 5. I have created an array of 5 elements which are 10,10,10,10,10 and have initialized the sum to 0. Then I have used the #pragma omp parallel for which would iterate through all the elements of the array and compute its sum. So here the expected value we should get is 50. But what we observe is that we get inconsistent results like 40,50. • Thus when we use the omp atomic write the value of a shared variable is written exclusively by a single thread at a time which avoids errors from simultaneous writes. And as we can see in this example we get a consistent result which is 50 in this case every time we run this program. • • This is the syntax for the capture clause. It Updates the value of a variable while capturing the original or final value of the variable atomically. • • • Lets see an example of the capture clause . So In this example I have defined MAX to be 100, initialized the count to 0 and then used the #pragma omp parallel num_threads(MAX) and inside which I have incremented the value of count and assigned it to the variable k. And thus the capture clause updates the value while capturing the original or final value atomically and returns us 99 in this case. • Thanks for watching, see you again in the next video. Bye!

#############################









Content Report
Youtor.org / YTube video Downloader © 2025

created by www.youtor.org