gpt4 book ai didi

CUDA 内核函数比等效的主机函数花费更长的时间

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

我正在关注 http://code.google.com/p/stanford-cs193g-sp2010/和在线发布的视频讲座,做一个发布的问题集(第一个)我遇到了一些稍微违反直觉的事情,至少在问题的提出方式上。该问题要求我推导出 cpu 和 gpu 上执行时间的时序模型,假设基于在我自己的机器上运行的示例应用程序的时序进行线性缩放。

-Plug the timing numbers printed by the code on the computer you're working on into that equation and report what the break even point (when the cpu version is as fast as the gpu version) will be.

我遇到的问题是我的内核比等效函数的主机版本花费的时间长得多(我将在下面发布),因此没有收支平衡点。我得到的数字如下。

done with copy to gpu kernel
copy to gpu took 26.30630 ms
done with gpu shift cypher kernel
gpu shift cypher took 7.33203 ms
done with copy from gpu kernel
copy from gpu took 28.54141 ms
host shift cypher took 0.00186 ms
Worked! CUDA and reference output match.

您认为我做事的方式有问题吗?这是内核和宿主函数。

// This kernel implements a per element shift
__global__ void shift_cypher(unsigned int *input_array, unsigned int *output_array,
unsigned int shift_amount, unsigned int alphabet_max, unsigned int array_length)
{
int gid = blockIdx.x * blockDim.x + threadIdx.x;
output_array[gid] = (input_array[gid] + shift_amount)%(alphabet_max+1);
}

void host_shift_cypher(unsigned int *input_array, unsigned int *output_array, unsigned int shift_amount, unsigned int alphabet_max, unsigned int array_length)
{
for(unsigned int i=0;i<array_length;i++)
{
int element = input_array[i];
int shifted = element + shift_amount;
if(shifted > alphabet_max)
{
shifted = shifted % (alphabet_max + 1);
}
output_array[i] = shifted;
}
}

示例应用程序以 16MB 的整数元素运行, block 大小为 512。这里是相关文件的完整源代码 http://pastebin.com/htYH0bA2

最佳答案

host shift cypher took 0.00186 ms

这看起来很奇怪。无论您在 CPU 上使用 16MB 做什么,都应该花费不到一毫秒的时间。

通过查看 pastebin 代码,您似乎可以使用 CUDA 事件对所有内容进行计时。虽然我没有使用过它们,但我的猜测是你用它来测量 GPU 内核执行的实际时间。其中,在仅调用主机代码的情况下几乎没有。这真的是他们衡量斯坦福类(class)中主机代码执行的方式吗?

你可以用任何一种 C 定时器检查这个结果来证明我是错的。

关于CUDA 内核函数比等效的主机函数花费更长的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7216716/

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