ATTRIBUTES-VARIABLES(3clc)

πŸ“¦
version 1.0~svn33624 Β· 3 versions
, in ubuntu jammy, ubuntu noble

✨ NAME ✨

okay bestie, welcome to ATTRIBUTES OF VARIAB(3clc) β€” a page from the OpenCL Manual πŸ’– and today we're talking about Attributes_of_Variables, aka Attributes of Variables. that's the whole vibe.

here's the lineup of the little guys this page is about:

__attribute__ ((aligned)) __attribute__
((aligned (n))) __attribute__
((packed)) __attribute__
((endian(host))) __attribute__
((endian(device)))

πŸ¦‹ DESCRIPTION πŸ¦‹

so. the keyword __attribute__ lets you hand-pick special attributes for your variables or your structure fields. the keyword gets followed by an attribute specification all cozy inside double parentheses (( )). the three qualifiers we're defining down below are aligned, packed, and endian. periodt. πŸ’…

🌸 aligned (alignment) 🌸

this attribute sets a minimum alignment for a variable or a structure field, and it's measured in bytes. like, for example, this declaration:

int x __attribute__ ((aligned (16))) =
0;

makes the compiler allocate the global variable x on a 16-byte boundary. and heads up: the alignment value you give must be a power of two. no cap. ✨

you can also set the alignment of structure fields! for example, to create a double-word aligned int pair, you could write:

struct foo { int x[2] __attribute__
((aligned (8))); };

this is an alternative to making a union with a double member that forces the whole union to be double-word aligned. same energy, different outfit. πŸ’β€β™€οΈ

and just like in the examples above, you can explicitly say what alignment (in bytes) you want the compiler to use for a given variable or structure field. OR β€” plot twist β€” you can leave out the alignment factor entirely and just ask the compiler to align a variable or field to the maximum useful alignment for the target machine you're compiling for. like so:

short array[3] __attribute__
((aligned));

whenever you leave out the alignment factor in an aligned attribute spec, the OpenCL compiler automatically sets the alignment for the declared variable or field to the largest alignment ever used for any data type on the target device you're compiling for. it's giving "i'll handle it, don't worry." πŸ‘‘

important tea β˜•: when used on a struct, or a struct member, the aligned attribute can only increase the alignment. to decrease it, you gotta specify the packed attribute as well. but when used as part of a typedef, the aligned attribute can both increase and decrease alignment β€” and specifying packed will generate a warning. πŸ™ˆ

also ngl, the effectiveness of aligned attributes may be limited by inherent limitations of the OpenCL device and compiler. for some devices, the OpenCL compiler may only be able to arrange for variables to be aligned up to a certain maximum alignment. so if the OpenCL compiler can only align variables up to a maximum of 8 byte alignment, then specifying aligned(16) in an __attribute__ will still only give you 8 byte alignment. 😭 check your platform-specific documentation for more info, fr fr.

πŸŽ€ packed πŸŽ€

the packed attribute says a variable or structure field should have the smallest possible alignment β€” one byte for a variable, unless you specify a larger value with the aligned attribute. tiny. compact. iconic.

here's a structure where the field x is packed, so it immediately follows a:

struct foo { char a; int x[2]
__attribute__ ((packed)); };

and here's a lil rule you need in your notes app πŸ“: an attribute list placed at the beginning of a user-defined type applies to the variable of that type and not the type, while attributes following the type body apply to the type.

for example:

/* a has alignment of 128 */
__attribute__((aligned(128))) struct A
{int i;} a;

/* b has alignment of 16 */
__attribute__((aligned(16))) struct B
{double d;}
__attribute__((aligned(32))) b ;

struct A a1; /* a1 has alignment of 4
*/

struct B b1; /* b1 has alignment of 32
*/

look at that glow-up. ✨

πŸ’œ endian (endiantype) πŸ’œ

the endian attribute determines the byte ordering of a variable. endiantype can be set to:

  • host β€” the variable uses the endianness of the host processor
  • device β€” the variable uses the endianness of the device on which the kernel will be executed

and the default is device. for example:

float4 *p   __attribute__
((endian(host)));

specifies that data stored in memory pointed to by p will be in the host endian format. slay. 🌈


πŸ“œ SPECIFICATION πŸ“œ

OpenCL Specification[1]


πŸ‘€ SEE ALSO πŸ‘€


🫢 AUTHORS 🫢

The Khronos Group


Copyright Β© 2007-2011 The Khronos Group Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.


🧚 NOTES 🧚

  1. OpenCL Specification page 239, section 6.11.3 - Specifying Attributes of Variables

okay that's the whole page bestie, you ate that πŸ’…βœ¨ this was ATTRIBUTES OF VARIAB(3clc) from the OpenCL Manual, brought to you by The Khronos Group, dated 01/14/2021 β€” mwah, go align something cute πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from