Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/cuda-c++/vector_add_tile.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "cuda_tile.h"

__tile_global__ void vector_add_tile(float* a, float* b, float* out, int n) {
namespace ct = cuda::tiles;
using namespace ct::literals;

a = ct::assume_aligned(a, 16_ic);
b = ct::assume_aligned(b, 16_ic);
out = ct::assume_aligned(out, 16_ic);

// Step 1: attach a shape to each raw pointer. n is a runtime value (dynamic extent).
auto aSpan = ct::tensor_span{a, ct::extents{n}};
auto bSpan = ct::tensor_span{b, ct::extents{n}};
auto oSpan = ct::tensor_span{out, ct::extents{n}};

// Step 2: partition each span into tiles of TILE_SIZE elements (tuned by kernel_tuner).
constexpr auto tile = ct::integral_constant<TILE_SIZE>{};
auto aView = ct::partition_view{aSpan, ct::shape{tile}};
auto bView = ct::partition_view{bSpan, ct::shape{tile}};
auto oView = ct::partition_view{oSpan, ct::shape{tile}};

int bx = ct::bid().x; // this block's tile-space index along .x
auto aTile = aView.load(bx); // pick the bx-th tile of a
auto bTile = bView.load(bx);
oView.store(aTile + bTile, bx); // write the tile back at the bx-th position of out
}
35 changes: 35 additions & 0 deletions examples/cuda-c++/vector_add_tile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python
""" This is a minimal example to tune a CUDA Tile vector add kernel """

import numpy
from kernel_tuner import tune_kernel

def tune():

size = 3*2**24

a = numpy.random.randn(size).astype(numpy.float32)
b = numpy.random.randn(size).astype(numpy.float32)
c = numpy.zeros_like(b)
n = numpy.int32(size)

args = [a, b, c, n]

# TILE_SIZE controls how many elements each tile processes. kernel_tuner injects
# it as a #define so the kernel can use it as a compile-time constant.
# grid_x = size / TILE_SIZE (number of tiles), computed automatically by kernel_tuner.
tune_params = {"TILE_SIZE": [8, 16, 32, 64, 128, 256]}

answer = [None, None, a+b, None]

compiler_options = ["-enable-tile", "-std=c++20"]

results, env = tune_kernel("vector_add_tile<TILE_SIZE>", "vector_add_tile.cu", size, args,
tune_params, lang="NVCUDA", compiler_options=compiler_options,
answer=answer, verbose=True, grid_div_x=["TILE_SIZE"])

return results


if __name__ == "__main__":
tune()
Loading
Loading