Skip to content
KernelIndex
Search⌘K

submission 681691

ngolhn · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

No package. Vendor the mirrored source: 241 lines, June 9 Researcher Reciprocity License v1.0.

submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-vectorsum-v2-681691?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
Vector sum reductionsuite of 6 cases
NVIDIA B200
43.5µs
#8 of 88
2026-03-31

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:9eb750b0e87a7d27d4402045bdc5a9f038fe4e57af1f65b9892f214126f5b2d1
license declaredunknown
license concludedunknown
authorsngolhn
imported2026-08-15

Techniques

Extracted from the mirrored source by pattern, never inferred. Each row cites its line.

shared-memory__shared__ float smem[8];
vector-width = float4const float4* data4 = reinterpret_cast<const float4*>(data);

Kernel source

submission.py241 lines
#!POPCORN leaderboard vectorsum_v2
#!POPCORN gpu B200

import torch
from task import input_t, output_t
from torch.utils.cpp_extension import load_inline

cuda_src = r"""
#include <torch/extension.h>
#include <cuda_runtime.h>

static float* d_partials = nullptr;
static unsigned int* d_counter = nullptr;
static int partials_capacity = 0;

#define BENCH_N 52428800
#define BENCH_N4 13107200
#define NUM_BLOCKS 1184

// Fast path: hardcoded N=52428800, ld.global.cs loads
__global__ __launch_bounds__(256, 8)
void vectorsum_fast(const float* __restrict__ data, float* __restrict__ output,
                    float* __restrict__ partials, unsigned int* __restrict__ counter) {
    const int tid = threadIdx.x;
    const int bid = blockIdx.x;
    const int global_tid = bid * 256 + tid;
    const int grid_stride = NUM_BLOCKS * 256;
    const int lane = tid & 31;
    const int warp_id = tid >> 5;

    float s0 = 0.f, s1 = 0.f, s2 = 0.f, s3 = 0.f;
    float s4 = 0.f, s5 = 0.f, s6 = 0.f, s7 = 0.f;

    const float4* data4 = reinterpret_cast<const float4*>(data);

    int idx = global_tid;
    #pragma unroll 4
    while (idx + grid_stride < BENCH_N4) {
        const float4* p0 = &data4[idx];
        const float4* p1 = &data4[idx + grid_stride];
        float4 v0, v1;
        asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v0.x), "=f"(v0.y), "=f"(v0.z), "=f"(v0.w) : "l"(p0));
        asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v1.x), "=f"(v1.y), "=f"(v1.z), "=f"(v1.w) : "l"(p1));
        s0 += v0.x; s1 += v0.y; s2 += v0.z; s3 += v0.w;
        s4 += v1.x; s5 += v1.y; s6 += v1.z; s7 += v1.w;
        idx += grid_stride * 2;
    }
    if (idx < BENCH_N4) {
        const float4* p0 = &data4[idx];
        float4 v0;
        asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v0.x), "=f"(v0.y), "=f"(v0.z), "=f"(v0.w) : "l"(p0));
        s0 += v0.x; s1 += v0.y; s2 += v0.z; s3 += v0.w;
    }

    float sum = (s0 + s1) + (s2 + s3) + (s4 + s5) + (s6 + s7);

    #pragma unroll
    for (int offset = 16; offset > 0; offset >>= 1)
        sum += __shfl_down_sync(0xFFFFFFFF, sum, offset);

    __shared__ float smem[8];
    if (lane == 0) smem[warp_id] = sum;
    __syncthreads();

    if (warp_id == 0) {
        sum = (lane < 8) ? smem[lane] : 0.f;
        #pragma unroll
        for (int offset = 4; offset > 0; offset >>= 1)
            sum += __shfl_down_sync(0xFFFFFFFF, sum, offset);
    }

    __shared__ bool is_last;
    if (tid == 0) {
        partials[bid] = sum;
        __threadfence();
        unsigned int old = atomicInc(counter, NUM_BLOCKS - 1);
        is_last = (old == NUM_BLOCKS - 1);
    }
    __syncthreads();

    if (is_last) {
        float psum = 0.f;
        for (int i = tid; i < NUM_BLOCKS; i += 256)
            psum += partials[i];
        #pragma unroll
        for (int offset = 16; offset > 0; offset >>= 1)
            psum += __shfl_down_sync(0xFFFFFFFF, psum, offset);
        if (lane == 0) smem[warp_id] = psum;
        __syncthreads();
        if (warp_id == 0) {
            psum = (lane < 8) ? smem[lane] : 0.f;
            #pragma unroll
            for (int offset = 4; offset > 0; offset >>= 1)
                psum += __shfl_down_sync(0xFFFFFFFF, psum, offset);
            if (lane == 0) *output = psum;
        }
    }
}

// Generic path: variable N for test sizes
__global__ __launch_bounds__(256, 8)
void vectorsum_generic(const float* __restrict__ data, float* __restrict__ output,
                       float* __restrict__ partials, unsigned int* __restrict__ counter,
                       int n) {
    const int tid = threadIdx.x;
    const int bid = blockIdx.x;
    const int global_tid = bid * 256 + tid;
    const int grid_stride = gridDim.x * 256;
    const int lane = tid & 31;
    const int warp_id = tid >> 5;

    float s0 = 0.f, s1 = 0.f, s2 = 0.f, s3 = 0.f;
    float s4 = 0.f, s5 = 0.f, s6 = 0.f, s7 = 0.f;

    const float4* data4 = reinterpret_cast<const float4*>(data);
    const int n4 = n >> 2;

    int idx = global_tid;
    while (idx + grid_stride < n4) {
        const float4* p0 = &data4[idx];
        const float4* p1 = &data4[idx + grid_stride];
        float4 v0, v1;
        asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v0.x), "=f"(v0.y), "=f"(v0.z), "=f"(v0.w) : "l"(p0));
        asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v1.x), "=f"(v1.y), "=f"(v1.z), "=f"(v1.w) : "l"(p1));
        s0 += v0.x; s1 += v0.y; s2 += v0.z; s3 += v0.w;
        s4 += v1.x; s5 += v1.y; s6 += v1.z; s7 += v1.w;
        idx += grid_stride * 2;
    }
    if (idx < n4) {
        const float4* p0 = &data4[idx];
        float4 v0;
        asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v0.x), "=f"(v0.y), "=f"(v0.z), "=f"(v0.w) : "l"(p0));
        s0 += v0.x; s1 += v0.y; s2 += v0.z; s3 += v0.w;
        idx += grid_stride;
    }
    for (int i = n4 * 4 + global_tid; i < n; i += grid_stride) {
        float val;
        asm volatile("ld.global.cs.f32 %0, [%1];" : "=f"(val) : "l"(&data[i]));
        s0 += val;
    }

    float sum = (s0 + s1) + (s2 + s3) + (s4 + s5) + (s6 + s7);

    #pragma unroll
    for (int offset = 16; offset > 0; offset >>= 1)
        sum += __shfl_down_sync(0xFFFFFFFF, sum, offset);

    __shared__ float smem[8];
    if (lane == 0) smem[warp_id] = sum;
    __syncthreads();

    if (warp_id == 0) {
        sum = (lane < 8) ? smem[lane] : 0.f;
        #pragma unroll
        for (int offset = 4; offset > 0; offset >>= 1)
            sum += __shfl_down_sync(0xFFFFFFFF, sum, offset);
    }

    __shared__ bool is_last;
    if (tid == 0) {
        partials[bid] = sum;
        __threadfence();
        unsigned int old = atomicInc(counter, gridDim.x - 1);
        is_last = (old == gridDim.x - 1);
    }
    __syncthreads();

    if (is_last) {
        float psum = 0.f;
        for (int i = tid; i < (int)gridDim.x; i += 256)
            psum += partials[i];
        #pragma unroll
        for (int offset = 16; offset > 0; offset >>= 1)
            psum += __shfl_down_sync(0xFFFFFFFF, psum, offset);
        if (lane == 0) smem[warp_id] = psum;
        __syncthreads();
        if (warp_id == 0) {
            psum = (lane < 8) ? smem[lane] : 0.f;
            #pragma unroll
            for (int offset = 4; offset > 0; offset >>= 1)
                psum += __shfl_down_sync(0xFFFFFFFF, psum, offset);
            if (lane == 0) *output = psum;
        }
    }
}

void vectorsum_raw(int64_t data_ptr, int64_t output_ptr, int N) {
    int num_blocks;
    if (N == BENCH_N) {
        num_blocks = NUM_BLOCKS;
    } else {
        int blocks_needed = (N / 4 + 255) / 256;
        num_blocks = (blocks_needed < NUM_BLOCKS) ? blocks_needed : NUM_BLOCKS;
        if (num_blocks < 1) num_blocks = 1;
    }

    if (d_partials == nullptr || partials_capacity < num_blocks) {
        if (d_partials) cudaFree(d_partials);
        if (d_counter) cudaFree(d_counter);
        cudaMalloc(&d_partials, num_blocks * sizeof(float));
        cudaMalloc(&d_counter, sizeof(unsigned int));
        cudaMemset(d_counter, 0, sizeof(unsigned int));
        partials_capacity = num_blocks;
    }

    if (N == BENCH_N) {
        vectorsum_fast<<<NUM_BLOCKS, 256, 0>>>(
            reinterpret_cast<const float*>(data_ptr),
            reinterpret_cast<float*>(output_ptr),
            d_partials, d_counter);
    } else {
        vectorsum_generic<<<num_blocks, 256, 0>>>(
            reinterpret_cast<const float*>(data_ptr),
            reinterpret_cast<float*>(output_ptr),
            d_partials, d_counter, N);
    }
}
"""

