c - Openmp parallel sections vs parallel for -
i have these 2 sections of code
#pragma omp parallel #pragma omp sections { #pragma omp section printf("h"); #pragma omp section printf("e"); #pragma omp section printf("l"); #pragma omp section printf("l"); #pragma omp section printf("o"); #pragma omp section printf(" "); #pragma omp section printf("w"); #pragma omp section printf("o"); #pragma omp section printf("r"); #pragma omp section printf("l"); #pragma omp section printf("d"); #pragma omp section printf("!"); } and
char word[] = "hello world!"; int n; #pragma omp parallel for(n=0; n<12; n++) { printf("%c", word[n]); } while first 1 prints hello world! second 1 prints hello world! , prints helld!lo wor
why first 1 seems deterministic , other not?
first of all, official gcc 4.1.2 does not support openmp. have redhat-derived linux distribution (rhel, fedora, centos, scientific linux, etc.) has openmp support backported gcc 4.1.2 newer version. rh used maintain backport quite time until switched newer gcc version.
writing shared stream results in non-deterministic behaviour in both openmp sections , parallel loops. observe here result of dynamic scheduling nature of sections implementation in gcc. libgomp (gcc openmp runtime) distributes sections on first-come first-served basis among threads in team. happens in case sections short in size , therefore in execution time, first thread exit docking barrier @ beginning of parallel region consumes work items before other threads have caught up, resulting in serial execution of sections.
as parallel for loop, observe result of default loop scheduling in libgomp being static, i.e. 12 iterations evenly split among threads in linear fashion. guess there 6 threads in case (based on text segments scrambled output), thread 0 gets iterations 0 1, thread 1 gets iterations 2 3, , on. again, execution of iterations in each thread defined, there no guarantee in order threads execute.
note behaviour gcc-specific. openmp standard says:
the method of scheduling structured blocks among threads in team implementation defined.
for example, intel's compiler distributes sections in round-robin fashion, i.e. section n given thread n % num_threads, parallel for loop static scheduling , chunk size of 1 do.
Comments
Post a Comment