Going Parallel: Overture

Early this year I implemented basic multithreading support for simulations by having two threads running in parallel: the main one dealing with processing the rendering queue, while the simulation was running in the background. That was a straightforward approach and made the system more efficient, but it was hardly the best solution.

I’ve been reading a lot about concurrency lately and I wanted to give it a try, knowing that it might end up in a disaster. Truth be said, Crimild is far from being concurrency friendly, having a lot of shared, mutable data. Surprisingly, things seems to be working great so far

I’m going to write more about how the new implementation works in the future (that is, once I understand what I’m actually doing ;)). For the moment I’m just going to say that it looks promising. I know that this is going to be a long process if I want Crimild to be fully parallel (which might involve a bit of paradigm shifting), so I expect to have a lot of trial and error.

Here’s a quick look at something I’m working on right now. One of my goals is to provide a set of tools to write parallel code the easy way. For example, if a programmer needs to perform a computation in background, now he can use the newly crimild::async function, like this:

crimild::async( []() {
// do something in background
});

It may look like C++x11 std::async function but, instead of spawning new threads, crimild::async pushes tasks to the new Task Manager, which in turn executes them in parallel following a job stealing strategy.

Current implementation includes a way to provide scheduling hints when invoking crimild::async, like whether or not to run the task in the main thread, and a future-ish way to serialize task calls.

crimild::async( 
   crimild::AsyncDispatchPolicy::MAIN_QUEUE, 
   []() {
       // execute something in main queue
   },
   []() {
       // execute this code once the task is completed
   });

Before I go, here’s how the new implementation is working on Windows. Notice that it’s using four processors instead of two:

concurrency

It’s not using every available resource yet since there aren’t many parallel tasks, but that’s going to change soon. But first, I need to improve the Profiler…

Wish me luck