Speclib  0.1.2
The library for writing better CUDA libraries
sp::Grid< BlockSizes, GridSizes > Struct Template Reference

Class to represent a CUDA grid. More...

#include <GridTraits.hpp>

Static Public Member Functions

template<int I>
constexpr static __device__ int blockDim ()
 Get the I'th block dimension, which may be a compile-time constant. More...
 
template<int I>
constexpr static __device__ int gridDim ()
 Get the I'th grid dimension, which may be a compile-time constant. More...
 
constexpr static __device__ sp::Vec< int, BlockRank > blockDims ()
 Get a Vec representing the block size. More...
 
constexpr static __device__ sp::Vec< int, GridRank > gridDims ()
 Get a Vec representing the block size. More...
 
static __device__ sp::Vec< int, GridRank > blockIdx ()
 Get the block index as a Vec. More...
 
template<int I>
static __device__ int blockIdx ()
 Get the I'th block idx. More...
 
static __device__ sp::Vec< int, BlockRank > threadIdx ()
 Get the thread index as a Vec. More...
 
template<int I>
static __device__ int threadIdx ()
 Get the I'th thread idx. More...
 
static __device__ int blockId ()
 Get the block ID. This can be thought of as the 1D block index. More...
 
static __device__ int threadId ()
 Get the thread ID. This can be thought of as the 1D thread index within the block. More...
 
constexpr static __device__ int numBlocks ()
 Total number of blocks. More...
 
constexpr static __device__ int threadsPerBlock ()
 Number of threads per block. More...
 
constexpr static __device__ int warpsPerBlock ()
 Warps per block. More...
 
constexpr static __device__ int warpsInGrid ()
 Total number of warps in the grid. More...
 
constexpr static __device__ int threadsInGrid ()
 Total number of threads in the grid. More...
 
static __device__ int warpIdx ()
 Get the warp index, given the block dimensionality. More...
 
static __device__ int warpGridIdx ()
 Index of this warp in grid. More...
 
static __device__ int threadGridIdx ()
 Index of this thread in grid. More...
 
static __device__ int laneIdx ()
 Get the lane index of this thread. More...
 

Static Public Attributes

constexpr static int GridRank = GridSizes::Size
 
constexpr static int BlockRank = BlockSizes::Size
 

Detailed Description

template<typename BlockSizes, typename GridSizes>
struct sp::Grid< BlockSizes, GridSizes >

Class to represent a CUDA grid.

  • Provides convenient accessors to blockIdx/blockDim/etc. using numerical indices.
  • Allows accessing blockIdx/blockDim/etc. as sp::Vec.
  • Allows you to specify assumptions about the range of values blockIdx/blockDim/etc. can take, allowing the compiler to constant-propagate them away.
  • Provides APIs for higher-level grid queries, like warpIdx().

When you declare your kernel's Grid type, you specify the dimensionality and any fixed size components of the block or grid dimensions. Those values can then be used in constexpr contexts when accessed through blockDim<I>() and friends. This also improves optimisation, since the compiler can't typically determine when some of these are constants.

Example

// Blocks are always 1D, and exactly 512 threads.
// Grids are always 1D, but the number of blocks is not known.
// The compiler will be able to constant-propagate gridDims.y/z as 1, blockDims.y/z as 1, and blockDims.x as 512.
// Any of those can be used as template parameters when accessed through `MyGrid`, as they will be constexpr.
Encodes a sequence of integral type T.
Definition: integer_sequence.hpp:113
Class to represent a CUDA grid.
Definition: GridTraits.hpp:172
Template Parameters
BlockSizesAn sp::int_sequence<> specifying the block size. Use -1 for entries that are non-constant.
GridSizesAn sp::int_sequence<> specifying the grid size. Use -1 for entries that are non-constant.

Member Function Documentation

◆ blockDim()

template<typename BlockSizes , typename GridSizes >
template<int I>
constexpr static __device__ int sp::Grid< BlockSizes, GridSizes >::blockDim ( )
staticconstexpr

Get the I'th block dimension, which may be a compile-time constant.

◆ blockDims()

