Semih Ozmen
Semih Ozmen

Reputation: 591

How to set number of PPL threads to one?

I have a number crunching function, so I have paralleled it by using PPL..however another developer requires this function to be run in serial because of some reason..I need to give a parameter so that he can call my function in serial mode...I dont want to duplicate the code so I need a way to limit the number of PPL threads..Although I have sad

Concurrency::SchedulerPolicy sp( 1, Concurrency::MaxConcurrency, 1 );
CurrentScheduler::Create(sp);

PPL creates two threads and running my code in parallel...Any suggestions how to serialize a ppl enhanced code.

Upvotes: 1

Views: 2445

Answers (1)

23W
23W

Reputation: 1530

For this problem better not set scheduler policies, and use some manual task group initialization control, for example:

using namespace Concurrency;

std::vector< task_handle< std::function< void() > > > aTask;
aTask.push_back( make_task([](){ /*taks 1*/}) );
aTask.push_back( make_task([](){ /*taks 2*/}) );
aTask.push_back( make_task([](){ /*taks 3*/}) );

task_group tGroup;

bool bSerialMode = true; /* or false */
if (!bSerialMode)
{
    std::for_each(aTask.begin(), aTask.end(), [&](task_handle< std::function< void() > >& handle){
       tGroup.run( handle );
    });
}
else
{
    tGroup.run( [&](){
      std::for_each(aTask.begin(), aTask.end(), [&](task_handle< std::function< void() > >& handle){
       tGroup.run_and_wait( handle ); });
    });
}

If you do decide to limit all tasks of one virtual processor, then set MinConcurrency too.

CurrentScheduler::Create( SchedulerPolicy( 2, Concurrency::MinConcurrency, 1, Concurrency::MaxConcurrency, 1 ) );

Upvotes: 1

Related Questions