Hotspur
Hotspur

Reputation: 33

Maximum Threads in openMp

I am new to parallel programming and wanted to know what is maximum number of threads i can launch.

i tried this

#include<stdio.h>
#include<omp.h>  
void pooh(int id,int a[])
{
    a[id]=a[id]-1 ;
    printf("%d\n",id) ; 
}
int main()
{
     int a[1001] ;
     int  i ;
     for(i=0;i<1000;i++)
    {
         a[i]=i+1 ;
    }
    omp_set_num_threads(1000) ;
    #pragma omp parallel
    {
        int id=omp_get_thread_num() ;
        pooh(id,a) ;
    }

    return 0 ;
 }

but when i tried omp_set_num_threads(10000) ; the programs doesnt run. I wanted to know the maximum number threads tha can be launched to get a job done.

Upvotes: 0

Views: 13579

Answers (2)

OMD_AT
OMD_AT

Reputation: 62

YOUR QUESTION: The maximum number of threads you CAN create depends on your system. In Linux you can find this out using cat /proc/sys/kernel/threads-max (see e.g., Maximum number of threads per process in Linux?).

WHAT I THINK YOU NEED TO KNOW: However, this is not what you want, since this would totally overload your system. As a rule of thumb, you should not need to exceed the number of available processors in your system. You can find this out using nproc in Linux (see e.g., How to obtain the number of CPUs/cores in Linux from the command line?).

Using more than the number of processors available in your system will make your application to run slower.

Upvotes: 3

There are two aspects you have to understand here from your problem statement. To answer your question directly, the max threads that is recommended to be set is the number of cores * hyperthreads. Otherwise the threads just wait around for resources. This is usually 2, 4, 8, 16 .. and almost never 1000 unless you plan to use it on intel GPU.

The second aspect is I suggest you to change your implementation strategy. Take a look at: #pragma omp parallel for

Or divide the workload yourself, although why would you want to do this (unless for a college assignment or something) when omp can do it automatically.

Upvotes: 3

Related Questions