Binding to the current processor

Just hacked up a snippet of code to stop a thread migrating to a different CPU while it runs. This should help the thread get, and keep, local memory. This in turn should reduce run-to-run variance.

#include <sys/processor.h>

void bindnow()
{
  processorid_t proc = getcpuid();
  if (processor_bind(P_LWPID, P_MYID, proc, 0)) 
    { printf("Warning: Binding failed\n"); } 
  else
    { printf("Bound to CPU %i\n", proc); }
}
Comments:

On the other hand, I wonder if that exact processor isn't available, the process wouldn't be put into sleep. Would it?

Posted by Carlos Azevedo on May 31, 2013 at 06:30 AM PDT #

The thread would wait until it could get onto that exact processor. So binding is in general, not a good idea. However, it can be very useful if you control the machine and can place the threads exactly where you want them. The scenario I most often need is when I want to control every variable so that I can be sure that a run-to-run difference is due to something I've done rather than random chance.

Posted by Darryl Gove on May 31, 2013 at 09:03 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

Darryl Gove is a senior engineer in the Solaris Studio team, working on optimising applications and benchmarks for current and future processors. He is also the author of the books:
Multicore Application Programming
Solaris Application Programming
The Developer's Edge

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
5
6
8
9
10
12
13
14
15
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
The Developer's Edge
Solaris Application Programming
Publications
Webcasts
Presentations
OpenSPARC Book
Multicore Application Programming
Docs