Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set number of threads in C++

I have written the following multi-threaded program for multi-threaded sorting using std::sort. In my program grainSize is a parameter. Since grainSize or the number of threads which can spawn is a system dependent feature. Therefore, I am not getting what should be the optimal value to which I should set the grainSize to? I work on Linux?

 int compare(const char*,const char*)
{
   //some complex user defined logic    
}
void multThreadedSort(vector<unsigned>::iterator data, int len, int grainsize)
{
    if(len < grainsize) 
    {
        std::sort(data, data + len, compare);
    }
    else
    {
        auto future = std::async(multThreadedSort, data, len/2, grainsize);

        multThreadedSort(data + len/2, len/2, grainsize); // No need to spawn another thread just to block the calling thread which would do nothing.

        future.wait();

        std::inplace_merge(data, data + len/2, data + len, compare);
    }
}

int main(int argc, char** argv) {

    vector<unsigned> items;
    int grainSize=10;
    multThreadedSort(items.begin(),items.size(),grainSize);
    std::sort(items.begin(),items.end(),CompareSorter(compare));
    return 0;
}

I need to perform multi-threaded sorting. So, that for sorting large vectors I can take advantage of multiple cores present in today's processor. If anyone is aware of an efficient algorithm then please do share.

I dont know why the value returned by multiThreadedSort() is not sorted, do you see some logical error in it, then please let me know about the same

like image 206
Steg Verner Avatar asked Apr 22 '15 09:04

Steg Verner


People also ask

What is the maximum number of threads in C?

The maximum number of threads per process is 512. The maximum number of threads can be retrieved at compilation time using the PTHREAD_THREADS_MAX symbolic constant defined in the pthread.

Can you create threads in C?

In main(), we declare a variable called thread_id, which is of type pthread_t, which is an integer used to identify the thread in the system. After declaring thread_id, we call pthread_create() function to create a thread.

How many threads can be executed at a time in C?

Only 1 native CPU thread can be executed at a time by 1 single core. If for example you have 4 cores, only 4 threads can be executed at the same time (if you have 2 thread/core still one will execute at a time, next will wait to be executed as soon as first finish).


1 Answers

This gives you the optimal number of threads (such as the number of cores):

unsigned int nThreads = std::thread::hardware_concurrency();

As you wrote it, your effective thread number is not equal to grainSize : it will depend on list size, and will potentially be much more than grainSize.

Just replace grainSize by :

unsigned int grainSize= std::max(items.size()/nThreads, 40);

The 40 is arbitrary but is there to avoid starting threads for sorting to few items which will be suboptimal (the time starting the thread will be larger than sorting the few items). It may be optimized by trial-and-error, and is potentially larger than 40.

You have at least a bug there:

multThreadedSort(data + len/2, len/2, grainsize);

If len is odd (for instance 9), you do not include the last item in the sort. Replace by:

multThreadedSort(data + len/2, len-(len/2), grainsize);
like image 133
galinette Avatar answered Oct 09 '22 09:10

galinette