[英]Performance with MS concurrency::parallel_for() for a single iteration
[英]Marginal performance gain using Concurrency::parallel_for()
在我的應用程序中,我有一個運行於大約一千萬個項目的for循環,如下所示:
int main(int argc, char* argv [])
{
unsigned int nNodes = 10000000;
Node** nodeList = new Node* [nNodes];
initialiseNodes(nodeList); // nodes are initialised here
for (unsigned int ii = 0l ii < nNodes; ++ii)
nodeList[ii]->update();
showOutput(nodeList) // show the output in some way
}
我不會詳細介紹如何精確初始化或顯示節點。 重要的是Node::update()
方法是一個小方法,獨立於其他節點。 因此,並行執行該for循環將是非常有利的。 由於這只是一件小事,所以這次我想遠離OpenCL / CUDA / OpenMP,所以我改用了C ++ Concurrency::parallel_for
。 因此,代碼如下所示:
#include <ppl.h>
int main(int argc, char* argv [])
{
unsigned int nNodes = 10000000;
Node** nodeList = new Node* [nNodes];
initialiseNodes(nodeList); // nodes are initialised here
Concurrency::parallel_for(unsigned int(0), nNodes, [&](unsigned int ii) {
nodeList[ii]->update();
});
showOutput(nodeList) // show the output in some way
}
我的確發現,這的確確實加快了程序的速度,但通常只提高20%左右。 坦率地說,我期望更多。 有人可以告訴我在使用parallel_for
時這是否是典型的加速因素嗎? 還是有辦法從中獲得更多收益(無需切換到GPU實現)?
在問題上投入更多的核心並不一定會帶來改善。 實際上,在最壞的情況下,它甚至可能會降低性能。 受益於使用多個內核取決於很多事情,例如涉及的共享數據量。 有些問題本質上是可並行化的,而有些則不是。
我發現我認為對性能提升的貢獻最大。 當然,就像@ anthony-burleigh所說的那樣,任務必須是可並行化的,並且共享數據的影響也應如此。 但是,我發現並行化方法的計算量要重要得多。 大任務似乎比小任務具有更高的加速率。
因此,例如:
Concurrency::parallel_for(unsigned int(0), nNodes, [&](unsigned int ii) {
nodeList[ii]->update(); // <-- very small task
});
我的加速因子只有1.2。 但是,在繁重的任務中,例如:
Concurrency::parallel_for(unsigned int(0), nNodes, [&](unsigned int ii) {
ray[ii]->recursiveRayTrace(); // <-- very heavy task
});
該程序突然以3倍的速度運行。
我確信所有這些都有更深層的解釋,但這是我通過反復試驗發現的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.