
    wg                         d dl Z d dlmZmZ d dlmZ  ed       G d de             Zedk(  r e j                          yy)    N)CUDATestCaseskip_on_cudasim)captured_stdoutz4cudasim doesn't support cuda import at non-top-levelc                   2     e Zd ZdZ fdZ fdZd Z xZS )TestMonteCarloz&
    Test monte-carlo integration
    c                 t    t               | _        | j                  j                          t        |           y N)r   _captured_stdout	__enter__supersetUpself	__class__s    r/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/numba/cuda/tests/doc_examples/test_montecarlo.pyr   zTestMonteCarlo.setUp   s)     / 1'')    c                 \    | j                   j                  d d d        t        |           y r	   )r
   __exit__r   tearDownr   s    r   r   zTestMonteCarlo.tearDown   s&    &&tT48r   c                   	
 dd l }dd lddl m ddlmm
 d}j                  
fd       j                  d        		fd}|j                  d         |d	d
|        |d
d|       j                  j                   |d	d
|      dd       j                  j                   |d
d|      dd       y )Nr   )cuda)create_xoroshiro128p_statesxoroshiro128p_uniform_float32i@B c                     t        |       }j                  d      }||k  r" 
||      }|||z
  z  |z   } 	|      }|| |<   yy)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gidsampyr   funcr   s           r   mc_integrator_kernelz?TestMonteCarlo.test_ex_montecarlo.<locals>.mc_integrator_kernel)   s]     s8D))A,CTz4ZE y945	A
 JC r   c                     | |z   S r	    )abs     r   
sum_reducez5TestMonteCarlo.test_ex_montecarlo.<locals>.sum_reduceA   s    q5Lr   c                     j                  	j                  |d            } |d      } j                  |      ||| |       || z
  |dz
  z  } 
|      |z  S )zq
            approximate the definite integral of `func` from
            `lower_lim` to `upper_lim`
            float32)dtype*   )seedr   )	to_devicezerosforall)r    r!   nsampsr   r   factorr   r   r'   npr,   s         r   mc_integratez7TestMonteCarlo.test_ex_montecarlo.<locals>.mc_integrateE   sq    
 ..&	!BCC4V"EJ 0 ''/ZI
  )+
;Fc?V++r   c                     d| z  S )Ng      ?r)   )xs    r   r&   z/TestMonteCarlo.test_ex_montecarlo.<locals>.func[   s    7Nr   r         gH.?gMbP?)atolgʡE?)
numbanumpyr   numba.cuda.randomr   r   jitreducetestingassert_allclose)r   r>   r5   r8   r   r   r&   r'   r7   r,   r   s       @@@@@@@r   test_ex_montecarloz!TestMonteCarlo.test_ex_montecarlo   s    	
  
	 
	. 
	 
		, 	,, 
	 
	 	Q6"Q6" 	

""Av&e 	# 	
 	

""Av&U 	# 	
r   )__name__
__module____qualname____doc__r   r   rE   __classcell__)r   s   @r   r   r      s    
Q
r   r   __main__)	unittestnumba.cuda.testingr   r   numba.tests.supportr   r   rF   mainr)   r   r   <module>rP      sN     < / GHa
\ a
 Ia
H zHMMO r   