Skip to content

Instantly share code, notes, and snippets.

@Garciat
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garciat/f5ec63cfa385f0bc69e8 to your computer and use it in GitHub Desktop.
Save Garciat/f5ec63cfa385f0bc69e8 to your computer and use it in GitHub Desktop.
// compile with: gcc openmp.c -o openmp -std=c11 -O3 -Wall -fopenmp
#include <stdio.h>
#include <stdlib.h>
typedef int num;
int main(int argc, char *argv[]) {
int n = 20000;
int m = 20000;
num *p = (num*)malloc(n * m * sizeof(num));
if (p == NULL)
return 1;
for (int i = 0; i < n * m; ++i) {
p[i] = 1;
}
num s = 0;
#pragma omp parallel for
for (int i = 0; i < n; ++i) {
num ss = 0;
for (int j = 0; j < m; ++j) {
ss += p[i * m + j];
}
#pragma omp atomic
s += ss;
}
printf("%lld\n", s);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment