|
| 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