submission 539360
Jaber Jaber · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 162 lines, June 9 Researcher Reciprocity License v1.0.
submission_final.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-vectorsum-v2-539360?include=source"interfacepython
Compatibility
measured onNVIDIA B200
declared hardwareNVIDIA B200
architecturessm_100
dtypesfp32
Benchmark evidence
1 measurement across 1 GPU, fastest first.
Operation / workload
Hardware
Latency
Rank
Observed
Reported · How evidence levels are derived →
Source and license
sourceavailable
revision digestsha256:52b02fcc281f7408735ab34503b200777f2bab37c844efa15bcdd3c83c282768
license declaredunknown
license concludedunknown
authorsJaber Jaber
imported2026-08-15
Techniques
Extracted from the mirrored source by pattern, never inferred. Each row cites its line.
shared-memory
__shared__ double shared[16];vector-width = float4
const float4* input4 = reinterpret_cast<const float4*>(input);Kernel source
submission_final.py162 lines
import torch
from task import input_t, output_t
from torch.utils.cpp_extension import load_inline
cuda_source = """
__device__ __forceinline__ double warp_reduce(double val) {
#pragma unroll
for (int offset = 16; offset > 0; offset >>= 1) {
int lo = __double2loint(val);
int hi = __double2hiint(val);
lo = __shfl_down_sync(0xFFFFFFFF, lo, offset);
hi = __shfl_down_sync(0xFFFFFFFF, hi, offset);
val += __hiloint2double(hi, lo);
}
return val;
}
__global__ void __launch_bounds__(512, 2)
vectorsum_kernel(
const float* __restrict__ input,
float* __restrict__ output,
const int N,
double* __restrict__ block_sums,
unsigned int* __restrict__ block_counter
) {
double s0 = 0.0, s1 = 0.0, s2 = 0.0, s3 = 0.0;
double s4 = 0.0, s5 = 0.0, s6 = 0.0, s7 = 0.0;
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
const int stride = blockDim.x * gridDim.x;
const int N4 = N >> 2;
const float4* input4 = reinterpret_cast<const float4*>(input);
const int stride8 = stride << 3;
int i = tid;
const int limit8 = N4 - (stride << 3) + stride;
for (; i < limit8; i += stride8) {
float4 v0 = __ldg(&input4[i]);
float4 v1 = __ldg(&input4[i + stride]);
float4 v2 = __ldg(&input4[i + stride * 2]);
float4 v3 = __ldg(&input4[i + stride * 3]);
float4 v4 = __ldg(&input4[i + stride * 4]);
float4 v5 = __ldg(&input4[i + stride * 5]);
float4 v6 = __ldg(&input4[i + stride * 6]);
float4 v7 = __ldg(&input4[i + stride * 7]);
s0 += (double)v0.x + (double)v0.y + (double)v0.z + (double)v0.w;
s1 += (double)v1.x + (double)v1.y + (double)v1.z + (double)v1.w;
s2 += (double)v2.x + (double)v2.y + (double)v2.z + (double)v2.w;
s3 += (double)v3.x + (double)v3.y + (double)v3.z + (double)v3.w;
s4 += (double)v4.x + (double)v4.y + (double)v4.z + (double)v4.w;
s5 += (double)v5.x + (double)v5.y + (double)v5.z + (double)v5.w;
s6 += (double)v6.x + (double)v6.y + (double)v6.z + (double)v6.w;
s7 += (double)v7.x + (double)v7.y + (double)v7.z + (double)v7.w;
}
for (; i < N4; i += stride) {
float4 v = __ldg(&input4[i]);
s0 += (double)v.x + (double)v.y + (double)v.z + (double)v.w;
}
const int tail = N4 << 2;
for (int j = tail + tid; j < N; j += stride) {
s0 += (double)__ldg(&input[j]);
}
double total = ((s0 + s1) + (s2 + s3)) + ((s4 + s5) + (s6 + s7));
total = warp_reduce(total);
__shared__ double shared[16];
const int lane = threadIdx.x & 31;
const int warp_id = threadIdx.x >> 5;
if (lane == 0) shared[warp_id] = total;
__syncthreads();
if (warp_id == 0) {
total = (lane < 16) ? shared[lane] : 0.0;
total = warp_reduce(total);
}
__shared__ bool is_last;
if (threadIdx.x == 0) {
block_sums[blockIdx.x] = total;
__threadfence();
unsigned int count = atomicAdd(block_counter, 1u);
is_last = (count == gridDim.x - 1);
}
__syncthreads();
if (is_last) {
double sum = 0.0;
for (int k = threadIdx.x; k < (int)gridDim.x; k += blockDim.x) {
sum += block_sums[k];
}
sum = warp_reduce(sum);
if (lane == 0) shared[warp_id] = sum;
__syncthreads();
if (warp_id == 0) {
sum = (lane < 16) ? shared[lane] : 0.0;
sum = warp_reduce(sum);
if (lane == 0) {
output[0] = (float)sum;
*block_counter = 0;
}
}
}
}
torch::Tensor vectorsum_cuda(torch::Tensor input, torch::Tensor output,
torch::Tensor block_sums, torch::Tensor counter) {
const int N = input.numel();
const int grid = block_sums.size(0);
vectorsum_kernel<<<grid, 512>>>(
input.data_ptr<float>(),
output.data_ptr<float>(),
N,
block_sums.data_ptr<double>(),
(unsigned int*)counter.data_ptr<int>()
);
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
throw std::runtime_error(cudaGetErrorString(err));
}
return output;
}
"""
cpp_source = """
#include <torch/extension.h>
torch::Tensor vectorsum_cuda(torch::Tensor input, torch::Tensor output,
torch::Tensor block_sums, torch::Tensor counter);
"""
module = load_inline(
name='vectorsum_module',
cpp_sources=cpp_source,
cuda_sources=cuda_source,
functions=['vectorsum_cuda'],
verbose=True,
)
_block_sums = None
_counter = None
def custom_kernel(data: input_t) -> output_t:
global _block_sums, _counter
input_tensor, output_tensor = data
if _block_sums is None:
num_sms = torch.cuda.get_device_properties(0).multi_processor_count
grid = num_sms * 2
_block_sums = torch.zeros(grid, dtype=torch.float64, device=input_tensor.device)
_counter = torch.zeros(1, dtype=torch.int32, device=input_tensor.device)
module.vectorsum_cuda(input_tensor, output_tensor, _block_sums, _counter)
return output_tensor[0]
scrolls · 162 lines total
Source code from GPU Mode and the KernelBot dataset · June 9 Researcher Reciprocity License v1.0
Best evidence level for this revision: reported
JSON