Friday, January 28, 2011

Parallel Programming Frameworks - How to assign a CPU core to a Thread?

Here is Part I of the series of articles on Hands-on Programming for Parallelism using Multi-Cores.

Explicitly Programming Threads

Threads can be explicitly created in programs either using Posix standard Thread library APIs available on Linux platforms and the Windows threads based on Win32 APIs. Application programs typically defer the assignment of these threads to a specific processor core to the Operating System scheduler.

Overriding the Scheduler to assign a CPU cores to a Thread - Is this possible?

If the CPU has four or eight cores, an application can now assign a specific core to a thread. Sometimes this is referred to as hardware thread. There is typically an assignment of the CPU core to the process or more explicitly to a thread. This is typically accomplished by programming a bit mask known as a thread affinity mask. For example on a windows platform, using the following API, an application can override the scheduler to assign a specific core to a thread,




#include
#include

// Set OpenMP thread affinity
void set_thread_affinity () {

#pragma omp parallel default(shared)
{
DWORD_PTR mask = (1 << omp_get_thread_num()); SetThreadAffinityMask( GetCurrentThread(), mask ); } }


The SetThreadAffinityMask function is the function that assigns a cpu core to a thread. This API can assign upto 64 processors and more can be assigned by specifying a thread's current processor group.

omp.h header needs some explanation. This is strictly not required. OMP stands for openMP which is a standard for parallel programming implemented by most compiler vendors including GCC and Visual Studio and provides for APIs and compiler directives to create compiler supported implicit parallelism. As an example, the compiler can insert code to create a team of threads to process a for loop that can be parallelized based on directives in the program.

On Linux platforms the following APIs offer a similar facility. The pthread_setaffinity_np API is available to you when using Posix APIs to create threads.


int sched_setaffinity(pid_t pid,size_t cpusetsize,cpu_set_t *mask);
pthread_setaffinity_np();


Here is Part I of the series of articles on Hands-on Programming for Parallelism using Multi-Cores

Here is Part II of the series of articles on How to assign a cpu core to a Thread?

Here is Part III of the series of articles on These Thread Libraries offer Help

Here is Part IV of the series of articles on Building Blocks of a Parallel Framwork

Here is Part V of the series of articles on Rampaging Threads - Erecting an Effective Barrier

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home