-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example demonstrating use of Complex
- Loading branch information
1 parent
c644961
commit 8115b59
Showing
3 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# | ||
# Copyright 2014-2021 Erik Zenker, Benjamin Worpitz, Jan Stephan, Sergei Bastrakov | ||
# | ||
# This file exemplifies usage of alpaka. | ||
# | ||
# Permission to use, copy, modify, and/or distribute this software for any | ||
# purpose with or without fee is hereby granted, provided that the above | ||
# copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED “AS IS” AND ISC DISCLAIMS ALL WARRANTIES WITH | ||
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY | ||
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR | ||
# IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
# | ||
|
||
################################################################################ | ||
# Required CMake version. | ||
|
||
cmake_minimum_required(VERSION 3.15) | ||
|
||
set_property(GLOBAL PROPERTY USE_FOLDERS ON) | ||
|
||
################################################################################ | ||
# Project. | ||
|
||
set(_TARGET_NAME complex) | ||
|
||
project(${_TARGET_NAME}) | ||
|
||
#------------------------------------------------------------------------------- | ||
# Find alpaka. | ||
|
||
if(NOT TARGET alpaka::alpaka) | ||
option(USE_ALPAKA_SOURCE_TREE "Use alpaka's source tree instead of an alpaka installation" OFF) | ||
|
||
if(USE_ALPAKA_SOURCE_TREE) | ||
# Don't build the examples recursively | ||
set(alpaka_BUILD_EXAMPLES OFF) | ||
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../.." "${CMAKE_BINARY_DIR}/alpaka") | ||
else() | ||
find_package(alpaka REQUIRED) | ||
endif() | ||
endif() | ||
|
||
#------------------------------------------------------------------------------- | ||
# Add executable. | ||
|
||
alpaka_add_executable( | ||
${_TARGET_NAME} | ||
src/complex.cpp) | ||
target_link_libraries( | ||
${_TARGET_NAME} | ||
PUBLIC alpaka::alpaka) | ||
|
||
set_target_properties(${_TARGET_NAME} PROPERTIES FOLDER example) | ||
|
||
add_test(NAME ${_TARGET_NAME} COMMAND ${_TARGET_NAME}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* Copyright 2021 Sergei Bastrakov | ||
* | ||
* This file exemplifies usage of alpaka. | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED “AS IS” AND ISC DISCLAIMS ALL WARRANTIES WITH | ||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY | ||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR | ||
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#include <alpaka/alpaka.hpp> | ||
#include <alpaka/example/ExampleDefaultAcc.hpp> | ||
|
||
#include <cstdint> | ||
#include <iostream> | ||
|
||
|
||
//! Complex numbers demonstration kernel | ||
struct ComplexKernel | ||
{ | ||
template<typename TAcc> | ||
ALPAKA_FN_ACC auto operator()(TAcc const& acc) const -> void | ||
{ | ||
// alpaka::Complex<T> supports the same methods as std::complex<T>, they are also useable inside kernels | ||
auto x = alpaka::Complex<float>(0.1f, 0.2f); | ||
float const real = x.real(); | ||
auto y = alpaka::Complex<float>(0.3f, 0.4f); | ||
|
||
// Operators are also the same | ||
x *= 2.0f; | ||
alpaka::Complex<float> z = x + y; | ||
|
||
// In-kernel math functions are accessed via alpaka wrappers, the same way as for real numbers | ||
float zAbs = alpaka::math::abs(acc, z); | ||
} | ||
}; | ||
|
||
auto main() -> int | ||
{ | ||
// Fallback for the CI with disabled sequential backend | ||
#if defined(ALPAKA_CI) && !defined(ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED) | ||
return EXIT_SUCCESS; | ||
#else | ||
using Idx = std::size_t; | ||
|
||
// Define the accelerator | ||
// | ||
// It is possible to choose from a set of accelerators: | ||
// - AccGpuCudaRt | ||
// - AccGpuHipRt | ||
// - AccCpuThreads | ||
// - AccCpuFibers | ||
// - AccCpuOmp2Threads | ||
// - AccCpuOmp2Blocks | ||
// - AccOmp5 | ||
// - AccCpuTbbBlocks | ||
// - AccCpuSerial | ||
// | ||
// Each accelerator has strengths and weaknesses. Therefore, | ||
// they need to be choosen carefully depending on the actual | ||
// use case. Furthermore, some accelerators only support a | ||
// particular workdiv, but workdiv can also be generated | ||
// automatically. | ||
|
||
// By exchanging the Acc and Queue types you can select where to execute the kernel. | ||
using Acc = alpaka::ExampleDefaultAcc<alpaka::DimInt<1>, Idx>; | ||
std::cout << "Using alpaka accelerator: " << alpaka::getAccName<Acc>() << std::endl; | ||
|
||
// Defines the synchronization behavior of a queue | ||
using QueueProperty = alpaka::Blocking; | ||
using Queue = alpaka::Queue<Acc, QueueProperty>; | ||
|
||
// Select a device | ||
auto const devAcc = alpaka::getDevByIdx<Acc>(0u); | ||
|
||
// Create a queue on the device | ||
Queue queue(devAcc); | ||
|
||
// Define the work division | ||
Idx const threadsPerGrid = 1u; | ||
Idx const elementsPerThread = 1u; | ||
auto const workDiv = alpaka::getValidWorkDiv<Acc>( | ||
devAcc, | ||
threadsPerGrid, | ||
elementsPerThread, | ||
false, | ||
alpaka::GridBlockExtentSubDivRestrictions::Unrestricted); | ||
|
||
// Run the kernel | ||
alpaka::exec<Acc>(queue, workDiv, ComplexKernel{}); | ||
alpaka::wait(queue); | ||
|
||
// Usage of alpaka::Complex<T> on the host side is the same as inside kernels, except math functions are not | ||
// supported | ||
auto x = alpaka::Complex<float>(0.1f, 0.2f); | ||
float const real = x.real(); | ||
auto y = alpaka::Complex<float>(0.3f, 0.4f); | ||
x *= 2.0f; | ||
alpaka::Complex<float> z = x + y; | ||
|
||
return EXIT_SUCCESS; | ||
#endif | ||
} |