Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support read-only numpy arrays #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pauldmccarthy
Copy link

@pauldmccarthy pauldmccarthy commented Apr 11, 2019

When using opengl-accelerate, many operations will fail if passed a numpy.array which is contiguous, but is not writeable, e.g.:

print(value.flags)
gl.glBufferData(gl.GL_ARRAY_BUFFER,
                value.nbytes,
                value,
                gl.GL_STATIC_DRAW)

results in:

  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
Traceback (most recent call last):
 ...
  File ".../program.py", line 313, in setAtt
    gl.GL_STATIC_DRAW)
  File "latebind.pyx", line 44, in OpenGL_accelerate.latebind.Curry.__call__ (src/latebind.c:1201)
  File ".../OpenGL/GL/VERSION/GL_1_5.py", line 86, in glBufferData
    data = ArrayDatatype.asArray( data )
  File "arraydatatype.pyx", line 176, in OpenGL_accelerate.arraydatatype.ArrayDatatype.asArray (src/arraydatatype.c:4253)
  File "numpy_formathandler.pyx", line 112, in OpenGL_accelerate.numpy_formathandler.NumpyHandler.c_asArray (src/numpy_formathandler.c:2689)
  File "numpy_formathandler.pyx", line 132, in OpenGL_accelerate.numpy_formathandler.NumpyHandler.contiguous (src/numpy_formathandler.c:3045)
OpenGL.error.CopyError: ('Non-contiguous array passed', array([0.00093461, 0.00061148, 0.00495276, ..., 0.00381922, 0.00935766,
       0.00224066], dtype=float32))

This is because accelerate/src/numpy_formathandler.pyx uses the PyArray_ISCARRAY function to test whether an array is contiguous. However, PyArray_ISCARRAY also tests whether an array is writeable.

This PR simply changes numpy_formathandler.pyx so it uses PyArray_ISCARRAY_RO rather than PyArray_ISCARRAY, so that read-only arrays are accepted.

This issue has arisen in my work due to a recent change in numpy which causes data loaded via np.frombuffer to be set as read-only.

The change to numpy_formathandler.pyx seems to work fine for me, although I am not sure whether some circumstances do actually require arrays to be writeable. If this is the case, please disregard this PR.

@whydoubt
Copy link

whydoubt commented Dec 2, 2023

It appears that equivalent changes have been made to the codebase, so I think this PR should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants