CUDA和共享变量在不同的全局函数 [英] CUDA and shared variables among different global functions

查看:171
本文介绍了CUDA和共享变量在不同的全局函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对CUDA只有几点疑问。也许他们可能看起来很蠢的问题;

如果我在GPU上声明了一个变量(例如,一个数组 alpha ,并且 ,那么我会为此感到抱歉。)



<元素, cudaMalloc((void **)& alpha,N * sizeof(double)))并在全局函数中分配其值,而不释放其内存,可以用于其他连续的全局函数,对吗?



此外,是否可能(或建议)计算GPU上的标量变量,在GPU上,或者最好每次作为参数从CPU传递?



感谢您的关注。


<如果你把值写入分配的全局内存中,这些值会持续到你释放内存,甚至是内核调用。


$

b $ b

对于访问标量变量(这是常量),更好的方法是将其作为参数传递到全局内核启动,而不是将其放在全局内存中并从中读取。全局内存访问是昂贵的,这避免了每次你需要读取它时从全局内存加载标量。


Just a few doubts about CUDA. Perhaps they may seem silly questions; I apologise for it.

If I declare a variable on the GPU (e.g., an array alpha with N elements, cudaMalloc((void**)&alpha, N * sizeof(double))) and allocate its value in a global function without freeing its memory, this variable should be available for other successive global functions, right?

Besides, is it possible (or advisable) to compute a scalar variable on the GPU and make it shared among several global functions on the GPU, or is it better to pass it every time as an argument from the CPU?

Thank you for your attention.

解决方案

Yes, if you write values into allocated global memory those values are persistent until you free that memory, even across kernel invocations.

As for accessing scalar variables (which are constants), the better approach would be to pass it as a parameter to the global kernel launch rather than putting it in global memory and reading it from there. Global memory accesses are expensive, and this avoids loading that scalar from global memory every time you need to read it.

这篇关于CUDA和共享变量在不同的全局函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