2

I'm trying to do numpy view-casting (which I believe in C/C++ land would be called reinterpret-casting) in pythran:

The following silly made-up example takes an array of unsigned 8-byte integers, reinterprets them as twice as many unsigned 4-byte integers, slices off the first and last (this also does not touch the actual data; it only changes the "base pointer") and reinterprets as unsigned 8-byte again, the total effect being a frame shift. (We'll worry about endianness some other time.)

import numpy as np

A = np.arange(5,dtype="u8")
a = A.view("u4")
B = a[1:9].view("u8")
A
# array([0, 1, 2, 3, 4], dtype=uint64)
B
# array([ 4294967296,  8589934592, 12884901888, 17179869184], dtype=uint64)
np.shares_memory(A,B)
# True

I cannot have pythran translate this directly because it doesn't know the .view attribute.

Is there a way to reinterpret cast arrays in pythran?

Albert.Lang
  • 523
  • 3
  • 8
  • Having 8-byte integers not aligned on an 8-byte boundary, as you do with your reinterpret-cast here, is potentially dangerous. On some architectures this is a reason to kill the process. On others it’s just a slowdown of memory reads. – Cris Luengo May 10 '23 at 23:08
  • @CrisLuengo My bad for using a "silly made-up example". I'd be happy to just use the non frame shifting cast from 8 byte to 4 byte or back (respecting alignment). – Albert.Lang May 11 '23 at 09:21

1 Answers1

2

As far as I can see there is no direct way to perform reinterpret casting of arrays in Pythran. Pythran doesn't support the numpy.view function, and there isn't a direct equivalent that can be used instead. Its just a limitation of Pythran as it only supports a subset of numpy's functionality.

Your best bet is probably to perform the casting in Python using numpy, then pass the result to the Pythran function. that could be feasible if the casting operation isn't a major bottleneck in your code.

Or you could use a different compiler such as Cython if you're familiar with one.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Luketl98
  • 71
  • 2
  • I am familiar with Cython which is precisely why I do not wish to use it. – Albert.Lang May 11 '23 at 15:04
  • 1
    Pythran author here: there's nothing that prevents Pythran from supporting `numpy.ndarray.view` if the dtype is an actual type and not a string literal. I've opened https://github.com/serge-sans-paille/pythran/issues/2112 – serge-sans-paille May 19 '23 at 09:21