Skip to content

Instantly share code, notes, and snippets.

@ugovaretto
Created January 10, 2013 12:49
Show Gist options
  • Save ugovaretto/4501831 to your computer and use it in GitHub Desktop.
Save ugovaretto/4501831 to your computer and use it in GitHub Desktop.
Compute number of CUDA blocks given grid size and number of threads per block.
int compute_blocks(int length, int threads_per_block) {
//integer division:
//if length is evenly divisable by the number of threads
//is equivalent to length / threads_per_block, if not
//it is equivalent to length / threads_per_block + 1
return (length + threads_per_block - 1) / threads_per_block;
}
dim3 compute_blocks(int xsize, int ysize, int zsize,
int threads_per_block_x,
int threads_per_block_y,
int threads_per_block_z) {
return dim3(compute_blocks(xsize, threads_per_block_x),
compute_blocks(ysize, threads_per_block_y),
compute_blocks(zsize, threads_per_block_z));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment