Making the Most of Your CPU: Asynchronous Programming and Multithreading in the context of the Solar Energy tool

Hello again!

In my previous article, we explored some of the mathematics behind the Solar Energy tool we’re developing for Plan.City. We looked at how relatively simple concepts, such as vectors and dot products, can help us understand how sunlight interacts with the rooftops of buildings in a 3D environment.

Today, I’d like to continue exploring that same tool – but from a slightly different perspective this time around. Rather than focusing on the mathematics, we’re going to look at how we can turn those calculations into something that works efficiently within an actual application!

1. When simple calculations become a big problem

As we saw in the previous post, calculating the solar exposure of a single point involves knowing where that point is, and also which way its parent surface is facing, where the Sun is located, and whether any surrounding geometry is blocking the sunlight. That’s already a bit of work for one single location.

However, we want to analyse a building (ore even multiple ones!). If we imagine that we divide the rooftops of those buildings into, let’s say, some 5,000 sampling points (to use a round number), and that we want to calculate their solar exposure over an entire year, at hourly intervals… For a single building, that gives us…

…43.8 million individual evaluations!

Of course, each evaluation involves several operations, so the actual number of calculations is considerably higher. And while modern computers are incredibly powerful, asking them to perform all that work isn’t necessarily something we can do instantaneously.

This introduces a problem that anyone developing interactive applications will eventually encounter: how do we perform computationally expensive operations without making our application unusable in the meantime?

2. Asynchronous programming: keeping things responsive

Let’s imagine that we’ve implemented our calculations in a straightforward way. We have a function that receives a collection of rooftop points, processes them one after another, and returns the results once everything is finished.

There’s nothing inherently wrong with that approach. In fact, it’s often the simplest way to implement a new feature, and sometimes it’s perfectly adequate. However, a problem appears when we execute that function on the same thread responsible for updating our application’s interface.

In Unreal Engine, much of the application’s gameplay logic runs on what we call the Game Thread. If we ask that thread to perform a calculation that takes several seconds to complete, we risk preventing it from carrying out its other responsibilities during that time. The result? Our application might appear to freeze until the calculation finishes.

This is where asynchronous programming becomes useful.

Instead of forcing our application to wait for a lengthy operation to finish before continuing, we can arrange for that operation to execute independently and handle its results when they’re ready. In our Solar Energy tool, for example, we can launch the analysis as a background task, allowing the application to continue responding to the user while the calculations take place.

Illustration comparing synchronous blocking work, where a computer waits during calculation, with asynchronous non-blocking work, where background processing allows other tasks to continue.

This distinction is worth remembering: asynchronous programming doesn’t necessarily make a calculation faster. Its main advantage is that it allows us to organise work without unnecessarily blocking other operations.

And that brings us to our next concept.

3. Multithreading: sharing the workload

Moving our calculations away from the Game Thread is a good start, but we still have thousands of sampling points to process. And, didn’t you pay a nice sum for a good CPU that has several cores in it? Wouldn’t it be nice if we could distribute that work across them?

Well, that’s precisely the kind of problem that multithreading and parallel processing can help us solve!

Modern processors generally have multiple cores capable of executing instructions simultaneously. Instead of processing every rooftop point sequentially, we can divide the work into smaller groups and have several tasks process those groups concurrently. For example, imagine that we have 4,000 points to analyse. We could divide them into four groups of 1,000 points and assign each group to a separate task.

If those tasks can execute on different CPU cores, we may be able to complete the overall calculation considerably faster than if we processed everything sequentially. And, a great thing about the Solar Energy analysis we’ve been talking about: each sampling point can be evaluated largely independently of the others.

That makes the problem a natural candidate for parallel processing. We don’t generally need to finish calculating one point before moving on to the next, so we can distribute the workload without having to coordinate every individual calculation.

Illustration comparing single-threaded processing, where one character handles eight tasks sequentially, with parallel processing, where four characters handle two tasks each.

Of course, things aren’t always quite that simple! If ever… Creating and coordinating tasks introduces some overhead, and using more threads doesn’t automatically mean better performance. We also need to consider how those tasks access shared information, particularly when working within a complex application such as Unreal Engine.

Nevertheless, the underlying idea is straightforward: if a large problem can be divided into smaller, independent pieces, we may be able to solve those pieces simultaneously!

4. Putting everything back together

We’ve divided our calculations into separate tasks, how do we collect their results? You might be thinking that this is a trivial problem, but this is a good opportunity to expand upon what I just mentioned above: things aren’t quite that simple!

Let’s try to answer the question: one approach is to let each task work with its own collection of results, rather than having every task constantly modify the same shared array. When all the tasks have finished, we can combine those collections into a single dataset that our application can use.

This is particularly important because multiple threads accessing and modifying the same information can introduce problems that are surprisingly difficult to diagnose. That would be another approach, and it’s the one that we want to avoid.

Imagine two threads trying to update the same variable at precisely the same time. Depending on how those operations are implemented, we might end up with an incorrect result, even though the individual calculations appear perfectly reasonable.

These problems are commonly known as race conditions, and they’re one of the reasons why multithreaded programming requires a little more care than writing sequential code.

Fortunately, programming languages and engines provide tools to help us manage these situations. In Unreal Engine, for example, we have access to a task system, synchronisation mechanisms and thread-safe operations that make it easier to organise concurrent work.

The important thing is understanding why those tools exist and when we need to use them.

It’s also worth remembering that not every Unreal Engine operation is safe to perform from a background thread. Interacting with game-world objects, collision systems or engine-managed data may require particular care, so deciding which parts of a calculation can safely run in parallel is just as important as deciding how to divide the workload.

5. A few final thoughts

One of the things I enjoy most about software development is discovering how apparently complicated problems can often be broken down into relatively simple ideas. And I think our Solar Energy tool is a good example of that.

If we do a quick recap, you’ll see that we started with some mathematics to understand how sunlight interacts with a surface. We then realised that repeating those calculations across thousands of points could become computationally expensive, so we looked at ways to keep the application responsive and distribute the work more efficiently. That has lead us to asynchronous execution and parallel processing, a pair of concepts that appear in all sorts of applications: video games, simulation software, scientific computing or data processing. Understanding them opens the door to solving problems that might otherwise seem intimidating!

I hope this little look behind the scenes of our Solar Energy tool has helped make some of these concepts a little more approachable 🙂

Cheers!