POSIX Threads

API that provides data types and API calls to manage threads It specifies a set of interfaces (functions, header files) for threaded programming commonly known as POSIX threads, or pthreads. A single process can contain multiple threads, all of which are executing the same program. These threads share the same global memory (data and heap segments), but each thread has its own stack (automatic variables).

Process-Wide Shared Attributes

  • Process ID
  • Parent Process ID
  • Process Group ID and Session ID
  • Controlling Terminal
  • User and group ID’s
  • Open File descriptors
  • Record Locks
  • Signal dispositions
  • File mode masks
  • Current Directory and root directory
  • Interval Timers and POSIX timers
  • Nice value (Scheduling priority of a process)
  • Resource limits
  • Resources and CPU consumption
  • Thread ID
  • Signal Mask
  • errno
  • Scheduling Policy
  • Privileged/Un-privileged implementations of a process
  • …etc

Thread Management Calls

pthread_create()

Takes a pointer to the subroutine, creates a thread with a pointer to it’s argument

pthread_exit()

Used to terminate the called thread

pthread_join()

Waits for the indicated threads to enter the joinable state

Examples

void simple(int n, float *a, float *b){
	int i;
	#pragma omp parallel for
	for (int i=1; i<n; i++){ /* i is priv by default */
		b[i] = (a[i] + a[i-1]) / 2.0;
	}
}

The use of #pragma omp specifies to the compile to execute our loop in parallel using POSIX threads

Control Directives

#pragma omp critical

Critical section, executed by one thread at a time acquiring a lock, carries a substantial overhead. multiple critical sections, are mutually exclusive

#pragma omp atomic

Section of code atomically updated, some performance advantages over critical section limited to the update of a single memory location, not exclusive, more efficient, assume hardware mechanism for making them critical.

#pragma omp barrier

All active threads will stop until all threads have arrived at that point. Guarantees those calculation finish.

#pragma omp exclusive

If a thread is in a critical section, the other ones are all blocked

Control Directives Examples

#pragma omp parallel
{
int mytid = omp_get_thread_num();
double tmp = some_functino(mytid);
#pragma omp critical
sum += tmp;
}
#pragma omp parallel
{
int mytid = omp_get_thread_nunm();
x[mytid] = some_calc();
#pragma omp barrier
y[mytid] = x[mytid]+x[mytid+1];
}

OpenMP

OpenMP is an implementation of multithreading, a method of parallelizing whereby a master thread (a series of instructions executed consecutively) forks a specified number of slave threads and the system divides a task among them. The threads then run concurrently, with the runtime environment allocating threads to different processors.

It is based on a set of compiler directives or pragmas, combined with a programming API to specify parallel regions, data scope, synchronization…