Making program/thread that take advantage of HyperThread or Multi Processor computers

Started by
16 comments, last by flangazor 19 years, 4 months ago
**Moderator: if this belongs to the lounge, please move immediately. Thank you.** I'm a little clueless and curious on how a program is supposed to take advantage of a mulit processor automatically. I always thought that if a program spawned more than one thread then if a multiprocessor was present it would automatically take over that thread. Or if two programs (which is the case 99% of the time) are running at the same time each processor would execute one of the them. Could someone drop a little pseudocode or knowledge on how we as programmers are supposed to take (full) advantage of this technology or how we as consumers should know when to buy such a thing? Thanks.

Beginner in Game Development?  Read here. And read here.

 

Advertisement
As far as I'm aware, windows at least will automatically schedule threads to take advantage of whatever hardware is available, so all we have to do is use threads where they make sense and windows will do the rest.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
In most programs only one thread performs CPU dependant operations while other threads are used for networking, IO, etc. purely due to convinience and/or user experience. Naturally these programs won't benefit from hyperthreading. Only programs that deal will parallelizable problems can benefit from it. For instance, if you create two theads, one for physics and one for AI, both threads do expensive calculations on the CPU and don't depend on each other, then you'll be taking advantage of hyperthreading. You don't need to call any special instructions. You just need two threads that both do heavy CPU calculations at the same time and don't have to wait on each other.
Using multiple worker threads will slow it down on single CPU (or non-ht enabled) machines. So if you're going to use it, make sure it can be turned off or disabled.

I think Quake3 uses it, not much else though.

Mark
Does Hyper-threading work for all types of operations? IE, In my program, I spend a lot of time running code in the FPU and/or SSE2/3Dnow! instructions. Will these operations also see a speed increase?

Also, say I that my program has a fairly sensible way to break down into 2 threads. How much of a performance gain, a rough range would be nice, can I expect to get. If it isn't too much, I just wont bother, but if it is significant, I might put the extra time in.

Thanks

Dwiel
If your code has two groups of tasks which don't need access to the same data and take roughly equal time to execute, hopefully you could get close to 100%.

But in a game, I think it's very unlikely that the two sets of tasks would be equal time or non-conflicting.

If you start doing significant amounts of locking and waiting it will quickly negate the advantage, or if one thread spends most of its time idle, the advantage will go as well.

Plus add that to the extra effort of implementing and debugging a multithreaded application, I suspect it won't be worth it.

You need to do some profiling of your two sets of tasks, ideally on a frame-by-frame basis as well as a longer term basis to judge whether they can be usefully parallelised.

Mark
Apart from Hyperthreading which will clearly be more prevalent in the mid term future, most desktops only have one processor and are therefore not going to be able to take advantage of paralellised tasks. However, if you program for servers or mainframes (e.g. the backend of all those MMORPGWTFBBQs that people seem to want) then multiple processors will more than likely be available.

When you are at your prompt and you enter "Generate_probable_primes | Print_if_really_prime" then your computer will be creating two processes: an instance of enerate_probable_primes and an instance of Print_if_really_prime. On most Unicies, these can be automatically delegated to separate processors. They do rely on each other insofar as Generate_probable_primes passing it's stdout to Print_if_really_a_prime's stdin, but otherwise they are don't rely on each other very much and are therefore going to take advantage of being separate processes.

You can find more information on how Unix handles it in Advanced Programming in the UNIX Environment.
Design a flexible codepath system, which will enable you to spawn some more worker threads in case the machine is either an Hyperthreading or a Multi-Cpu System.

Max % output work off of a second Cpu is 80%, that means that if your game is running at 30fps on a single cpu machine, and you add a 2nd unit, you'll probably get a speed boost up to 54fps or less.
Quote:Original post by Prozak
Design a flexible codepath system, which will enable you to spawn some more worker threads in case the machine is either an Hyperthreading or a Multi-Cpu System.

Max % output work off of a second Cpu is 80%, that means that if your game is running at 30fps on a single cpu machine, and you add a 2nd unit, you'll probably get a speed boost up to 54fps or less.
For home computing, sure.

On the other hand, there is a reason why the Earth Simulator has 5140 processors.

OP: if you have some serious number crunching to do or are running a server that serves many people, then multi-processor systems are very important. Consider Telephone companies, banks, or geological departments at reasearch universities. They have a lot of information to process.

However, for your videogames and whatnot, you likely don't need multiple processors since you're the only user and all tasks your computer is doing in a single application regard what you are doing. Hence they are going to be connected somehow, hence the 80% that Prozak mentions.

For playing media files, the bottleneck will be the memory and not your single ALU, so forget about 'what if I want to play two movies at once and junk'.
Quote:Original post by flangazor
Quote:Original post by Prozak
Design a flexible codepath system, which will enable you to spawn some more worker threads in case the machine is either an Hyperthreading or a Multi-Cpu System.

Max % output work off of a second Cpu is 80%, that means that if your game is running at 30fps on a single cpu machine, and you add a 2nd unit, you'll probably get a speed boost up to 54fps or less.
For home computing, sure.

On the other hand, there is a reason why the Earth Simulator has 5140 processors.

OP: if you have some serious number crunching to do or are running a server that serves many people, then multi-processor systems are very important. Consider Telephone companies, banks, or geological departments at reasearch universities. They have a lot of information to process.

However, for your videogames and whatnot, you likely don't need multiple processors since you're the only user and all tasks your computer is doing in a single application regard what you are doing. Hence they are going to be connected somehow, hence the 80% that Prozak mentions.

For playing media files, the bottleneck will be the memory and not your single ALU, so forget about 'what if I want to play two movies at once and junk'.


Yeah, I am doing heavy computing on a hyper-threading machine. I actually have a network of 4 computers to work on the problem, which was surprisingly easy, but I think I might be able to gain some extra speed on my HT machine by finding a away to use threads. Although it might be worth more of my time to convert to sse2/3dnow! instructions due to the large ammounts of # crunching I am doing...

Sorry to slightly derail the thread...

Dwiel

This topic is closed to new replies.

Advertisement