ó
V¬6Mc           @   sÛ   d  Z  d d l m Z d Z d Z d d l Z d d l m Z d d l Z e j	 Z	 e j
 Z
 e j Z e d „  ƒ Z d „  Z d	 „  Z d
 d d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z d S(   s?   Various helpful bits and pieces without much of a common theme.iÿÿÿÿ(   t   divisions$   Copyright (C) 2010 Andreas Kloeckners   
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
N(   t	   decoratorc         G   s    d |  j  t |  ƒ f } y t | | ƒ | SWnk t k
 rh |  | | Œ } t | | i | | 6ƒ | St k
 r› |  | | Œ } | t | | ƒ | <| SXd S(   s»   Provides memoization for things that get created inside
    a context, i.e. mainly programs and kernels. Assumes that
    the first argument of the decorated function is the context.
    s   _ctx_memoize_dic_%s_%xN(   t   __name__t   hasht   getattrt   AttributeErrort   setattrt   KeyError(   t   funct   contextt   argst   dicnamet   result(    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt   context_dependent_memoize.   s    c      
      sO  d d
 ‡  f d †  ƒ  Y} d |  j  k s7 d |  j  k rý i  } xt j ƒ  D]¬ } d |  j  k rl | | d <n  x‡ | j ƒ  D]y ‰  d |  j  k r› ˆ  | d <n  d |  j  k r½ | ˆ  ƒ | d <n  |  j d | j ƒ  d d j d	 „  | j ƒ  Dƒ ƒ ƒ qy WqJ WnN d |  j  k rKx< t j ƒ  D]+ } |  j d t d | ƒ d t	 | ƒ ƒ qWn  d  S(   Nt   ContextGetterc              s)   e  Z d  „  Z ‡  f d †  Z d „  Z RS(   c         S   s   | |  _  d  S(   N(   t   device(   t   selfR   (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt   __init__G   s    c            s   t  j ˆ  g ƒ S(   N(   t   clt   Context(   R   (   R   (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt   __call__J   s    c         S   s   d |  j  S(   Ns   <context getter for %s>(   R   (   R   (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt   __str__M   s    (   R   t
   __module__R   R   R   (    (   R   (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyR   F   s   	R   t
   ctx_gettert   platformt   funcargst   ids   , c         s   s%   |  ] \ } } d  | | f Vq d S(   s   %s=%sN(    (   t   .0t   argt   value(    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pys	   <genexpr>_   s   (    (
   t   funcargnamesR   t   get_platformst   get_devicest   addcallt   copyt   joint	   iteritemst   dictt   str(   t   metafuncR   t   arg_dictR   (    (   R   s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt"   pytest_generate_tests_for_pyopenclE   s(    		c         C   s2  |  d  k r t d ƒ ‚ n  t j |  ƒ }  |  t j k r= d S|  t j k rP d S|  t j k rc d S|  t j k rv d S|  t j k r‰ d S|  t j	 k rœ d S|  t j
 k r¯ d S|  t j k rÂ d	 S|  t j k rÕ d
 S|  t j k rè d S|  t j k rû d S|  t j k rd S|  t j k r!d St d |  ‚ d  S(   Ns   dtype may not be Nonet   longs   unsigned longt   ints   unsigned ints	   short ints   short unsigned ints   signed chars   unsigned chart   boolt   floatt   doubles   complex floats   complex doubles   unable to map dtype '%s'(   t   Nonet
   ValueErrort   numpyt   dtypet   int64t   uint64t   int32t   uint32t   int16t   uint16t   int8t   uint8R,   t   float32t   float64t	   complex64t
   complex128(   R2   (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt   dtype_to_ctypel   s<    t   Argumentc           B   s   e  Z d  d „ Z d „  Z RS(   i   c         C   s(   t  j | ƒ |  _ | |  _ | |  _ d  S(   N(   R1   R2   t   namet
   vector_len(   R   R2   RA   RB   (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyR   •   s    	c         C   s#   d |  j  j |  j |  j |  j f S(   Ns   %s(%r, %s, %d)(   t	   __class__R   RA   R2   RB   (   R   (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt   __repr__š   s
    	(   R   R   R   RD   (    (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyR@   ”   s   t	   VectorArgc           B   s   e  Z d  „  Z RS(   c         C   sD   |  j  d k r d } n t |  j  ƒ } d t |  j ƒ | |  j f S(   Ni   t    s   __global %s%s *%s(   RB   R&   R?   R2   RA   (   R   t   vlen_str(    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt
   declarator¢   s
    	(   R   R   RH   (    (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyRE   ¡   s   t	   ScalarArgc           B   s   e  Z d  „  Z RS(   c         C   sD   |  j  d k r d } n t |  j  ƒ } d t |  j ƒ | |  j f S(   Ni   RF   s   %s%s %s(   RB   R&   R?   R2   RA   (   R   RG   (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyRH   ¬   s    	(   R   R   RH   (    (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyRI   «   s   c         C   sŠ  |  j  d d ƒ j  d d ƒ j  d d ƒ j  d d ƒ j  d d ƒ }  d d  l } | j d ƒ } | j |  ƒ } | d  k r‹ t d	 |  ƒ ‚ n  | j d
 ƒ } | j d ƒ s¾ | j d ƒ d  k	 rÇ t } n t } |  | j	 ƒ   } d j
 | j ƒ  ƒ } | j d ƒ } | j | ƒ } | s)t d | ƒ ‚ n  | j d ƒ } | j d
 ƒ r_t | j d
 ƒ ƒ }	 n d }	 | d k r}t j }
 ný | d k r•t j }
 nå | d" k r­t j }
 nÍ | d# k rÅt j }
 nµ | d$ k rÝt j }
 n | d% k rõt j }
 n… | d& k rt j }
 nm | d' k r%t j }
 nU | d( k r=t j }
 n= | d) k rUt j }
 n% | d* k rmt j }
 n t d! | ‚ | |
 | |	 ƒ S(+   Nt   constRF   t   volatilet   __globalt   __localt
   __constantiÿÿÿÿs,   (\**)\s*([_a-zA-Z0-9]+)(\s*\[[ 0-9]*\])*\s*$s    couldn't parse C declarator '%s'i   i   i   t    s   ^([a-z ]+)([0-9]*)$s.   type '%s' did not match expected shape of typeR-   R.   R+   s
   signed intt   unsigneds   unsigned intR*   s   long ints   unsigned longs   unsigned long intt   shorts	   short ints   unsigned shorts   unsigned short intt   chars   unsigned charR,   s   unknown type '%s'(   s   ints
   signed int(   RP   s   unsigned int(   s   longs   long int(   s   unsigned longs   unsigned long int(   RQ   s	   short int(   s   unsigned shorts   unsigned short int(   s   char(   s   unsigned char(   s   bool(   t   replacet   ret   compilet   searchR/   R0   t   groupRE   RI   t   startR#   t   splitt   matcht   RuntimeErrorR+   R1   R;   R<   R5   R6   R3   R4   R7   R8   R9   R:   R,   (   t   c_argRT   t   decl_ret
   decl_matchRA   t	   arg_classt   tpt   type_ret
   type_matchRB   R2   (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt   parse_c_arg¸   sd    $	          c         C   s1   x* |  j  j d ƒ D] } | d k r t Sq Wt S(   NRO   t   cl_khr_fp64(   t
   extensionsRY   t   Truet   False(   t   devt   ext(    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt   has_double_supportó   s    (    (   t   __doc__t
   __future__R    t   __copyright__t   __license__R1   R   t   pyopenclR   t   PooledBuffert   CLAllocatort
   MemoryPoolR   R)   R?   R@   RE   RI   Rc   Rj   (    (    (    s/   C:\Python27\Lib\site-packages\pyopencl\tools.pyt   <module>   s"   				'	(
	;