o
    8ήc	S                     @   s  d Z ddlZddlmZ ddlZddlmZmZm	Z	m
Z
 ddlZddlmZmZmZmZmZmZmZmZ ddlmZ ddlmZmZ G dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZ G dd deZ!e"dkre#  dS dS )z
Tests for sub-components of parfors.
These tests are aimed to produce a good-enough coverage of parfor passes
so that refactoring on these passes are easier with faster testing turnaround.
    Nreduce)njittypeofprangepndindex)typingrewritestyped_passesuntyped_passesinline_closurecallcompilercpuerrors)
cpu_target)TestCaseis_parfors_unsupportedc                   @   s   e Zd Zdd ZdS )
MyPipelinec                 C   sN   t  | _|| j_|| j_|| j_|| j_d | j_d | j_d | j_	i | j_
d S N)r   	StateDictstate	typingctx	targetctxargsfunc_irtypemapreturn_type	calltypesmetadata)selfr   r   r   test_ir r!   F/tmp/pip-target-vg8gfxp4/lib/python/numba/tests/test_parfors_passes.py__init__   s   
zMyPipeline.__init__N)__name__
__module____qualname__r#   r!   r!   r!   r"   r      s    r   c                   @   sT   e Zd ZedddZedd ZedddZdd	 Zd
d Zdd Z	dd Z
dS )BaseTestNc                 C   sZ  t  }t|}t|}td}t||||}t	|| |
  |
  tj|jj|dd}	|	  tjd|j t |j t|jj|jj|jj|jjd \|j_|j_|j_}
t |j tj j!" }tj j!j#|jj|jj|jj|jj|jj||j$|d}|  tjd|j ||||fW  d    S 1 sw   Y  d S )NT)typedzbefore-inference)swappedreplace_functions_mapzafter-inference)%r   Contextr   
CPUContextr   run_frontendParallelOptionsr   r   nested_contextrefreshr   InlineClosureCallPassr   r   runr	   rewrite_registryapplyr   ReconstructSSArun_passr
   type_inference_stager   r   r   r   r   r   PreLowerStripPhisnumbaparforsparforParforDiagnosticsPreParforPassreplaced_fns)cls	test_funcr   swap_mapr   r   r    optionstpinline_pass_diagnosticspreparfor_passr!   r!   r"   _run_parfor)   sN   





$zBaseTest._run_parforc           
      C   s~   |  ||\}}}}t }tjjj|jj|jj	|jj
|jj|jj|jj|||jj|d
}|  | |}	|	|jj |	S )N)rF   )rH   r   Flagsr9   r:   r;   
ParforPassr   r   r   r   r   r   r   r   _pre_runsub_pass_classr2   blocks)
r?   r@   r   rC   rB   rF   rE   flagsparfor_passsub_passr!   r!   r"   run_parfor_sub_pass^   s$   
zBaseTest.run_parfor_sub_passc                 C   s   |  |||\}}}}|S r   )rH   )r?   r@   r   rA   rC   rB   rF   rG   r!   r!   r"   run_parfor_pre_passv   s   zBaseTest.run_parfor_pre_passc                 O   s2   t dd|}||i |}||i |}||fS )NT)parallel)r   )r   funcr   kwargscfuncexpectgotr!   r!   r"   _run_parallel}   s   zBaseTest._run_parallelc                 O   s4   t rd S | j|g|R i |\}}| || d S r   )r   rY   assertPreciseEqualr   rT   r   rU   rW   rX   r!   r!   r"   run_parallel   s   zBaseTest.run_parallelc                 O   sT   t rd S | j|g|R i |\}}| |tj | |tj | |j|j d S r   )r   rY   assertIsInstancenpndarrayassertEqualshaper[   r!   r!   r"   run_parallel_check_output_array   s   z(BaseTest.run_parallel_check_output_arrayc                 C   s$   |D ]}|  |d tjjj qd S )Nnew)r]   r9   r:   r;   Parfor)r   recordsrecr!   r!   r"   check_records   s   zBaseTest.check_recordsr   )r$   r%   r&   classmethodrH   rQ   rR   rY   r\   rb   rg   r!   r!   r!   r"   r'   (   s    4

