Skip to content

Instantly share code, notes, and snippets.

@jepio
Last active August 29, 2015 14:03
Show Gist options
  • Save jepio/658186202f40d0f037ce to your computer and use it in GitHub Desktop.
Save jepio/658186202f40d0f037ce to your computer and use it in GitHub Desktop.
A short OpenMP example showing how you can make your code parallel with minimal effort.
CC=gcc
# Without the fopenmp flag the program will normally still work and be
# completely serial. In this case however it won't because I used omp.h
CFLAGS=-fopenmp -Wall
LDFLAGS=$(CFLAGS)
all: omp_example
omp_example: omp_example.o
clean:
rm -rf *.o omp_example
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
int main(void){
int th_id, nthreads, i;
srand(time(NULL));
// launch team of threads and keep th_id private
#pragma omp parallel private(th_id)
{
th_id = omp_get_thread_num();
printf("Hello thread %d\n",th_id);
// all threads wait at this barrier so that total prints last
#pragma omp barrier
if (th_id == 0) {
nthreads = omp_get_num_threads();
printf("Total: %d threads\n",nthreads);
}
}
// specify how many threads to launch explicitly and parallelize loop
#pragma omp parallel for num_threads(3) private(i)
for (i=0;i<6;++i)
printf("Boo %d\n",i);
// parallel reduce
int a[1000],b[1000];
for (i=0;i<1000;i++) {
a[i] = rand();
b[i] = rand();
}
long sum = 0;
#pragma omp parallel for reduction (+:sum) private(i)
for (i=0;i<1000;i++)
sum += a[i]*b[i];
printf("Parallel scalar product result: %ld \n",sum);
return 0;
}
@jepio
Copy link
Author

jepio commented Jul 6, 2014

You can control the number of threads spawned (where not specified in code) at runtime using

export OMP_NUM_THREADS=10

@jepio
Copy link
Author

jepio commented Jul 18, 2014

It is not clear to me whether it is necessary to define the loop variable as private in OMP when using C. This is default behaviour in C++.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment