.NET Forum / .NET Framework / New Users / March 2008
Multiple Processors - or - Multi-Core Processor
|
|
Thread rating:  |
Mythran - 18 Mar 2008 16:29 GMT Just as a quick note before my question, I am not creating an application for this, or library, or anything else. Just knowledge I'd like to obtain.
Now, question is, when developing a multi-threaded application (such as a game) is there anything special that needs to be done to make use of the multi-processors (or cores)?
That's it :)
Mythran
Jon Skeet [C# MVP] - 18 Mar 2008 16:39 GMT > Just as a quick note before my question, I am not creating an application > for this, or library, or anything else. Just knowledge I'd like to obtain. [quoted text clipped - 4 lines] > > That's it :) Not really - you're just slightly more likely to see any threading bugs if you've *actually* got it running on multiple cores.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Cowboy (Gregory A. Beamer) - 18 Mar 2008 17:26 GMT In most instances, the multiple core/multi proc set up may actually help you notice bugs in your code, which is a good thing (and what Jon stated, albeit in a more roundabout way). Provided you are coding threads properly, it should not matter.
 Signature Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog http://gregorybeamer.spaces.live.com/lists/feed.rss
*************************************************
| Think outside the box! *************************************************
> Just as a quick note before my question, I am not creating an application > for this, or library, or anything else. Just knowledge I'd like to [quoted text clipped - 7 lines] > > Mythran Jon Skeet [C# MVP] - 18 Mar 2008 17:30 GMT > In most instances, the multiple core/multi proc set up may actually help you > notice bugs in your code, which is a good thing (and what Jon stated, albeit > in a more roundabout way). Provided you are coding threads properly, it > should not matter. The other thing I didn't think of - it will give you a better idea of whether your threading is actually having an effect or not. If you have a multi-threaded version and a single-threaded version, but they run at the same speed even on a 4-core box, then either you're doing something wrong or your problem isn't very parallelisable :)
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk
Cowboy (Gregory A. Beamer) - 18 Mar 2008 17:50 GMT In addition, if you are not properly locking the shared resources, you will more likely end up with a FUB when working on a multi-core, especially when process #1 runs on the primary core that windows is running on and another process runs on another core.
There are no guarantees of course, where the thread will fire, so testing of threaded applications needs to be more than a couple of unit test hits. Of course, that applies to all programming, so I am being redundant. :-)
 Signature Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog http://gregorybeamer.spaces.live.com/lists/feed.rss
*************************************************
| Think outside the box! *************************************************
>> In most instances, the multiple core/multi proc set up may actually help >> you [quoted text clipped - 8 lines] > the same speed even on a 4-core box, then either you're doing something > wrong or your problem isn't very parallelisable :) Peter Duniho - 18 Mar 2008 20:15 GMT > Just as a quick note before my question, I am not creating an > application for this, or library, or anything else. Just knowledge I'd [quoted text clipped - 3 lines] > a game) is there anything special that needs to be done to make use of > the multi-processors (or cores)? On the whole, the other replies are correct. Assuming no bugs, code that is already multi-threaded should "just work" in terms of taking advantage of multiple processors. However, there are some caveats:
* Probably the biggest one may not be very relevant any more, with Hyperthreaded CPUs not being a big thing anymore. But if you've got an algorithm that parallelizes in a way that makes heavy use of local variables (e.g. a recursive algorithm), naive multithreaded code on a HT process can make performance much worse, rather than even the modest improvement one might otherwise see. The virtual cores on a HT CPU share a cache, and when you've got multiple threads aliasing cache lines because their stack lives in the same offset within the cache, you wind up setting up a situation where the threads are trashing each other's cached data.
* If you are multi-threading as a way of getting performance, as opposed to it simply being a architectural "simplification" (yes, writing multi-threaded code can actually simplify the design, even as it introduces the new complexities related to multithreading), then you should be careful to not have too many threads. In an optimal situation, you would have exactly as many threads as you have CPU cores. In reality, you might want a handful of extras, especially if your algorithm involves some i/o. But going too far beyond that may result in inefficiency as you switch from one thread to the next (though for completely CPU-bound algorithms, this inefficiency should never be huge).
* Even on a single-CPU system, multi-threading _may_ improve performance, if your algorithm is i/o-bound. Having multiple threads ready to process i/o results when they complete can be very helpful. If you can use an IOCP-based mechanism, even better (but I don't know in .NET which async mechanisms do this, except for the Socket class which I know does), since using IOCP you have one thread per processor (or thereabouts) that can handling i/o completions for some arbitrary number of tasks.
These are just a few things to keep in mind. In reality, getting the best results from multi-threaded code on a multi-processor system is non-trivial. It's a good idea to try the naive approach first, but you should at least be prepared for the possibility that something may thwart that approach. Multiple threads and multiple CPUs introduce some non-intuitive behaviors (or at least, behaviors that are non-intuitive until you've been doing multi-threaded code for awhile :) ), and so if something doesn't seem to be working as you expected it to, don't worry. It probably just means there's one of these non-intuitive things getting in your way. :)
Pete
Cowboy (Gregory A. Beamer) - 19 Mar 2008 16:45 GMT Great addition. My two cents.
In general, multi-threading is done for architectural reasons, not performance. I know there are instances where you do it solely for performance, but that can lead to serious problems if designed incorrectly. Of course, bad multi-threaded design is dangerous, no matter how you slice it. :-)
You generally do it, at least today, to run in parallel rather than serial (or allow a user to continue working while a long running process runs on another thread,k aka run in parallel). This is changing a bit now, but also leading to some really bad applications.
Please note that I do not consider allowing a second process to run while a long running process has its thread locked down a performance gain, although that is one of its symptoms.
Performance is overrated. It is too often made the primary concern when it should not be.
 Signature Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog http://gregorybeamer.spaces.live.com/lists/feed.rss
