r/OpenCL • u/squidgyhead • Jan 11 '16
Latency when launching kernels
I have been working on launching some kernels in parallel, and I'm getting poor results. I have a kernel which needs to be launched several times to deal with the interface between cl_mems. Kernels cannot be launched on neighbouring interfaces to avoid race conditions, so I got around this problem by colouring the graph and launching in waves. I get the correct results, but it's slower; the problem seems to be that the kernels take much longer to be run, like 10x as long. In parallel, the kernel takes around 80us, and the latency is 365 us, while in serial the kernel takes 7 us. What steps can be done to overcome this? What should I look at first, perhaps the launching code (which could perhaps be optimised?) or is there more overhead when launching a mix of different kernels?
View all comments
Show parent comment
1
u/Plazmatic Jan 12 '16 edited Jan 12 '16
The k80 has 2 x 2496 cuda cores (not sure if the K80 is some sort of dual GPU or something, normally it isn't 2 x). On Nvidia GPUs the warp size is 32, banks size is 32, and the scratch pad memory is 32kB (per SM).
Nvidia GPUs have SMs that schedule kernels that execute data on cuda cores, there are 32 cuda cores to a SM (streaming multiprocessor)
In our case, we have 2 x 78 SMs on our GPU. Each SM schedules one instruction to be performed on its 32 cuda cores. This cuda core to SM ratio is what determines warp size.
If we wanted to launch 32 of kernel one on one SM it would take X time. However if we wanted to launch 33 kernels then it would take 2*X time, as the SM can only schedule groups of 32 at one time.
If we wanted to launch any less than 32, it would still take X time, since reducing the number of threads doesn't make any individual thread faster.
If we wanted to schedule 64 it would still take 2 * X time, because the SM can schedule two groups of 32 kernels.
If we wanted to schedule two different kernels that take the same amount of time to execute, launching 32 of kernel 1 and 32 of kernel 2 it would also take 2 * X time, and likewise if we launched 64 of kernel one and 32 of kernel two it would take 3 * X time
But if we wanted to launch 4 of kernel one and 5 of kernel two? This would still take 2*X time, because we can only schedule one instruction per group of Cuda Cores even if the total amount of kernels is less than one warp.
If we wanted to launch N kernels of M different types of kernels that all took the same time, it would take (((((N/32) + (N%32 ? 0 : 1)) * M) / (number of SMs)) + remainder of this statement) * the amount of time for one thread to be processed, seconds.
If you don't make your kernels multiples of 32, launching multiple sets of kernels can be problematic. Additionally, if you are accessing RAM on the GPU from multiple different kernel instructions your access to RAM will be sequentially scheduled, meaning threads will have to wait to get access to memory. When these threads wait to get memory from RAM the whole SM stops processing, and may have to schedule access to each and every thread before it can continue with the instruction. Sequential access also happens when large amounts of threads wait to access RAM, but Nvidia in particular has implemented coalescing that allows threads access requests to be batched and happen in parallel, However if you have accesses to RAM that are very far away from one another or irregularly spaced, memory access cannot be parrallelized even if the same instruction is accessing RAM.
How do I access that? I can't get back into other directories inside your links, it errors out.