1

是否有任何标准方法来预初始化 PPL 线程池?问题是:PPL 在运行时创建它的线程池,例如 parallel_for() 正在执行。由于创建了额外的线程,这在第一次运行期间会降低一些性能。

为了澄清问题,这里有一个例子:

#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;
}
4

1 回答 1

0

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

于 2013-08-08T14:08:48.000 回答