Skip to content

Commit e73af15

Browse files
committed
Add an example demonstrating use of Complex
1 parent d87d4d5 commit e73af15

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ project("alpakaExamples" LANGUAGES CXX)
2929
################################################################################
3030

3131
add_subdirectory("bufferCopy/")
32+
add_subdirectory("complex/")
3233
add_subdirectory("heatEquation/")
3334
add_subdirectory("helloWorld/")
3435
add_subdirectory("helloWorldLambda/")

example/complex/CMakeLists.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#
2+
# Copyright 2014-2021 Erik Zenker, Benjamin Worpitz, Jan Stephan, Sergei Bastrakov
3+
#
4+
# This file exemplifies usage of alpaka.
5+
#
6+
# Permission to use, copy, modify, and/or distribute this software for any
7+
# purpose with or without fee is hereby granted, provided that the above
8+
# copyright notice and this permission notice appear in all copies.
9+
#
10+
# THE SOFTWARE IS PROVIDED “AS IS” AND ISC DISCLAIMS ALL WARRANTIES WITH
11+
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
13+
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
16+
# IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
#
18+
19+
################################################################################
20+
# Required CMake version.
21+
22+
cmake_minimum_required(VERSION 3.15)
23+
24+
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
25+
26+
################################################################################
27+
# Project.
28+
29+
set(_TARGET_NAME complex)
30+
31+
project(${_TARGET_NAME})
32+
33+
#-------------------------------------------------------------------------------
34+
# Find alpaka.
35+
36+
if(NOT TARGET alpaka::alpaka)
37+
option(USE_ALPAKA_SOURCE_TREE "Use alpaka's source tree instead of an alpaka installation" OFF)
38+
39+
if(USE_ALPAKA_SOURCE_TREE)
40+
# Don't build the examples recursively
41+
set(alpaka_BUILD_EXAMPLES OFF)
42+
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../.." "${CMAKE_BINARY_DIR}/alpaka")
43+
else()
44+
find_package(alpaka REQUIRED)
45+
endif()
46+
endif()
47+
48+
#-------------------------------------------------------------------------------
49+
# Add executable.
50+
51+
alpaka_add_executable(
52+
${_TARGET_NAME}
53+
src/complex.cpp)
54+
target_link_libraries(
55+
${_TARGET_NAME}
56+
PUBLIC alpaka::alpaka)
57+
58+
set_target_properties(${_TARGET_NAME} PROPERTIES FOLDER example)
59+
60+
add_test(NAME ${_TARGET_NAME} COMMAND ${_TARGET_NAME})

example/complex/src/complex.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* Copyright 2021 Sergei Bastrakov
2+
*
3+
* This file exemplifies usage of alpaka.
4+
*
5+
* Permission to use, copy, modify, and/or distribute this software for any
6+
* purpose with or without fee is hereby granted, provided that the above
7+
* copyright notice and this permission notice appear in all copies.
8+
*
9+
* THE SOFTWARE IS PROVIDED “AS IS” AND ISC DISCLAIMS ALL WARRANTIES WITH
10+
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
12+
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15+
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16+
*/
17+
18+
#include <alpaka/alpaka.hpp>
19+
#include <alpaka/example/ExampleDefaultAcc.hpp>
20+
21+
#include <cstdint>
22+
#include <iostream>
23+
24+
25+
//! Complex numbers demonstration kernel
26+
struct ComplexKernel
27+
{
28+
template<typename TAcc>
29+
ALPAKA_FN_ACC auto operator()(TAcc const& acc) const -> void
30+
{
31+
// alpaka::Complex<T> supports the same methods as std::complex<T>, they are also useable inside kernels
32+
auto x = alpaka::Complex<float>(0.1f, 0.2f);
33+
float const real = x.real();
34+
auto y = alpaka::Complex<float>(0.3f, 0.4f);
35+
36+
// Operators are also the same
37+
x *= 2.0f;
38+
alpaka::Complex<float> z = x + y;
39+
40+
// In-kernel math functions are accessed via alpaka wrappers, the same way as for real numbers
41+
float zAbs = alpaka::math::abs(acc, z);
42+
}
43+
};
44+
45+
auto main() -> int
46+
{
47+
// Fallback for the CI with disabled sequential backend
48+
#if defined(ALPAKA_CI) && !defined(ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED)
49+
return EXIT_SUCCESS;
50+
#else
51+
using Idx = std::size_t;
52+
53+
// Define the accelerator
54+
//
55+
// It is possible to choose from a set of accelerators:
56+
// - AccGpuCudaRt
57+
// - AccGpuHipRt
58+
// - AccCpuThreads
59+
// - AccCpuFibers
60+
// - AccCpuOmp2Threads
61+
// - AccCpuOmp2Blocks
62+
// - AccOmp5
63+
// - AccCpuTbbBlocks
64+
// - AccCpuSerial
65+
//
66+
// Each accelerator has strengths and weaknesses. Therefore,
67+
// they need to be choosen carefully depending on the actual
68+
// use case. Furthermore, some accelerators only support a
69+
// particular workdiv, but workdiv can also be generated
70+
// automatically.
71+
72+
// By exchanging the Acc and Queue types you can select where to execute the kernel.
73+
using Acc = alpaka::ExampleDefaultAcc<alpaka::DimInt<1>, Idx>;
74+
std::cout << "Using alpaka accelerator: " << alpaka::getAccName<Acc>() << std::endl;
75+
76+
// Defines the synchronization behavior of a queue
77+
using QueueProperty = alpaka::Blocking;
78+
using Queue = alpaka::Queue<Acc, QueueProperty>;
79+
80+
// Select a device
81+
auto const devAcc = alpaka::getDevByIdx<Acc>(0u);
82+
83+
// Create a queue on the device
84+
Queue queue(devAcc);
85+
86+
// Define the work division
87+
Idx const threadsPerGrid = 1u;
88+
Idx const elementsPerThread = 1u;
89+
auto const workDiv = alpaka::getValidWorkDiv<Acc>(
90+
devAcc,
91+
threadsPerGrid,
92+
elementsPerThread,
93+
false,
94+
alpaka::GridBlockExtentSubDivRestrictions::Unrestricted);
95+
96+
// Run the kernel
97+
alpaka::exec<Acc>(queue, workDiv, ComplexKernel{});
98+
alpaka::wait(queue);
99+
100+
// Usage of alpaka::Complex<T> on the host side is the same as inside kernels, except math functions are not
101+
// supported
102+
auto x = alpaka::Complex<float>(0.1f, 0.2f);
103+
float const real = x.real();
104+
auto y = alpaka::Complex<float>(0.3f, 0.4f);
105+
x *= 2.0f;
106+
alpaka::Complex<float> z = x + y;
107+
108+
return EXIT_SUCCESS;
109+
#endif
110+
}

0 commit comments

Comments
 (0)