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

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


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 <> function is used to force all threads of the process to execute on the designated processor. The SetThreadAffinityMask <> function forces a particular thread to only be scheduled on a specific processor. You will need to use GetProcessAffinityMask <> 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 <> 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 <> 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: <>
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 <>. 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

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 on 12-07-2005 04:56 PM

0 Kudos
0 Replies