1 Reply Latest reply on Jun 15, 2010 6:09 AM by Jawed

    Passing structures to kernel

    makhan
      how to align?

      Hi,

      I wrote a simple implementation of Genetic Algorithm for GPU. It works fine as long as the only arguments I pass are float arrays, but as soon as I try to enclose it into a structure, everything breaks down and I'm getting memory access errors.

      How am I supposed to align the following structure? I read about alignment in few places, but honestly I don't understand the idea.

      struct __attribute__ ((aligned(16))) individual {
         float x[16];
         int num_of_dim;
         float fitness;
       };

      In main function I'm passing individuals array.

        • My understanding
          Jawed

          Alignment is in bytes and is used to "expand" a structure so that it is spaced in memory. e.g. it's useful, if you build an array of these structures, to align them so that they match the memory system, for best coalescing and minimal spanning of cache lines.

          You don't need the attribute on your struct definition.

          If you do want it, then it needs to be larger than the size of the struct. Since a float and an int are both 4 bytes, and you have 18 of these, the minimum size should be 72.

          So the smallest meaningful value is 72. Without it the implicit alignment is 128.

          128 might be preferable to play nice with cache lines. e.g. if a cache line is 128 bytes, then the hardware would always fetch only one line to access this structure. Without this attribute setting, the hardware would most-often find itself fetching two cache lines to access one structure.

          Fetching extra cache lines would hurt performance if accesses are random, so alignment can be used to minimise the count of cache lines accessed.

          Similarly, it can be used to control access to memory banks.

          Overall, it's a super-tweaky subject.