Thinking Parallel

A Blog on Parallel Programming and Concurrency by Michael Suess

Thoughts on “Valve goes Multicore”

GamesA little while ago, an event was held by Valve Software, Inc. at their headquarters in Bellevue, Washington. If you don’t know Valve, it is the company behind the Source Engine, which powers Half Life 2. If you still don’t know what I am talking about, this short article is probably of no interest to you. πŸ™‚

Anyways, they have invited a couple of tech-journalists to tell them how they have parallelized their game-engine. And while I wasn’t invited (now who would have guessed :P), I have stumbled across two articles covering the event: one from and one from ArsTechnica. Reading through both of these articles, I could not resist the urge to comment on some of the points they made (need some practice in the self-control department, I guess). πŸ˜‰

As usual, let me add a little disclaimer beforehand: I don’t know too much about game-programming. I have been to a seminar or two on the subject, and have had regular conversations with a colleague of mine who is very knowledgeable about it. But of course, this does not make me an expert or anything, therefore I am going to try to stick to the parallel and threading part, because I am way more at home there.

I have thought about whether or not to give you an executive summary of the articles beforehand, but decided it was not worth it because you can always read through the referenced articles yourselves (they are not that long). Sorry, I am a bit on the lazy side today. Or you can of course just try to make sense of my comments without the context.

Enough of the preface, let’s start with the article on

“Fundamentally, games are about what you do, not what you see. So in terms of things which make games fundamentally profound experiences – and differentiate them from non-interactive entertainment such as TV and film – it’s more about what you do on the CPU than on the GPU.”

I wonder if NVIDIA agrees? But no, I am not going to get into this topic now. πŸ™‚ I just found it funny to hear this quote and all the announcements about doing traditional CPU-tasks on the GPU in such a short time-frame. πŸ™‚

To do this, Valve created its own set of tools. Whilst it evaluated using existing programmes – such as operating system-level threading engines, or open source threading engines – it found that these were inefficient when it came to games.

Unfortunately, both articles are very light on details about what exactly makes these threading systems so inefficient for games – a point I would have very much liked to know more about. Too heavy-weight? Not enough features? Too slow? Or even the old “not invented here”-syndrome? Its a shame they don’t go into this further, especially since I would find it a considerable pain to have to maintain my own threading library along with my application when there are other, probably more tested alternatives available. But then again, these companies operate in another league when it comes to resources, so what do I know. πŸ™‚

“We gravitated towards creating a custom work management system that’s designed specifically for gaming problems – its job is to keep the cores 100% utilised. We make it so that there are N-1 threads for N cores, with the other thread being the master synchronisation thread. The system allows the main thread to throw off work to as many cores as there might be available to run on, and it also allows itself to be subjugated to other threads if needs be.”

Or they could have just called it Master-Slave, like the High-Performance-Computing-world has done for ages :). They did not have to invent their own threading-system for that, that’s for sure…

The engine uses lock-free algorithms to solve one of the major problems of threading – access to data.

“We realised that, 95% of the time, you’re trying to read from a data set, and only 5% of the time are you actually trying to write to it, to modify it. So, we just allowed all the threads to read any time they want, and only write when the data was locked to that thread.”

This part is very misleading, and I hope the journalists misunderstood something here. What he describes are not lock-free algorithms in any way, he describes good old Reader-Writer locks. These are far easier to write and far more portable than lock-free algorithms (in fact, one of my students just wrote a Reader-Writer-Lock implementation with C++ and OpenMP without too many problems). And therefore the terms should not be freely mixed in my humble opinion, because we are talking a whole new level of difficulty here (and by the way, Reader-Writer Locks are also available in many threading systems, so I am starting to think it was not lack of features that made them implement their own).

That’s the bits I wanted to comment on from the first article. Let’s take a look at the second one from ArsTechnica:

To accomplish this, programmer Leonard made use of a technique called lock-free algorithms. He implemented a spin lock to replace the more traditional mutexes (mutual-exclusion algorithms) and semaphores that are used to flag threads as being “safe” to multithread. The spin loop utilizes a new interlock instruction that is built into the CPU.

Now they claim lock-free algorithms can be implemented with spin-locks. *sigh*. Same mistake as in the other article.

Tom examined the threads’ activity with profiling tools, and it turned out that 95 percent of the time the threads were reading memory while only spending 5 percent of their time writing. A read/write lock allowed many threads to read the same bit of memory, but only one thread to write to it.

This sounds better and more correct: so they do know what a Reader-Writer lock is after all, and the above blurbs were probably things the journalists misunderstood. *sigh of relief* πŸ™‚

