Skip to content

Commit 6e75a2e

Browse files
authored
Add bitonic-sort sample. (oneapi-src#27)
1 parent 09c74d7 commit 6e75a2e

File tree

9 files changed

+634
-0
lines changed

9 files changed

+634
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# required cmake version
2+
cmake_minimum_required(VERSION 3.5)
3+
4+
project (bitonic-sort)
5+
6+
if(WIN32)
7+
set(CMAKE_CXX_COMPILER "dpcpp-cl")
8+
else()
9+
set(CMAKE_CXX_COMPILER "dpcpp")
10+
endif()
11+
12+
# Set default build type to RelWithDebInfo if not specified
13+
if (NOT CMAKE_BUILD_TYPE)
14+
message (STATUS "Default CMAKE_BUILD_TYPE not set using Release with Debug Info")
15+
set (CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE
16+
STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel"
17+
FORCE)
18+
endif()
19+
20+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fsycl -std=c++17")
21+
22+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lOpenCL -lsycl")
23+
24+
add_executable (bitonic-sort src/bitonic-sort.cpp)
25+
26+
add_custom_target (run
27+
COMMAND bitonic-sort 21 47
28+
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
29+
)
30+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2020 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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# `Bitonic Sort` sample
2+
3+
This code sample demonstrates the implementation of bitonic sort using Intel Data Parallel C++ to
4+
offload the computation to a GPU. In this implementation, a random sequence of 2**n elements is given
5+
(n is a positive number) as input, the algorithm sorts the sequence in parallel. The result sequence is
6+
in ascending order.
7+
8+
For comprehensive instructions regarding DPC++ Programming, go to
9+
https://software.intel.com/en-us/oneapi-programming-guide
10+
and search based on relevant terms noted in the comments.
11+
12+
| Optimized for | Description
13+
|:--- |:---
14+
| OS | Linux Ubuntu 18.04
15+
| Hardware | Skylake with GEN9 or newer
16+
| Software | Intel® oneAPI DPC++ Compiler (beta); Intel C++ Compiler (beta)
17+
| What you will learn | Implement bitonic sort using Intel DPC++ compiler
18+
| Time to complete | 15 minutes
19+
20+
21+
## Purpose
22+
23+
The algorithm converts a randomized sequence of numbers into
24+
a bitonic sequence (two ordered sequences), and then merge these two ordered
25+
sequences into a ordered sequence. Bitonic sort algorithm is briefly
26+
described as followed:
27+
28+
- First, it decomposes the randomized sequence of size 2\*\*n into 2\*\*(n-1)
29+
pairs where each pair consists of 2 consecutive elements. Note that each pair
30+
is a bitonic sequence.
31+
- Step 0: for each pair (sequence of size 2), the two elements are swapped so
32+
that the two consecutive pairs form a bitonic sequence in increasing order,
33+
the next two pairs form the second bitonic sequence in decreasing order, the
34+
next two pairs form the third bitonic sequence in increasing order, etc, ....
35+
At the end of this step, we have 2\*\*(n-1) bitonic sequences of size 2, and
36+
they follow an order increasing, decreasing, increasing, .., decreasing.
37+
Thus, they form 2\*\*(n-2) bitonic sequences of size 4.
38+
- Step 1: for each new 2\*\*(n-2) bitonic sequences of size 4, (each new
39+
sequence consists of 2 consecutive previous sequences), it swaps the elements
40+
so that at the end of step 1, we have 2\*\*(n-2) bitonic sequences of size 4,
41+
and they follow an order: increasing, decreasing, increasing, ...,
42+
decreasing. Thus, they form 2\*\*(n-3) bitonic sequences of size 8.
43+
- Same logic applies until we reach the last step.
44+
- Step n: at this last step, we have one bitonic sequence of size 2\*\*n. The
45+
elements in the sequence are swapped until we have a sequence in increasing
46+
oder.
47+
48+
The code will attempt first to execute on an available GPU and fallback to the system's CPU
49+
if a compatible GPU is not detected.
50+
51+
## Key Implementation Details
52+
53+
The basic DPC++ implementation explained in the code includes device selector, buffer, accessor, kernel, and command g
54+
roups. Unified Shared Memory (USM) is used for data management.
55+
56+
## License
57+
This code sample is licensed under MIT license
58+
59+
## Building the `bitonic-sort` Program for CPU and GPU
60+
61+
### Running Samples In DevCloud
62+
If running a sample in the Intel DevCloud, remember that you must specify the compute node (CPU, GPU,
63+
FPGA) as well whether to run in batch or interactive mode. For more information see the Intel® oneAPI
64+
Base Toolkit Get Started Guide (https://devcloud.intel.com/oneapi/get-started/base-toolkit/)
65+
66+
### On a Linux* System
67+
1. Build the program using the following `cmake` commands.
68+
```
69+
$ cd bitonic-sort
70+
$ mkdir build
71+
$ cd build
72+
$ cmake ..
73+
$ make
74+
```
75+
76+
2. Run the program:
77+
```
78+
make run
79+
```
80+
81+
3. Clean the program using:
82+
```
83+
make clean
84+
```
85+
86+
### On a Windows* System
87+
* Build the program using VS2017 or VS2019
88+
Right click on the solution file and open using either VS2017 or VS2019 IDE.
89+
Right click on the project in Solution explorer and select Rebuild.
90+
From top menu select Debug -> Start without Debugging.
91+
92+
* Build the program using MSBuild
93+
Open "x64 Native Tools Command Prompt for VS2017" or "x64 Native Tools Command Prompt for
94+
VS2019"
95+
Run - MSBuild bitonic-sort.sln /t:Rebuild /p:Configuration="Release"
96+
97+
## Running the sample
98+
### Application Parameters
99+
100+
Usage: bitonic-sort <exponent> <seed>
101+
102+
where
103+
104+
exponent is a positive number. The according length of the sequence is 2**exponent.
105+
106+
seed is the seed used by the random generator to generate the randomness.
107+
108+
The sample offloads the computation to GPU and then performs the computation in serial in the CPU.
109+
The results from the parallel and serial computation are compared. If the results are matched and
110+
the ascending order is verified, the application will display a “Success!” message.
111+
112+
### Example of Output
113+
```
114+
$ ./bitonic-sort 21 47
115+
Array size: 2097152, seed: 47
116+
Device: Intel(R) Gen9 HD Graphics NEO
117+
Kernel time: 0.416827 sec
118+
CPU serial time: 0.60523 sec
119+
Success!
120+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.1062
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bitonic-sort", "bitonic-sort.vcxproj", "{46454D0B-76F3-45EB-A186-F315A2E22DEA}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Release|x64 = Release|x64
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{46454D0B-76F3-45EB-A186-F315A2E22DEA}.Debug|x64.ActiveCfg = Debug|x64
15+
{46454D0B-76F3-45EB-A186-F315A2E22DEA}.Debug|x64.Build.0 = Debug|x64
16+
{46454D0B-76F3-45EB-A186-F315A2E22DEA}.Release|x64.ActiveCfg = Release|x64
17+
{46454D0B-76F3-45EB-A186-F315A2E22DEA}.Release|x64.Build.0 = Release|x64
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {B1D84B81-F5D5-4459-AA6E-38B695FB908B}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|x64">
5+
<Configuration>Debug</Configuration>
6+
<Platform>x64</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|x64">
9+
<Configuration>Release</Configuration>
10+
<Platform>x64</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<ItemGroup>
14+
<ClCompile Include="src\bitonic-sort.cpp" />
15+
</ItemGroup>
16+
<PropertyGroup Label="Globals">
17+
<VCProjectVersion>15.0</VCProjectVersion>
18+
<ProjectGuid>{46454d0b-76f3-45eb-a186-f315a2e22dea}</ProjectGuid>
19+
<Keyword>Win32Proj</Keyword>
20+
<RootNamespace>bitonic_sort</RootNamespace>
21+
<WindowsTargetPlatformVersion>$(WindowsSDKVersion.Replace("\",""))</WindowsTargetPlatformVersion>
22+
</PropertyGroup>
23+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
24+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
25+
<ConfigurationType>Application</ConfigurationType>
26+
<UseDebugLibraries>true</UseDebugLibraries>
27+
<PlatformToolset>Intel(R) oneAPI DPC++ Compiler</PlatformToolset>
28+
<CharacterSet>Unicode</CharacterSet>
29+
</PropertyGroup>
30+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
31+
<ConfigurationType>Application</ConfigurationType>
32+
<UseDebugLibraries>false</UseDebugLibraries>
33+
<PlatformToolset>Intel(R) oneAPI DPC++ Compiler</PlatformToolset>
34+
<WholeProgramOptimization>true</WholeProgramOptimization>
35+
<CharacterSet>Unicode</CharacterSet>
36+
</PropertyGroup>
37+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
38+
<ConfigurationType>Application</ConfigurationType>
39+
<UseDebugLibraries>true</UseDebugLibraries>
40+
<PlatformToolset>Intel(R) oneAPI DPC++ Compiler</PlatformToolset>
41+
<CharacterSet>Unicode</CharacterSet>
42+
</PropertyGroup>
43+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
44+
<ConfigurationType>Application</ConfigurationType>
45+
<UseDebugLibraries>false</UseDebugLibraries>
46+
<PlatformToolset>Intel(R) oneAPI DPC++ Compiler</PlatformToolset>
47+
<WholeProgramOptimization>true</WholeProgramOptimization>
48+
<CharacterSet>Unicode</CharacterSet>
49+
</PropertyGroup>
50+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
51+
<ImportGroup Label="ExtensionSettings">
52+
</ImportGroup>
53+
<ImportGroup Label="Shared">
54+
</ImportGroup>
55+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
56+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
57+
</ImportGroup>
58+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
59+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
60+
</ImportGroup>
61+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
62+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
63+
</ImportGroup>
64+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
65+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
66+
</ImportGroup>
67+
<PropertyGroup Label="UserMacros" />
68+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
69+
<LinkIncremental>true</LinkIncremental>
70+
</PropertyGroup>
71+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
72+
<LinkIncremental>true</LinkIncremental>
73+
</PropertyGroup>
74+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
75+
<LinkIncremental>false</LinkIncremental>
76+
</PropertyGroup>
77+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
78+
<LinkIncremental>false</LinkIncremental>
79+
</PropertyGroup>
80+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
81+
<ClCompile>
82+
<PrecompiledHeader>
83+
</PrecompiledHeader>
84+
<PrecompiledHeaderFile>
85+
</PrecompiledHeaderFile>
86+
</ClCompile>
87+
<Link>
88+
<SubSystem>Console</SubSystem>
89+
<GenerateDebugInformation>true</GenerateDebugInformation>
90+
</Link>
91+
</ItemDefinitionGroup>
92+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
93+
<ClCompile>
94+
<PrecompiledHeader>
95+
</PrecompiledHeader>
96+
<PrecompiledHeaderFile>
97+
</PrecompiledHeaderFile>
98+
<AdditionalIncludeDirectories>%ONEAPI_ROOT%\dev-utilities\latest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
99+
</ClCompile>
100+
<Link>
101+
<SubSystem>Console</SubSystem>
102+
<GenerateDebugInformation>true</GenerateDebugInformation>
103+
</Link>
104+
</ItemDefinitionGroup>
105+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
106+
<ClCompile>
107+
<PrecompiledHeader>
108+
</PrecompiledHeader>
109+
<PrecompiledHeaderFile>
110+
</PrecompiledHeaderFile>
111+
</ClCompile>
112+
<Link>
113+
<SubSystem>Console</SubSystem>
114+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
115+
<OptimizeReferences>true</OptimizeReferences>
116+
<GenerateDebugInformation>true</GenerateDebugInformation>
117+
</Link>
118+
</ItemDefinitionGroup>
119+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
120+
<ClCompile>
121+
<PrecompiledHeader>
122+
</PrecompiledHeader>
123+
<PrecompiledHeaderFile>
124+
</PrecompiledHeaderFile>
125+
<AdditionalIncludeDirectories>%ONEAPI_ROOT%\dev-utilities\latest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
126+
</ClCompile>
127+
<Link>
128+
<SubSystem>Console</SubSystem>
129+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
130+
<OptimizeReferences>true</OptimizeReferences>
131+
<GenerateDebugInformation>true</GenerateDebugInformation>
132+
</Link>
133+
</ItemDefinitionGroup>
134+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
135+
<ImportGroup Label="ExtensionTargets">
136+
</ImportGroup>
137+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="src\bitonic-sort.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
4+
<LocalDebuggerCommandArguments>21 47</LocalDebuggerCommandArguments>
5+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
6+
</PropertyGroup>
7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
8+
<LocalDebuggerCommandArguments>21 47</LocalDebuggerCommandArguments>
9+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
10+
</PropertyGroup>
11+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"guid": "4D5B57B8-6F34-4A11-89F5-3F07E766DB39",
3+
"name": "bitonic-sort",
4+
"categories": [ "Toolkit/Intel® oneAPI Base Toolkit/oneAPI DPC++ Compiler/CPU and GPU" ],
5+
"description": "Bitonic Sort using Intel® oneAPI DPC++ Language",
6+
"toolchain": [ "dpcpp" ],
7+
"targetDevice": [ "CPU", "GPU" ],
8+
"languages": [ { "cpp": {} } ],
9+
"os": [ "linux", "windows" ],
10+
"builder": [ "ide", "cmake" ],
11+
"ciTests": {
12+
"linux": [{
13+
"steps": [
14+
"mkdir build",
15+
"cd build",
16+
"cmake ..",
17+
"make",
18+
"make run"
19+
]
20+
}],
21+
"windows": [{
22+
"steps": [
23+
"MSBuild bitonic-sort.sln /t:Rebuild /p:Configuration=\"Release\"",
24+
"cd x64/Release",
25+
"bitonic-sort.exe 21 47"
26+
]
27+
}]
28+
}
29+
}

0 commit comments

Comments
 (0)