gpt4 book ai didi

c++ - parallel for each嵌套在for循环中

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:13 25 4
gpt4 key购买 nike

我有大约 4000 个具有 3000 个维度的 vector ,我需要逐对计算差异 vector 。

问题来了。我尝试了两种方法。

differ = (double *)malloc(sizeof(double) * testNum * trainNum * featureDim);
array_view<double, 2> differAMP(testNum, trainNum, featureDim, differ);
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
parallel_for_each(
differAMP.extent,
[=](concurrency::index<3> idx) restrict(amp) {
differAMP[idx] = (test(idx[0], idx[2]) - train(idx[1], idx[2]));
}
);

但我认为 vs 会抛出运行时异常,因为内存限制。然后我改了代码

differ = (double *)malloc(sizeof(double) trainNum * featureDim);
array_view<double, 2> differAMP(trainNum, featureDim, differ);
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
parallel_for_each(
differAMP.extent,
[=](concurrency::index<2> idx) restrict(amp) {
differAMP[idx] = (test(testIndex, idx[1]) - train(idx[0], idx[1]));
}
);

我在一个循环中为每个测试用例运行它。但是,vs 抛出了一个 array_view removed 异常。现在我不知道该怎么办。

最佳答案

在第一种情况下,您失败是因为内存不足。在第二种情况下,您可能会失败,因为您的任务超过了 2 秒的 TDR 限制。请记住,parallel_for_each 将工作异步排队到 GPU 的 DMA 缓冲区。所以将它放在 for 循环中将导致所有的

for (int testIndex = 0; testIndex < testNum; ++testIndex)
{
differ = (double *)malloc(sizeof(double) trainNum * featureDim);
array_view<double, 2> differAMP(trainNum, featureDim, differ);
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
parallel_for_each(
differAMP.extent,
[=](concurrency::index<2> idx) restrict(amp) {
differAMP[idx] = (test(testIndex, idx[1]) - train(idx[0], idx[1]));
});

// Force this parallel_for_each to finish before running the next one.
differAmp.source_accelerator_view.wait();
}

上面的代码防止所有工作在一个 block 中排队和执行,导致 TDR 超时,这将引发异常。

您还可以通过创建具有特定排队模式的 View 来改变排队行为。即时模式强制任务立即排队,以吞吐量为代价改善延迟。

accelerator acc(accelerator::default_accelerator);
acc.create_view(queuing_mode::queuing_mode_immediate);

关于c++ - parallel for each嵌套在for循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26312157/

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