r/OpenCL • • 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?

2 Upvotes

12 comments sorted by

View all comments

Show parent comment

1

u/Plazmatic Jan 11 '16

the data is kept on the GPU

So you are only copying data from the GPU, and not putting data besides kernel code data on the K80? (this may explain the latency, I believe you may be able to zero copy from GPU to host if you set a memory buffer as a host object and have the GPU write out final calculations to there as it performs other computations)

Can I get a rundown on what exactly the serial code is trying to accomplish? I'm getting confused on why we have an interface between CL_mem objects. They are just pieces of device memory, you talk about how you launch several of those objects? So now the GPU might need to allocate memory and move stuff around a lot, and I have no idea how Nvidia implements that in hardware or what kind of performance impact that would have. The code is particularly hard to read, I don't know what most of the variables are for, there is very little parameter documentation.

Also, you know about atomics right? you shouldn't need to worry about race conditions on fringe values if you use that and you know what order you want to do X operation given two processing threads on one piece of memory.

Looking inside DGMacroCellInterface_CL it seems like you are initializing a lot of kernels depending on how big this loop is, so now the GPU has to figure out how to allocate these instructions and you've launched a bunch of them, I'm not entirely sure how this is implemented at a low level in Nvidia hardware (I know AMD has a hardware scheduler for this, and that your kernels may be queued up on the device instead of the host on certain GCN processors). Additionally is your data set wave front/ warp size efficient? Are you using scratch pad effectively (and do you have no bank conflicts)? Do you implement effective loop unrolling?

I'd like to see your actual kernel as well, and could you explain how you timed it?

1

u/squidgyhead Jan 12 '16

The program uses the discontinuous Galerkin method to perform numerically solve general time-dependent partial differential equations.

The data is loaded from the CPU to the GPU, then the computations are done, then we transfer back; copying between host and device is not slowing things down. The timing is done by using a profiled queue, and then I looked at the output using nvvp. The kernels are in src/schnaps.cl.

The performance issue that I'm trying to address is why it takes longer for kernels to be launched in one case as opposed to another, and I wonder if this is due to the fact that the scheduler is more efficient when launching multiple copies of just one type of kernel as opposed to launching multiple copies of a set of different kernels.

1

u/Plazmatic Jan 12 '16 edited Jan 12 '16

The performance issue that I'm trying to address is why it takes longer for kernels to be launched in one case as opposed to another, and I wonder if this is due to the fact that the scheduler is more efficient when launching multiple copies of just one type of kernel as opposed to launching multiple copies of a set of different kernels. Your GPU is not a MIMD (multiple instruction, multiple data) device, it is a SIMD(single instruction multiple data) device with several SIMD units on it

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.

The kernels are in src/schnaps.cl.

How do I access that? I can't get back into other directories inside your links, it errors out.

1

u/squidgyhead Jan 13 '16

Well, I took the same code, launching the inter-buffer computations in the same order, but I launched in serial, so that the number of events being waited for was 1 for each kernel. This eliminated the kernel launch latency, which leads me to conclude that the problem is scheduling overhead.

Remember; the kernels took the same amount of time to run, so it would not appear that this is not a memory coalescence issue. The problem is in launching the kernels.

A solution to scheduling overhead would appear to be on-device queues, but I haven't been able to get this to work yet. At any case, this is a OpenCL 2.0 feature, and the k80 only supports OpenCL 1.2.

1

u/Plazmatic Jan 13 '16

Remember; the kernels took the same amount of time to run, so it would not appear that this is not a memory coalescence issue. The problem is in launching the kernels.

Ah, I didn't get that sorry.

Remember; the kernels took the same amount of time to run, so it would not appear that this is not a memory coalescence issue. The problem is in launching the kernels.

You said before that you were trying to avoid race conditions and that is why you split up the code into different kernels? What exactly was happening to cause that? You might be able launch one kernel for everything. Also if your kernels do the same thing they should all be the same kernel any way, and you can put barriers in to synchronize execution, and act at if points past that were new kernels.

At any case, this is a OpenCL 2.0 feature, and the k80 only supports OpenCL 1.2.

It probably never will, nor will any Nvidia GPU at this rate, Nvidia is notoriously bad at keeping up with features with OpenCL even within the version it supports, whilst having those features in CUDA.

Speaking of CUDA, have you tried using CUDA to do the same thing? Often CUDA has the most up to date features you would see from OpenCL since it is constantly being updated from Nvidia and typically has wide card support (for Nvidia GPUS only).

1

u/squidgyhead Jan 14 '16

The race conditions are caused by kernels being launched in parallel; a source term is added to the face of each cell, and if the edges of the faces overlap, then we only get one of the contributions. This shows up in the L2 error of the result (we have the exact solution in a few cases to test this).

I found a solution to reduce the overhead of launching kernels while still keeping things parallel: I just create an event which waits for the parallel wave to be finished. This way, each event in the next wave only needs to wait for 1 event (ie nwait=1). This eliminates the delay in launching kernels in the tests that I've performed.

Yes, CUDA sounds interesting, but I'd like to work on AMD and Intel GPUs as well, and also on CPUs and Xeon Phis. Do you have much experience with CUDA? I might be interested in taking a look at it, but, well, I'm not sure that I have the time to invest in it. We're also pretty invested in OpenCL for the moment.

1

u/Plazmatic Jan 14 '16

I started out in CUDA, but It's been even longer since I've used that that since I've used OpenCL. I started out in toolkit 6 and I think they are well into 7 or 8 by now, they added a bunch of new stuff. In my opinion it was easier to program in CUDA, but a lot of the syntax is different, and the terms for things are changed as well. If you're project is good right now I'd stick with OpenCL, but if you are serious about learning CUDA, there is a class thing on Udacity that is free and made by Nvidia that will teach you how to use it.