r'   c                   @   s>   e Zd ZejjjZdd Zdd Z	dd Z
dd Zd	d
 ZdS )TestConvertSetItemPassc                 C   X   dd }|  |d}| t|jd |j\}| |d d | |j | | d S )Nc                  S   s   d} t | }d|d d < |S )N
      r^   onesnar!   r!   r"   	test_impl      
zATestConvertSetItemPass.test_setitem_full_slice.<locals>.test_implr!      reasonslicerQ   r`   len	rewrittenrg   r\   r   rr   rP   recordr!   r!   r"   test_setitem_full_slice      z.TestConvertSetItemPass.test_setitem_full_slicec                 C   rj   )Nc                  S   s   d} t | }d|d d< |S )Nrk   rl      rm   ro   r!   r!   r"   rr      rs   zGTestConvertSetItemPass.test_setitem_slice_stop_bound.<locals>.test_implr!   rt   ru   rv   rw   rz   r!   r!   r"   test_setitem_slice_stop_bound   r}   z4TestConvertSetItemPass.test_setitem_slice_stop_boundc                 C   rj   )Nc                  S   s   d} t | }d|dd < |S )Nrk   rl      rm   ro   r!   r!   r"   rr      rs   zHTestConvertSetItemPass.test_setitem_slice_start_bound.<locals>.test_implr!   rt   ru   rv   rw   rz   r!   r!   r"   test_setitem_slice_start_bound   r}   z5TestConvertSetItemPass.test_setitem_slice_start_boundc                 C   rj   )Nc                  S   s*   d} t | }t j|t jd}d||< |S )Nrk   dtyperl   r^   rn   	ones_likebool_)rp   rq   br!   r!   r"   rr      s
   
zGTestConvertSetItemPass.test_setitem_gather_if_scalar.<locals>.test_implr!   rt   ru   masked_assign_broadcast_scalarrw   rz   r!   r!   r"   test_setitem_gather_if_scalar      z4TestConvertSetItemPass.test_setitem_gather_if_scalarc                 C   rj   )Nc                  S   s8   d} t | }t j|t jd}t |}|| ||< |S )Nrk   r   r   )rp   rq   r   cr!   r!   r"   rr      s   

zFTestConvertSetItemPass.test_setitem_gather_if_array.<locals>.test_implr!   rt   ru   masked_assign_arrayrw   rz   r!   r!   r"   test_setitem_gather_if_array      z3TestConvertSetItemPass.test_setitem_gather_if_arrayN)r$   r%   r&   r9   r:   r;   ConvertSetItemPassrL   r|   r   r   r   r   r!   r!   r!   r"   ri      s    
ri   c                   @   sV   e Zd ZejjjZdd Zdd Z	dd Z
dd Zd	d
 Zdd Zdd Zdd ZdS )TestConvertNumpyPassc                    \    fdd}|  |d}| t|jd |j\}| |d d | |j | | d S )Nc                        d}  | }|S Nrk   r!   ro   fnr!   r"   rr         z>TestConvertNumpyPass.check_numpy_allocators.<locals>.test_implr!   rt   ru   numpy_allocatorrw   r   r   rr   rP   r{   r!   r   r"   check_numpy_allocators      z+TestConvertNumpyPass.check_numpy_allocatorsc                    r   )Nc                     r   r   r!   ro   r   r!   r"   rr      r   z:TestConvertNumpyPass.check_numpy_random.<locals>.test_implr!   rt   ru   r   )rQ   r`   rx   ry   rg   rb   r   r!   r   r"   check_numpy_random   r   z'TestConvertNumpyPass.check_numpy_randomc              	   C   sP   t jt jg}|D ]}| |j | | W d    n1 s w   Y  qd S r   )r^   rn   zerossubTestr$   r   r   fnsr   r!   r!   r"   test_numpy_allocators
  s   z*TestConvertNumpyPass.test_numpy_allocatorsc              	   C   sN   t jjg}|D ]}| |j | | W d    n1 sw   Y  qd S r   )r^   randomr   r$   r   r   r!   r!   r"   test_numpy_random  s   
