Open MP1
Open MP1
Open MP1
CS3006
Lecture 8
Shared Memory & OpenMP
16th March 2022
Memory
Time
join
When a parallel portion of the code is
reached, the Master thread can “fork”
more threads to work on it. fork
Compiler support
C, C++ & Fortran
Intel (icc -openmp), and GNU (gcc -fopenmp)
Format:
#pragma omp parallel for
for (i = 0; i < N; i++)
a[i] = b[i] + c[i];
omp_get_num_threads
int threads = omp_get_num_threads() //# of active threads
//should be called from a parallel region
omp_get_max_threads
printf(“Only %d threads can be forked\n",omp_get_max_threads());
//can be called outside of a parallel region. Returns value of env.
//variable ‘OMP_NUM_THREADS’
omp_get_thread_num
printf("Hello from thread id %d\n",omp_get_thread_num());
omp_set_num_threads –
omp_set_dynamic(0); // disable dynamic adjustment
omp_set_num_threads(4); //setting thread count to 4
CS3006 - Spring 2022
helloworld.c
int main(){;
int procs,i,a,b,c,t,m;