palaniappan-r's picture
Add markdown sources
d59bb1b verified

Ideas for multithreaded rendering

Tool: Graphical User Interface

Subcategory: Performance enhancement

Conversation

rovinski

I saw the conversation in #3330, #3342, #3348, etc. and started thinking about how the rendering time might be improved aside from not drawing objects that aren't useful.

It seems that the rendering, for the most part, is embarrassingly parallel. In LayoutViewer::drawInstanceOutlines() for example (the most expensive layer measured in #3348), a function is applied to every instance in a vector to determine how / if it gets drawn, and then it makes a draw call.

https://github.com/The-OpenROAD-Project/OpenROAD/blob/6920ac530b6959f742e75b7f55062b1642730b14/src/gui/src/layoutViewer.cpp#L2041-L2091

This is essentially a map reduce paradigm which should be extremely parallel, except that Qt painter objects aren't thread safe. There was discussion in #3279 that the database is not "thread safe", but I think that was in the context of read/write operations and not read-only operations.

Beyond that, each layer is independently but sequentially rendered:

https://github.com/The-OpenROAD-Project/OpenROAD/blob/6920ac530b6959f742e75b7f55062b1642730b14/src/gui/src/layoutViewer.cpp#L2555-L2599

There may or may not be ordering required on draw calls to these layers, I'm not sure.

I looked at a few Qt guides and they all recommend separating computation from rendering in separate threads for better performance (and interactivity).

I think some of these things were mentioned by @QuantamHD in #1930 but the PR itself seems to be unrelated (and abandoned).

I have some interest in looking into this, but I wonder what the best approach would be for this? Qt has QThread and QThreadPool for this exact purpose. There's also QtConcurrent which may have some uses.

In an ideal world, it seems like there should be a thread pool with getThreadCount()-1 threads and the main thread. The main thread has a render queue which atomically pops the queue for draw calls and does nothing else until all worker threads yield. The worker threads can parallelize at the granularity of layer or instance/shape depending on what's optimal. But this could take a lot of refactoring, possibly be overkill, or possibly be difficult to tune the amount of threading correctly.

An intermediate step could be to simply parallelize some of the heavy-weight tasks that block the GUI thread, such as using std::transform(std::execution::par) or std::for_each(std::execution::par) and separating the draw calls from the filtering. But also these functions don't seem to be well supported on old versions of GCC/Clang (it requires Intel libtbb and may error or just use a serial version if it isn't linked, but I think it also think it doesn't work on Apple silicon). Maybe using OpenMP would work?

Any thoughts or opinions? @gadfort @maliberty @oharboe @QuantamHD

oharboe

After the recent performance improvement, the biggest performance problem currently for us, is that all intermediate zoom levels are rendered https://github.com/The-OpenROAD-Project/OpenROAD/issues/3278

So if that could be fixed FIRST... :-)

Afterwards, the suggestions that you propose sound wicked!

It is true that Qt nor the database is thread safe, then on the other hand, this means that the GUI is single threaded, so I suppose that you are right that you can use as many or few threads as you like as long as Qt nor the database functions are non-the-wiser. Rendering should be especially amenable to this since it is a read only operation...

Also, rendering doesn't have to be perfect. I have been thinking that a draft could be rendered while zooming and then when the user stops giving input, more detail can be rendered.

maliberty

@rovinski I've considered all these ideas but lack the time to implement them all. Right now I'm looking at moving the rendering to a separate thread and making it interruptible. The main challenge is figuring out how to trigger the right events at the right time to make Qt happy. It is very particular that you can only paint in paintEvent.

Assuming I can land this then it is more straightforward to split that worker thread into multiple threads. KLayout works in a similar fashion where they render per layer in each thread and composite in the main thread.