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
Interoperability with numpy #6
Comments
We are trying to ensure ArrayFire works out of the box on a python distribution. Numpy may not always be installed on the machine. That said, I pushed out 14d5bc6 that should add the ability to interop with numpy in the following manner: import numpy as np
import arrayfire as af
def af_array(src, transpose=False):
res = af.array(src.ctypes.data, src.shape, src.dtype.char)
return res if not transpose else af.transpose(res)
a = np.random.random((5,5))
b = af_array(a)
print(a)
af.display(b) |
The transpose flag may be necessary because numpy is row major by default and ArrayFire is column major. |
Works great, thanks! |
This only work for square matrix with import numpy as np
import arrayfire as af
def af_array(src, transpose=False):
res = af.array(src.ctypes.data, src.shape, src.dtype.char)
return res if not transpose else af.transpose(res)
a = np.random.random((5000,5000))[100:103,200:203]
b = af_array(a,transpose=True)
print(a)
af.display(b) will print: Using cuda backend
[[ 0.00902216 0.72194459 0.93649477]
[ 0.74371952 0.94558585 0.49367796]
[ 0.01659066 0.38938187 0.44101624]]
b
[3 3 1 1]
0.0090 0.7219 0.9365
0.0837 0.7593 0.4400
0.1749 0.0054 0.7595 the data of the two array are different. import numpy as np
import arrayfire as af
def af_array(src):
src_ = src if np.isfortran(src) else np.asfortranarray(src)
res = af.array(src_.ctypes.data, src_.shape, src_.dtype.char)
return res |
@FilipeMaia Can you comment here on how you are achieving this in afnumpy ? |
This is actually present in master as a constructor thanks to @FilipeMaia The lay out changes are going to be difficult to implicitly handle inside arrayfire. I think it should be left to the user to change it to proper format before calling arrayfire functions. |
The only way to ensure a conversion where the dimensions of the arrayfire array match dimensions of the numpy is like @mklf did in the last code snippet (N.B. this often means the data will have to be transposed as numpy uses C order by default). In afnumpy I'm currently assuming the numpy arrays are always in C order and have the shape of the arrayfire array be the inverse of the numpy array, e.g.:
|
I closed the issue hastily. arrayfire --> numpy is supported but the opposite is not. |
This is fixed in master. All you need to do now is call the following function:
This will take care of all cases @mklf mentions. |
Thank you! |
@cmey I have uploaded the latest version to be available via |
Would it be possible to support interoperability with numpy?
Currently:
The text was updated successfully, but these errors were encountered: