gpt4 book ai didi

c++-amp - 为什么对 array_view::synchronize() 的调用这么慢?

转载 作者:行者123 更新时间:2023-12-01 09:34:53 25 4
gpt4 key购买 nike

我已经开始尝试使用 C++ AMP。我创建了一个简单的测试应用程序只是为了看看它可以做什么,但是结果让我很惊讶。考虑以下代码:

#include <amp.h>
#include "Timer.h"

using namespace concurrency;

int main( int argc, char* argv[] )
{
uint32_t u32Threads = 16;
uint32_t u32DataRank = u32Threads * 256;
uint32_t u32DataSize = (u32DataRank * u32DataRank) / u32Threads;
uint32_t* pu32Data = new (std::nothrow) uint32_t[ u32DataRank * u32DataRank ];

for ( uint32_t i = 0; i < u32DataRank * u32DataRank; i++ )
{
pu32Data[i] = 1;
}

uint32_t* pu32Sum = new (std::nothrow) uint32_t[ u32Threads ];

Timer tmr;

tmr.Start();

array< uint32_t, 1 > source( u32DataRank * u32DataRank, pu32Data );
array_view< uint32_t, 1 > sum( u32Threads, pu32Sum );

printf( "Array<> deep copy time: %.6f\n", tmr.Stop() );

tmr.Start();

parallel_for_each(
sum.extent,
[=, &source](index<1> idx) restrict(amp)
{
uint32_t u32Sum = 0;
uint32_t u32Start = idx[0] * u32DataSize;
uint32_t u32End = (idx[0] * u32DataSize) + u32DataSize;
for ( uint32_t i = u32Start; i < u32End; i++ )
{
u32Sum += source[i];
}
sum[idx] = u32Sum;
}
);

double dDuration = tmr.Stop();
printf( "gpu computation time: %.6f\n", dDuration );

tmr.Start();

sum.synchronize();

dDuration = tmr.Stop();
printf( "synchronize time: %.6f\n", dDuration );
printf( "first and second row sum = %u, %u\n", pu32Sum[0], pu32Sum[1] );

tmr.Start();

for ( uint32_t idx = 0; idx < u32Threads; idx++ )
{
uint32_t u32Sum = 0;
for ( uint32_t i = 0; i < u32DataSize; i++ )
{
u32Sum += pu32Data[(idx * u32DataSize) + i];
}
pu32Sum[idx] = u32Sum;
}

dDuration = tmr.Stop();
printf( "cpu computation time: %.6f\n", dDuration );
printf( "first and second row sum = %u, %u\n", pu32Sum[0], pu32Sum[1] );

delete [] pu32Sum;
delete [] pu32Data;

return 0;
}

请注意,Timer 是一个使用 QueryPerformanceCounter 的简单计时类。无论如何,代码的输出如下:

Array<> deep copy time: 0.089784
gpu computation time: 0.000449
synchronize time: 8.671081
first and second row sum = 1048576, 1048576
cpu computation time: 0.006647
first and second row sum = 1048576, 1048576

为什么调用 synchronize() 需要这么长时间?有没有办法解决这个问题?除了计算性能令人惊叹之外,sync() 开销让我无法使用它。

也有可能是我做错了什么,如果是这样,请告诉我。提前致谢。

最佳答案

函数 synchronize() 可能需要很长时间,因为它正在等待实际的内核完成其工作。

来自 parallel_for_each from amp.h :

Please note that the parallel_for_each executes as if synchronous to the calling code, but in reality, it is asynchronous. I.e. once the parallel_for_each call is made and the kernel has been passed to the runtime, the [code after the parallel_for_each] continues to execute immediately by the CPU thread, while in parallel the kernel is executed by the GPU threads.

因此,衡量在 parallel_for_each 中花费的时间并没有特别的意义。

编辑:算法的编写方式不会从 GPU 加速中受益。 source[i] 的读取是非合并的,因此它将比合并读取慢 16 倍。可以通过使用共享内存来合并读取,但这并不是很简单。我建议阅读 GPU 编程。

如果您只是想要一个演示 C++ AMP 实用程序的简单示例,请尝试 matrix multiplication .

当然,您观察到的性能也很大程度上取决于您的 GPU 硬件型号。

关于c++-amp - 为什么对 array_view::synchronize() 的调用这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9844559/

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