Source: http://blog.renren.com/share/30860442/5821521068
Linux systems provide API functions sched_setaffinity and sched_getaffinity used to set or obtain the available CPU cores for a thread.
int sched_setaffinity (pid_t pid, unsigned int cpusetsize, cpu_set_t *mask);
The PID in this function represents the thread ID (or process ID) that needs to set or get the binding information, if 0, the current calling thread is set, and the 2nd parameter cpusetsize is generally set to sizeof (cpu_set_t). The size of the memory structure object that the 3rd argument points to, and the 3rd parameter mask a pointer to the type CPU_SET_T object that sets or gets the list of CPU cores that the specified thread can use. Linux provides functions Cpu_zero, Cpu_set, and Cpu_isset operate on objects of cpu_set_t type, where Cpu_zero is used to empty the contents of the Cpu_set_t type object, Cpu_set is used to set the Cpu_set_t type Object , Cpu_isset is used to determine whether the corresponding bit of the cpu_set_t type object and the kernel is set. The following is a simple code example to illustrate the specific uses of these two functions.
To set the thread binding code:
cpu_set_t Mask;
int blist[8]={2, 5, 13, 9, 3, 6, 7, 4}; Set the list of cores that need to be bound
#pragma omp parallel Private (mask)
{
Cpu_zero (&mask);
Cpu_set (Blist[omp_get_thread_num ()], &mask); To set the binding scheme for each thread
Sched_setaffinity (0,sizeof (cpu_set_t), &mask);
}
This code binds the 8 threads in the Paralle region to the kernel 2,5,13,9,3,6,7,4 in turn. You can also use the sched_getaffinity function to get a list of the available cores for a thread, as shown in the sample code:
int num_processors = sysconf (_sc_nprocessors_conf); Gets the number of current node cores
cpu_set_t get;
int i = 0;
Cpu_zero (&get);
Sched_getaffinity (0, sizeof (cpu_set_t), &get); Gets the available kernel for the current calling thread
for (i = 0; i < num_processors; i++)
{
if (Cpu_isset (i, &get))
{
printf ("The current thread%d bound to core%d\n", Omp_get_thread_num (), i);
}
}
The following is a complete example
File bind.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#define __use_gnu
#include <sched.h>
#include <ctype.h>
#include <string.h>
#include <pthread.h>
Maximum number of processes in the #define THREAD_MAX_NUM 100//1 CPUs
int num=0; Number of cores in CPU
void* Threadfun (void* arg)//arg pass thread label (own definition)
{
cpu_set_t Mask; The collection of CPU cores
cpu_set_t get; Gets the CPU in the collection
int *a = (int *) arg;
printf ("The A is:%d\n", *a); Display is the first few threads
Cpu_zero (&mask); Empty
Cpu_set (*a,&mask); Set Affinity values
if (sched_setaffinity (0, sizeof (mask), &mask) = = 1)//Set Thread CPU affinity
{
printf ("Warning:could not set CPU affinity, continuing...\n");
}
while (1)
{
Cpu_zero (&get);
if (sched_getaffinity (0, sizeof (GET), &get) = = 1)//Acquire thread CPU affinity
{
printf ("Warning:cound not get thread affinity, continuing...\n");
}
int i;
for (i = 0; i < num; i++)
{
if (Cpu_isset (i, &get))//Determine which CPU the thread is affinity with
{
printf ("This thread%d is running processor:%d\n", i,i);
}
}
}
return NULL;
}
int main (int argc, char* argv[])
{
num = sysconf (_sc_nprocessors_conf); Get the number of cores
pthread_t Thread[thread_max_num];
printf ("System has%i processor (s)." \ n ", num);
int Tid[thread_max_num];
int i;
for (i=0;i<num;i++)
{
Tid[i] = i; Each thread must have a tid[i]
Pthread_create (&thread[0],null,threadfun, (void*) &tid[i]);
}
for (i=0; i< num; i++)
{
Pthread_join (thread[i],null);/Waiting for all threads to end, thread to dead loop so Ctrl + C ends
}
return 0;
}
Compile command: gcc bind.c-o bind-lpthread
Execution:./bind
Output result: slightly
Special attention:
#define __USE_GNU Don't write #define _USE_GNU.
#include <pthread.h> must be written after #define __USE_GNU, otherwise the compilation will report an error
View your thread situation you can use top-h in another window at execution time to view the threads, view the individual cores, use the top command, and then press the number "1" to view it.