简体   繁体   中英

PPL: Initialization of the thread pool

Is there any standard way to pre-initialize the PPL thread pool? The problem is: PPL creates its thread pool at runtime when eg 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;
}

您可以通过在concrt.h中调用Concurrency :: Scheduler :: Create来初始化ppl线程池。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM