B
    d                 @   sJ   d Z ddlZdgZG dd dZdd Zdd	 Zd
d ZG dd dZdS )z
The wrapper around multiprocessing pool that can be helpful
with dynesty since it avoids some overhead that one would get
with standard pool
    NPoolc               @   s   e Zd ZdZdS )FunctionCachezU
    Singleton class to cache the functions and optional arguments between calls
    N)__name__
__module____qualname____doc__ r   r   Y/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/dynesty/pool.pyr      s   r   c             C   s(   | t _|t _|t _|t _|t _|t _dS )zi
    Initialized function used to initialize the
    singleton object inside each worker of the pool
    N)r   loglikeprior_transform	logl_argslogl_kwargsptform_argsptform_kwargs)r
   r   r   r   r   r   r   r   r	   initializer   s    r   c             C   s   t j| ft jt jS )z"
    Likelihood function call
    )r   r
   r   r   )xr   r   r	   loglike_cache"   s    r   c             C   s   t j| ft jt jS )z
    Prior transform call
    )r   r   r   r   )r   r   r   r	   prior_transform_cache*   s    r   c               @   sN   e Zd ZdZdddZdd Zdd Zd	d
 Zedd Z	dd Z
dd ZdS )r   a9  
    The multiprocessing pool wrapper class
    It is intended to be used as a context manager for dynesty sampler only.

    Parameters
    ----------
    njobs: int
        The number of multiprocessing jobs/processes
    loglikelihood: function
        ln(likelihood) function
    prior_transform: function
        Function transforming from a unit cube to the parameter
        space of interest according to the prior
    logl_args: tuple(optional)
        The optional arguments to be added to the likelihood
        function call
    logl_kwargs: tuple(optional)
        The optional keywords to be added to the likelihood
        function call
    ptform_args: tuple(optional)
        The optional arguments to be added to the prior transform
        function call
    ptform_kwargs: tuple(optional)
        The optional keywords to be added to the prior transform
        function call

    Attributes
    ----------
    loglikelihood: function
        ln(likelihood) function
    prior_transform: function
        Function transforming from a unit cube to the parameter
        space of interest according to the prior

    Examples
    --------
    To use the dynest pool you have to use it with the context manager::

        with dynesty.pool.Pool(16, like, prior_transform) as pool:
            dns = DynamicNestedSampler(pool.like, pool.prior_transform, ndim,
                                     pool =pool)

    Also note that you have to provide the .like/.prior_transform attributes
    from the pool object to the Nested samper rather than your original
    functions!
    Nc             C   s@   || _ || _|| _|| _|| _|| _|| _t| _t	| _
d | _d S )N)r   r   r   r   njobs	loglike_0prior_transform_0r   r
   r   r   pool)selfr   r
   r   r   r   r   r   r   r   r	   __init__b   s    zPool.__init__c             C   sJ   | j | j| jpd| jpi | jp d| jp(i f}t| jt	|| _
t	|  | S )z#
        Activate the pool
        r   )r   r   r   r   r   r   mpr   r   r   r   )r   Zinitargsr   r   r	   	__enter__u   s    zPool.__enter__c             C   s   | j ||S )z| Apply the function F to the list x

        Parameters
        ==========

        F: function
        x: iterable
        )r   map)r   Fr   r   r   r	   r      s    	zPool.mapc             C   sJ   y| j   W n   Y nX yt`t`t`t`t`t`W n   Y nX d S )N)	r   	terminater   r
   r   r   r   r   r   )r   exc_typeexc_valexc_tbr   r   r	   __exit__   s    zPool.__exit__c             C   s   | j S )z<
        Return the number of processes in the pool
        )r   )r   r   r   r	   size   s    z	Pool.sizec             C   s   | j   d S )N)r   close)r   r   r   r	   r$      s    z
Pool.closec             C   s   | j   d S )N)r   join)r   r   r   r	   r%      s    z	Pool.join)NNNN)r   r   r   r   r   r   r   r"   propertyr#   r$   r%   r   r   r   r	   r   2   s   .   
)	r   multiprocessingr   __all__r   r   r   r   r   r   r   r   r	   <module>   s   