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
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
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
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
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 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
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 staging rocm-6.1.2
clang staging rocm-6.2.4
clang staging rocm-6.3.3
clang staging rocm-6.4.0
clang trunk rocm-6.1.2
clang trunk rocm-6.2.4
clang trunk rocm-6.3.3
clang trunk rocm-6.4.0
trunk sm_100a CUDA-12.8.1
Options
Source code
#include <vector> #include <iostream> #include <vector> #include <stdexcept> #include <iostream> #include <cuda.h> #include <cassert> void check(const auto err, const int line) { if (err != cudaSuccess) { std::cerr << "error: " << cudaGetErrorString(err) << "in line: " << line << "\n"; } } #define CUDA(err) check(err, __LINE__) template <typename T> class Vec { size_t size; // points to a memory location on device NOT host T* data_ptr; public: static_assert(std::is_same<T, float>::value || std::is_same<T, double>::value, "Vec<T> can only be instantiated with float or double."); // initialize vector of zeros of dimension size Vec(size_t size); // initialize from vector of values Vec(const std::vector<T>& vec); // destructor for device memory ~Vec(); // dot product [TODO: Add an exterior product] template <typename U> friend U dot_product(const Vec<U>& A, const Vec<U>& B); }; extern template class Vec<float>; extern template class Vec<double>; constexpr auto BLOCK_SIZE = 512u; static_assert(BLOCK_SIZE <= 1024); // dot product kernel using shared memory template <typename T> __global__ void vec_dot(const T* vecA, const T* vecB, const int size, T* out) { static_assert(std::is_arithmetic<T>::value, "T must be a numeric type"); assert(BLOCK_SIZE == blockDim.x); __shared__ T sdata[BLOCK_SIZE]; const auto tid = threadIdx.x; sdata[tid] = 0; size_t idx = threadIdx.x + blockDim.x * blockIdx.x; while (idx < size) { sdata[tid] += vecA[idx] * vecB[idx]; idx += blockDim.x * gridDim.x; } for (auto s = blockDim.x/2; s > 0; s >>= 1) { __syncthreads(); if (tid < s) { sdata[tid] += vecA[tid + s] * vecB[tid + s]; } } __syncthreads(); if (tid == 0) { atomicAdd(out, sdata[0]); } } template <typename T> T dot_product(const Vec<T>& A, const Vec<T>& B) { if (A.size != B.size) { throw std::invalid_argument("Vectors are not the same size."); } T* out_device; CUDA(cudaMalloc(&out_device, sizeof(T))); vec_dot<<<100, BLOCK_SIZE>>>(A.data_ptr, B.data_ptr, A.size, out_device); CUDA(cudaDeviceSynchronize()); T out_host; CUDA(cudaMemcpy(&out_host, out_device, sizeof(T), cudaMemcpyDeviceToHost)); CUDA(cudaFree(out_device)); return out_host; } template <typename T> Vec<T>::~Vec() { CUDA(cudaFree(data_ptr)); } template <typename T> Vec<T>::Vec(size_t size) : size(size), data_ptr(nullptr) { CUDA(cudaMalloc(&data_ptr, size * sizeof(T))); CUDA(cudaMemset(data_ptr, 0, size * sizeof(T))); } template <typename T> Vec<T>::Vec(const std::vector<T>& vec) : size(vec.size()) { CUDA(cudaMalloc(&data_ptr /*nullptr*/, size * sizeof(T))); CUDA(cudaMemcpy(data_ptr, vec.data(), size * sizeof(T), cudaMemcpyHostToDevice)); } template class Vec<float>; template class Vec<double>; template float dot_product<float>(const Vec<float>&, const Vec<float>&); template double dot_product<double>(const Vec<double>&, const Vec<double>&); int main() { std::vector<float> vec; for (auto i = 0; i < 10000; i++) { vec.push_back(1.0f); } Vec<float> vector(vec); auto product = dot_product(vector, vector); std::cout << "dot product = " << product << "\n"; return 0; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree