Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
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#
Go
Haskell
HLSL
Hook
Hylo
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Zig
Javascript
GIMPLE
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
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 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
trunk sm_86 CUDA-11.3
Options
Source code
#include <cuda/std/atomic> #include<cuda_runtime.h> #include<cstdlib> #include<iostream> #include<chrono> #include <cuda/annotated_ptr> namespace chrono = std::chrono; using clock_type = chrono::high_resolution_clock; template<typename Ptr_y, typename Ptr_x> __global__ void squre(Ptr_y y, Ptr_x x, int n) { for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += blockDim.x * gridDim.x) { y[i] = x[i] * x[i]; } } __inline__ __device__ float warp_reduce(float val) { int warp_size = 32; for (int offset = warp_size / 2; offset > 0; offset /= 2) val += __shfl_down_sync(0xFFFFFFFF, val, offset); return val; } template<typename Ptr_z, typename Ptr_x, typename Ptr_y> __global__ void reduce(Ptr_z z, Ptr_x x, Ptr_y y, int N) { int warp_size = 32; float sum = float(0); for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < N; i += blockDim.x * gridDim.x) { sum += x[i] + y[i]; } sum = warp_reduce(sum); // Obtain the sum of values in the current warp; if ((threadIdx.x & (warp_size - 1)) == 0) // Same as (threadIdx.x % warp_size) == 0 but faster atomicAdd(z, sum); // The first thread in the warp updates the output; } int main(int argc, char *argv[]) { // ./a device_id num_blcoks bs_1d N cudaError_t err; int N = 70000000; int device_id = 2; int num_blocks = 512; int block_size_1d = 32; if(argc >= 2) device_id = atoi(argv[1]); cudaSetDevice(device_id); if(argc >= 3) num_blocks = atoi(argv[2]); if(argc >= 4) block_size_1d = atoi(argv[3]); if(argc >= 5) N = atoi(argv[4]); cudaDeviceProp prop; cudaGetDeviceProperties(&prop, device_id); size_t l2_size = min(int(prop.l2CacheSize * 0.75), prop.persistingL2CacheMaxSize); std::cout<<"set aside L2 cache has "<<l2_size<<" Byte\n"; cudaDeviceSetLimit(cudaLimitPersistingL2CacheSize, l2_size); /* set-aside 3/4 of L2 cache for persisting accesses or the max allowed*/ float * x = nullptr; float * y = nullptr; float * x1 = nullptr; float * y1 = nullptr; float * res = nullptr; cudaMallocManaged((void**)&x, sizeof(float) * N); cudaMallocManaged((void**)&y, sizeof(float) * N); cudaMallocManaged((void**)&x1, sizeof(float) * N); cudaMallocManaged((void**)&y1, sizeof(float) * N); cudaMallocManaged((void**)&res, sizeof(float)); cudaStream_t s1,s2; cudaStreamCreate(&s1); cudaStreamCreate(&s2); cudaEvent_t e1; cudaEventCreate(&e1); //init data for (int i = 0; i < N; i++) { x[i] = 1.0 / (i + 1); y[i] = 2.0 / (i + 1); } err = cudaGetLastError(); if(err != cudaSuccess) { std::cout<<"Shit before prefetch\n"; } //prefetch cudaMemPrefetchAsync(x, sizeof(float) * N, device_id, 0); cudaMemPrefetchAsync(y, sizeof(float) * N, device_id, 0); cudaMemPrefetchAsync(x1, sizeof(float) * N, device_id, 0); cudaMemPrefetchAsync(y1, sizeof(float) * N, device_id, 0); cudaMemPrefetchAsync(res, sizeof(float), device_id, 0); cudaDeviceSynchronize(); //compute for(int i = 0; i < 10; i++){ //timing auto start = clock_type::now(); //Sync // squre<<<num_blocks, block_size_1d, 0, s1>>>(x1, x, N); // squre<<<num_blocks, block_size_1d, 0, s1>>>(y1, y, N); // reduce<<<num_blocks, block_size_1d, 0, s1>>>(res, x1, y1, N); // cudaDeviceSynchronize(); //multi-stream CKE with L2 //bind L2 cache ptr with stream cudaStreamAttrValue stream_attribute_x1; // Stream level attributes data structure stream_attribute_x1.accessPolicyWindow.base_ptr = reinterpret_cast<void*>(x1); // Global Memory data pointer stream_attribute_x1.accessPolicyWindow.num_bytes = l2_size; // Number of bytes for persistence access. // (Must be less than cudaDeviceProp::accessPolicyMaxWindowSize) //TODO: How to set this hitRatio stream_attribute_x1.accessPolicyWindow.hitRatio = 0.9; // Hint for cache hit ratio stream_attribute_x1.accessPolicyWindow.hitProp = cudaAccessPropertyPersisting; // Type of access property on cache hit stream_attribute_x1.accessPolicyWindow.missProp = cudaAccessPropertyStreaming; // Type of access property on cache miss. //Set the attributes to a CUDA stream of type cudaStream_t cudaStreamSetAttribute(s1, cudaStreamAttributeAccessPolicyWindow, &stream_attribute_x1); // bind L2 cache ptr with stream cudaStreamAttrValue stream_attribute_y1; // Stream level attributes data structure stream_attribute_y1.accessPolicyWindow.base_ptr = reinterpret_cast<void*>(y1); // Global Memory data pointer stream_attribute_y1.accessPolicyWindow.num_bytes = l2_size; // Number of bytes for persistence access. // (Must be less than cudaDeviceProp::accessPolicyMaxWindowSize) //TODO: How to set this hitRatio stream_attribute_y1.accessPolicyWindow.hitRatio = 0.9; // Hint for cache hit ratio stream_attribute_y1.accessPolicyWindow.hitProp = cudaAccessPropertyPersisting; // Type of access property on cache hit stream_attribute_y1.accessPolicyWindow.missProp = cudaAccessPropertyStreaming; // Type of access property on cache miss. //Set the attributes to a CUDA stream of type cudaStream_t cudaStreamSetAttribute(s2, cudaStreamAttributeAccessPolicyWindow, &stream_attribute_y1); //easy API for L2 cache // cuda::annotated_ptr<float, cuda::access_property::persisting> x1_p{x1}; // cuda::annotated_ptr<float, cuda::access_property::persisting> y1_p{y1}; err = cudaGetLastError(); if(err != cudaSuccess) { std::cout<<"Shit in cache\n"; } squre<<<num_blocks, block_size_1d, 0, s1>>>(x1, x, N); squre<<<num_blocks, block_size_1d, 0, s2>>>(y1, y, N); cudaEventRecord(e1, s1); cudaStreamWaitEvent(s2, e1); reduce<<<num_blocks, block_size_1d, 0, s2>>>(res, x1, y1, N); cudaDeviceSynchronize(); err = cudaGetLastError(); if(err != cudaSuccess) { std::cout<<"Shit in running\n"; } //timing auto end = clock_type::now(); auto it_time = chrono::duration_cast<chrono::microseconds>(end - start).count(); std::cout<<"Iteration "<<i<<" : "<<(float)it_time / 1000.0<<" ms\n"; } //validate std::cout<<"Result is "<<*res<<"\n"; cudaFree(x); cudaFree(y); cudaFree(x1); cudaFree(y1); cudaFree(res); }
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
About the author
Statistics
Changelog
Version tree