'Parallel for nested loops in openmp

I have problem to parallel for-loop code in OpenMP, result of parallel for-loop is different with a sequential for-loop. How to make this code parallel with same result as sequential code.

const long nx = 20;
const long ny = 20;
const long nz = 20;
int i, j, k, a, v;

#pragma omp parallel private(tid_2, i,j,k,a,v) shared(numt_2,nx,ny,nz)
{
    numt_2 = omp_get_num_threads();
    tid_2 = omp_get_thread_num();
    printf("Thread %d Total thread%d\n", tid_2, numt_2);

#pragma omp parallel for collapse(4) //num_threads(3)
for (i = 0; i <= nx; i++)
{
    for (j = 0; j <= ny; j++)
    {
        for (k = 0; k <= nz; k++)
        {
            for (a = 0; a < 19; a++)
            {
                ff[fineindex(i, j, k, a)] = 0.0;

                //#pragma omp barrier

                for (v = 0; v < 19; v++)
                {          
                    ff[fineindex(i, j, k, a)] += Minv2[a][v] * rf[v];
                }
            }
        }
    }
}
}


Solution 1:[1]

Your outer parallel region will execute the inner parallel for region multiple times. Assume there are 8 cores on your machine, the loops will be calculated 8 times, compared to the sequential version only running loops once.

ff is implicitly shared between threads in the parallel region. Therefore, during the computation, data race may exist for ff[fineindex(i, j, k, a)]. Since 8 threads are working on ff at the same time, when two threads try to write to the same index of ff, it may lead to an unpredicted result.

To resolve this issue, you may use omp for instead of omp parallel for for the loops. omp for is just used for worksharing, which distributes your loop iterations to the threads in the outer parallel region. It will not start another parallel region. In this way, each thread in the outer parallel will handle different loop iterations.

Solution 2:[2]

  • In Azure SDK 2.0 an instance of JobHostConfiguration is used to configure settings Like, Tracing and Timer Methods and then this configuration object is passed as a parameter in JobHost instance, JobHost is a runtime container to execute continuously and triggered WebJobs by calling RunAndBlock method of JobHost instance.

enter image description here

  • In SDK 3.0, JobHostConfiguration has been removed and in place of it, all configurations are configured by HostBuilder.

  • The Host Builder is much like the latest ASP.Net Core. Firstly you need to create an instance of HostBuilder and configure it as ‘Development’ environment and then configure WebJobs by calling ConfigureWebJobs method.

In this method setup AddAzureStorageCoreServices (it will hook WebJobs to Azure storage Account) and AddTimers ( will let WebJobs to periodically trigger tasks defined in the project).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ouankou
Solution 2 RajkumarMamidiChettu-MT