*************************************************
| Think outside the box! *************************************************
>> Just as a quick note before my question, I am not creating an >> application for this, or library, or anything else. Just knowledge I'd [quoted text clipped - 49 lines] > > Pete Mythran - 20 Mar 2008 17:17 GMT > Great addition. My two cents. > [quoted text clipped - 74 lines] >> >> Pete Thanks all who have responded thus far, the comments have been great...
One reason I would use multiple threads in an application is ANY time an app I'm writing is downloading something off a slower than intranet speeds (anything off the Internet or off an extranet). Also, anytime my application would otherwise *hang* for even a brief period of time (ie: looping or writing to disk), I would separate that logic into another thread and then display a message stating what the thread is doing, or allow the user to continue working, if appropriate. It all depends on the situation.
Now, is there anyway for the programmer to actually *take advantage* of a multiple processor/core configuration and actually do some lower level development to ensure the application runs specific parts on a dedicated processor/core (all it's threads run in the same core/processor) while the rest of the application (ie: GUI) runs on one of the others? I can see using something like this for games or even advanced imaging/video/sound software where processing power may be important to physics (imaging/video) software, or dynamic realistic sound generation for 3-D environments (or anything else NASA wants). Having a physics processor would help with that, but if all I have is something like a QUAD-CORE processor, I'd like to run some threads on a dedicated core that the O.S. would not use for other things (lock it down temporarily for my own use)...
I could see some bad side effects of doing this (what if the machine has only a single-processor with a single-core?). But all in all, can .Net, or ANY OTHER programming framework do this?
Once again, this is all hypothetical for me. Just some questions and thoughts that I've had regarding multiple-processors and, more accurately, multiple-core processors.
Thanks, Mythran
Peter Duniho - 20 Mar 2008 18:01 GMT > [...] > One reason I would use multiple threads in an application is ANY time an > app I'm writing is downloading something off a slower than intranet > speeds (anything off the Internet or off an extranet). Keeping in mind that in .NET, it's not necessary to explicitly use threads to do this. All of the i/o classes have an asynchronous API that allows for starting an i/o operation to be completed at a later time, without blocking the current thread.
The underlying implementation does use threads, but in that case you don't need to explicitly start any new threads yourself.
> [...] > Now, is there anyway for the programmer to actually *take advantage* of > a multiple processor/core configuration and actually do some lower level > development to ensure the application runs specific parts on a dedicated > processor/core (all it's threads run in the same core/processor) while > the rest of the application (ie: GUI) runs on one of the others? You seem to be putting the cart before the horse.
To take advantage of multiple CPUs, all you need to do is make sure you've got enough threads to keep all the CPUs busy. The OS will take care of the rest.
It is in fact possible to assign a specific thread to a specific CPU, but this is usually not actually what you want to do. The CPU will preserve affinity as much as possible/necessary, but if you've explicitly set CPU affinity for a thread, you could in fact wind up interfering with performance. For example, for some other threads they actually require affinity to work correctly, and if you dedicate your own thread to the same CPU those threads have affinity to, your own thread could wind up waiting on a CPU core when there's another core sitting idle.
Pete
Mythran - 21 Mar 2008 16:49 GMT >> [...] >> One reason I would use multiple threads in an application is ANY time an [quoted text clipped - 32 lines] > > Pete Ahh yeah, I understand where you are going with that. Theoretically, if I was to write an application for a machine that would be dedicated for this application only (and of course, the OS, drivers, etc for that system), it would be optimal. But in reality, what your saying would still be true and my application's dedicated thread for a core would end up waiting for an "in-use" core until the core is free'd, even if another core was not in use. I understand that.
Thanks, Mythran
Peter Duniho - 21 Mar 2008 17:48 GMT > [...] > Ahh yeah, I understand where you are going with that. Theoretically, if > I was to write an application for a machine that would be dedicated for > this application only (and of course, the OS, drivers, etc for that > system), it would be optimal. Even in that situation, unless you are very careful in your implementation to _never_ have any other thread that might compete for a given CPU, assigning CPU affinity can result in a thread waiting when it otherwise could be running.
Note that on Windows, it is practically impossible to ensure that no other thread will compete for a given CPU. The situation I described, where there are already other threads with CPU affinity, is the worst-case scenario, but _all_ threads can be preempted. As soon as your own thread is preempted, some other thread can and will be scheduled on the CPU that thread was using. In the meantime, another CPU core might become available, but your thread will still sit around doing nothing because "its" CPU core is occupied by some random thread that just happened to be scheduled there.
> But in reality, what your saying would still be true and my > application's dedicated thread for a core would end up waiting for an > "in-use" core until the core is free'd, even if another core was not in > use. I understand that. Right. And really, there's no compelling performance reason to use CPU affinity IMHO. Windows will do the best it can, and if it can't keep a thread on the same CPU, it's for a good reason and you're better off with the thread running on a different CPU than not running at all.
Pete
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|