1

Is there any standard way to pre-initialize the PPL thread pool? The problem is: PPL creates its thread pool at runtime when e.g. parallel_for() is executing. This costs a little performance during the first run, due to creation of additional threads.

For the clarification of the problem, here is an example:

#pragma once

#include <vector>
#include <ppl.h>

using namespace std;
using namespace Concurrency;

// Use this define for experiments.
#define PPL_INIT_METHOD 2

int main()
{
#if (PPL_INIT_METHOD == 0)    // experiment 1: initialize default scheduler

  CurrentScheduler::Create(SchedulerPolicy());
  // After this call only one additional thread is created

#elif (PPL_INIT_METHOD == 1)  // experiment 2: initialize custom scheduler

  SchedulerPolicy my_policy(3,
                            MinConcurrency, 12,
                            MaxConcurrency, 12,
                            ContextPriority, THREAD_PRIORITY_NORMAL);
  Scheduler* my_scheduler = Scheduler::Create(my_policy);
  // After this call only one additional thread is created
  my_scheduler->Attach();
  assert(my_scheduler->GetNumberOfVirtualProcessors() == 12);
  // Still same number of threads (= 2)

  // cleanup stuff ...

#else      // experiment 3: execute dummy parallel_for()

  std::vector<int> v(1024*1024, 42);
  parallel_for(0u, v.size(), [&v](size_t i) { v[i] += 1; }, static_partitioner());
  // After this call all 12 threads are created and ready for more work!

#endif

  // Do real work now!!!

  return 0;
}
mem64k
  • 748
  • 1
  • 11
  • 25

1 Answers1

0

You can initialize the ppl threadpool with calls to Concurrency::Scheduler::Create in concrt.h.

Rick
  • 3,285
  • 17
  • 17
  • No, this doesn't work! I've tried: { SchedulerPolicy my_policy(3, MinConcurrency, 12,MaxConcurrency, 12, ContextPriority, THREAD_PRIORITY_NORMAL); Scheduler* my_scheduler = Scheduler::Create(my_policy); }. – mem64k Aug 08 '13 at 15:43
  • Once you create the scheduler you have to Attach() it to the current context, or it won't get used. – Edward Kmett Aug 14 '16 at 10:05