z&TestConvertNumpyPass.test_numpy_randomc                 C      dd }t d }}||f}dd |D }| ||}| t|jd |j\}| |d d | |j | j|g|R   d S )	Nc                 S   s   | | S r   r!   rq   r   r!   r!   r"   rr     s   z<TestConvertNumpyPass.test_numpy_arrayexpr.<locals>.test_implrk   c                 S      g | ]}t |qS r!   r   .0xr!   r!   r"   
<listcomp>      z=TestConvertNumpyPass.test_numpy_arrayexpr.<locals>.<listcomp>rt   ru   	arrayexprr^   rn   rQ   r`   rx   ry   rg   r\   r   rr   rq   r   r   argtypesrP   r{   r!   r!   r"   test_numpy_arrayexpr     z)TestConvertNumpyPass.test_numpy_arrayexprc                 C   r   )	Nc                 S   s    t |  t dt |  S Nrt   )r^   sinfloat64sqrtr   r!   r!   r"   rr   (  s    zBTestConvertNumpyPass.test_numpy_arrayexpr_ufunc.<locals>.test_implrk   c                 S   r   r!   r   r   r!   r!   r"   r   .  r   zCTestConvertNumpyPass.test_numpy_arrayexpr_ufunc.<locals>.<listcomp>rt   ru   r   r   r   r!   r!   r"   test_numpy_arrayexpr_ufunc'  r   z/TestConvertNumpyPass.test_numpy_arrayexpr_ufuncc                 C   s   dd }t d}t d}||f}dd |D }| ||}| t|jd |j\}| |d d	 | |j | j|g|R   d S )
Nc                 S   s   | | t d S r   )r^   arrayr   r!   r!   r"   rr   9  s   zFTestConvertNumpyPass.test_numpy_arrayexpr_boardcast.<locals>.test_implrk   )   rk   c                 S   r   r!   r   r   r!   r!   r"   r   @  r   zGTestConvertNumpyPass.test_numpy_arrayexpr_boardcast.<locals>.<listcomp>rt   ru   r   r   r   r!   r!   r"   test_numpy_arrayexpr_boardcast8  s   

z3TestConvertNumpyPass.test_numpy_arrayexpr_boardcastc                 C   s   dd }t d}t d}||f}dd |D }| ||}| t|jd |j\}| |d d | |j | j|g|R   d S )	Nc                 S   s   |  d| j} | | S r   )reshapesizer   r!   r!   r"   rr   K  s   zETestConvertNumpyPass.test_numpy_arrayexpr_reshaped.<locals>.test_implrk   c                 S   r   r!   r   r   r!   r!   r"   r   S  r   zFTestConvertNumpyPass.test_numpy_arrayexpr_reshaped.<locals>.<listcomp>rt   ru   r   r   r   r!   r!   r"   test_numpy_arrayexpr_reshapedJ  s   

z2TestConvertNumpyPass.test_numpy_arrayexpr_reshapedN)r$   r%   r&   r9   r:   r;   ConvertNumpyPassrL   r   r   r   r   r   r   r   r   r!   r!   r!   r"   r      s    
r   c                   @   s&   e Zd ZejjjZdd Zdd Z	dS )TestConvertReducePassc                 C   ~   dd }t d}|f}dd |D }| ||}| t|jd |j\}| |d d | |j | j|g|R   d S )	Nc                 S   s   t dd | dS )Nc                 S   
   t | |S r   maxr   yr!   r!   r"   <lambda>c     
 zPTestConvertReducePass.test_reduce_max_basic.<locals>.test_impl.<locals>.<lambda>        r   arrr!   r!   r"   rr   b  s   z>TestConvertReducePass.test_reduce_max_basic.<locals>.test_implrk   c                 S   r   r!   r   r   r!   r!   r"   r   g  r   z?TestConvertReducePass.test_reduce_max_basic.<locals>.<listcomp>rt   ru   r   r   r   rr   r   r   r   rP   r{   r!   r!   r"   test_reduce_max_basica     
z+TestConvertReducePass.test_reduce_max_basicc                 C   r   )	Nc                 S   s   t dd | | dk dS )Nc                 S   r   r   r   r   r!   r!   r"   r   s  r   zQTestConvertReducePass.test_reduce_max_masked.<locals>.test_impl.<locals>.<lambda>r~   r   r   r   r!   r!   r"   rr   r  s   z?TestConvertReducePass.test_reduce_max_masked.<locals>.test_implrk   c                 S   r   r!   r   r   r!   r!   r"   r   w  r   z@TestConvertReducePass.test_reduce_max_masked.<locals>.<listcomp>rt   ru   r   r   r   r!   r!   r"   test_reduce_max_maskedq  r   z,TestConvertReducePass.test_reduce_max_maskedN)
r$   r%   r&   r9   r:   r;   ConvertReducePassrL   r   r   r!   r!   r!   r"   r   ^  s    
r   c                   @   s   e Zd ZejjjZdd Zdd Z	dd Z
dd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )TestConvertLoopPassc                 C   rj   )Nc                  S   s"   d} d}t | D ]}||7 }q|S N   r   )r   )rp   r   ir!   r!   r"   rr     s
   
z@TestConvertLoopPass.test_prange_reduce_simple.<locals>.test_implr!   rt   ru   looprw   rz   r!   r!   r"   test_prange_reduce_simple  r   z-TestConvertLoopPass.test_prange_reduce_simplec                 C   rj   )Nc                  S   s0   d} t | }t| D ]
}||  |7  < q|S Nr   r^   rn   r   rp   r   r   r!   r!   r"   rr     s
   
z=TestConvertLoopPass.test_prange_map_simple.<locals>.test_implr!   rt   ru   r   rw   rz   r!   r!   r"   test_prange_map_simple  r   z*TestConvertLoopPass.test_prange_map_simplec                 C   rj   )Nc                  S   s2   d} t | }td| D ]
}||  |7  < q|S )Nr   r   r   r   r!   r!   r"   rr     s
   
z;TestConvertLoopPass.test_prange_two_args.<locals>.test_implr!   rt   ru   r   rw   rz   r!   r!   r"   test_prange_two_args  r   z(TestConvertLoopPass.test_prange_two_argsc                 C   V   dd }|  tj}| |d W d    n1 sw   Y  | dt|j d S )Nc                  S   s4   d} t | }td| dD ]
}||  |7  < q|S )Nr   r      r   r   r!   r!   r"   rr     s
   
z=TestConvertLoopPass.test_prange_three_args.<locals>.test_implr!   z4Only constant step size of 1 is supported for prangeassertRaisesr   UnsupportedRewriteErrorrQ   assertInstr	exceptionr   rr   raisesr!   r!   r"   test_prange_three_args  s   z*TestConvertLoopPass.test_prange_three_argsc                 C   rj   )Nc                  S   sN   d} t | | f}t| D ]}t|D ]}|||f  |||   7  < qq|S r   )r^   rn   r   rangerp   r   r   jr!   r!   r"   rr        zATestConvertLoopPass.test_prange_map_inner_loop.<locals>.test_implr!   rt   ru   r   rw   rz   r!   r!   r"   test_prange_map_inner_loop  r   z.TestConvertLoopPass.test_prange_map_inner_loopc                 C   s\   dd }|  |d}| t|jd | |j |jD ]
}| |d d q| | d S )Nc                  S   sN   d} t | | f}t| D ]}t|D ]}|||f  |||   7  < qq|S r   r   r   r!   r!   r"   rr     r   zDTestConvertLoopPass.test_prange_map_nested_prange.<locals>.test_implr!   r   ru   r   rw   rz   r!   r!   r"   test_prange_map_nested_prange  s   
z1TestConvertLoopPass.test_prange_map_nested_prangec                 C   X   dd }|  |d}| t|jd | |j |j\}| |d d | | d S )Nc                  S   s@   d} t | }t| D ]}|||d  }|d  d7  < q|S )Nr   rt   r!   r   )rp   r   r   innerr!   r!   r"   rr     s   
zATestConvertLoopPass.test_prange_map_none_index.<locals>.test_implr!   rt   ru   r   rw   rz   r!   r!   r"   test_prange_map_none_index  s   z.TestConvertLoopPass.test_prange_map_none_indexc                 C   r   )Nc                  S   s4   d} t | }t| D ]}|d7 }|||d < q|S )Nr   rt   r   r   r!   r!   r"   rr     s   
zFTestConvertLoopPass.test_prange_map_overwrite_index.<locals>.test_implr!   z Overwrite of parallel loop indexr   r   r!   r!   r"   test_prange_map_overwrite_index  s   z3TestConvertLoopPass.test_prange_map_overwrite_indexc                 C   r   )Nc                  S   sF   d} t | }tjj  d}tjjt|D ]}||| 7 }q|S r   )r^   rn   r9   r:   r;   init_prangeinternal_prangerx   )rp   r   valr   r!   r!   r"   rr     s   
z7TestConvertLoopPass.test_init_prange.<locals>.test_implr!   rt   ru   r   rw   rz   r!   r!   r"   test_init_prange  s   	z$TestConvertLoopPass.test_init_prangec                 C   r   )Nc                  S   s>   d} t | | f}d}t|jD ]}||d |d  7 }q|S )Nr   r   rt   )r^   rn   r   ra   )rp   r   r   idxr!   r!   r"   rr     s   z4TestConvertLoopPass.test_pndindex.<locals>.test_implr!   rt   ru   r   rw   rz   r!   r!   r"   test_pndindex  s   z!TestConvertLoopPass.test_pndindexc                 C      dd }d}t t ||}|f}dd |D }| ||}| t|jd |j\}| |d d | |j | j	|g|R   d S )	Nc                 S   
   t | S r   r^   sumr   r!   r!   r"   rr   ,     
z5TestConvertLoopPass.test_numpy_sum.<locals>.test_impl      c                 S   r   r!   r   r   r!   r!   r"   r   2  r   z6TestConvertLoopPass.test_numpy_sum.<locals>.<listcomp>rt   ru   r   
r^   arangeprodr   rQ   r`   rx   ry   rg   r\   r   rr   ra   r   r   r   rP   r{   r!   r!   r"   test_numpy_sum+  s   z"TestConvertLoopPass.test_numpy_sumc                 C   r   )	Nc                 S   s*   | d d df }t | |dkddf S )Nr   r   rt   r   r   )r   slicedr!   r!   r"   rr   <  s   zGTestConvertLoopPass.test_numpy_sum_bool_array_masked.<locals>.test_implr   c                 S   r   r!   r   r   r!   r!   r"   r   C  r   zHTestConvertLoopPass.test_numpy_sum_bool_array_masked.<locals>.<listcomp>rt   ru   r   r   r   r!   r!   r"    test_numpy_sum_bool_array_masked;  s   z4TestConvertLoopPass.test_numpy_sum_bool_array_maskedc                 C   s   dd }d}t t ||}|f}dd |D }| ||}| t|jd |jD ]
}| |d d q-| |j | j	|g|R   d S )	Nc                 S   s&   t | jd }t | d d |f S r   )r^   r   ra   r   )r   selr!   r!   r"   rr   M  s   zFTestConvertLoopPass.test_numpy_sum_int_array_masked.<locals>.test_implr   c                 S   r   r!   r   r   r!   r!   r"   r   T  r   zGTestConvertLoopPass.test_numpy_sum_int_array_masked.<locals>.<listcomp>r   ru   r   r   r   r!   r!   r"   test_numpy_sum_int_array_maskedL  s   
z3TestConvertLoopPass.test_numpy_sum_int_array_maskedc                 C   r   )	Nc                 S   s   |  d | S )Nr   )fillr   r!   r!   r"   rr   _  s   
z=TestConvertLoopPass.test_numpy_fill_method.<locals>.test_implr   c                 S   r   r!   r   r   r!   r!   r"   r   f  r   z>TestConvertLoopPass.test_numpy_fill_method.<locals>.<listcomp>rt   ru   r   r   r   r!   r!   r"   test_numpy_fill_method^  s   z*TestConvertLoopPass.test_numpy_fill_methodN)r$   r%   r&   r9   r:   r;   ConvertLoopPassrL   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r!   r!   r!   r"   r     s     
r   c                   @   s2   e Zd ZG dd dZdd Zdd Zdd Zd	S )
TestPreParforPassc                   @   s   e Zd Zdd Zdd ZdS )z TestPreParforPass.sub_pass_classc                 C      d S r   r!   )r   pass_statesr!   r!   r"   r#   s     z)TestPreParforPass.sub_pass_class.__init__c                 C   r
  r   r!   )r   rM   r!   r!   r"   r2   v  r  z$TestPreParforPass.sub_pass_class.runN)r$   r%   r&   r#   r2   r!   r!   r!   r"   rL   r  s    rL   c                 C   l   dd }t d}|f}dd |D }| ||}| |jd d | |jd d	 | j|g|R   d S )