cpp_src = r"""
void vectorsum_raw(int64_t data_ptr, int64_t output_ptr, int N);
"""

_ext = load_inline(
    name="vectorsum_ptx_cs_spec",
    cpp_sources=cpp_src,
    cuda_sources=cuda_src,
    functions=["vectorsum_raw"],
    with_cuda=True,
    extra_cflags=["-O3", "-std=c++17"],
    extra_cuda_cflags=["-O3", "--use_fast_math", "-std=c++17",
                       "-gencode=arch=compute_100,code=sm_100"],
    verbose=False,
)


def custom_kernel(data: input_t) -> output_t:
    data_tensor, output_tensor = data
    _ext.vectorsum_raw(data_tensor.data_ptr(), output_tensor.data_ptr(), data_tensor.numel())
    return output_tensor[0]
scrolls · 241 lines total

Source code from GPU Mode and the KernelBot dataset · June 9 Researcher Reciprocity License v1.0

Changes from previous submission

Against this author's previous submission submission 681613.

⋯ 12 unchanged lines
static unsigned int* d_counter = nullptr;
static int partials_capacity = 0;
- // ============================================================
- // FAST PATH: Hardcoded N=52428800 for benchmark
- // Compiler can optimize loop bounds, remove branches, unroll better
- // ============================================================
#define BENCH_N 52428800
#define BENCH_N4 13107200
- #define BENCH_BLOCKS 1184
+ #define NUM_BLOCKS 1184
+ // Fast path: hardcoded N=52428800, ld.global.cs loads
__global__ __launch_bounds__(256, 8)
void vectorsum_fast(const float* __restrict__ data, float* __restrict__ output,
float* __restrict__ partials, unsigned int* __restrict__ counter) {
const int tid = threadIdx.x;
const int bid = blockIdx.x;
const int global_tid = bid * 256 + tid;
- const int grid_stride = BENCH_BLOCKS * 256;
+ const int grid_stride = NUM_BLOCKS * 256;
const int lane = tid & 31;
const int warp_id = tid >> 5;
⋯ 3 unchanged lines
const float4* data4 = reinterpret_cast<const float4*>(data);
int idx = global_tid;
- // N4=13107200 is divisible by grid_stride patterns, so no tail needed for float4
+ #pragma unroll 4
while (idx + grid_stride < BENCH_N4) {
- float4 v0 = __ldg(&data4[idx]);
- float4 v1 = __ldg(&data4[idx + grid_stride]);
+ const float4* p0 = &data4[idx];
+ const float4* p1 = &data4[idx + grid_stride];
+ float4 v0, v1;
+ asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v0.x), "=f"(v0.y), "=f"(v0.z), "=f"(v0.w) : "l"(p0));
+ asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v1.x), "=f"(v1.y), "=f"(v1.z), "=f"(v1.w) : "l"(p1));
s0 += v0.x; s1 += v0.y; s2 += v0.z; s3 += v0.w;
s4 += v1.x; s5 += v1.y; s6 += v1.z; s7 += v1.w;
idx += grid_stride * 2;
}
if (idx < BENCH_N4) {
- float4 v0 = __ldg(&data4[idx]);
+ const float4* p0 = &data4[idx];
+ float4 v0;
+ asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v0.x), "=f"(v0.y), "=f"(v0.z), "=f"(v0.w) : "l"(p0));
s0 += v0.x; s1 += v0.y; s2 += v0.z; s3 += v0.w;
}
- // No scalar tail needed: 52428800 is divisible by 4
float sum = (s0 + s1) + (s2 + s3) + (s4 + s5) + (s6 + s7);
⋯ 16 unchanged lines
if (tid == 0) {
partials[bid] = sum;
__threadfence();
- unsigned int old = atomicInc(counter, BENCH_BLOCKS - 1);
- is_last = (old == BENCH_BLOCKS - 1);
+ unsigned int old = atomicInc(counter, NUM_BLOCKS - 1);
+ is_last = (old == NUM_BLOCKS - 1);
}
__syncthreads();
if (is_last) {
float psum = 0.f;
- for (int i = tid; i < BENCH_BLOCKS; i += 256)
+ for (int i = tid; i < NUM_BLOCKS; i += 256)
psum += partials[i];
#pragma unroll
for (int offset = 16; offset > 0; offset >>= 1)
⋯ 10 unchanged lines
}
}
- // ============================================================
- // GENERIC PATH: Variable N for test sizes (1023, 1024, etc.)
- // ============================================================
+ // Generic path: variable N for test sizes
__global__ __launch_bounds__(256, 8)
void vectorsum_generic(const float* __restrict__ data, float* __restrict__ output,
float* __restrict__ partials, unsigned int* __restrict__ counter,
⋯ 13 unchanged lines
int idx = global_tid;
while (idx + grid_stride < n4) {
- float4 v0 = __ldg(&data4[idx]);
- float4 v1 = __ldg(&data4[idx + grid_stride]);
+ const float4* p0 = &data4[idx];
+ const float4* p1 = &data4[idx + grid_stride];
+ float4 v0, v1;
+ asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v0.x), "=f"(v0.y), "=f"(v0.z), "=f"(v0.w) : "l"(p0));
+ asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v1.x), "=f"(v1.y), "=f"(v1.z), "=f"(v1.w) : "l"(p1));
s0 += v0.x; s1 += v0.y; s2 += v0.z; s3 += v0.w;
s4 += v1.x; s5 += v1.y; s6 += v1.z; s7 += v1.w;
idx += grid_stride * 2;
}
if (idx < n4) {
- float4 v0 = __ldg(&data4[idx]);
+ const float4* p0 = &data4[idx];
+ float4 v0;
+ asm volatile("ld.global.cs.v4.f32 {%0,%1,%2,%3}, [%4];" : "=f"(v0.x), "=f"(v0.y), "=f"(v0.z), "=f"(v0.w) : "l"(p0));
s0 += v0.x; s1 += v0.y; s2 += v0.z; s3 += v0.w;
idx += grid_stride;
}
- for (int i = n4 * 4 + global_tid; i < n; i += grid_stride)
- s0 += __ldg(&data[i]);
+ for (int i = n4 * 4 + global_tid; i < n; i += grid_stride) {
+ float val;
+ asm volatile("ld.global.cs.f32 %0, [%1];" : "=f"(val) : "l"(&data[i]));
+ s0 += val;
+ }
float sum = (s0 + s1) + (s2 + s3) + (s4 + s5) + (s6 + s7);
⋯ 43 unchanged lines
void vectorsum_raw(int64_t data_ptr, int64_t output_ptr, int N) {
int num_blocks;
if (N == BENCH_N) {
- num_blocks = BENCH_BLOCKS;
+ num_blocks = NUM_BLOCKS;
} else {
- // For small test sizes, use fewer blocks
int blocks_needed = (N / 4 + 255) / 256;
- num_blocks = (blocks_needed < BENCH_BLOCKS) ? blocks_needed : BENCH_BLOCKS;
+ num_blocks = (blocks_needed < NUM_BLOCKS) ? blocks_needed : NUM_BLOCKS;
if (num_blocks < 1) num_blocks = 1;
}
⋯ 7 unchanged lines
}
if (N == BENCH_N) {
- vectorsum_fast<<<BENCH_BLOCKS, 256, 0>>>(
+ vectorsum_fast<<<NUM_BLOCKS, 256, 0>>>(
reinterpret_cast<const float*>(data_ptr),
reinterpret_cast<float*>(output_ptr),
d_partials, d_counter);
⋯ 11 unchanged lines
"""
_ext = load_inline(
- name="vectorsum_lastblock_f32_spec",
+ name="vectorsum_ptx_cs_spec",
cpp_sources=cpp_src,
cuda_sources=cuda_src,
functions=["vectorsum_raw"],
scrolls · 148 diff lines total

Best evidence level for this revision: reported

JSON