Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to determine threads per block, no. of blocks on a CUDA Device?

For example: i need to multiply two single dimensioned arrays A, B and Copy the result into C Array.

int N = 10; //Array Containing Maximum of 10 elements
size_t size = N*sizeof(float);
...
cudaMalloc((**void &&)a_d, size);
cudaMalloc((**void &&)b_d, size);
cudaMalloc((**void &&)c_d, size);
...
...
cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice);
cudaMemcpy(b_d, b_h, size, cudaMemcpyHostToDevice);

//How to determine no. of threads here???
int threadsPerBlock = ???
int noOfBlocks = ??

fmultiply<<<threadsPerBlock, noOfBlocks>>>(a_d, b_d, c_d);

cudaMemcpy(c_d, c_h, size, cudaMemcpyDeviceToHost);
...
...
Posted

It depends on the data set that you want to process and the number of the threads that you want to assign to each block.

In your example you are only processing 20 elements (2 * 10). You are supposed to have about 256 threads per block for max performance. In this case 256 threads is 246 more threads than you need for your calculation, because each kernel thread will process one element from A and B and output the result to C.

Generally the quick and easy way to figure out how many blocks you need is by dividing your total data by the number of threads you want to assign to each block.

number of blocks = (TOTAL_ELEMENTS/NUMBER_OF_THREADS)
 
Share this answer
 
v2
if the array is large you should e able to have threadsPerBlock as 512.

Int noOfBlocks = size / threadsPerBlock;

If ( size % threadsPerBlock) noOfBlocks++;

This takes into account the cases where size is not a multiple of noOfBlocks;

You can query the CUDA device for maximum number of threads you can use. It is reported that next gen devices will allow 1024.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900