U
    f»{j§  ã                   @   s„   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZmZmZ eddddœd	d
„ƒZedƒddd„ƒZdd„ ZdS )zParallel util function.é    Né   )Ú
get_config)ÚloggerÚverboseÚwarnÚProgressBarÚ_validate_typeÚ_check_optionÚ_ensure_intÚ
deprecatedÚautoÚn_jobs)Úmax_jobsr   c                   s`  t jtjk}t|dƒ |dkr\zddlm}	m}
 W n( tk
rZ   |dk	rRt	dƒ d}Y nX |dkrrd}| }t
‰ nÂtddƒ}t|tƒr˜|dkr˜td	dƒ}|dk	r²|dkr²t  d
¡ d|rÄˆdkrÄdndi}||d< ||d< |dkræd}||d< ||d< |	|f|Ž‰ tˆ jƒ}|dk	�r,t|tt|dƒdƒƒ}|
| ƒ}ˆdk	�rR‡ ‡fdd„}|}nˆ }|||fS )a’  Return parallel instance with delayed function.

    Util function to use joblib only if available

    Parameters
    ----------
    func : callable
        A function.
    %(n_jobs)s
    max_nbytes : int, str, or None
        Threshold on the minimum size of arrays passed to the workers that
        triggers automated memory mapping. Can be an int in Bytes,
        or a human-readable string, e.g., '1M' for 1 megabyte.
        Use None to disable memmaping of large arrays. Use 'auto' to
        use the value set using mne.set_memmap_min_size.
    pre_dispatch : int, or str, optional
        See :class:`joblib.Parallel`.
    total : int | None
        If int, use a progress bar to display the progress of dispatched
        jobs. This should only be used when directly iterating, not when
        using ``split_list`` or :func:`np.array_split`.
        If None (default), do not add a progress bar.
    prefer : str | None
        If str, can be "processes" or "threads". See :class:`joblib.Parallel`.

        .. versionadded:: 0.18
    max_jobs : int | None
        The upper limit of jobs to use. This is useful when you know ahead
        of a the maximum number of calls into :class:`joblib.Parallel` that
        you will possibly want or need, and the returned ``n_jobs`` should not
        exceed this value regardless of how many jobs the user requests.
    %(verbose)s INFO or DEBUG
        will print parallel status, others will not.

    Returns
    -------
    parallel: instance of joblib.Parallel or list
        The parallel object.
    my_func: callable
        ``func`` if not parallel or delayed(func).
    n_jobs: int
        Number of jobs >= 1.
    ©zint-likeNr   r   )ÚParallelÚdelayedNz-joblib not installed. Cannot run in parallel.ZMNE_CACHE_DIRr   ZMNE_MEMMAP_MIN_SIZEzâjoblib supports memapping pool but "MNE_CACHE_DIR" is not set in MNE-Python config. To enable it, use, e.g., mne.set_cache_dir('/tmp/shm'). This will store temporary files under /dev/shm and can result in large memory savings.r   é   Úpre_dispatchÚpreferZtemp_folderÚ
max_nbytesr   c                    s   ˆ t | ˆd�ƒS )N)ÚiterableZ	max_value)r   )Zop_iter©ÚparallelÚtotal© úK/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/parallel.pyÚparallel_progressi   s    z(parallel_func.<locals>.parallel_progress)r   ÚlevelÚloggingÚINFOr   Zjoblibr   r   ÚImportErrorr   Úlistr   Ú
isinstanceÚstrÚinfoÚ_check_n_jobsr   ÚminÚmaxr
   )Úfuncr   r   r   r   r   r   r   Zshould_printr   r   Zmy_funcÚ	cache_dirÚkwargsr   Zparallel_outr   r   r   Úparallel_func   sJ    .



ÿ


r+   zQcheck_n_jobs is deprecated and will be removed in 1.1, use parallel_func directlyFc                 C   sB   d}|r|d }t | |dƒ t| tƒr:td| ddd� dS t| ƒS )aJ  Check n_jobs in particular for negative values.

    Parameters
    ----------
    n_jobs : int
        The number of jobs.
    allow_cuda : bool
        Allow n_jobs to be 'cuda'. Default: False.

    Returns
    -------
    n_jobs : int
        The checked number of jobs. Always positive (or 'cuda' if
        applicable).
    r   )r#   r   )Úcudazwhen str)Úextrar,   )r   r"   r#   r	   r%   )r   Z
allow_cudaÚtypesr   r   r   Úcheck_n_jobsr   s    
r/   c                 C   s‚   t | ddd�} t dd¡ ¡ dkr:| dkr:d} t d¡ nD| d	kr~t ¡ }| }t||  d |ƒ} | d	kr~t	d
|› d|› d�ƒ‚| S )Nr   zan int or None)Zmust_beZMNE_FORCE_SERIALÚ )ÚtrueÚ1r   z;... MNE_FORCE_SERIAL set. Processing in forced serial mode.r   z$If n_jobs has a non-positive value (z7) it must not be less than the number of CPUs present (ú))
r
   ÚosÚgetenvÚlowerr   r$   ÚmultiprocessingÚ	cpu_countr&   Ú
ValueError)r   Zn_coresZn_jobs_origr   r   r   r%   �   s    ÿÿr%   )r   r   NN)F)Ú__doc__r   r7   r4   r0   r   Úutilsr   r   r   r   r   r	   r
   r   r+   r/   r%   r   r   r   r   Ú<module>   s    (    ÿ ÿa