From 8115b5962aed0bff5a0dfcbc3bc72de1c351be6f Mon Sep 17 00:00:00 2001 From: Sergei Bastrakov Date: Wed, 24 Nov 2021 18:53:11 +0000 Subject: [PATCH] Add an example demonstrating use of Complex --- example/CMakeLists.txt | 1 + example/complex/CMakeLists.txt | 60 +++++++++++++++++ example/complex/src/complex.cpp | 110 ++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 example/complex/CMakeLists.txt create mode 100644 example/complex/src/complex.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index f204331d29fe..c5661362319d 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -29,6 +29,7 @@ project("alpakaExamples" LANGUAGES CXX) ################################################################################ add_subdirectory("bufferCopy/") +add_subdirectory("complex/") add_subdirectory("heatEquation/") add_subdirectory("helloWorld/") add_subdirectory("helloWorldLambda/") diff --git a/example/complex/CMakeLists.txt b/example/complex/CMakeLists.txt new file mode 100644 index 000000000000..ee94d6b2aabb --- /dev/null +++ b/example/complex/CMakeLists.txt @@ -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}) diff --git a/example/complex/src/complex.cpp b/example/complex/src/complex.cpp new file mode 100644 index 000000000000..62109af10f5f --- /dev/null +++ b/example/complex/src/complex.cpp @@ -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 +#include + +#include +#include + + +//! Complex numbers demonstration kernel +struct ComplexKernel +{ + template + ALPAKA_FN_ACC auto operator()(TAcc const& acc) const -> void + { + // alpaka::Complex supports the same methods as std::complex, they are also useable inside kernels + auto x = alpaka::Complex(0.1f, 0.2f); + float const real = x.real(); + auto y = alpaka::Complex(0.3f, 0.4f); + + // Operators are also the same + x *= 2.0f; + alpaka::Complex 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, Idx>; + std::cout << "Using alpaka accelerator: " << alpaka::getAccName() << std::endl; + + // Defines the synchronization behavior of a queue + using QueueProperty = alpaka::Blocking; + using Queue = alpaka::Queue; + + // Select a device + auto const devAcc = alpaka::getDevByIdx(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( + devAcc, + threadsPerGrid, + elementsPerThread, + false, + alpaka::GridBlockExtentSubDivRestrictions::Unrestricted); + + // Run the kernel + alpaka::exec(queue, workDiv, ComplexKernel{}); + alpaka::wait(queue); + + // Usage of alpaka::Complex on the host side is the same as inside kernels, except math functions are not + // supported + auto x = alpaka::Complex(0.1f, 0.2f); + float const real = x.real(); + auto y = alpaka::Complex(0.3f, 0.4f); + x *= 2.0f; + alpaka::Complex z = x + y; + + return EXIT_SUCCESS; +#endif +}