Nc                 S   s   t jd| jd}|S )Nr   r   )r^   rn   r   r   r!   r!   r"   rr   {  s   z:TestPreParforPass.test_dtype_conversion.<locals>.test_implrk   c                 S   r   r!   r   r   r!   r!   r"   r     r   z;TestPreParforPass.test_dtype_conversion.<locals>.<listcomp>replaced_funcr   replaced_dtypert   r^   r   rR   r`   statsr\   r   rr   r   r   r   pre_passr!   r!   r"   test_dtype_conversiony  s   
z'TestPreParforPass.test_dtype_conversionc                 C   r  )
Nc                 S   r   r   r   rq   r!   r!   r"   rr     r   z9TestPreParforPass.test_sum_replacement.<locals>.test_implrk   c                 S   r   r!   r   r   r!   r!   r"   r     r   z:TestPreParforPass.test_sum_replacement.<locals>.<listcomp>r  rt   r  r   r  r  r!   r!   r"   test_sum_replacement  s   
z&TestPreParforPass.test_sum_replacementc                 C   s   dd }t d}|f}dd |D }tjjj }|d | |||}| 	|j
d d | 	|j
d	 d | j|g|R   d S )
Nc                 S   r   r   r   r  r!   r!   r"   rr     r   z9TestPreParforPass.test_replacement_map.<locals>.test_implrk   c                 S   r   r!   r   r   r!   r!   r"   r     r   z:TestPreParforPass.test_replacement_map.<locals>.<listcomp>)r   numpyr  r   r  )r^   r   r9   r:   r;   swap_functions_mapcopypoprR   r`   r  r\   )r   rr   r   r   r   rA   r  r!   r!   r"   test_replacement_map  s   

z&TestPreParforPass.test_replacement_mapN)r$   r%   r&   rL   r  r  r  r!   r!   r!   r"   r	  q  s
    r	  __main__)$__doc__unittest	functoolsr   r  r^   r9   r   r   r   r   numba.parfors.parfor
numba.corer   r	   r
   r   r   r   r   r   numba.core.registryr   numba.tests.supportr   r   objectr   r'   ri   r   r   r   r	  r$   mainr!   r!   r!   r"   <module>   s(    (
qRs$ p4