Skip to content

Instantly share code, notes, and snippets.

@petarjs
Created June 9, 2016 10:47
Show Gist options
  • Save petarjs/16d2564f74767e888a14b69a0cbd1967 to your computer and use it in GitHub Desktop.
Save petarjs/16d2564f74767e888a14b69a0cbd1967 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <omp.h>
void main(int argv, char** argc) {
int i;
int sum = 0;
int a[100];
int b[100];
for(i=0; i<100; i++) {
a[i] = 2*i + 1010;
b[i] = 4*i + 130;
}
// sequential
for(i=0; i<100; i++) {
sum += a[i] * b[i];
}
// paralel
#pragma omp parallel for {
for(i=0; i<100; i++) {
#pragma omp critical
sum += a[i] * b[i];
}
}
// parallel reduction
#pragma omp parallel for recuction(+:sum) {
for(i=0; i<100; i++) {
sum += a[i] * b[i];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment