CUDA 分配对齐是 256 字节 - 认真吗? [英] CUDA allocation alignment is 256 bytes - seriously?

查看:9
本文介绍了CUDA 分配对齐是 256 字节 - 认真吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CUDA C 编程指南 5.0"中,第 73 页(also here) 表示驻留在全局内存中或由驱动程序或运行时 API 的内存分配例程之一返回的变量的任何地址始终与至少 256 个字节对齐".我不知道这句话的确切含义.谁能给我举个例子?非常感谢.

In "CUDA C Programming Guide 5.0", p73 (also here) says "Any address of a variable residing in global memory or returned by one of the memory allocation routines from the driver or runtime API is always aligned to at least 256 bytes". I do not know the exact meaning of this sentence. Could anyone show an example for me? Many thanks.

一个衍生问题:那么,如何分配基本元素(如 int)或自定义元素的一维数组呢?数组的起始地址会是256B的倍数,而数组中每个元素的地址不一定是256B的倍数?

A derivative question: So, what about allocating an one-dimensional array of basic elements (like int) or self-defined ones? The starting address of the array will be multiples of 256B, while the address of each element in the array is not necessarily multiples of 256B?

推荐答案

使用 CUDA Runtime 的任何设备内存分配函数分配的指针,例如 cudaMalloccudaMallocPitch 保证256字节对齐,即地址是256的倍数.

The pointers which are allocated by using any of the CUDA Runtime's device memory allocation functions e.g cudaMalloc or cudaMallocPitch are guaranteed to be 256 byte aligned, i.e. the address is a multiple of 256.

考虑以下示例:

char *ptr1, *ptr2;

int bytes = 1;

cudaMalloc((void**)&ptr1,bytes);
cudaMalloc((void**)&ptr2,bytes);

假设 ptr1 中返回的地址是 256 的倍数,那么 ptr2 中返回的地址将至少是 (ptr1 + 256).

Suppose the address returned in ptr1 is some multiple of 256, then the address returned in ptr2 will be atleast (ptr1 + 256).

这是分配内存的设备所施加的限制.大多数情况下,指针是出于性能目的而对齐的.(一些 NVIDIA 的家伙应该能够判断是否还有其他原因).

This is a restriction imposed by the device on which the memory is being allocated. Mostly, pointers are aligned due to performance purposes. (Some NVIDIA guy should be able to tell if there is some other reason also).

指针对齐方式并不总是 256.在我的设备(GTX460M)上是512,可以通过cudaDeviceProp::textureAlignment字段获取设备指针对齐方式.

Pointer alignment is not always 256. On my device (GTX460M), it is 512. You can get the device pointer alignment by the cudaDeviceProp::textureAlignment field.

指针对齐也是将指针绑定到纹理的要求.

Alignment of pointers is also a requirement for binding the pointer to textures.

这篇关于CUDA 分配对齐是 256 字节 - 认真吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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