lospollos
lospollos

Reputation: 55

char array __attribute__ aligned

I have a question about the following line of code:

char buffer[256] __attribute__((aligned(4096)));

The content of the global array "buffer" are strings, which i get from stdin. I have read https://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Type-Attributes.html (gcc/gnu online documentation). I get that this attribute specifies a minimum alignment for variables in bytes.

My question regards the reason WHY i would need such an alignment for an char array?

Just because of perfomance reasons?

Upvotes: 3

Views: 3388

Answers (2)

user1537765
user1537765

Reputation: 89

I will guess that a specific HW wants it to be aligned that way. (mass storage reader, DMA etc). I must say it is rare to see alignment that big.

Upvotes: 0

Jose
Jose

Reputation: 3460

Perhaps the use of a constant is not the best idea, at least, without a good explanation about the target (it looks like the page size of the example is 4096). Some architectures have specific instructions for copying big chunks of memory (such as the whole page), which may do the process faster:

GCC also provides a target specific macro BIGGEST_ALIGNMENT, which is the largest alignment ever used for any data type on the target machine you are compiling for. For example, you could write: short array[3] attribute ((aligned (BIGGEST_ALIGNMENT)));

The compiler automatically sets the alignment for the declared variable or field to BIGGEST_ALIGNMENT. Doing this can often make copy operations more efficient, because the compiler can use whatever instructions copy the biggest chunks of memory when performing copies to or from the variables or fields that you have aligned this way. Note that the value of BIGGEST_ALIGNMENT may change depending on command-line options.

[...]

Note that the effectiveness of aligned attributes may be limited by inherent limitations in your linker. On many systems, the linker is only able to arrange for variables to be aligned up to a certain maximum alignment. (For some linkers, the maximum supported alignment may be very very small.) If your linker is only able to align variables up to a maximum of 8-byte alignment, then specifying aligned(16) in an attribute still only provides you with 8-byte alignment. See your linker documentation for further information.

For the completeness sake, it wants to ensure that the array will be set in one page, in order to increase performance.

This link explains how to the aligned-page data (in that case, with aligned_malloc) improves the code: https://software.intel.com/en-us/articles/getting-the-most-from-opencl-12-how-to-increase-performance-by-minimizing-buffer-copies-on-intel-processor-graphics

Upvotes: 1

Related Questions