cuda对齐256byte严重? [英] cuda alignment 256bytes seriously?

查看:169
本文介绍了cuda对齐256byte严重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CUDA C编程指南5.0中,p73表示驻留在全局内存中或由驱动程序或运行时API中的一个内存分配例程返回的变量的任何地址始终至少对齐到256个字节。我不知道这句话的确切含义。任何人都能给我一个例子吗?非常感谢。



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

解决方案

使用任何CUDA运行时的设备内存分配函数分配的指针,例如 cudaMalloc cudaMallocPitch 保证为256字节对齐,即地址是256的倍数。



请考虑以下示例:

  char * ptr1,* ptr2; 

int bytes = 1;

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

假设 ptr1 中返回的地址是的倍数,则 ptr2 中返回的地址将为(ptr1 + 256)



这是由分配内存的设备施加的限制。大多数情况下,由于性能目的,指针是对齐的。



重要提示:



指针对齐并不总是256 。在我的设备(GTX460M),它是512.你可以通过 cudaDeviceProp :: textureAlignment 字段获得设备指针对齐。

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


In "CUDA C Programming Guide 5.0", p73 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 shows an example for me? Many thanks.

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?

解决方案

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.

Consider the following example:

char *ptr1, *ptr2;

int bytes = 1;

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

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

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).

Important:

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对齐256byte严重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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