information about blockidx/blockDim ..


i’m a new and need some help.

i got this program and i change some variables but i dont get what does it do.

#include <stdio.h>

__global__ void kernel( int *a )


int idx = blockIdx.x*blockDim.x + threadIdx.x;

a[idx] = 7;


int main()


int dimx = 16;

int num_bytes = dimx*sizeof(int);

int *d_a=0, *h_a=0; // device and host pointers

h_a = (int*)malloc(num_bytes);

cudaMalloc( (void**)&d_a, num_bytes );

if( 0==h_a || 0==d_a )


printf("couldn't allocate memory\n");

return 1;


cudaMemset( d_a, 0, num_bytes );

[u][b]dim3 grid, block;

block.x = 4;

grid.x = dimx / block.x;

kernel<<<grid, block>>>( d_a );[/b][/u]

cudaMemcpy( h_a, d_a, num_bytes, cudaMemcpyDeviceToHost );

for(int i=0; i<dimx; i++)

printf("%d ", h_a[i] );


free( h_a );

cudaFree( d_a );

return 0;


i dont understand what does

dim3 grid, block;

block.x = 4;

grid.x = dimx / block.x;

kernel<<<grid, block>>>( d_a );

what the diffrenet if it was y insted of x ?

same for

blockIdx.x*blockDim.x + threadIdx.x;

what does the x do ?

i know its the dim but how does it come to play visualey.

thanks ahead.

Sounds like you havn’t found the CUDA documentation,

I suggest you start with:
Programming Guide

Chapter two answers your questions

Others you will particularly need later:
Getting Started Guide Windows
Reference Manual
CUDA Best Best Practices Guide

NB briefly that program is almost a “Hello World”, except that what it does is put 7 into every element of a small array. And you can do far far far more than that in CUDA.

PS this code isn’t doing anything visual, except printing a list of numbers. Cuda uses the processing power of the GPU to do NON-graphics processing. The SDK (Software development kit) contains a lot of examples that use the GPU to do a mountain of calculations and then also display them. My favourite is the n-body simulation, 12000 particles with the gravity interactions between every particle calculated every 1/20th of a second :)

I’m not sure what you’re asking.

Dim3 is a 3-Dimensional type; that is, it has an x, y and z component.

eg. This Rubik’s Cube might be described as such:

dim3 rCube;

rCube.x = 3;

rCube.y = 3;

rCube.z = 3;

So, the cube has 3x3x3 = 27 elements.