template<typename BlockSizes , typename GridSizes >
constexpr static __device__ sp::Vec< int, BlockRank > sp::Grid< BlockSizes, GridSizes >::blockDims ( )
staticconstexpr

Get a Vec representing the block size.

◆ blockId()

template<typename BlockSizes , typename GridSizes >
static __device__ int sp::Grid< BlockSizes, GridSizes >::blockId ( )
static

Get the block ID. This can be thought of as the 1D block index.

◆ blockIdx() [1/2]

template<typename BlockSizes , typename GridSizes >
static __device__ sp::Vec< int, GridRank > sp::Grid< BlockSizes, GridSizes >::blockIdx ( )
static

Get the block index as a Vec.

◆ blockIdx() [2/2]

template<typename BlockSizes , typename GridSizes >
template<int I>
static __device__ int sp::Grid< BlockSizes, GridSizes >::blockIdx ( )
static

Get the I'th block idx.

◆ gridDim()

template<typename BlockSizes , typename GridSizes >
template<int I>
constexpr static __device__ int sp::Grid< BlockSizes, GridSizes >::gridDim ( )
staticconstexpr

Get the I'th grid dimension, which may be a compile-time constant.

◆ gridDims()

template<typename BlockSizes , typename GridSizes >
constexpr static __device__ sp::Vec< int, GridRank > sp::Grid< BlockSizes, GridSizes >::gridDims ( )
staticconstexpr

Get a Vec representing the block size.

◆ laneIdx()

template<typename BlockSizes , typename GridSizes >
static __device__ int sp::Grid< BlockSizes, GridSizes >::laneIdx ( )
static

Get the lane index of this thread.

◆ numBlocks()

template<typename BlockSizes , typename GridSizes >
constexpr static __device__ int sp::Grid< BlockSizes, GridSizes >::numBlocks ( )
staticconstexpr

Total number of blocks.

◆ threadGridIdx()

template<typename BlockSizes , typename GridSizes >
static __device__ int sp::Grid< BlockSizes, GridSizes >::threadGridIdx ( )
static

Index of this thread in grid.

◆ threadId()

template<typename BlockSizes , typename GridSizes >
static __device__ int sp::Grid< BlockSizes, GridSizes >::threadId ( )
static

Get the thread ID. This can be thought of as the 1D thread index within the block.

◆ threadIdx() [1/2]

template<typename BlockSizes , typename GridSizes >
static __device__ sp::Vec< int, BlockRank > sp::Grid< BlockSizes, GridSizes >::threadIdx ( )
static

Get the thread index as a Vec.

◆ threadIdx() [2/2]

template<typename BlockSizes , typename GridSizes >
template<int I>
static __device__ int sp::Grid< BlockSizes, GridSizes >::threadIdx ( )
static

Get the I'th thread idx.

◆ threadsInGrid()

template<typename BlockSizes , typename GridSizes >
constexpr static __device__ int sp::Grid< BlockSizes, GridSizes >::threadsInGrid ( )
staticconstexpr

Total number of threads in the grid.

◆ threadsPerBlock()

template<typename BlockSizes , typename GridSizes >
constexpr static __device__ int sp::Grid< BlockSizes, GridSizes >::threadsPerBlock ( )
staticconstexpr

Number of threads per block.

◆ warpGridIdx()

template<typename BlockSizes , typename GridSizes >
static __device__ int sp::Grid< BlockSizes, GridSizes >::warpGridIdx ( )
static

Index of this warp in grid.

◆ warpIdx()

template<typename BlockSizes , typename GridSizes >
static __device__ int sp::Grid< BlockSizes, GridSizes >::warpIdx ( )
static

Get the warp index, given the block dimensionality.

◆ warpsInGrid()

template<typename BlockSizes , typename GridSizes >
constexpr static __device__ int sp::Grid< BlockSizes, GridSizes >::warpsInGrid ( )
staticconstexpr

Total number of warps in the grid.

◆ warpsPerBlock()

template<typename BlockSizes , typename GridSizes >
constexpr static __device__ int sp::Grid< BlockSizes, GridSizes >::warpsPerBlock ( )
staticconstexpr

Warps per block.