Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ project("alpakaExamples" LANGUAGES CXX)
################################################################################

add_subdirectory("bufferCopy/")
add_subdirectory("complex/")
add_subdirectory("heatEquation/")
add_subdirectory("helloWorld/")
add_subdirectory("helloWorldLambda/")
Expand Down
60 changes: 60 additions & 0 deletions example/complex/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#
# Copyright 2022 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})
110 changes: 110 additions & 0 deletions example/complex/src/complex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* Copyright 2022 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
}
1 change: 1 addition & 0 deletions include/alpaka/alpaka.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
#include <alpaka/kernel/TaskKernelOmp5.hpp>
#include <alpaka/kernel/Traits.hpp>
// math
#include <alpaka/math/Complex.hpp>
#include <alpaka/math/MathGenericSycl.hpp>
#include <alpaka/math/MathStdLib.hpp>
#include <alpaka/math/MathUniformCudaHipBuiltIn.hpp>
Expand Down
Loading