Skip to content

Commit 70aa25f

Browse files
authored
Merge pull request #2478 from vidyalathabadde/Atomic_b1
Adding openmp Atomic sample to openACCtoOpenMP tools folder
2 parents 47fbb8d + e131a2e commit 70aa25f

File tree

7 files changed

+810
-0
lines changed

7 files changed

+810
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright Intel Corporation
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Define the compiler
2+
CC = icpx
3+
4+
# Define the compiler flags
5+
CFLAGS = -fiopenmp -fopenmp-targets=spir64
6+
7+
8+
# Define the source files
9+
SRCS = ./openMP_migrated_output/atomic_translated.c
10+
11+
# Define the object files
12+
OBJS = $(SRCS:.c=.o)
13+
14+
# Define the target executable
15+
TARGET = Atomic
16+
17+
# Default rule to build the target
18+
all: $(TARGET)
19+
20+
# Rule to build the target executable
21+
$(TARGET): $(OBJS)
22+
$(CC) $(CFLAGS) $(OBJS) -o $(TARGET)
23+
24+
# Rule to compile the source files into object files
25+
%.o: %.c
26+
$(CC) $(CFLAGS) -c $< -o $@
27+
28+
# Rule to run the target executable
29+
run: $(TARGET)
30+
./$(TARGET)
31+
32+
# Clean rule to remove the generated files
33+
clean:
34+
rm -f $(OBJS) $(TARGET)
35+
36+
# Phony targets
37+
.PHONY: all clean
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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).
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
* Line 18 contains the following statement
2+
#pragma acc parallel loop copy(data[0:n]) copyout(readSum)
3+
that has been as been translated into:
4+
#pragma omp target teams loop map(tofrom:data[0:n]) map(from:readSum)
5+
6+
7+
* Line 21 contains the following statement
8+
#pragma acc atomic read
9+
that has been as been translated into:
10+
#pragma omp atomic read
11+
12+
13+
* Line 28 contains the following statement
14+
#pragma acc parallel loop copy(data[0:n]) copyout(writeSum)
15+
that has been as been translated into:
16+
#pragma omp target teams loop map(tofrom:data[0:n]) map(from:writeSum)
17+
18+
19+
* Line 31 contains the following statement
20+
#pragma acc atomic write
21+
that has been as been translated into:
22+
#pragma omp atomic write
23+
24+
25+
* Line 37 contains the following statement
26+
#pragma acc parallel loop copy(data[0:n]) copyout(captureSum)
27+
that has been as been translated into:
28+
#pragma omp target teams loop map(tofrom:data[0:n]) map(from:captureSum)
29+
30+
31+
* Line 40 contains the following statement
32+
#pragma acc atomic capture
33+
that has been as been translated into:
34+
#pragma omp atomic capture
35+
36+
37+
* Line 49 contains the following statement
38+
#pragma acc parallel loop copy(data[0:n]) copyout(updateSum)
39+
that has been as been translated into:
40+
#pragma omp target teams loop map(tofrom:data[0:n]) map(from:updateSum)
41+
42+
43+
* Line 52 contains the following statement
44+
#pragma acc atomic update
45+
that has been as been translated into:
46+
#pragma omp atomic update
47+
48+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "iostream"
2+
#include "stdlib.h"
3+
4+
int main(){
5+
int n = 100;
6+
double * data = (double *)malloc( n * sizeof(double));
7+
for( int x = 0; x < n; ++x){
8+
data[x] = x;
9+
}
10+
11+
double readSum = 0.0;
12+
double writeSum = 0.0;
13+
double captureSum = 0.0;
14+
double updateSum = 0.0;
15+
16+
#pragma omp target teams loop map(tofrom:data[0:n]) map(from:readSum)
17+
for(int x = 0; x < n; ++x){
18+
if(data[x] >= n/2){
19+
#pragma omp atomic read
20+
readSum = x;
21+
}
22+
}
23+
24+
#pragma omp target teams loop map(tofrom:data[0:n]) map(from:writeSum)
25+
for(int x = 0; x < n; ++x){
26+
if(data[x] >= n/2){
27+
#pragma omp atomic write
28+
writeSum = x*2 + 1;
29+
}
30+
}
31+
32+
#pragma omp target teams loop map(tofrom:data[0:n]) map(from:captureSum)
33+
for(int x = 0; x < n; ++x){
34+
if(data[x] >= n/2){
35+
#pragma omp atomic capture
36+
captureSum = data[x]--;
37+
}
38+
}
39+
40+
std::cout << captureSum << std::endl;
41+
42+
#pragma omp target teams loop map(tofrom:data[0:n]) map(from:updateSum)
43+
for(int x = 0; x < n; ++x){
44+
if(data[x] >= n/2){
45+
#pragma omp atomic update
46+
updateSum++;
47+
}
48+
}
49+
return 0;
50+
}
51+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"guid": "D739EAEB-568B-437B-B044-48A23DD9894A",
3+
"name": "Atomic",
4+
"categories": ["Toolkit/oneAPI-samples/Tools/OpenACCToOpenMP"],
5+
"description": "This openMP sample illustrates the read, write, update & capture clauses for the atomic directive",
6+
"toolchain": [ "icpx" ],
7+
"dependencies": [ "compiler|icpx","openmp" ],
8+
"languages": [ { "c": { "properties": { "projectOptions": [ { "projectType": "makefile" } ] } } } ],
9+
"targetDevice": [ "GPU" ],
10+
"gpuRequired": ["pvc"],
11+
"os": [ "linux" ],
12+
"builder": [ "make" ],
13+
"ciTests": {
14+
"linux": [{
15+
"steps": [
16+
"make",
17+
"make run"
18+
]
19+
}]
20+
},
21+
"expertise": "Concepts and Functionality"
22+
}

0 commit comments

Comments
 (0)