#NEWLINE# def pyopencl_to_af_array( pycl_arr ) : #NEWLINE# #INDENT# '\n        Convert pyopencl.gpuarray to arrayfire.Array\n\n        Parameters\n        -----------\n        pycl_arr  : pyopencl.Array()\n\n        Returns\n        ----------\n        af_arr    : arrayfire.Array()\n\n        Note\n        ----------\n        The input array is copied to af.Array\n        ' #NEWLINE# ctx = pycl_arr . context . int_ptr #NEWLINE# que = pycl_arr . queue . int_ptr #NEWLINE# dev = pycl_arr . queue . device . int_ptr #NEWLINE# dev_idx = None #NEWLINE# ctx_idx = None #NEWLINE# for n in range ( get_device_count ( ) ) : #NEWLINE# #INDENT# set_device ( n ) #NEWLINE# dev_idx = _get_device_id ( ) #NEWLINE# ctx_idx = _get_context ( ) #NEWLINE# if ( ( dev_idx == dev ) and ( ctx_idx == ctx ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# if ( ( dev_idx == None ) or ( ctx_idx == None ) or ( dev_idx != dev ) or ( ctx_idx != ctx ) ) : #NEWLINE# #INDENT# _add_device_context ( dev , ctx , que ) #NEWLINE# _set_device_context ( dev , ctx ) #NEWLINE# #UNINDENT# in_ptr = pycl_arr . base_data . int_ptr #NEWLINE# in_shape = pycl_arr . shape #NEWLINE# in_dtype = pycl_arr . dtype . char #NEWLINE# if pycl_arr . flags . f_contiguous : #NEWLINE# #INDENT# res = Array ( in_ptr , in_shape , in_dtype , is_device = True ) #NEWLINE# lock_array ( res ) #NEWLINE# return res #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if pycl_arr . flags . c_contiguous : #NEWLINE# #INDENT# if ( pycl_arr . ndim == 1 ) : #NEWLINE# #INDENT# return Array ( in_ptr , in_shape , in_dtype , is_device = True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( pycl_arr . ndim == 2 ) : #NEWLINE# #INDENT# shape = ( in_shape [ 1 ] , in_shape [ 0 ] ) #NEWLINE# res = Array ( in_ptr , shape , in_dtype , is_device = True ) #NEWLINE# lock_array ( res ) #NEWLINE# return reorder ( res , 1 , 0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( pycl_arr . ndim == 3 ) : #NEWLINE# #INDENT# shape = ( in_shape [ 2 ] , in_shape [ 1 ] , in_shape [ 0 ] ) #NEWLINE# res = Array ( in_ptr , shape , in_dtype , is_device = True ) #NEWLINE# lock_array ( res ) #NEWLINE# return reorder ( res , 2 , 1 , 0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( pycl_arr . ndim == 4 ) : #NEWLINE# #INDENT# shape = ( in_shape [ 3 ] , in_shape [ 2 ] , in_shape [ 1 ] , in_shape [ 0 ] ) #NEWLINE# res = Array ( in_ptr , shape , in_dtype , is_device = True ) #NEWLINE# lock_array ( res ) #NEWLINE# return reorder ( res , 3 , 2 , 1 , 0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise RuntimeError ( 'Unsupported ndim' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# return pyopencl_to_af_array ( pycl_arr . copy ( ) )