2

Update: numpy bug.


Unfortunately the following:

import numpy as np
a = np.zeros(4, dtype=np.dtype([('t', '<f8'), ('d', [('a', '<i4'), ('b', '<f8')], (100,))], align=True))
b = np.require(a, requirements=['ALIGNED'])
print([x.flags['ALIGNED'] for x in [a, b]])

prints [False, False]!

How do I align a?

Neil G
  • 32,138
  • 39
  • 156
  • 257

1 Answers1

5

You can specify the alignment requirement in the dtype.

In [9]: a = np.zeros(4, dtype= np.dtype([('x', '<f8'), ('y', '<i4')], align=False))

In [10]: a.data
Out[10]: <read-write buffer for 0x2f94440, size 48, offset 0 at 0x2f8caf0>

In [11]: a = np.zeros(4, dtype=np.dtype([('x', '<f8'), ('y', '<i4')], align=True))
In [12]: a.data
Out[12]: <read-write buffer for 0x2f94030, size 64, offset 0 at 0x2f8c5b0>

Note the difference in size. For structured types, the alignment flag was misleading in previous versions of Numpy, the requirement is now 16 bytes for strings and structured types in order to make things work correctly on SPARC. Julian Taylor gives a more extended explanation at http://article.gmane.org/gmane.comp.python.numeric.general/59123

Charles Harris
  • 934
  • 6
  • 4
  • Thanks for your answer. While it works with this simple test case. It doesn't work in general unfortunately. Please see my updated question. – Neil G Oct 23 '14 at 00:42
  • 1
    OK, looks like a bug. The `recarray` class accepts an aligned parameter, but looks to lose it in `format_parser`. Please open a Numpy issue and we can move the conversation there. – Charles Harris Oct 23 '14 at 03:06
  • 1
    Thanks, sounds good. Please let me know if you have seen the motivation for my question: http://stackoverflow.com/questions/26484665/opengl-says-from-param-received-a-non-contiguous-array This bug is breaking PyOpenGL I think. – Neil G Oct 23 '14 at 03:17