OpenMP (Processes vs. Threads)
- Process
- The basic unit of work for the operating system.
Processes have a lot of overhead due to their isolation.
- Thread
- A portion of a process that can be run concurrently with another.
Threads have low overhead as there is implicit trust. They share a heap but have their own stack.
OpenMP
OpenMP, available at https://www.openmp.org, is a parallel library that operates on a thread level.
This is unlike OpenMPI that operates on a process level.
Consider the following example program:
#include <omp.h>
#include <stdio.h>
int main(void) {
#pragma omp parallel
{
printf("Thread id is %d\n", omp_get_thread_num());
}
return 0;
}
We can compile this with the following flags:
$ gcc -fopenmp thread.c
$ ./a.out
We can define the number of threads to spawn with the following variable:
OMP_NUM_THREADS=200 ./a.out
Otherwise the total number of system threads are used.