Skip to content

[wip] Align Struct fields to their natural alignment - #197

Open
szymonlopaciuk wants to merge 1 commit into
xsuite:mainfrom
szymonlopaciuk:xo-struct-packed
Open

szymonlopaciuk wants to merge 1 commit into
xsuite:mainfrom
szymonlopaciuk:xo-struct-packed

Conversation

@szymonlopaciuk

Copy link
Copy Markdown
Contributor

Description

This replaces Struct's fixed 8-byte field padding with per-type _alignment spec. Structs align each field individually and themselves take as alignment the maximum among their fields (for Complex64/Complex128 4/8 bytes is used which is a natural alignment for x86_64/arm64 and on GPU they are defined as float[2]/double[2], respectively). Array elements follow their type’s alignment. Separately allocated objects also respect the context’s minimum alignment; fields within structs use only their type’s alignment.

This alignment method follows the standard C struct alignment for x86_64, arm64, CUDA, and OpenCL (see the caveat about complex values as noted above, and some pointer peculiarities for OpenCL, which are not relevant for us). It should allow for less waste of space and lower memory pressure.

The alignment specified here is for Xobjects types, and is conceptually different to Context.minimum_alignment: the latter governs the alignment of context buffers. This is generally the same, except on OpenCL, where, depending on the platform the alignment of pointers passed as kernel arguments can be required to be as large as 16 or even 32 bytes. As things stand, it is not always safe to pass Xobject pointers to kernels if they are not at the beginning of a buffer, or if they are not aligned to this boundary. However, this is already in theory a problem, and this PR doesn't make it better or worse. For beam elements in Xtrack, these are always reallocated when a tracker is built on a new context, and in the process each beam element is aligned to the context's minimum alignment. Some issues related to this are already known xsuite/xsuite#1021.

This is [wip] because I still need to comprehensively test this on all contexts.

Checklist

Mandatory:

  • I have added tests to cover my changes
  • All the tests are passing, including my new ones
  • I described my changes in this PR description

Optional:

  • The code I wrote follows good style practices (see PEP 8 and PEP 20).
  • I have updated the docs in relation to my changes, if applicable
  • I have tested also GPU contexts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant