1

I would like to construct a Numpy array that starts at a specific memory address. How would I do that? I assume the solution involves ctypes but I can't figure it out from the docs.

More Details and Context

I would like to create a number of arrays whose values are all at sequential memory addresses. For example, two size 2 arrays, where the 1st mem address of the 1st array is N and the last mem address of the 2nd array is N + 3. I could achieve the same affect by making one size 4 array and slicing it... but I want to call the array constructor a separate time for each "slice." I am trying to track a bug in a certain C library.

6
  • I don't like asking "What have you tried so far?", but still people tend me to do so. Commented Dec 19, 2016 at 12:54
  • np.ndarray can make an array with an existing data buffer. Commented Dec 19, 2016 at 14:09
  • 1
    For more details, what is at that memory address? Commented Dec 19, 2016 at 16:39
  • I will update the question... Commented Dec 19, 2016 at 18:05
  • "but I want to call the array constructor a separate time for each "slice."" - this is exactly what slicing will do anyway, other than the sliced version will have .base is not None Commented Dec 19, 2016 at 21:30

1 Answer 1

4

Make an array of 10 bytes:

In [287]: x = np.arange(10, dtype=np.uint8)
In [288]: x
Out[288]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint8)
In [289]: x.__array_interface__
Out[289]: 
{'data': (155596184, False),
 'descr': [('', '|u1')],
 'shape': (10,),
 'strides': None,
 'typestr': '|u1',
 'version': 3}
In [290]: x.data
Out[290]: <memory at 0xaec88f34>

Make another array with ndarray, using the same data buffer, but with an offset:

In [291]: y=np.ndarray(shape=(3,), dtype=x.dtype, buffer=x.data, offset=3)
In [292]: y
Out[292]: array([3, 4, 5], dtype=uint8)
In [293]: y.__array_interface__
Out[293]: 
{'data': (155596187, False),
 'descr': [('', '|u1')],
 'shape': (3,),
 'strides': None,
 'typestr': '|u1',
 'version': 3}

this looks the same as a 3 element slice:

In [294]: z=x[3:6]
In [295]: z
Out[295]: array([3, 4, 5], dtype=uint8)
In [296]: z.__array_interface__
Out[296]: 
{'data': (155596187, False),
 'descr': [('', '|u1')],
 'shape': (3,),
 'strides': None,
 'typestr': '|u1',
 'version': 3}

If I knew more about specifying a memory address I probably could get by with just the buffer parameter, and default 0 offset. But from your additions, the use of offset might be just what you want.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for responding. This looks promising, and I will play with it tomorrow at work. Ideally, I would like to be able to specify an array to start a particular pointer... so for instance the first memory address after the tail end of the first array..I think I can work with this. I'm really trying to avoid the weeds of going into the C library I'm dealing with, but it may be unavoidable. Here is a link to the issue I'm trying to trace in PyFFTW. I posted this example with a script that reproduces the error. Maybe you might want to look at it. :) github.com/pyFFTW/pyFFTW/issues/139
PyFFTW is a Python wrapper around the FFTW Fast Fourier Transform library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.