Remove Locks

In the Source window, you located the synchronization objects that caused significant waits while the processor cores were underutilized and generated multiple wait count. To resolve the issues, do the following:

  1. Open the code editor.

  2. Modify the code to remove locks.

  3. Recompile the project and check the result.

Open the Code Editor



Depending on the sample code version, your source line numbers may slightly differ from the numbers provided in this tutorial.


Click the Source Editor button to open the nqueens_parallel.f90 file in your default editor:

Remove Locks

The critical section introduced in line 141 protects the global variable from a race condition in a multithreaded application but it spawns a redundant synchronization. To resolve this issue, you may replace the critical section with an atomic operation as follows:

  1. Edit like 141 to replace the OMP CRITICAL with the OMP ATOMIC directive.

  2. Comment out or remove line 143.

A threads barrier, created by OpenMP* directive in line 159, synchronizes the threads but creates a lock with long Wait time. You may resolve this by enabling dynamic load scheduling as follows:

  1. Edit line 159 to add the SCHEDULE(DYNAMIC) directive to the OpenMP pragma:

  2. Save your changes.

Recompile the Project and Check the Result

  1. Browse to the directory where you extracted the sample code (for example, /home/vtune/nqueens_fortran/linux).

  2. Rebuild your target in the release mode using the make command as follows:

    $ make clean

    $ make

    The nqueens_parallel application is rebuilt.

  3. Run nqueens_parallel as follows:

    ./nqueens_parallel 15

    System runs the nqueens_parallel. Note that execution time reduced from 3174 ms to 3075 ms.

Key Terms

Parent topic: Finding Hotspots