Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.
1695 Discussions

Q&A: How to assign an application to a logical processor

Intel_Software_Netw1
286 Views

Here is a question received by Intel Software Network Support, as well as the response provided by our Application Engineers:

Q. I was wondering if you knew of tools or a way to assign an application to a logical CPU? We want to optimize a new feature with the Intel Xeon processor and hyper-threaded processors where logical CPU utilization is monitored.

A. We are not certain of your specific situation, so we are going to give several answers to your question.

If you are trying to specify which processor a process (application) should run on from a system administrator point of view, where you do not have the ability to modify the application itself, and you are using a Microsoft* OS, you can simply use Windows* Task Manager. When the process is running press Ctrl+Shift+ESC, click the Processes tab, select the name of the process, right click, and choose "Set Affinity...". From there you can specify which processor you want the entire application to run on. I also know that there is a Microsoft* Windows* 2000 Datacenter Server version of their OS which has sophisticated controls over which process and threads run on which processors.

Programmatically, if you are using a Microsoft* OS, the Microsoft* Platform SDK contains APIs that allow developers to control which processor their process or threads will run on. The SetProcessAffinityMask <http://msdn.microsoft.com/library/en-us/dllproc/base/setprocessaffinitymask.asp> function is used to force all threads of the process to execute on the designated processor. The SetThreadAffinityMask <http://msdn.microsoft.com/library/en-us/dllproc/base/setthreadaffinitymask.asp> function forces a particular thread to only be scheduled on a specific processor. You will need to use GetProcessAffinityMask <http://msdn.microsoft.com/library/en-us/dllproc/base/getprocessaffinitymask.asp> to determine which processors are available to your process, since as we mentioned earlier, the system administrator can limit which processors are available to the process.

There is also an Platform SDK API called SetThreadIdealProcessor <http://msdn.microsoft.com/library/en-us/dllproc/base/setthreadidealprocessor.asp> which doesnt force the thread to run on the specific processor, but rather strongly suggests it to the OS. The OS can still run that thread on other processors, but would attempt when possible to run it on the designated processor.

You can use the GetSystemInfo <http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp> function to determine the number of processors on the computer. However to determine which processors logical processors are associated with which physical processor, we recommend the Intel Developer Services article "Detecting Support for Hyper-Threading Technology Enabled Processors" available at: <http://www.intel.com/cd/ids/developer/asmo-na/eng/technologies/threading/hyperthreading/20416.htm>
That article includes sample code that determines how many processors are on the system, whether they are HT enabled, and which logical processors belong to which physical processor. Also you will notice that to accomplish this, the sample uses SetProcessAffinityMask.

Programmatically on a Linux* OS, there are some thread affinity APIs included with Red Hat* Linux* 9.0 and a patch that can be applied to the Red Hat* Linux* 8.0 distribution. Details on the thread affinity patch are available at <http://www.kernel.org/pub/linux/kernel/people/rml/cpu-affinity>. The thread APIs:

int sched_setaffinity (pid_t pid, unsigned int len, unsigned long *new_mask_ptr)
int sched_getaffinity(pid_t pid, unsigned int len,unsigned long *user_mask_ptr)

===
Lexi S.

IntelSoftware NetworkSupport

http://www.intel.com/software

Contact us

Intel is a registered trademark of Intel Corporation or its subsidiaries in the United States and other countries.

*Other names and brands may be claimed as the property of others.

Message Edited by intel.software.network.support on 12-07-2005 04:56 PM

0 Kudos
0 Replies
Reply