U
    ?h                     @   sP   d dl Z d dlmZmZ d dlmZ edG dd deZedkrLe   dS )    N)CUDATestCaseskip_on_cudasim)captured_stdoutz4cudasim doesn't support cuda import at non-top-levelc                       s4   e Zd ZdZ fddZ fddZdd Z  ZS )TestMonteCarloz&
    Test monte-carlo integration
    c                    s    t  | _| j  t   d S N)r   _captured_stdout	__enter__supersetUpself	__class__ _/var/www/html/venv/lib/python3.8/site-packages/numba/cuda/tests/doc_examples/test_montecarlo.pyr
      s    
zTestMonteCarlo.setUpc                    s   | j d d d  t   d S r   )r   __exit__r	   tearDownr   r   r   r   r      s    zTestMonteCarlo.tearDownc                    s   dd l }dd lddl m ddlm m d}jfddjdd  fd	d
}|jdd |dd| |dd| jj	|dd|ddd jj	|dd|ddd d S )Nr   )cuda)create_xoroshiro128p_statesxoroshiro128p_uniform_float32i@B c                    sH   t | } d}||k rD||}|||  | }|}|| |< dS )z
            kernel to draw random samples and evaluate the function to
            be integrated at those sample values
               N)lengrid)out
rng_states	lower_lim	upper_limsizegidZsampy)r   funcr   r   r   mc_integrator_kernel)   s    

z?TestMonteCarlo.test_ex_montecarlo.<locals>.mc_integrator_kernelc                 S   s   | | S r   r   )abr   r   r   
sum_reduceA   s    z5TestMonteCarlo.test_ex_montecarlo.<locals>.sum_reducec                    sP    j|dd} |dd}|||| | ||  |d  }|| S )zq
            approximate the definite integral of `func` from
            `lower_lim` to `upper_lim`
            Zfloat32)Zdtype*   )seedr   )Z	to_deviceZzerosforall)r   r   nsampsr   r   factor)r   r   r!   npr$   r   r   mc_integrateE   s       z7TestMonteCarlo.test_ex_montecarlo.<locals>.mc_integratec                 S   s   d|  S )Ng      ?r   )xr   r   r   r    [   s    z/TestMonteCarlo.test_ex_montecarlo.<locals>.funcr         gH.?gMbP?)ZatolgʡE?)
numbanumpyr   Znumba.cuda.randomr   r   ZjitreduceZtestingZassert_allclose)r   r/   r(   r+   r   )r   r   r    r!   r*   r$   r   r   test_ex_montecarlo   s0    


  
  z!TestMonteCarlo.test_ex_montecarlo)__name__
__module____qualname____doc__r
   r   r2   __classcell__r   r   r   r   r      s   r   __main__)	ZunittestZnumba.cuda.testingr   r   Znumba.tests.supportr   r   r3   mainr   r   r   r   <module>   s   d