forked from arrayfire/arrayfire-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterop.py
More file actions
59 lines (51 loc) · 1.91 KB
/
interop.py
File metadata and controls
59 lines (51 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
"""
Interop with other python packages.
This module provides interoperability with the following python packages.
1. numpy
"""
from .array import *
from .data import reorder
try:
import numpy as np
AF_NP_FOUND=True
def np_to_af_array(np_arr):
"""
Convert numpy.ndarray to arrayfire.Array.
Parameters
----------
np_arr : numpy.ndarray()
Returns
---------
af_arry : arrayfire.Array()
"""
if (np_arr.flags['F_CONTIGUOUS']):
return Array(np_arr.ctypes.data, np_arr.shape, np_arr.dtype.char)
elif (np_arr.flags['C_CONTIGUOUS']):
if np_arr.ndim == 1:
return Array(np_arr.ctypes.data, np_arr.shape, np_arr.dtype.char)
elif np_arr.ndim == 2:
shape = (np_arr.shape[1], np_arr.shape[0])
res = Array(np_arr.ctypes.data, shape, np_arr.dtype.char)
return reorder(res, 1, 0)
elif np_arr.ndim == 3:
shape = (np_arr.shape[2], np_arr.shape[1], np_arr.shape[0])
res = Array(np_arr.ctypes.data, shape, np_arr.dtype.char)
return reorder(res, 2, 1, 0)
elif np_arr.ndim == 4:
shape = (np_arr.shape[3], np_arr.shape[2], np_arr.shape[1], np_arr.shape[0])
res = Array(np_arr.ctypes.data, shape, np_arr.dtype.char)
return reorder(res, 3, 2, 1, 0)
else:
raise RuntimeError("Unsupported ndim")
else:
return np_to_af_array(np.asfortranarray(np_arr))
except:
AF_NP_FOUND=False