5

Is there a way to allocate the data section (i.e. the data) of a numpy array on a page boundary?

For why I care, if I were using PyOpenCL on an Intel device, and I wanted to create a buffer using CL_MEM_USE_HOST_PTR, they recommend that the data is 1) page aligned and 2) size a multiple of a cache line.

There are various ways in C of allocating page aligned memory, see for example: aligned malloc() in GCC?

Community
  • 1
  • 1
Alex Rothberg
  • 10,243
  • 13
  • 60
  • 120
  • No, numpy can't do that at the moment, but if you allocate the memory elsewhere, you could easily wrap it into a numpy array. Or you overallocate... – seberg Jun 14 '14 at 19:19
  • I see how the over allocate satisfies the multiple of cache line requirement, but how do I solve the alignment? – Alex Rothberg Jun 14 '14 at 20:53
  • @AlexRothberg with Cython you can use the ways you know in C to do it, would it be enough for you? – Saullo G. P. Castro Jun 15 '14 at 10:17

1 Answers1

2

I'm not aware that Numpy has any explicit calls to align memory at this time. The only way I can think of doing this, short of Cython as suggested by @Saulio Castro, would be through judicious allocation of memory, with "padding", using the numpy allocation or PyOpenCL APIs.

You would need to create a buffer "padded" to align on multiples of 64K bytes. You would also need to "pad" the individual data structure elements you were allocating in the array so they too, in turn, were aligned to 4k byte boundaries. This would of course depend on what your elements look like, whether they were built in numpy data types, or structures created using the numpy dtype. The API for dtype has an "align" keyword but I would be wary of that, based on the discussion at this link.

An old school trick to align structure is to start with the largest elements, work your way down, then "pad" with enough uint8's so one or N structs fill out the alignment boundary.

Hope that's not too vague...

paisanco
  • 4,098
  • 6
  • 27
  • 33