ó
àÆ÷Xc           @` so   d  d l  m Z m Z m Z d  d l Z d  d l m Z e j e	 k rV e
 d ƒ ‚ n  e	 d „ Z d „  Z d S(   i    (   t   absolute_importt   print_functiont   divisionN(   t   cudas%   Optional theano package cuda disabledc         C` sô   t  |  t j ƒ s! t d ƒ ‚ nÏ d } t } xh t |  j d d d ƒ D]M } |  j | d k rf qG n  |  j | | k rƒ t	 } Pn  | |  j | 9} qG W| sÂ | r³ |  j
 ƒ  }  qÂ t d ƒ ‚ n  t j j |  j |  j d |  d |  j ƒ} | Sd S(   sÈ   take a CudaNdarray and return a pycuda.gpuarray.GPUArray

    :type x: CudaNdarray
    :param x: The array to transform to pycuda.gpuarray.GPUArray.
    :type copyif: bool
    :param copyif: If False, raise an error if x is not c contiguous.
                   If it is c contiguous, we return a GPUArray that share
                   the same memory region as x.
                   If True, copy x if it is no c contiguous, so the return won't
                   shape the same memory region. If c contiguous, the return
                   will share the same memory region.

                   We need to do this as GPUArray don't fully support strided memory.

    :return type: pycuda.gpuarray.GPUArray
    s<   We can transfer only CudaNdarray to pycuda.gpuarray.GPUArrayi   iÿÿÿÿsE   We were asked to not copy memory, but the memory is not c contiguous.t   baset   gpudataN(   t
   isinstanceR   t   CudaNdarrayt
   ValueErrort   Truet   ranget   ndimt   shapet   _stridest   Falset   copyt   pycudat   gpuarrayt   GPUArrayt   dtypeR   (   t   xt   copyift   sizet   c_contiguoust   it   px(    (    s8   /tmp/pip-build-X4mzal/theano/theano/misc/pycuda_utils.pyt   to_gpuarray	   s"     *c         C` sÐ   t  |  t j j ƒ s$ t d ƒ ‚ n¨ |  j d k rB t d ƒ ‚ nŠ d g } x7 |  j d d d … d  D] } | j | d | ƒ qf Wt | d d d … ƒ } t	 |  j
 ƒ } t j | |  j | |  ƒ } | Sd S(   s©    take a pycuda.gpuarray.GPUArray and make a CudaNdarray that point to its memory

    :note: CudaNdarray support only float32, so only float32 GPUArray are accepted
    s<   We can transfer only pycuda.gpuarray.GPUArray to CudaNdarrayt   float32s    CudaNdarray support only float32i   Niÿÿÿÿ(   R   R   R   R   R   R   R   t   appendt   tuplet   intR   R   t   from_gpu_pointer(   R   t   stridesR   t   ptrt   z(    (    s8   /tmp/pip-build-X4mzal/theano/theano/misc/pycuda_utils.pyt   to_cudandarray2   s    	!(   t
   __future__R    R   R   t   pycuda.gpuarrayR   t   theano.sandboxR   t   cuda_availableR   t   ImportErrorR   R#   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/misc/pycuda_utils.pyt   <module>   s   )