Back to Browse

OpenMP: Master

4.7K views
Mar 31, 2021
2:35

Hi guys! Welcome to HPC education. Today we will be learning about the Master Thread. When an OpenMP program is run, it will use one thread in the sequential sections, and several threads in the parallel sections. There is one thread that runs from the beginning to the end, and this is called the master thread. The parallel sections of the program will cause additional threads to fork. These are called the slave threads. #pragma omp master can define a master section. Code in this section is only executed once by the master thread. The construct takes no arguments and is often seen along with barrier constructs. This is due to the fact that master has no implicit barrier at the end unlike single or parallel for constructs. Each thread has an ID attached to it that can be obtained using a runtime library function called omp_get_thread_num(). The ID of the master thread is 0. Here we are finding the squares of the first 5 numbers. Next we print the intermediate values, before resuming computation. As you can see in the results, we get inaccurate results. Let us see the master construct in action. By using pragma omp master, we ensure that the print loop is only being executed once. By using omp_get_num_thread(), we can see that only thread 0 is executing. Unfortunately we still get an unexpected result. This is due to the lack of an implicit barrier. Now we add a barrier so all threads can reach the barrier first before continuing execution and as you can see the output is correct. That’s all for this video, see you again in the next one.

Download

1 formats

Video Formats

360pmp43.3 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

OpenMP: Master | NatokHD