[python-arrayfire] 238/250: Adding a simple "to_array" function to ease interoperability

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:52 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository python-arrayfire.

commit 1030103c01f5ed81f19ddd1d93808b6725db8eed
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Sun Mar 20 04:44:53 2016 -0400

    Adding a simple "to_array" function to ease interoperability
---
 arrayfire/interop.py | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/arrayfire/interop.py b/arrayfire/interop.py
index 0c6348d..0fb55c1 100644
--- a/arrayfire/interop.py
+++ b/arrayfire/interop.py
@@ -21,6 +21,7 @@ from .device import *
 
 try:
     import numpy as np
+    from numpy import ndarray as NumpyArray
     from .data import reorder
 
     AF_NUMPY_FOUND=True
@@ -69,7 +70,8 @@ except:
     AF_NUMPY_FOUND=False
 
 try:
-    import pycuda.gpuarray as CudaArray
+    import pycuda.gpuarray
+    from pycuda.gpuarray import GPUArray as CudaArray
     AF_PYCUDA_FOUND=True
 
     def pycuda_to_af_array(pycu_arr):
@@ -83,6 +85,10 @@ try:
         Returns
         ----------
         af_arr    : arrayfire.Array()
+
+        Note
+        ----------
+        The input array is copied to af.Array
         """
 
         in_ptr = pycu_arr.ptr
@@ -92,6 +98,7 @@ try:
         if (pycu_arr.flags.f_contiguous):
             res = Array(in_ptr, in_shape, in_dtype, is_device=True)
             lock_array(res)
+            res = res.copy()
             return res
         elif (pycu_arr.flags.c_contiguous):
             if pycu_arr.ndim == 1:
@@ -119,7 +126,7 @@ except:
     AF_PYCUDA_FOUND=False
 
 try:
-    import pyopencl.array as CLArray
+    from pyopencl.array import Array as OpenclArray
     from .opencl import add_device_context as _add_device_context
     from .opencl import set_device_context as _set_device_context
     from .opencl import get_device_id as _get_device_id
@@ -137,6 +144,10 @@ try:
         Returns
         ----------
         af_arr    : arrayfire.Array()
+
+        Note
+        ----------
+        The input array is copied to af.Array
         """
 
         ctx = pycl_arr.context.int_ptr
@@ -189,3 +200,28 @@ try:
             return pyopencl_to_af_array(pycl_arr.copy())
 except:
     AF_PYOPENCL_FOUND=False
+
+
+def to_array(in_array):
+    """
+    Helper function to convert input from a different module to af.Array
+
+    Parameters
+    -------------
+
+    in_array : array like object
+             Can be one of numpy.ndarray, pycuda.GPUArray, pyopencl.Array, array.array, list
+
+    Returns
+    --------------
+    af.Array of same dimensions as input after copying the data from the input
+
+
+    """
+    if AF_NUMPY_FOUND and isinstance(in_array, NumpyArray):
+        return np_to_af_array(in_array)
+    if AF_PYCUDA_FOUND and isinstance(in_array, CudaArray):
+        return pycuda_to_af_array(in_array)
+    if AF_PYOPENCL_FOUND and isinstance(in_array, OpenclArray):
+        return pyopencl_to_af_array(in_array)
+    return Array(src=in_array)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-arrayfire.git



More information about the debian-science-commits mailing list