参数到CUDA内核 [英] Parameters to CUDA kernels

查看:201
本文介绍了参数到CUDA内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当为特定线程配置调用CUDA内核时,对
有什么严格的规则,内存空间(设备/主机)内核参数应该驻留在什么类型中?

When invoking a CUDA kernel for a specific thread configuration, are there any strict rules on which memory space (device/host) kernel parameters should reside in and what type they should be?

假设我用

kernel<<<numblocks, threadsperblock >>> (/*parameters*/)

我可以传递一个整数参数 int foo 这是一个主机 - 整数变量,
直接到CUDA内核?或者我应该为 cudaMalloc 内存单个整数说 dev_foo 然后 cudaMemcpy foo 转换为 devfoo ,然后传递 devfoo 内核参数?

Can I pass an integer parameter int foo which is a host-integer variable, directly to the CUDA kernel? Or should I cudaMalloc memory for a single integer say dev_foo and then cudaMemcpy foo into devfoo and then pass devfoo as a kernel parameter?

推荐答案

内核参数的规则是C ++参数传递规则的逻辑结果以及设备和主机内存在物理上分离的事实。

The rules for kernel arguments are a logical consequence of C++ parameter passing rules and the fact that device and host memory are physically separate.

CUDA不允许通过引用传递参数,并且必须小心使用指针。

CUDA does not allow passing arguments by reference and you must be careful with pointers.

必须按值传递参数。传递用户定义的类型需要默认的复制构造函数或您自己的复制构造函数(如果存在)不包含任何内存分配(使用new或malloc的堆分配)。

Specifically, you must pass parameters by value. Passing user-defined types requires that the default copy-constructor or your own copy-constructor (if present) does not contain any memory allocations (heap allocations with "new" or "malloc").

总而言之,pass-by-value适用于整数,浮点或其他原始类型,以及简单的平面用户定义结构或类对象。

In summary pass-by-value works well for integral, floating point or other primitive types, and simple flat user-defined structs or class objects.

这篇关于参数到CUDA内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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