
\K]c           @@ s  d  d l  m Z m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z
 d  d l m Z m Z d  d l m Z d  d l m Z m Z m Z d d l m Z m Z m Z m Z d d l m Z d d	 l m Z d  d
 l m Z e j dF k rd  d l  Z  n  y d  d l! m" Z" e# Z$ Wn e% k
 rOe& Z$ n Xy d  d l! m' Z' e# Z( Wn e% k
 re& Z( n Xy d  d l) Z* e# Z+ Wn e% k
 re& Z+ n Xe j, e( d  Z- e j, e$ d  Z. e( oe' j/ d k Z0 e j, e0 d  Z1 e j, e j dG k d  Z2 e j3 j4 d  Z5 e j3 j4 d  Z6 e j3 j4 d  o`e j7 d  dH k Z8 e j9 dI k Z: e: p{e8 Z; e5 Z< d   Z= e+ rd   Z> n	 d   Z> d   Z? d   Z@ d eA f d     YZB d  eB f d!     YZC d" eB f d#     YZD d$ eB f d%     YZE d& eB f d'     YZF d(   ZG d)   ZH e jI ZJ d* eA f d+     YZK d,   ZL eH eJ e jM  ZN eH eL d-    ZO e5 seH eL d.    ZP eH eL d/    ZQ n  eH eL d0    ZR d1 e f d2     YZS e jT dJ k ZU e j, eU d6  d7 eS f d8     Y ZV eV jW   d9 eS f d:     YZX eX jW   d; e f d<     YZY e d= eY f d>     Y ZZ eZ jW   e e2 d? eY f d@     Y  Z[ e e1 dA eY f dB     Y  Z\ e dC e f dD     Y Z] e^ dE k re j_   n  d S(K   i    (   t   print_functiont   absolute_importN(   t   configt   utils(   t   unittest_support(   t   jitt	   vectorizet   guvectorizei   (   t   temp_directoryt   override_configt   TestCaset   tag(   t   skip_unsupported(   t
   linux_only(   t   queuei   (   t   tbbpool(   t   omppools   OpenMP threadpool requireds   TBB threadpool requiredt   GNUs   GNU OpenMP only testss   Test runs on Python 3 onlyt   wint   darwint   win32i   i   i    c         C@ s   t  j |   | S(   N(   t   npt   ones(   t   nt   v(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   fooI   s    c         C@ sE   t  j t  j |  |  f  t  j |  |  f   } | t  j |   | S(   N(   R   t   dotR   t   arange(   R   R   t   x(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   linalgN   s    0c         C@ s   t  j |   | S(   N(   R   R   (   R   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR   R   s    c         C@ s   |  | S(   N(    (   t   at   b(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt	   ufunc_fooW   s    c         C@ s   |  | | d <d  S(   Ni    (    (   R   R   t   out(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt
   gufunc_foo[   s    t   runnablec           B@ s   e  Z d    Z RS(   c         K@ s   | |  _  d  S(   N(   t   _options(   t   selft   options(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   __init__`   s    (   t   __name__t
   __module__R'   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR#   _   s   t
   jit_runnerc           B@ s   e  Z d    Z RS(   c         C@ sV   t  |  j   t  } d } d } t | |  } | | |  } t j j | |  d  S(   Ni   i
   (   R   R$   R   R   t   testingt   assert_allclose(   R%   t   cfuncR   R   t   expectedt   got(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   __call__f   s    (   R(   R)   R0   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR*   d   s   t   linalg_runnerc           B@ s   e  Z d    Z RS(   c         C@ sV   t  |  j   t  } d } d } t | |  } | | |  } t j j | |  d  S(   Ni   i
   (   R   R$   R   R   R+   R,   (   R%   R-   R   R   R.   R/   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR0   q   s    (   R(   R)   R0   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR1   o   s   t   vectorize_runnerc           B@ s   e  Z d    Z RS(   c         C@ sr   t  d g |  j  t  } t j j d  j t j  } } t | |  } | | |  } t j j | |  d  S(   Ns   (f4, f4)i
   (	   R   R$   R    R   t   randomt   astypet   float32R+   R,   (   R%   R-   R   R   R.   R/   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR0   |   s
    "(   R(   R)   R0   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR2   z   s   t   guvectorize_runnerc           B@ s   e  Z d    Z RS(   c         C@ s{   d g } t  | d |  j  t  } t j j d  j t j  } } t | |  } | | |  } t j j	 | |  d  S(   Ns   (f4, f4, f4[:])s	   (),()->()i
   (
   R   R$   R"   R   R3   R4   R5   R    R+   R,   (   R%   t   sigR-   R   R   R.   R/   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR0      s    	"(   R(   R)   R0   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR6      s   c         K@ s   | j  d  } y] t j d k r. t j   n  x: t t t |   d   D] } t j	 |   } |   qK WWn  t
 k
 r } | j |  n Xd  S(   NR   i   i    g      ?(   i   i    (   t   getR   t	   PYVERSIONt   faulthandlert   enablet   ranget   intt   lenR3   t   choicet   BaseExceptiont   put(   t   fnlistt   kwargst   qt   _t   fnt   e(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   chooser   s    #c         @ s      f d   } | S(   Nc   	   	   @ s      } i | d 6} g  t  d  D]$ }   d t d |  f d |  ^ q# } x | D] } | j   qT Wx | D] } | j   qo W| j   s g  } x& | j   s | j | j t   q Wd } t | d j g  | D] } t	 |  ^ q    n  d  S(   NR   i   t   targett   argsRC   s)   Error(s) occurred in delegated runner:
%ss   
(
   R<   RH   t   startt   joint   emptyt   appendR8   t   Falset   RuntimeErrort   repr(	   RB   RD   t   kwst   it   thst   tht   errorst   _msgR   (   t   parallel_classt
   queue_impl(    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   run_compile   s    	4(    (   RX   RY   RZ   (    (   RX   RY   s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   compile_factory   s    t   _proc_class_implc           B@ s   e  Z d    Z d   Z RS(   c         C@ s   | |  _  d  S(   N(   t   _method(   R%   t   method(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR'      s    c         O@ sE   t  j d k  r t j | |   St j |  j  } | j | |   Sd  S(   Ni   i    (   i   i    (   R   R9   t   multiprocessingt   Processt   get_contextR]   (   R%   RJ   RC   t   ctx(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR0      s    (   R(   R)   R'   R0   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR\      s   	c         C@ sj   t  j d k  r' t |   } t j } n9 |  d k r< d  }  n  t j |   } t |   } | j } | | f S(   Ni   i    t   default(   i   i    (   R   R9   R\   R_   t   Queuet   NoneRa   (   R^   t   procR   Rb   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   _get_mp_classes   s    		t   spawnt   forkt
   forkserverRc   t   TestParallelBackendBasec           B@ s
  e  Z d  Z e d e  e d e d e  e d e d e  e d e  e d e d e  e d e  e d e d d  e d e d d d e  e d e  e d e d d  e d e d d d e  g Z e	 s<e d e d e  e d e d e d e  e d e d e  e d e d e d e  g Z
 e j e
  n  d d g Z e j d k re j d
  e re j d  e j d  qn e j d  i e d e d e	  g d 6e d e d d  g d 6e d e d d  g d 6e d 6Z d d h Z d d  Z RS(   s6   
    Base class for testing the parallel backends
    t   nopythont   cachet   nogilRI   t   parallelt	   threadingR3   i   i    t   multiprocessing_spawnt   multiprocessing_forkt   multiprocessing_forkservert   multiprocessing_defaultt   concurrent_jitt   concurrect_vectorizet   concurrent_guvectorizet   concurrent_mix_uset   ompt   tbbc         C@ sC  t  |  j j  |  _ t d |  j  | d k rA t |  n | d k rZ t |  n | d k rs t |  n | d k r t |  n | d k r t	 |  n | d k r)t
 j d k  r t t	 g } n/ t t g } t r | j t  | j t  n  t j |  x+ | D] } | |  qWn t d
 |   Wd  QXd  S(   Nt	   CACHE_DIRRp   Rr   Rs   Rq   Rt   R3   i   i    s   Unknown parallelism supplied %s(   i   i    (   R   t	   __class__R(   t
   _cache_dirR	   t   thread_implt   fork_proc_implt   forkserver_proc_implt   spawn_proc_implt   default_proc_implR   R9   t   _HAVE_OS_FORKRN   R3   t   shufflet
   ValueError(   R%   RB   t   parallelismt   pst   impl(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyRZ     s0    (   i   i    (   R(   R)   t   __doc__R*   t   TrueR1   R2   R6   t	   all_implst   _parfors_unsupportedt   parfor_implst   extendR   R   R9   RN   R   t   runnerst   safe_backendsRZ   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyRk      s@   
Ry   Rz   t	   workqueues   Threading layer not explicitt   TestParallelBackendc           B@ s   e  Z d  Z e d    Z RS(   sv   These are like the numba.tests.test_threadsafety tests but designed
    instead to torture the parallel backend.
    If a suitable backend is supplied via NUMBA_THREADING_LAYER these tests
    can be run directly. This test class cannot be run using the multiprocessing
    option to the test runner (i.e. `./runtests -m`) as daemon processes cannot
    have children.
    c         C@ s{   xt |  j  D]i } x` |  j j   D]O \ } } d | d | } d   } | | |  } | | _ t |  | |  q  Wq
 Wd  S(   Nt   test_RE   c         @ s      f d   } | S(   Nc         @ sB   t  j   } | j r+ d } |  j |  n |  j   d  d  S(   Ns)   daemonized processes cannot have childrenR   (   R_   t   current_processt   daemont   skipTestRZ   (   R%   t   selfprocRW   (   R   t   p(    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   test_method@  s
    	(    (   R   R   R   (    (   R   R   s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   methgen?  s    (   R   R   t   itemsR(   t   setattr(   t   clsR   t   nameR   t   methnameR   RF   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   generate9  s    	
	(   R(   R)   R   t   classmethodR   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR   ,  s   t   TestSpecificBackendc           B@ sk   e  Z d  Z e Z i e d 6e d 6e j e d  d 6Z	 d   Z
 d   Z e d    Z e d    Z RS(	   s  
    This is quite contrived, for each test in the TestParallelBackend tests it
    generates a test that will run the TestParallelBackend test in a new python
    process with an environment modified to ensure a specific threadsafe backend
    is used. This is with view of testing the backends independently and in an
    isolated manner such that if they hang/crash/have issues, it doesn't kill
    the test suite.
    Rz   Ry   t    R   c         C@ s   t  j | d t  j d t  j d | } t j d | j  } zg | j   | j   \ } } | j d k r t	 d | j | j
   f   n  | j
   | j
   f SWd  | j   Xd	 S(
   Nt   stdoutt   stderrt   envi   g      N@i    s/   process failed with code %s: stderr follows
%s
g     r@(   NN(   t
   subprocesst   Popent   PIPERp   t   Timert   killRK   t   communicatet
   returncodet   AssertionErrort   decodet   cancelRe   (   R%   t   cmdlineR   t   popent   timeoutR!   t   err(    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   run_cmd`  s    			
c         C@ sD   t  j j   } t |  | d <t j d d | g } |  j | |  S(   Nt   NUMBA_THREADING_LAYERs   -ms   numba.runtests(   t   ost   environt   copyt   strt   syst
   executableR   (   R%   t   testt   threading_layert   env_copyR   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   run_test_in_separate_processs  s    c   
      @ s   |  j  } t j } d | d | } d | | | f     f d   } d | |   f }	 t |  |	 t d  | |    d  S(   NR   RE   s   %s.%s.%sc         @ sr   |  j      \ } } |  j r8 t d | | f  n  |  j d |  |  j d | k  |  j d | k  d  S(   Ns   stdout:
 "%s"
 stderr:
 "%s"t   OKt   FAILt   ERROR(   R   t   _DEBUGt   printt   assertInt
   assertTrue(   R%   t   oRG   (   t   backendt   injected_method(    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   test_template  s    	s   test_%s_%s_%st   long_running(   R)   R   R(   R   R   (
   R   R   R   R   t   backend_guardt   themodt   theclsR   R   t   injected_test(    (   R   R   s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   _injecty  s    			c         C@ s   x |  j  j   D] \ } } x |  j D] } x{ |  j j   D]j } | d k rr | d k rr t j j d  rr q< n  | d k r | d k r q< n  |  j | | | |  q< Wq& Wq Wd  S(	   NRr   R3   Ry   t   linuxRp   R   (   Rr   R3   (   Rp   R3   (	   t   backendsR   R   R   t   keysR   t   platformt
   startswithR   (   R   R   R   R   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR     s    (   R(   R)   R   RO   R   t   skip_no_tbbt   skip_no_ompt   unittestt   skipIfR   R   R   R   R   R   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR   Q  s   
		t   ThreadLayerTestHelperc           B@ sA   e  Z d  Z d e j j e  Z d i e d 6Z d d  Z
 RS(   sP   
    Helper class for running an isolated piece of code based on a template
    s   %rs  if 1:
    import sys
    sys.path.insert(0, "%(here)r")
    import multiprocessing
    import numpy as np
    from numba import njit
    import numba
    try:
        import threading_backend_usecases
    except ImportError as e:
        print("DEBUG:", sys.path)
        raise e
    import os

    sigterm_handler = threading_backend_usecases.sigterm_handler
    busy_func = threading_backend_usecases.busy_func

    def the_test():
        %%s

    if __name__ == "__main__":
        the_test()
    t   herec         C@ s   | d  k r. t j j   } t d  | d <n  t j | d t j d t j d | } t j	 d
 | j
  } zQ | j   | j   \ } } | j d k r t d	 | j | j   f   n  Wd  | j   X| j   | j   f S(   NRy   R   R   R   R   i   g      N@i    s/   process failed with code %s: stderr follows
%s
g     r@(   Re   R   R   R   R   R   R   R   Rp   R   R   RK   R   R   R   R   R   (   R%   R   R   R   R   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR     s"    			
 N(   R(   R)   R   R   t   patht   dirnamet   __file__t   _heret   templateRe   R   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR     s   t   TestThreadingLayerSelectionc           B@ sY   e  Z d  Z e Z i e d 6e d 6e j e d  d 6Z	 e
 d    Z e
 d    Z RS(   s@   
    Checks that numba.threading_layer() reports correctly.
    Rz   Ry   R   R   c         @ s?     f d   } d   } t  |  | t d  | |    d  S(   Nc         @ s   d } |  j  |   } t j d | g } t j j   } t    | d <|  j | d | \ } } |  j r| t	 | |  n  d  S(   Ns   if 1:
                X = np.arange(1000000.)
                Y = np.arange(1000000.)
                Z = busy_func(X, Y)
                assert numba.threading_layer() == '%s'
            s   -cR   R   (
   R   R   R   R   R   R   R   R   R   R   (   R%   t   bodyt   runmeR   R   R!   R   (   R   (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR     s    	s    test_threading_layer_selector_%st	   important(   R   R   (   R   R   R   R   R   (    (   R   s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR     s    
	c         C@ s4   x- |  j  j   D] \ } } |  j | |  q Wd  S(   N(   R   R   R   (   R   R   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR     s    (   R(   R)   R   RO   R   R   R   R   R   R   R   R   R   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR     s   
t   TestMiscBackendIssuesc           B@ s2   e  Z d  Z e Z e d    Z e d    Z RS(   sL   
    Checks fixes for the issues with threading backends implementation
    c         C@ s   d } t  j d | g } t j j   } d | d <d | d <|  j | d | \ } } |  j ro t | |  n  |  j d |  d	 S(
   s8   
        Tests that OMP does not overflow stack
        sz  if 1:
            from numba import vectorize, threading_layer
            import numpy as np

            @vectorize(['f4(f4,f4,f4,f4,f4,f4,f4,f4)'], target='parallel')
            def foo(a, b, c, d, e, f, g, h):
                return a+b+c+d+e+f+g+h

            x = np.ones(2**20, np.float32)
            foo(*([x]*8))
            print("@%s@" % threading_layer())
        s   -cRy   R   t   100Kt   OMP_STACKSIZER   s   @omp@N(	   R   R   R   R   R   R   R   R   R   (   R%   R   R   R   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   test_omp_stack_overflow  s    

	c         C@ s   d } t  j d | g } t j j   } d | d <d | d <|  j | d | \ } } |  j ro t | |  n  |  j d |  d	 S(
   sq   
        Tests that TBB works well with single thread
        https://github.com/numba/numba/issues/3440
        s<  if 1:
            from numba import njit, prange, threading_layer

            @njit(parallel=True)
            def foo(n):
                acc = 0
                for i in prange(n):
                    acc += i
                return acc

            foo(100)
            print("@%s@" % threading_layer())
        s   -cRz   R   t   1t   NUMBA_NUM_THREADSR   s   @tbb@N(	   R   R   R   R   R   R   R   R   R   (   R%   R   R   R   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   test_single_thread_tbb%  s    

	(	   R(   R)   R   RO   R   R   R   R   R   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR     s   t   TestForkSafetyIssuesc           B@ sq   e  Z d  Z e Z d   Z d   Z d   Z e e	 d     Z
 e	 d    Z d   Z e e	 d     Z RS(   sV   
    Checks Numba's behaviour in various situations involving GNU OpenMP and fork
    c         C@ s1   d } t  j d | g } |  j |  \ } } d  S(   Nss   if 1:
            from numba.npyufunc import omppool
            assert omppool.openmp_vendor == 'GNU'
            s   -c(   R   R   R   (   R%   R   R   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt!   test_check_threading_layer_is_gnuK  s    c         C@ sn   d } |  j  | } t j d | g } y |  j |  \ } } Wn) t k
 ri } |  j d t |   n Xd S(   s~   
        Whilst normally valid, this actually isn't for Numba invariant of OpenMP
        Checks SIGABRT is received.
        s   if 1:
            X = np.arange(1000000.)
            Y = np.arange(1000000.)
            Z = busy_func(X, Y)
            pid = os.fork()
            if pid  == 0:
                Z = busy_func(X, Y)
            else:
                os.wait()
        s   -cs   failed with code -6N(   R   R   R   R   R   R   R   (   R%   R   R   R   R!   R   RG   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt!   test_par_parent_os_fork_par_childS  s    c         C@ sW   d } |  j  | } t j d | g } |  j |  \ } } |  j rS t | |  n  d S(   su  
        Implicit use of multiprocessing fork context.
        Does this:
        1. Start with OpenMP
        2. Fork to processes using OpenMP (this is invalid)
        3. Joins fork
        4. Check the exception pushed onto the queue that is a result of
           catching SIGTERM coming from the C++ aborting on illegal fork
           pattern for GNU OpenMP
        s  if 1:
            X = np.arange(1000000.)
            Y = np.arange(1000000.)
            q = multiprocessing.Queue()

            # Start OpenMP runtime on parent via parallel function
            Z = busy_func(X, Y, q)

            # fork() underneath with no exec, will abort
            proc = multiprocessing.Process(target = busy_func, args=(X, Y, q))
            proc.start()

            err = q.get()
            assert "Caught SIGTERM" in str(err)
        s   -cN(   R   R   R   R   R   R   (   R%   R   R   R   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt*   test_par_parent_implicit_mp_fork_par_childi  s    	c         C@ sW   d } |  j  | } t j d | g } |  j |  \ } } |  j rS t | |  n  d S(   su  
        Explicit use of multiprocessing fork context.
        Does this:
        1. Start with OpenMP
        2. Fork to processes using OpenMP (this is invalid)
        3. Joins fork
        4. Check the exception pushed onto the queue that is a result of
           catching SIGTERM coming from the C++ aborting on illegal fork
           pattern for GNU OpenMP
        s  if 1:
            X = np.arange(1000000.)
            Y = np.arange(1000000.)
            q = multiprocessing.Queue()

            # Start OpenMP runtime on parent via parallel function
            Z = busy_func(X, Y, q)

            # fork() underneath with no exec, will abort
            ctx = multiprocessing.get_context('fork')
            proc = ctx.Process(target = busy_func, args=(X, Y, q))
            proc.start()
            proc.join()

            err = q.get()
            assert "Caught SIGTERM" in str(err)
        s   -cN(   R   R   R   R   R   R   (   R%   R   R   R   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt*   test_par_parent_explicit_mp_fork_par_child  s    	c         C@ sW   d } |  j  | } t j d | g } |  j |  \ } } |  j rS t | |  n  d S(   s   
        Explicit use of multiprocessing spawn, this is safe.
        Does this:
        1. Start with OpenMP
        2. Spawn to processes using OpenMP
        3. Join spawns
        4. Run some more OpenMP
        s  if 1:
            X = np.arange(1000000.)
            Y = np.arange(1000000.)
            q = multiprocessing.Queue()

            # Start OpenMP runtime and run on parent via parallel function
            Z = busy_func(X, Y, q)
            procs = []
            ctx = multiprocessing.get_context('spawn')
            for x in range(20): # start a lot to try and get overlap
                ## fork() + exec() to run some OpenMP on children
                proc = ctx.Process(target = busy_func, args=(X, Y, q))
                procs.append(proc)
                sys.stdout.flush()
                sys.stderr.flush()
                proc.start()

            [p.join() for p in procs]

            try:
                q.get(False)
            except multiprocessing.queues.Empty:
                pass
            else:
                raise RuntimeError("Queue was not empty")

            # Run some more OpenMP on parent
            Z = busy_func(X, Y, q)
        s   -cN(   R   R   R   R   R   R   (   R%   R   R   R   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt-   test_par_parent_mp_spawn_par_child_par_parent  s    &	c         C@ sW   d } |  j  | } t j d | g } |  j |  \ } } |  j rS t | |  n  d S(   s  
        Implicit use of multiprocessing (will be fork, but cannot declare that
        in Py2.7 as there's no process launch context).
        Does this:
        1. Start with no OpenMP
        2. Fork to processes using OpenMP
        3. Join forks
        4. Run some OpenMP
        s,  if 1:
            X = np.arange(1000000.)
            Y = np.arange(1000000.)
            q = multiprocessing.Queue()

            # this is ok
            procs = []
            for x in range(10):
                # fork() underneath with but no OpenMP in parent, this is ok
                proc = multiprocessing.Process(target = busy_func,
                                               args=(X, Y, q))
                procs.append(proc)
                proc.start()

            [p.join() for p in procs]

            # and this is still ok as the OpenMP happened in forks
            Z = busy_func(X, Y, q)
            try:
                q.get(False)
            except multiprocessing.queues.Empty:
                pass
            else:
                raise RuntimeError("Queue was not empty")
        s   -cN(   R   R   R   R   R   R   (   R%   R   R   R   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt=   test_serial_parent_implicit_mp_fork_par_child_then_par_parent  s    "	c         C@ sW   d } |  j  | } t j d | g } |  j |  \ } } |  j rS t | |  n  d S(   s   
        Explicit use of multiprocessing 'fork'.
        Does this:
        1. Start with no OpenMP
        2. Fork to processes using OpenMP
        3. Join forks
        4. Run some OpenMP
        s'  if 1:
            X = np.arange(1000000.)
            Y = np.arange(1000000.)
            q = multiprocessing.Queue()

            # this is ok
            procs = []
            ctx = multiprocessing.get_context('fork')
            for x in range(10):
                # fork() underneath with but no OpenMP in parent, this is ok
                proc = ctx.Process(target = busy_func, args=(X, Y, q))
                procs.append(proc)
                proc.start()

            [p.join() for p in procs]

            # and this is still ok as the OpenMP happened in forks
            Z = busy_func(X, Y, q)
            try:
                q.get(False)
            except multiprocessing.queues.Empty:
                pass
            else:
                raise RuntimeError("Queue was not empty")
        s   -cN(   R   R   R   R   R   R   (   R%   R   R   R   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt=   test_serial_parent_explicit_mp_fork_par_child_then_par_parent  s    #	(   R(   R)   R   RO   R   R   R   R   R   t   skip_unless_py3R   R   R   R   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR   C  s   			 #-	)t   TestInitSafetyIssuesc           B@ s#   e  Z e Z e e d      Z RS(   c         C@ s   d   } t  j j t  j j t  d  } t j | g } | |  \ } } |  j d |  |  j r t	 d |  t	 d |  n  d  S(   Nc         S@ s   t  j |  d t  j d t  j } t j d | j  } zQ | j   | j   \ } } | j d k r t	 d | j | j
   f   n  Wd  | j   X| j
   | j
   f S(   NR   R   i   g      N@i    s/   process failed with code %s: stderr follows
%s
g     r@(   R   R   R   Rp   R   R   RK   R   R   R   R   R   (   R   R   R   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR   9  s    	
 s   orphaned_semaphore_usecase.pys   leaked semaphores   OUT:s   ERR:(
   R   R   RL   R   R   R   R   t   assertNotInR   R   (   R%   R   t	   test_fileR   R!   R   (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   test_orphaned_semaphore3  s    			(   R(   R)   RO   R   R   R   R   (    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyR   .  s   t   __main__(   i   i    (   i   i    (   i   i   I       (   Ry   Rz   R   (`   t
   __future__R    R   Rp   R_   R3   R   R   R   t   numpyR   t   numbaR   R   R   R   R   R   R   t   supportR   R	   R
   R   t   test_parforsR   t   parfors_skip_unsupportedR   t   numba.six.movesR   t   t_queueR9   R:   t   numba.npyufuncR   R   t   _HAVE_TBB_POOLt   ImportErrorRO   R   t   _HAVE_OMP_POOLt   scipy.linalg.cython_lapackt   scipyt   _HAVE_LAPACKt
   skipUnlessR   R   t   openmp_vendort   _gnuompt   skip_unless_gnu_ompR   R   R   t   _windowst   _osxt   version_infot   _windows_py27t   maxsizet   _32bitR   R   R   R   R    R"   t   objectR#   R*   R1   R2   R6   RH   R[   t   Threadt   _thread_classR\   Rg   Rd   R~   R   R   R   R   Rk   t   THREADING_LAYERt   _specific_backendsR   R   R   R   R   R   R   R   R(   t   main(    (    (    s@   lib/python2.7/site-packages/numba/tests/test_parallel_backend.pyt   <module>   s   "





					
				R!
P
6%
>)