DeoChandra
DeoChandra

Reputation: 93

Number of threads in Loop parallelisation using OpenMP in C

I am learning OpenMP these days. I have read many blogs on internet and after reading them i have a very common doubt about "number of threads in loop parallelization". take the example below

int x,y;
#pragma omp parallel for private(y)
for(x=0; x < width; x++)
{
    for(y=0; y < height; y++)
    {
        finalImage[x][y] = RenderPixel(x,y, &sceneData);
    }
}

In this example how many threads are being used for parallelization. Is it the standard way or we should explicitly tell about number of threads ?.

Upvotes: 1

Views: 1955

Answers (1)

Marouen
Marouen

Reputation: 945

You have to specify the number of threads otherwise OPENMP will automatically use what is available. You can print the number of threads like the following

    #pragma omp parallel private(tid)
    {
        tid=omp_get_thread_num();
        if(tid==0){
            nthreads=omp_get_num_threads();
            printf("Number of threads = %d\n",nthreads);
        }

In order to run a program with a specific number (2 here) of threads, in bash you can run like the following

OMP_NUM_THREADS=2 ./program

Upvotes: 1

Related Questions