Skip to content

Commit cb4b513

Browse files
terdnerJoeOster
andauthored
pull request (#101)
* initial commit of openMP example. Signed-off-by: todd.erdner <[email protected]> * Initial commit of the dpc_reduce Signed-off-by: todd.erdner <[email protected]> * added guid to sample.json Signed-off-by: todd.erdner <[email protected]> * fixed sample.json files. Signed-off-by: todd.erdner <[email protected]> * fixed the include files. Somehow I copied a slightly old repo and it still had <chrono> and the omp_common.hpp file. They have been removed. Signed-off-by: todd.erdner <[email protected]> * added license.txt file ran through formating tool one more time removed all calls to "std::endl" and replaced with " \n" Signed-off-by: todd.erdner <[email protected]> * renamed license.txt to License.txt Signed-off-by: todd.erdner <[email protected]> * added "ciTests" to the sample.json file. It passed the check. Signed-off-by: todd.erdner <[email protected]> * fixed make error Signed-off-by: todd.erdner <[email protected]> * fixed sample.json Signed-off-by: todd.erdner <[email protected]> * removed "2020" from the License.txt file due to update guidelines. Signed-off-by: todd.erdner <[email protected]> * added comment regarding where you can find dpc_common in both files per Paul's comments. Signed-off-by: todd.erdner <[email protected]> * Modified names of the functions to represent what they do (ie. calc_pi_*) per suggestion from Paul. Signed-off-by: todd.erdner <[email protected]> * initial check-in to the C++ repo Signed-off-by: todd.erdner <[email protected]> * put correct comment on dpc_common.hpp Signed-off-by: todd.erdner <[email protected]> * added commenting indicating where they can find corresponding include files. Signed-off-by: todd.erdner <[email protected]> * added comment line Signed-off-by: todd.erdner <[email protected]> * removed openMP repo from DPC++ as it will be moved to C++ directory * Update README.md * Update README.md * Update README.md * Update README.md * fixed category line in sample.json to match exact text expected. Co-authored-by: JoeOster <[email protected]>
1 parent 36ff9f9 commit cb4b513

File tree

8 files changed

+272
-5
lines changed

8 files changed

+272
-5
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set(CMAKE_CXX_COMPILER "icpx")
2+
# Set default build type to RelWithDebInfo if not specified
3+
if (NOT CMAKE_BUILD_TYPE)
4+
message (STATUS "Default CMAKE_BUILD_TYPE not set using Release")
5+
set (CMAKE_BUILD_TYPE "Release" CACHE
6+
STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel"
7+
FORCE)
8+
endif()
9+
10+
cmake_minimum_required (VERSION 3.0)
11+
project(openmp_reduction LANGUAGES CXX)
12+
add_subdirectory (src)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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.
8+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# openmp_reduction Sample
2+
3+
The openmp_reduction sample is a simple program that calculates pi. This program is implemented using C++ and openMP for Intel(R) CPU and accelerators.
4+
5+
For comprehensive instructions regarding DPC++ Programming, go to https://software.intel.com/en-us/oneapi-programming-guide and search based on relevant terms noted in the comments.
6+
7+
8+
| Optimized for | Description
9+
|:--- |:---
10+
| OS | Linux* Ubuntu* 18.04,
11+
| Hardware | Skylake with GEN9 or newer
12+
| Software | Intel® oneAPI DPC++ Compiler (beta)
13+
| What you will learn | How to run openMP on cpu as well as GPU offload
14+
| Time to complete | 10 min
15+
16+
## Purpose
17+
This example demonstrates how to do reduction by using the CPU in serial mode,
18+
the CPU in parallel mode (using openMP), the GPU using openMP offloading.
19+
20+
All the different modes use a simple calculation for Pi. It is a well known
21+
mathematical formula that if you integrate from 0 to 1 over the function,
22+
(4.0 / (1+x*x) )dx the answer is pi. One can approximate this integral
23+
by summing up the area of a large number of rectangles over this same range.
24+
25+
Each of the different functions calculates pi by breaking the range into many
26+
tiny rectangles and then summing up the results.
27+
28+
## Key Implementation Details
29+
This code shows how to use OpenMP on the CPU host as well as using target offload capabilities.
30+
31+
## License
32+
This code sample is licensed under MIT license.
33+
34+
## Building the dpc_reduce program for CPU and GPU
35+
36+
### Include Files
37+
The include folder is located at `%ONEAPI_ROOT%\dev-utilities\latest\include` on your development system".
38+
39+
### Running Samples In DevCloud
40+
If running a sample in the Intel DevCloud, remember that you must specify the compute node (CPU, GPU, FPGA) as well whether to run in batch or interactive mode. For more information see the Intel® oneAPI Base Toolkit Get Started Guide (https://devcloud.intel.com/oneapi/get-started/base-toolkit/)
41+
42+
### On a Linux* System
43+
Perform the following steps:
44+
45+
mkdir build
46+
cd build
47+
cmake ..
48+
49+
1. Build the program using the following make commands
50+
make
51+
52+
2. Run the program using:
53+
make run or src/openmp_reduction
54+
55+
3. Clean the program using:
56+
make clean
57+
58+
59+
## Running the Sample
60+
61+
### Application Parameters
62+
There are no editable parameters for this sample.
63+
64+
### Example of Output (result vary depending on hardware)
65+
Number of steps is 1000000
66+
67+
Cpu Seq calc: PI =3.14 in 0.00105 seconds
68+
69+
Host OpenMP: PI =3.14 in 0.0010 seconds
70+
71+
Offload OpenMP: PI =3.14 in 0.0005 seconds
72+
73+
success
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"guid": "ECF6C8EB-753B-4107-AF64-60662CE41726",
3+
"name": "openmp_reduction",
4+
"categories": ["Toolkit/Intel® oneAPI Base Toolkit/oneAPI DPC++ Compiler/oneAPI DPC++ Library/CPU and GPU"],
5+
"description": "It models reduction in different ways showing capability of oneAPI.",
6+
"toolchain": ["dpcpp"],
7+
"languages": [{
8+
"cpp": {}
9+
}],
10+
"targetDevice": ["CPU", "GPU"],
11+
"os": ["linux"],
12+
"builder": ["cmake"],
13+
"ciTests": {
14+
"linux": [
15+
{
16+
"id": "openmp_reduction",
17+
"steps": [
18+
"mkdir build",
19+
"cd build",
20+
"cmake ..",
21+
"make ",
22+
"./src/openmp_reduction"
23+
]
24+
}
25+
]
26+
}
27+
}
28+
29+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
if (NOT CMAKE_CXX_STANDARD)
2+
set(CMAKE_CXX_STANDARD 14)
3+
endif()
4+
5+
if (NOT CMAKE_BUILD_TYPE)
6+
set(CMAKE_BUILD_TYPE RelWithDebInfo)
7+
endif()
8+
9+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fiopenmp -fopenmp-targets=spir64 -fsycl")
10+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
11+
12+
# Add an executable target from source files
13+
add_executable(${PROJECT_NAME} main.cpp)
14+
15+
if(WIN32)
16+
# Specify libraries to link with
17+
target_link_libraries(${PROJECT_NAME} sycl )
18+
19+
# Add custom target for running
20+
add_custom_target(run ${PROJECT_NAME}.exe)
21+
else()
22+
# Add custom target for running
23+
add_custom_target(run ./${PROJECT_NAME})
24+
endif()
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//==============================================================
2+
// Copyright © 2020 Intel Corporation
3+
//
4+
// SPDX-License-Identifier: MIT
5+
// =============================================================
6+
#include <iomanip> // setprecision library
7+
#include <iostream>
8+
9+
// dpc_common.hpp can be found in the dev-utilities include folder.
10+
// e.g., $ONEAPI_ROOT/dev-utilities//include/dpc_common.hpp
11+
#include "dpc_common.hpp"
12+
13+
14+
15+
// cpu_seq_calc_pi is a simple sequential CPU routine
16+
// that calculates all the slices and then
17+
// does a reduction.
18+
float cpu_seq_calc_pi(int num_steps) {
19+
float step = 1.0 / (float)num_steps;
20+
float x;
21+
float pi;
22+
float sum = 0.0;
23+
for (int i = 1; i < num_steps; i++) {
24+
x = ((float)i - 0.5f) * step;
25+
sum = sum + 4.0f / (1.0f + x * x);
26+
}
27+
pi = sum * step;
28+
return pi;
29+
}
30+
31+
// openmp_host_calc_pi is a simple parallel
32+
// calcuation that uses openmp running
33+
// on the host. By default openmp
34+
// will use all the cores available
35+
// and execute the code in parallel and
36+
// then perform a reduction.
37+
float openmp_host_calc_pi(int num_steps) {
38+
float step = (1.0f / num_steps);
39+
float pi = 0.0;
40+
float sum = 0.0;
41+
#pragma omp parallel for reduction(+ : sum)
42+
for (int i = 1; i < num_steps; i++) {
43+
float x = ((float)i - 0.5f) * step;
44+
sum = sum + 4.0f / (1.0f + x * x);
45+
}
46+
pi = step * sum;
47+
return pi;
48+
}
49+
50+
// openmp_device_calc_pi is a simple parallel
51+
// calcuation that uses openmp running
52+
// on the device through the use of the
53+
// target specifier.
54+
// This will execute the code in parallel.
55+
56+
float openmp_device_calc_pi(int num_steps) {
57+
float pi = 0.0;
58+
float step = (1.0f / num_steps);
59+
float sum = 0.0;
60+
#pragma omp target teams distribute parallel for reduction(+ : sum)
61+
for (int i = 1; i < num_steps; i++) {
62+
float x = ((float)i - 0.5f) * step;
63+
sum = sum + 4.0f / (1.0 + x * x);
64+
}
65+
pi = sum * step;
66+
return pi;
67+
}
68+
69+
int main(int argc, char** argv) {
70+
int num_steps = 1000000;
71+
printf("Number of steps is %d\n", num_steps);
72+
float pi;
73+
74+
// Due to the overhead associated with
75+
// JIT, run the offload calculation once
76+
// that allows code to be compiled. Execution
77+
// time is measured the 2nd time you run it.
78+
pi = openmp_device_calc_pi(num_steps);
79+
80+
dpc_common::TimeInterval T;
81+
pi = cpu_seq_calc_pi(num_steps);
82+
auto stop = T.Elapsed();
83+
std::cout << "Cpu Seq calc: \t\t";
84+
std::cout << std::setprecision(3) << "PI =" << pi;
85+
std::cout << " in " << stop << " seconds"
86+
<< "\n";
87+
88+
dpc_common::TimeInterval T2;
89+
pi = openmp_host_calc_pi(num_steps);
90+
auto stop2 = T2.Elapsed();
91+
std::cout << "Host OpenMP:\t\t";
92+
std::cout << std::setprecision(3) << "PI =" << pi;
93+
std::cout << " in " << stop2 << " seconds"
94+
<< "\n";
95+
96+
dpc_common::TimeInterval T3;
97+
pi = openmp_device_calc_pi(num_steps);
98+
auto stop3 = T3.Elapsed();
99+
std::cout << "Offload OpenMP:\t\t";
100+
std::cout << std::setprecision(3) << "PI =" << pi;
101+
std::cout << " in " << stop3 << " seconds"
102+
<< "\n";
103+
104+
std::cout << "success\n";
105+
return 0;
106+
}

DirectProgramming/DPC++/ParallelPatterns/dpc_reduce/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ The dpc_reduce is a simple program that calculates pi. This program is implemen
55

66
For comprehensive instructions regarding DPC++ Programming, go to https://software.intel.com/en-us/oneapi-programming-guide and search based on relevant terms noted in the comments.
77

8-
| Optimized for | Description
8+
9+
| Optimized for | Description
10+
|:--- |:---
911
| OS | Linux* Ubuntu* 18.04,
1012
| Hardware | Skylake with GEN9 or newer,
1113
| Software | Intel® oneAPI DPC++ Compiler (beta)
@@ -65,12 +67,23 @@ There are no editable parameters for this sample.
6567

6668
### Example of Output
6769
Number of steps is 1000000
70+
6871
Cpu Seq calc: PI =3.14 in 0.00348 seconds
72+
6973
Cpu TBB calc: PI =3.14 in 0.00178 seconds
74+
7075
dpstd native: PI =3.14 in 0.191 seconds
76+
7177
dpstd native2: PI =3.14 in 0.142 seconds
78+
7279
dpstd native3: PI =3.14 in 0.002 seconds
80+
7381
dpstd native4: PI =3.14 in 0.00234 seconds
82+
7483
dpstd two steps: PI =3.14 in 0.00138 seconds
84+
7585
dpstd transform_reduce: PI =3.14 in 0.000442 seconds
86+
7687
success
88+
89+

DirectProgramming/DPC++/ParallelPatterns/dpc_reduce/src/main.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
#include <CL/sycl.hpp>
77
#include <iomanip> // setprecision library
88
#include <iostream>
9+
10+
// The include folder is located at %ONEAPI_ROOT%\dev-utilities\latest\include
11+
// on your development system.
912
#include <oneapi/dpl/algorithm>
1013
#include <oneapi/dpl/execution>
1114
#include <oneapi/dpl/iterator>
15+
16+
// dpc_common.hpp can be found in the dev-utilities include folder.
17+
// e.g., $ONEAPI_ROOT/dev-utilities//include/dpc_common.hpp
1218
#include "dpc_common.hpp"
13-
// Many oneAPI code samples share common include files. These
14-
// include files are installed locally with the product installation
15-
// and can be located at %ONEAPI_ROOT%\dev-utilities\latest\include
16-
// on your development system.
1719

1820
using namespace sycl;
1921

0 commit comments

Comments
 (0)