Proposal: the Mojo python.Python module should have a function that creates a Python array.array
or memoryview
object from a Mojo array of numbers like object, such as a GPu HostBuffer
.
Use case: I wrote my first Mojo GPU program recently (Julia set fractal) and by far the slowest part of the code was converting the Mojo HostBuffer into a Python list. (I use the Python PIL to save as PNG.)
Python has a built-in module array
for C-style arrays of numbers which are popular with Python number crunchers and graphic programmers, and these Python arrays support the memoryview
buffer protocol which is widely supported by Python native extensions such as numpy. Memory views are just chunks of memory while arrays have an element type, so the latter is more useful but harder to get at from C/C++
I would like to see at minimum the GPU HostBuffer type should implement the ConvertableToPython
trait, as an array.array or buffer protocol object. I think a Python array() or memoryview() function that converts any Mojo sequence of numbers/SIMD numbers into a Python array like object would be very useful for people using Mojo as a Python accelerator or with other Python extensions.