B
    d	                 @   s&  d Z ddlZddlZddlZddlmZ dZdZej	ddsLej	ddrRdZn ej
dkrbdZnddlZdZdZej	ddreejd Zed	e  ndererd
d Zndd ZedZedZedZedZedZedZedZedZedZdd Zdd ZG dd deZdS )z
This module defines optimization flags and determines hardware features that some
other modules and packages may use in addition to some optimized utilities.
    N)OrderedDictFLEVEL2_CACHE_SIZEZ
NO_GETCONFdarwinTz0opt: using LEVEL2_CACHE_SIZE %d from environmentc             C   s   t td| gS )Ngetconf)int
subprocesscheck_output)confvar r
   V/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/opt.pyr   ,   s    r   c             C   s   t d|  }t|d S )Nzgetconf    )commandsgetstatusoutputr   )r	   Zretlistr
   r
   r   r   /   s    LEVEL1_DCACHE_SIZELEVEL1_DCACHE_ASSOCLEVEL1_DCACHE_LINESIZELEVEL2_CACHE_ASSOCLEVEL2_CACHE_LINESIZELEVEL3_CACHE_SIZELEVEL3_CACHE_ASSOCLEVEL3_CACHE_LINESIZEc             C   s*   |  d}|jddd |jddd dS )z
    Adds the options used to specify optimization-specific options.

    Parameters
    ----------
    parser : object
        OptionParser instance
    z4Options for selecting optimization-specific settingsz--cpu-affinityzz
                    A set of CPUs on which to run, specified in a format suitable
                    to pass to taskset.)helpz--cpu-affinity-from-envz
                    The name of an enivornment variable containing a set
                    of CPUs on which to run,  specified in a format suitable
                    to pass to taskset.N)add_argument_groupadd_argument)parserZoptimization_groupr
   r
   r    insert_optimization_option_group>   s
    	
r   c             C   s   d}| j dk	r~| jdk	r,td td tj| j }|dkr\td| j   td |dkr~td| j   td |dkr| j}|dk	rd|t	 f }t
|}|dkrtd	||f  td td
|  dS )a:  Parses the CLI options, verifies that they are consistent and
    reasonable, and acts on them if they are

    Parameters
    ----------
    opt : object
        Result of parsing the CLI with OptionParser, or any object with the
        required attributes
    parser : object
        OptionParser instance.
    Nz9Both --cpu_affinity_from_env and --cpu_affinity specifiedr   zTCPU affinity requested from environment variable %s but this variable is not defined zNCPU affinity requested from environment variable %s but this variable is emptyztaskset -pc %s %dr   z/taskset command <%s> failed with return code %dzPinned to CPUs %s )Zcpu_affinity_from_envZcpu_affinityloggingerrorsysexitosenvirongetgetpidsysteminfo)optr   Zrequested_cpuscommandretcoder
   r
   r   verify_optimization_optionsS   s0    









r*   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	LimitedSizeDictz" Fixed sized dict for FIFO cachingc             O   s,   | dd | _tj| f|| |   d S )N
size_limit)popr,   r   __init___check_size_limit)selfargskwdsr
   r
   r   r.      s    zLimitedSizeDict.__init__c             C   s   t | || |   d S )N)r   __setitem__r/   )r0   keyvaluer
   r
   r   r3      s    zLimitedSizeDict.__setitem__c             C   s.   | j d k	r*xt| | j kr(| jdd qW d S )NF)last)r,   lenpopitem)r0   r
   r
   r   r/      s    
z!LimitedSizeDict._check_size_limitN)__name__
__module____qualname____doc__r.   r3   r/   r
   r
   r
   r   r+      s   r+   )r<   r!   r   r   collectionsr   Z_USE_SUBPROCESSZHAVE_GETCONFr"   r#   platformr   r   r   r&   r   r   r   r   r   r   r   r   r   r   r*   r+   r
   r
   r
   r   <module>   s>   

/