I wrote a small C program to assess OpenMP's capability to yield to another task when idle time in a task occurs (e.g. wait for communicated data):
#include <stdio.h>
#include <sys/time.h>
#include <omp.h>
#define NTASKS 10
double wallClockTime(void) {
struct timeval t;
gettimeofday(&t, NULL);
return (double)(t.tv_sec + t.tv_usec/1000000.);
}
void printStatus(char *status, int taskNum, int threadNum) {
#pragma omp critical(printStatus)
{
int i;
for (i = 0; i < taskNum; i++) printf(" ");
printf(" %s%i \n", status, threadNum);
}
}
void task(int taskNum) {
// "r"un task
printStatus("r", taskNum, omp_get_thread_num());
sleep(1);
// "s"leeping task that can yield
printStatus("s", taskNum, omp_get_thread_num());
double idleStartTime = wallClockTime();
while (wallClockTime() < idleStartTime + 1) {
#pragma omp taskyield
}
// "c"ontinue task
printStatus("c", taskNum, omp_get_thread_num());
sleep(1);
}
int main(int argc, char* argv[]) {
#pragma omp parallel
#pragma omp single nowait
{
int i;
printf("thread %d is master\n\n", omp_get_thread_num());
for (i = 0; i < NTASKS; i++) printf(" %02d ", i);
printf("\n");
for (i = 0; i < NTASKS; i++) {
#pragma omp task untied
task(i);
}
}
return 0;
}
I used Intel C compiler 17.0.4. Here is the output from a run with 3 threads:
thread 0 is master
00 01 02 03 04 05 06 07 08 09
r1
r0
r2
s1
s0
s2
r0
c1
c2
s0
r0
r1
r2
s0
r0
s1
s2
s0
r0
c1
c2
s0
r0
s0
c0
c0
c0
c0
c0
c0
Thread 1 and 2 do not yield at all, but they stick to their assigned task instead. I would also expect threads 1 and 2 to continue on the suspended untied tasks 04 ... 09, but these are only handled by master thread 0 while the other threads are idle.
Do the tasks have to be issued or yielded in a different way, or is Intel's OpenMP runtime not (yet) capable to handle this? Btw., GNU gcc 4.9.2 does not yield from tasks at all.