为什么我会看到“不支持未对齐的内存访问”错误?
我收到“不支持未对齐内存访问错误”并进行了 Google 搜索 但没有明确的解释。 整个错误消息是:
/c:\cuda\include\math_functions_dbl_ptx1.h(1308): Error: Unaligned memory accesses not supported
以下代码导致错误:
for (j = low; j <= high; j++)
变量 j 和 high 被声明为 int。 以前遇到过这种错误,但自行解决了(我什么也没做)。
有人可以解释一下这个问题吗?
I got an "unaligned memory accesses not supported error" and did a Google search for that
but there were no clear explanations.
The whole error message is:
/c:\cuda\include\math_functions_dbl_ptx1.h(1308): Error: Unaligned memory accesses not supported
The following code caused the error:
for (j = low; j <= high; j++)
The variables j and high are declared as int.
This kind of error was encountered before but resolved by itself (I did nothing).
Can anybody explain about this matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理论
在许多机器上(但不是 Intel IA32 机器或其相关机器),如果要访问 2 字节数量(整数),则地址必须是偶数,而不是奇数;如果要访问 4 字节数量(整数或浮点数),则地址必须是 4 字节的倍数;如果要访问8字节数量(整数或双精度),则地址必须是8字节的倍数;等等。
从表面上看,您的代码以某种方式尝试取消引用一个指针,该指针在低位部分设置了位,而您不应该这样做。例如,强制解决该问题的一种方法(在 C 中)是:
当您完成指针算术时,
lp
中的地址不是 4 字节(或 8 字节)对齐;由于+1
,它相差一。最后一行将给出未对齐的内存访问指针。练习
由于您没有显示代码的声明,我们无法确定导致问题的原因(尽管您确实说
j
和high
是int< /code> 变量;没有关于
low
的评论)。事实上,几乎独立于声明,引用的for
循环似乎不太可能是问题的根源。它可能是接近该行的代码,但可能不是那行。您有可能在某处遇到缓冲区覆盖问题并且意外修改了指针,并且修改后的指针会生成错误。但是,由于该行似乎不包含任何指针,因此不太可能是该行真正触发了问题。
Theory
On many machines - but not Intel IA32 ones or their relatives - if you want to access a 2-byte quantity (integer), the address must be even, not odd; if you want to access a 4-byte quantity (integer or float) the address must be a multiple of 4 bytes; if you want to access an 8-byte quantity (integer or double), the address must be a multiple of 8 bytes; and so on.
Superficially, then, you have somehow got your code trying dereference a pointer which has bits set in the low-order parts when you should not. For example, one way of forcing the issue (in C) would be:
By the time you've gone through the pointer arithmetic, the address in
lp
is not 4-byte (or 8-byte) aligned; it is off-by-one because of the+1
. The last line would give an unaligned memory access pointer.Practice
Since you have not shown the declarations for your code, we can't be sure what causes the trouble (though you do say
j
andhigh
areint
variables; no comment aboutlow
). Indeed, almost independent of the declarations, it seems unlikely that the quotedfor
loop is the source of the trouble. It may be code close to that, but it probably is not that line.There is a chance that you have a buffer over-writing problem somewhere and are modifying a pointer by accident, and that modified pointer generates the error. But, since that line does not seem to include any pointers, it is unlikely to be that line that is actually triggering the trouble.