tags:

views:

69

answers:

1

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?

+2  A: 

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:

long l = 0x12345678;
void *v = (char *)&l + 1;
long *lp = v;
l = *lp;

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 and high are int variables; no comment about low). Indeed, almost independent of the declarations, it seems unlikely that the quoted for 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.

Jonathan Leffler
Your theory example caused horrible flashbacks of debugging a compiler written in C for me. Ahhhh. +1.
gnucom