【发布时间】:2018-07-26 19:23:45
【问题描述】:
我遇到了一些小麻烦,我正在寻求提示。我在Windows平台上,按如下方式进行计算:
int input = 0;
int output; // junk bytes here
while(true) {
async_enqueue_upload(input); // completes instantly, but transfer will take 10us
async_enqueue_calculate(); // completes instantly, but computation will take 80us
async_enqueue_download(output); // completes instantly, but transfer will take 10us
sync_wait_finish(); // must wait while output is fully calculated, and there is no junk
input = process(output); // i cannot launch next step without doing it on the host.
}
我在问关于 wait_finish() 的事情。我必须等待所有设备完成,合并所有结果并以某种方式处理数据并上传一个新部分,该部分基于先前的计算步骤。我需要在每个步骤之间同步数据,所以我不能并行化步骤。我知道,这不是很高效的案例。那么让我们继续提问吧。
我有两种检查完成的方法,在 wait_finish() 中。首先是让线程休眠,直到它被完成事件唤醒:
while( !is_completed() )
Sleep(1);
它的性能非常低,因为实际计算需要 100us,而最小的 Windows 调度器时间步长是 1ms,所以它的性能降低了 10 倍,这是不合适的。
第二种方法是在空的无限循环中检查完成:
while( !is_completed() )
{} // do_nothing();
它具有 10 倍的良好计算性能。但这也是不合适的解决方案,因为它充分利用了 cpu 核心利用率,完全无用的工作。如何让 cpu “睡眠”正是我需要的时间? (每个步骤的工作量相同)
当计算时间对于主动自旋等待来说太大但与调度器时间步相比太小时,通常如何解决这种情况?还有相关的子问题 - 如何在 linux 上做到这一点?
【问题讨论】:
标签: performance theory