简体   繁体   中英

all of the omp tasks are being scheduled in the same thread

I am playing around with the omp task function, and has encountered a problem. I have the following code:

void function1(int n, int j)
{
    //Serial part
    #pragma omp parallel
    {
        #pragma omp single nowait
        {
            //execcute function1() for every n
            for (i=0; i<n; i++)
            {
                //create a task for computing only large numbers of n
                if (i <= 10)
                    //execute serial
                else
                    #pragma omp task
                    //call function again 
                    function1(n, j+1);
                    printf("task id:\n", omp_get_thread_num());
            }
        }
    }
}

Right now the code will produce the correct result, but the performance is much slower than the original serial version. After some investigation I found that all the tasks are executed in thread 0 regardless there are 4 threads running in total. Does anyone know what's going on here? Thanks in advance!

The omp single nowait pragma means that the following block is to be executed by a single thread. In this case that means that your entire loop is executed by one thread. This should resolve your issue:

void function1(int n, int j)
{
    //Serial part
    #pragma omp parallel
    {
        //execcute function1() for every n
        for (i=0; i<n; i++)    //Notice this is outside the pragma
        {
            #pragma omp single nowait    //Notice this is inside the loop
            {
                //create a task for computing only large numbers of n
                if (i <= 10)
                    //execute serial
                else
                    #pragma omp task
                    //call function again 
                    function1(n, j+1);
                    printf("task id:\n", omp_get_thread_num());
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM