CPU affinitity – Why need and How to ?

August 5, 2011 at 2:40 pm 1 comment


CPU affinity  ( or processor affinity ) is an ability provided by operating systems such as Windows or Linux that allows you to select  specific CPUs or processors to run your program/application on. In a multi-core and multi-processor system, the assignment of a process to a CPU/processor is automatically decided by the OS via its scheduler. However, you still can interfere in this scheduling task by specifying a CPU that your program will be run on.

Why CPU affinity is needed ?

You may ask “Why do I need it if OS is hanlding everything for me?”. You are right. In most cases, you won’t need this function. However, if the runtime performance is your concern,  in some cases, it’s worth a try to decide whether to use CPU affinity. For demonstration, I wrote a simple parallel program doing some computations using multi-cores. When I ran this program on a 12-core machine (two prossesors , six cores per processor) with/without CPU affinity, I got below runtime performance.

CPU affinity improves the  runtime performance because it optimizes cache performance by reducing cache miss. In a NUMA system, setting CPU affinity and allocating memory also on the faster RAM can speed up the process as well.

How to ?

There are two ways to set the CPU affinity in both Linux and Windows.

In Windows:
Method 1: Set the CPU affinity using Task manager

  • Open Task manager by pressing Ctrl + Alt + Delete and selecting the Task manager
  • Select Processes tab 
  • Right click on the process that you want to set CPU affinity
  • Select “Set affinity…” from the drop down menu
  • Set the CPUs that you want your program to run on


Method 2: Second, program from your source code
 You can use the Windows API SetProcessAffinityMask to set the CPU affinity from your program code

In Linux:

Method 1: Launch the program from the command line using settask
The below command will launch gedit in CPU 1 & 4 (or 0 and 3).

taskset -c 0,3 gedit

Method 2: Second, program from your source code
You can use the function sched_setaffinity in sched.h to manage CPU affinity from your code

Advertisements

Entry filed under: C++ Programming, Computer Architecture, Linux, Parallel Programming, Windows. Tags: , , , .

Fix the error ” make: execvp: Permission denied “ How to fix: locate: can not open `/var/lib/mlocate/mlocate.db’: No such file or directory

1 Comment Add your own

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

/openmp AMD app fixing dead pixel iphone C++ cmd program CPU C sharp dead pixels directory download ibm synthetic data generator Dynamic Dynamic Memory Allocation Edit Environment Variables in Windows Environment Variables error lnk2019: unresolved external symbol _getprocessmemoryinfo@12 referenced in function error lnk2019: unresolved external symbol _ getprocessmemoryinfo@12 referenced in function "void __cdecl printmemoryinfo(unsigned long example code Fixing Dead Pixels and Gray Lines on the iPhone Screen Fixing Dead Pixels on the iPhone Screen Fixing Gray Lines on the iPhone Screen Fix iPhone getprocessmemoryinfo GPU Gray Line iPhone Screen gray lines gray pixels green pixels GUI how to "new" a two-dimension array in C++ how to use ibm quest synthetic data generator ibm data generator ibm quest data generator ibm quest data generator exe ibm quest data mining project ibm quest market-basket synthetic data generator ibm quest market basket market-basket synthetic data generator ibm quest synthetic data generator ibm quest synthetic data generator linux ibm synthetic data generator ibm synthetic generator Intel iPhone Iphone 3G iPhone 3GS iPhone 4 iphone gray lines on startup iphone pixel damage iPhone Screen iPhone screen damage Linux market-basket synthetic data generator Memory Allocation Multicore multithread multi thread multi threaded multithreading mysql extract data into file new OpenCL Path processor quest data generator quest synthetic data generator R SAS Set Environment Variables Set Environment Variables in Windows souce code source code stuck pixels system file two dimension array Windows 7 Windows Vista

%d bloggers like this: