I have a question about multi core processors like Intel core duo, core duo quad. Is there possibility from program or OS level to enforce process/program to be fetched on selected core.
I have a PC with CoreDuo processor with 2 cores. And I wont to run computationally expensive real time application on it. So is there a possibility to attached processes to cores??. E.g. all OS processes will be fetched on one core and only our application will be fetched on second core? Is there any OS which allow this?
- Windows: SetThreadAffinityMask()
- Linux: sched_set_affinity()
Have a look at the following article for example:
In addition to inserting code into your program on Windows you can use the Task Manager to specify processor affinity for a program (while it runs), Linux has a similar method. Also, on a Windows system there exists in the program header (where Date, Time, Attributes, etc.. reside) two affinity mask fields (Hard, Prefered). There is a program to read/write these fields but the name of this program escapes me at the moment.
For OpenMP programs, recent OpenMP libraries generally support environment variable thread affinity (KMP_AFFINITY for Intel OpenMP, GOMP_AFFINITY for gnu). Major MPI libraries have similar facilities for process affinity.
As may be apparent, particularly on Windows, these options aren't 100% effective. Task Manager involves human intervention after the thread has started. They don't prevent other tasks from jumping in and getting the core, forcing the OS scheduler to choose among any remaining available cores.
I could go on about this topic given the time, but for now here aresome code samples you may find useful.
- WinCPUID library- API you can use to get CPU features and topology information
- CPUCOUNT- Command-line utility that can show CPU features and topology to affinity mapping
Forcing your apps threads to run on a certain set of processors for the purpose of getting better performance is a tricky and fragile optimization technique. You are likely to run your code on different hardware or on a different OS, and find that your "optimization" actually hurt performance. Your time might be better spent improving your app's threading model (Intel Threading Tools may help here) or trying a different compiler and/or new optimization switches (i.e. Intel C++ Compiler). If you really want to do some affinity setting, I recommend carefully considering what will happen with different numbers of cores, withand without HyperThreading, and convince yourself that you will still get good results. I wrote some of the WinCPUID code for this purpose.
Ok, I did go on a bit... Consider yourselfwarned - there is a good chance you will not get the result you hoped for by playing with your app's thread affinity mask, especially if it is a high-volume commercial application that will run on a variety of hardware of hardware configurations.