Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created November 2, 2021 11:34
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 kaityo256/1d78d926fa8f46566ab003e08639ef59 to your computer and use it in GitHub Desktop.
Save kaityo256/1d78d926fa8f46566ab003e08639ef59 to your computer and use it in GitHub Desktop.
OpenMP Schedule Sample
#include <cstdio>
#include <omp.h>
const int N = 8;
int d[N] = {};
int main() {
int tid;
#pragma omp parallel for schedule(static)
for (int i = 0; i < N; i++) {
tid = omp_get_thread_num();
for (int j = 0; j < 1000; j++) {
d[i] += tid;
}
}
for (int i = 0; i < N; i++) {
printf("%d %d\n", i, d[i]);
}
}
@kaityo256
Copy link
Author

For the case of Intel Compiler

$ icpc --version
icpc (ICC) 19.1.3.304 20200925
Copyright (C) 1985-2020 Intel Corporation.  All rights reserved.

$ icpc -qopenmp -O0 test.cpp
$ OMP_NUM_THREADS=2 ./a.out
0 0
1 0
2 0
3 0
4 1000
5 1000
6 1000
7 1000

This is the expected behavior.

For the case of GCC

$ g++ --version
g++ (GCC) 10.1.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -fopenmp -O0 test.cpp

$ OMP_NUM_THREADS=2 ./a.out
0 886
1 0
2 560
3 0
4 223
5 170
6 1000
7 1000

WHY????

@kaityo256
Copy link
Author

I forgot private(tid).

#pragma omp parallel for schedule(static) private(tid)

This is the reason why the behaviours between g++ and icpc werr different.

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