|
| 1 | +# `Atomic` Sample |
| 2 | + |
| 3 | +This sample illustrates the read, write, update & capture clauses for the atomic directive. The original OpenACC source code is migrated to OpenMP to Offload on Intel® Platforms. |
| 4 | + |
| 5 | +| Area | Description |
| 6 | +|:--- |:--- |
| 7 | +| What you will learn | Migrating Atomic from OpenACC to OpenMP |
| 8 | +| Time to complete | 15 minutes |
| 9 | +| Category | Concepts and Functionality |
| 10 | + |
| 11 | +## Purpose |
| 12 | + |
| 13 | +OpenMP atomic operations allows multiple threads to safely update a shared numeric variable, such as on hardware platforms that support atomic operation use. An atomic operation applies only to the single assignment statement that immediately follows it, so atomic operations are useful for code that requires fine-grain synchronization. |
| 14 | + |
| 15 | +> **Note**: We use intel-application-migration-tool-for-openacc-to-openmp which assists developers in porting OpenACC code automatically to OpenMP code. |
| 16 | +
|
| 17 | +This sample contains two versions in the following folders: |
| 18 | + |
| 19 | +| Folder Name | Description |
| 20 | +|:--- |:--- |
| 21 | +| `openMP_migrated_output` | Contains the OpenMP migrated code. |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +| Optimized for | Description |
| 26 | +|:--- |:--- |
| 27 | +| OS | Ubuntu* 22.04 |
| 28 | +| Hardware | Intel® Gen9 <br> Intel® Gen11 <br> Intel® Data Center GPU Max |
| 29 | +| Software | Intel oneAPI Base Toolkit version 2024.2 <br> intel-application-migration-tool-for-openacc-to-openmp |
| 30 | + |
| 31 | +For more information on how to install the above Tool, visit [intel-application-migration-tool-for-openacc-to-openmp](https://github.com/intel/intel-application-migration-tool-for-openacc-to-openmp) |
| 32 | + |
| 33 | +## Key Implementation Details |
| 34 | + |
| 35 | +This sample demonstrates the migration of the following OpenACC pragmas: |
| 36 | +- #pragma acc parallel loop copy() copyout() |
| 37 | + |
| 38 | + The kernels construct identifies a region of code that may contain parallelism that has been as been translated into: |
| 39 | + - #pragma omp target teams loop map(tofrom:) map(from:) |
| 40 | +- #pragma acc atomic read |
| 41 | + |
| 42 | + The `atomic read` 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. This has been translated into: |
| 43 | + - #pragma omp atomic read |
| 44 | +- #pragma acc atomic write |
| 45 | + |
| 46 | + The `atomic write` writes the value of a variable atomically. The value of a shared variable can be written exclusively to avoid errors from simultaneous writes. This has been translated into: |
| 47 | + - #pragma omp atomic write |
| 48 | +- #pragma acc atomic update |
| 49 | + |
| 50 | + The `atomic update` updates the value of a variable atomically. Allows only one thread to write to a shared variable at a time, avoiding errors from simultaneous writes to the same variable. This has been translated into: |
| 51 | + - #pragma omp atomic update |
| 52 | +- #pragma acc atomic capture |
| 53 | + |
| 54 | + The `atomic capture` updates the value of a variable while capturing the original or final value of the variable atomically. This has been translated into: |
| 55 | + - #pragma omp atomic capture |
| 56 | + |
| 57 | + |
| 58 | +> **Note**: Refer to [Portability across Heterogeneous Architectures](https://www.intel.com/content/www/us/en/developer/articles/technical/openmp-accelerator-offload.html#gs.n33nuz) for general information about the migration of OpenACC to OpenMP. |
| 59 | +
|
| 60 | +## Set Environment Variables |
| 61 | + |
| 62 | +When working with the command-line interface (CLI), you should configure the oneAPI toolkits using environment variables. Set up your CLI environment by sourcing the `setvars` script every time you open a new terminal window. This practice ensures that the compiler, libraries, and tools are ready for development. |
| 63 | + |
| 64 | +## Migrate the `Atomic` Sample |
| 65 | + |
| 66 | +### Migrate the Code using intel-application-migration-tool-for-openacc-to-openmp |
| 67 | + |
| 68 | +For this sample, the tool takes application sources (either C/C++ or Fortran languages) with OpenACC constructs and generates a semantically-equivalent source using OpenMP. Follow these steps to migrate the code |
| 69 | + |
| 70 | + 1. Tool installation |
| 71 | + ``` |
| 72 | + git clone https://github.com/intel/intel-application-migration-tool-for-openacc-to-openmp.git |
| 73 | + ``` |
| 74 | +
|
| 75 | +The binary of the translator can be found inside intel-application-migration-tool-for-openacc-to-openmp/src location |
| 76 | + |
| 77 | + 2. The openacc sample is taken from [Openacc-samples](https://github.com/OpenACC/openacc-examples.git) |
| 78 | + ``` |
| 79 | + cd openacc-examples/Submissions/C/Atomic/ |
| 80 | + ``` |
| 81 | + 3. Now invoke the translator to migrate the openACC pragmas to OpenMP as shown below |
| 82 | + ``` |
| 83 | + intel-application-migration-tool-for-openacc-to-openmp/src/intel-application-migration-tool-for-openacc-to-openmp atomic.c |
| 84 | + ``` |
| 85 | +For each given input-file, the tool will generate a translation file named <input-file>.translated and will also dump a report with translation details into a file named <input-file>.report. |
| 86 | +
|
| 87 | +## Build the `Atomic` Sample for GPU |
| 88 | +
|
| 89 | +> **Note**: If you have not already done so, set up your CLI |
| 90 | +> environment by sourcing the `setvars` script in the root of your oneAPI installation. |
| 91 | +> |
| 92 | +> Linux*: |
| 93 | +> - For system wide installations: `. /opt/intel/oneapi/setvars.sh` |
| 94 | +> - For private installations: ` . ~/intel/oneapi/setvars.sh` |
| 95 | +> - For non-POSIX shells, like csh, use the following command: `bash -c 'source <install-dir>/setvars.sh ; exec csh'` |
| 96 | +> |
| 97 | +> For more information on configuring environment variables, see [Use the setvars Script with Linux* or macOS*](https://www.intel.com/content/www/us/en/develop/documentation/oneapi-programming-guide/top/oneapi-development-environment-setup/use-the-setvars-script-with-linux-or-macos.html). |
| 98 | +
|
| 99 | +### On Linux* |
| 100 | +
|
| 101 | +1. Change to the sample directory. |
| 102 | +2. Build the program. |
| 103 | + ``` |
| 104 | + $ make |
| 105 | + ``` |
| 106 | + |
| 107 | +By default, this command sequence will build the `openMP_migrated_output ` version of the program. |
| 108 | +
|
| 109 | +3. Run the program. |
| 110 | + ``` |
| 111 | + $ make run |
| 112 | + ``` |
| 113 | + |
| 114 | +#### Troubleshooting |
| 115 | +
|
| 116 | +If an error occurs, you can get more details by running `make` with |
| 117 | +the `VERBOSE=1` argument: |
| 118 | +``` |
| 119 | +make VERBOSE=1 |
| 120 | +``` |
| 121 | +If you receive an error message, troubleshoot the problem using the **Diagnostics Utility for Intel® oneAPI Toolkits**. The diagnostic utility provides configuration and system checks to help find missing dependencies, permissions errors, and other issues. See the [Diagnostics Utility for Intel® oneAPI Toolkits User Guide](https://www.intel.com/content/www/us/en/docs/oneapi/user-guide-diagnostic-utility/2024-0/overview.html) for more information on using the utility. |
| 122 | +
|
| 123 | +## License |
| 124 | +Code samples are licensed under the MIT license. See |
| 125 | +[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt) for details. |
| 126 | +
|
| 127 | +Third party program licenses are at [third-party-programs.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/third-party-programs.txt). |
0 commit comments