Skip to content

Instantly share code, notes, and snippets.

@aprell
Last active August 29, 2015 14:01
Show Gist options
  • Save aprell/3ecdb8bcd1e862104a87 to your computer and use it in GitHub Desktop.
Save aprell/3ecdb8bcd1e862104a87 to your computer and use it in GitHub Desktop.
Examples of implicit barriers in OpenMP and Cilk Plus
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <cilk/cilk.h>
int A[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int main(void)
{
int i;
cilk_for (i = 0; i < 10; i++) {
A[i] += 42;
sleep(1);
} // Implicit barrier
for (i = 0; i < 10; i++) {
printf("%d\n", A[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <omp.h>
int A[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int main(void)
{
int i;
#pragma omp parallel num_threads(8)
{
#pragma omp single
for (i = 0; i < 10; i++) {
#pragma omp task shared(A) firstprivate(i)
{ A[i] += 42; sleep(1); }
} // Implicit barrier
} // Implicit barrier
for (i = 0; i < 10; i++) {
printf("%d\n", A[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment