gpt4 book ai didi

c++ - PPL:线程池的初始化

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:28 24 4
gpt4 key购买 nike

是否有预初始化 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;
}

最佳答案

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

关于c++ - PPL:线程池的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18125072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com