Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
Clojure
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Helion
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Yul (Solidity IR)
Zig
Javascript
GIMPLE
Ygen
sway
cuda source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
10.0.0 sm_75 CUDA-10.2
10.0.1 sm_75 CUDA-10.2
11.0.0 sm_75 CUDA-10.2
16.0.0 sm_90 CUDA-11.8
17.0.1(libc++) sm_90 CUDA-12.1
18.1.0(libc++) sm_90 CUDA-12.3.1
19.1.0 sm_90 CUDA-12.5.1
20.1.0 sm_90 CUDA-12.5.1
20.1.0 sm_90 CUDA-12.6.1
20.1.0 sm_90 CUDA-12.6.2
NVCC 10.0.130
NVCC 10.1.105
NVCC 10.1.168
NVCC 10.1.243
NVCC 10.2.89
NVCC 11.0.2
NVCC 11.0.3
NVCC 11.1.0
NVCC 11.1.1
NVCC 11.2.0
NVCC 11.2.1
NVCC 11.2.2
NVCC 11.3.0
NVCC 11.3.1
NVCC 11.4.0
NVCC 11.4.1
NVCC 11.4.2
NVCC 11.4.3
NVCC 11.4.4
NVCC 11.5.0
NVCC 11.5.1
NVCC 11.5.2
NVCC 11.6.0
NVCC 11.6.1
NVCC 11.6.2
NVCC 11.7.0
NVCC 11.7.1
NVCC 11.8.0
NVCC 12.0.0
NVCC 12.0.1
NVCC 12.1.0
NVCC 12.2.1
NVCC 12.3.1
NVCC 12.4.1
NVCC 12.5.1
NVCC 12.6.1
NVCC 12.6.2
NVCC 12.8.1
NVCC 12.9.0
NVCC 12.9.1
NVCC 13.0.0
NVCC 13.0.1
NVCC 13.0.2
NVCC 9.1.85
NVCC 9.2.88
NVRTC 11.0.2
NVRTC 11.0.3
NVRTC 11.1.0
NVRTC 11.1.1
NVRTC 11.2.0
NVRTC 11.2.1
NVRTC 11.2.2
NVRTC 11.3.0
NVRTC 11.3.1
NVRTC 11.4.0
NVRTC 11.4.1
NVRTC 11.5.0
NVRTC 11.5.1
NVRTC 11.5.2
NVRTC 11.6.0
NVRTC 11.6.1
NVRTC 11.6.2
NVRTC 11.7.0
NVRTC 11.7.1
NVRTC 11.8.0
NVRTC 12.0.0
NVRTC 12.0.1
NVRTC 12.1.0
NVRTC 12.2.1
NVRTC 12.3.1
NVRTC 12.4.1
NVRTC 12.5.1
NVRTC 12.6.1
NVRTC 12.6.2
NVRTC 12.8.1
NVRTC 12.9.0
NVRTC 12.9.1
NVRTC 13.0.0
NVRTC 13.0.1
NVRTC 13.0.2
clang 7.0.0 sm_70 CUDA-9.1
clang 8.0.0 sm_75 CUDA-10.0
clang 9.0.0 sm_75 CUDA-10.1
clang rocm-4.5.2
clang rocm-5.0.2
clang rocm-5.1.3
clang rocm-5.2.3
clang rocm-5.3.2
clang rocm-5.7.0
clang rocm-6.0.2
clang rocm-6.1.2
clang rocm-6.2.4
clang rocm-6.3.3
clang rocm-6.4.0
clang rocm-7.0.1
clang staging rocm-6.1.2
clang staging rocm-6.2.4
clang staging rocm-6.3.3
clang staging rocm-6.4.0
clang staging rocm-7.0.1
clang trunk rocm-6.1.2
clang trunk rocm-6.2.4
clang trunk rocm-6.3.3
clang trunk rocm-6.4.0
clang trunk rocm-7.0.1
trunk sm_100a CUDA-12.8.1
Options
Source code
#include <https://alpaka3.readthedocs.io/en/latest/alpaka.hpp> #include <chrono> #include <cstdlib> #include <iostream> #include <random> #include <typeinfo> using namespace alpaka; auto validateResult(auto const& bufHostC, auto const& bufHostA, auto const& bufHostB, std::size_t extent) -> int { using Data = uint32_t; static constexpr int MAX_PRINT_FALSE_RESULTS = 20; int falseResults = 0; for(std::size_t i(0u); i < extent; ++i) { Data const& val(bufHostC[i]); Data const correctResult(bufHostA[i] + bufHostB[i]); if(val != correctResult) { if(falseResults < MAX_PRINT_FALSE_RESULTS) std::cerr << "C[" << i << "] == " << val << " != " << correctResult << std::endl; ++falseResults; } } if(falseResults == 0) return EXIT_SUCCESS; std::cout << "Found " << falseResults << " false results, printed no more than " << MAX_PRINT_FALSE_RESULTS << "\n" << "Execution results incorrect!" << std::endl; std::cout << std::endl; return EXIT_FAILURE; } //! A vector addition kernel. class VectorAddKernel { public: //! The kernel entry point. //! //! \tparam TAcc The accelerator environment to be executed on. //! \tparam TElem The matrix element type. //! \param acc The accelerator to be executed on. //! \param A The first source vector. //! \param B The second source vector. //! \param C The destination vector. //! \param numElements The number of elements. ALPAKA_FN_ACC auto operator()( auto const& acc, alpaka::concepts::IMdSpan auto const A, alpaka::concepts::IMdSpan auto const B, alpaka::concepts::IMdSpan auto C, auto const& numElements) const -> void { using namespace alpaka; static_assert(ALPAKA_TYPEOF(numElements)::dim() == 1, "The VectorAddKernel expects 1-dimensional indices!"); auto simdGrid = onAcc::SimdAlgo{onAcc::worker::threadsInGrid}; simdGrid.concurrent( acc, numElements, [&](auto const&, auto&& simdA, auto&& simdB, auto&& simdC) constexpr { simdC = simdA.load() + simdB.load(); }, A, B, C); } }; // In standard projects, you typically do not execute the code with any available accelerator. // Instead, a single accelerator is selected once from the active accelerators and the kernels are executed with the // selected accelerator only. If you use the example as the starting point for your project, you can rename the // example() function to main() and move the accelerator tag to the function body. auto example(auto const deviceSpec, auto const exec, size_t numElements, size_t numberOfRuns) -> int { using IdxVec = Vec<std::size_t, 1u>; // Define problem size IdxVec const extent(numElements); // Define the buffer element type using Data = uint32_t; std::cout << "Number of elements: " << numElements << std::endl; std::cout << "Element type: " << onHost::demangledName<Data>() << std::endl; std::cout << "Number of runs: " << numberOfRuns << std::endl; std::cout << "Using alpaka accelerator: " << onHost::demangledName(exec) << " for " << deviceSpec.getApi().getName() << " " << deviceSpec.getDeviceKind().getName() << std::endl; // Select a device auto devSelector = onHost::makeDeviceSelector(deviceSpec); onHost::Device devAcc = devSelector.makeDevice(0); // Create a queue on the device onHost::Queue queue = devAcc.makeQueue(); // Allocate 3 host memory buffers auto bufHostA = onHost::allocHost<Data>(extent); auto bufHostB = onHost::allocHostLike(bufHostA); auto bufHostC = onHost::allocHostLike(bufHostA); // C++14 random generator for uniformly distributed numbers in {1,..,42} std::random_device rd{}; std::default_random_engine eng{rd()}; std::uniform_int_distribution<Data> dist(1, 42); for(auto i(0u); i < extent.x(); ++i) { bufHostA[i] = dist(eng); bufHostB[i] = dist(eng); bufHostC[i] = 0; } // Allocate 3 buffers on the accelerator auto bufAccA = onHost::allocLike(devAcc, bufHostA); auto bufAccB = onHost::allocLike(devAcc, bufHostB); auto bufAccC = onHost::allocLike(devAcc, bufHostC); Vec<size_t, 1u> chunkSize = 256u; // how many elements one worker should compute to ensure vectorization or instruction parallelism uint32_t elementsPerWorker = getNumElemPerThread<Data>(queue); auto dataBlocking = onHost::FrameSpec{divCeil(extent, chunkSize * elementsPerWorker), chunkSize}; // Instantiate the kernel function object VectorAddKernel kernel; auto const taskKernel = KernelBundle{kernel, bufAccA, bufAccB, bufAccC, extent}; // Copy Host -> Acc onHost::memcpy(queue, bufAccA, bufHostA); onHost::memcpy(queue, bufAccB, bufHostB); double totalKernelRuntime = 0.0; double totalCopyRuntime = 0.0; std::size_t completedRuns = 0u; for(; completedRuns < numberOfRuns; ++completedRuns) { // set the device memory to all zeros (byte-wise, not element-wise) onHost::memset(queue, bufAccC, uint8_t{0}); // Kernel execution timing onHost::wait(queue); auto const beginT = std::chrono::high_resolution_clock::now(); // Enqueue the kernel execution task queue.enqueue(exec, dataBlocking, taskKernel); // wait in case we are using an asynchronous queue to time actual kernel runtime onHost::wait(queue); auto const endT = std::chrono::high_resolution_clock::now(); double kernelRuntime = std::chrono::duration<double>(endT - beginT).count(); totalKernelRuntime += kernelRuntime; // Copy back the result auto beginCopyT = std::chrono::high_resolution_clock::now(); onHost::memcpy(queue, bufHostC, bufAccC); onHost::wait(queue); auto const endCopyT = std::chrono::high_resolution_clock::now(); double copyRuntime = std::chrono::duration<double>(endCopyT - beginCopyT).count(); totalCopyRuntime += copyRuntime; if(completedRuns == 0) { if(int const result = validateResult(bufHostC, bufHostA, bufHostB, extent.x()); result != EXIT_SUCCESS) return result; } } if(completedRuns > 0u) { double avgKernelRuntime = totalKernelRuntime / static_cast<double>(completedRuns); double avgCopyRuntime = totalCopyRuntime / static_cast<double>(completedRuns); std::cout << "Average time for kernel execution: " << avgKernelRuntime << "s" << std::endl; std::cout << "Average time for HtoD copy: " << avgCopyRuntime << "s" << std::endl; } std::cout << "Execution results correct!" << std::endl; std::cout << std::endl; return EXIT_SUCCESS; } void help(char* argv[]) { std::cerr << argv[0] << " [-n numElements] [-h]" << std::endl; } auto main(int argc, char* argv[]) -> int { size_t numElements = 123456; size_t numberOfRuns = 1; int opt; while((opt = getopt(argc, argv, "hn:r:")) != -1) { switch(opt) { case 'n': try { numElements = std::stoul(optarg, nullptr, 0); } catch(std::invalid_argument const& e) { std::cerr << "Error: invalid argument '" << optarg << "'.\n"; return EXIT_FAILURE; } catch(std::out_of_range const& e) { std::cerr << "Error: value '" << optarg << "' out of range for size_t.\n"; return EXIT_FAILURE; } break; case 'r': try { numberOfRuns = std::stoul(optarg, nullptr, 0); } catch(std::invalid_argument const& e) { std::cerr << "Error: invalid number of runs '" << optarg << "'.\n"; return EXIT_FAILURE; } catch(std::out_of_range const& e) { std::cerr << "Error: number of runs '" << optarg << "' out of range for size_t.\n"; return EXIT_FAILURE; } if(numberOfRuns == 0) { std::cerr << "Error: number of runs must be greater than zero.\n"; return EXIT_FAILURE; } break; case 'h': help(argv); exit(EXIT_SUCCESS); default: help(argv); exit(EXIT_FAILURE); } } using namespace alpaka; /* Execute the example once for each backend (device specification + executor) * * If you would like to execute it for a single accelerator only you can use the following code. * @code{.cpp} * auto deviceSpec = onHost::DeviceSpec{api::cuda, deviceKind::nvidiaGpu}; * auto executor = exec::gpuCuda; * return example(deviceSpec, executor, numElements); * @endcode * * Some examples for device specifications (depending on the active dependencies). * * onHost::DeviceSpec{api::host, deviceKind::cpu} * onHost::DeviceSpec{api::cuda, deviceKind::nvidiaGpu} * onHost::DeviceSpec{api::hip, deviceKind::amdGpu} * onHost::DeviceSpec{api::oneApi, deviceKind::intelGpu} * * A list of api's and device kinds can be found * https://alpaka3.readthedocs.io/en/latest/basic/cheatsheet.html#available-apis * A list of executors can be found * https://alpaka3.readthedocs.io/en/latest/basic/cheatsheet.html#executors */ return onHost::executeForEachIfHasDevice( [=](auto const& backend) { return example( backend[alpaka::object::deviceSpec], backend[alpaka::object::exec], numElements, numberOfRuns); }, onHost::allBackends(onHost::enabledApis, onHost::example::enabledExecutors)); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree