How to properly execute / resume unrelated OpenMP tasks?

I wrote a small C program to evaluate the capabilities of OpenMP in order to give way to another task in case of downtime in the task (for example, wait for data to transfer):

#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 the Intel C compiler 17.0.4. Here is the result of starting with three 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 are not inferior at all, but instead they perform their assigned task. I also expected threads 1 and 2 to continue on suspended unrelated tasks 04 ... 09, but they are only processed by host thread 0, while the rest of the threads are idle.

Should tasks be issued or performed differently, or is Intel OpenMP runtime (yet) unable to handle this? Btw., GNU gcc 4.9.2 does not deduce from tasks at all.

+2
source share
1

, , . LLVM OpenMP, Intel, , . clang libiomp5.so ( ) icc 17.0.4, LD_LIBRARY_PATH .

thread 0 is master

 00  01  02  03  04  05  06  07  08  09 
                                     r0 
 r2 
     r1 
                                     s0 
                                 r0 
 s2 
         r2 
     s1 
             r1 
                                 s0 
                             r0 
         s2 
                 r2 
             s1 
                     r1 
                             s0 
                         r0 
                 s2 
                     s1 
                 c2 
                         s0 
                     c1 
                         c0 
         c2 
             c1 
                             c0 
 c2 
     c1 
                                 c0 
                                     c0 

, gcc , .

, , Intel.

: , - . , libiomp , taskwait, . , ( ), .

, , / , , ( ). , libiomp , . , , , /.

0

Source: https://habr.com/ru/post/1691071/


All Articles