1

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.

Semih Ozmen
  • 571
  • 5
  • 20
  • 1
    You'll probably be happier if you build an overload to the function / parameterize it so that you can fall through to a serial version. – Rick Jul 23 '12 at 16:29
  • overloading the function will cause dublicating the lower hierarchy function because parallelization is in several levels. I am not willing to follow overloading..maintenance will be hard to handle in the future...thanks anyway – Semih Ozmen Jul 24 '12 at 06:05
  • 1
    Tell the other developer to get all the globals, statics etc. out of his/her code/data so that it does not matter? – Martin James Jul 24 '12 at 06:34
  • No! for that developer it is important to get the exact list of results for each run of this function with the same inputs..Since this is parallelized now list of results are different in order! And it is not possible to sort the list of results since they are complex data structures. I am stuck and so disappointed to give a try to PPL..If I were used OpenMP, it would be just a single line of directive. – Semih Ozmen Jul 24 '12 at 06:46

1 Answers1

1

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 ) );
23W
  • 1,413
  • 18
  • 37
  • thanks for your effort...it had been sometime since I asked this question,so, I could not try your suggestion but it seems applicable to some extend. When I checked the question again it is seen that code duplication is not wanted. – Semih Ozmen Feb 04 '14 at 20:55