Not only was the speedup on the four-core Kentsfield chips nearly linear in most cases (an average of 3.2 times over a single core)

Please repeat after me: Speedups are measured against the fastest sequential version, not against the parallel version run on one core. Speedups are measures against…
If I had gotten a dollar for every time I heard this mistake in the past I would be a rich man by now – or maybe it’s just selective memory :?…

The demo was run on a 2.6GHz Kentsfield CPU with four cores and 2GB of RAM. On a single-core 3.2 GHz Pentium 4, fewer than 100 critters could run around at the same frame rate, which looked much less impressive.

Let’s not talk about comparing apples with oranges, shall we? Different architecture, different number of cores, different clock-speed – these numbers tell us exactly – not so much 8O.

The tools don’t completely remove the responsibility of the programmer to think about the best approach to adding multithreading support to their code, but they do help programmers avoid common pitfalls and greatly diminish the time it takes for coders who are new to multithreading to get up to speed.

How come they always stop when it gets interesting? Why do these tools help so much? How are they different from ordinary threading-systems? These are the questions that are interesting to a multi-threading-junkie like me (but I guess the target audience of both articles does not exactly fall into that category).

A question was asked about supporting executing game code directly on GPUs, an idea that some have considered due to the improvements in GPU speed over the last few years, Valve does not believe that this is the correct approach. GPUs can’t natively execute existing x86 code whereas multiple cores can execute it directly, meaning less work for the programmers.

What a nice coincidence: the closing quote from this article is about the same topic as the first one: the power of GPUs and how to harness it. I don’t find this last sentence too convincing, though: I am regularly writing code that runs quite nice on non-x86-hardware and as long as I have a compiler that understands and translates my programming language of choice into the target machine language, I could not care less about x86-compatibility. I would only expect problems if there were assembler-instructions scattered throughout the code, which of course would make a port extremely difficult. On the other hand, porting code to a whole new programming paradigm (like e.g. stream-processing) is of course hard – but if the graphics hardware companies do deliver, what they have been promising lately, programmers should not have to worry about this sometime in the not so distant future.

This closes my little commentary on the two articles covering the Valve-event, hope you enjoyed my perspective a little!

4 Responses to Thoughts on “Valve goes Multicore”


  1. Comment by Isaack | 2006/11/21 at 17:39:30

    What used to make it difficult to do fast multithreading with gaming when you are spending most of the time on the GPU, is that, usually only once thread can control the graphics system. At least on Windows and at least on OpenGL, I forgot if DirectX also had the same limitation.
    Also, I think that Epic Games always used DirectX.

    OpenGL on OS X does allow you to control the graphics with different threads. Haven’t played with it yet though.

    Anyway, so the problem was. You couldn’t load and upload textures into the GPU from different threads, only load from disk and then have the main thread upload the texture. And it went on like this. There wasn’t really enough to do in other threads to make it a worthwhile speedup.
    But I think that might have changed now, with todays games and their more advanced physics, AI and other CPU stuff.

    John Carmack of iD Software spend alot of time on this long ago and had SMP support in Quake2 to begin with, but it didn’t give him much speedup.

  2. Comment by Bill King | 2006/11/22 at 01:40:22

    Nicely put. Being a multi-threading junky too (and recently discovering the joys of OpenMP) led me to your blog, so I’m not quite the same target as either of those articles, but yeah, they left me with _exactly_ the same questions going on. In regards to isaac, the problem with game programmers is that the industry is geared towards speed of writing code, not quality of code. Some of my most amusing times have been speaking to friends in the gaming industry, and opening their eyes with techniques and ideas and concepts that most application developers see as standard practice. Game coding seems to be finally starting to evolve from it’s hacky ancestry into something finally resembling decent, only… it’s idea of decent is 5-10 years old. I personally think OpenMP would be a godsend for the gaming industry, because of it’s simplicity to move code from a single-threaded environment into a multi-threaded powerhouse with a few twists of the wrist.

    Anyway, I’ve diverged. Great summary πŸ™‚

  3. Comment by Michael Suess | 2006/12/07 at 10:56:49

    @Bill: I am not familiar enough with the computer games industry to comment on their software-engineering practices – but I suspect as in every industry, there are companies that are just happily hacking along and there are companies that are trying hard to improve their processes.

Trackbacks & Pingbacks »»

  1. […] A while ago I posted some comments on the press coverage of an event by Valve, where they explained threading in the Source Engine to a couple of tech-journalists. Unfortunately, the coverage left many open questions and some things even appeared to be wrong. Fortunately, Tom Leonard, the threading guru from Valve is a nice guy and agreed to answer some of my questions via email. This post contains the conversation we had. […]

Comments are closed