<s> from __future__ import print_function <EOL> import numpy as np <EOL> from numba import cuda <EOL> from numba import unittest_support as unittest <EOL> from numba . config import ENABLE_CUDASIM <EOL> sum_reduce = cuda . Reduce ( lambda a , b : a + b ) <EOL> class TestReduction ( unittest . TestCase ) : <EOL> def _sum_reduce ( self , n ) : <EOL> A = ( np . arange ( n , dtype = np . float64 ) + <NUM_LIT:1> ) <EOL> expect = A . sum ( ) <EOL> got = sum_reduce ( A ) <EOL> self . assertEqual ( expect , got ) <EOL> def test_sum_reduce ( self ) : <EOL> if ENABLE_CUDASIM : <EOL> test_sizes = [ <NUM_LIT:1> , <NUM_LIT:16> ] <EOL> else : <EOL> test_sizes = [ <NUM_LIT:1> , <NUM_LIT:15> , <NUM_LIT:16> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> for n in test_sizes : <EOL> self . _sum_reduce ( n ) <EOL> def test_empty_array_host ( self ) : <EOL> A = ( np . arange ( <NUM_LIT:0> , dtype = np . float64 ) + <NUM_LIT:1> ) <EOL> expect = A . sum ( ) <EOL> got = sum_reduce ( A ) <EOL> self . assertEqual ( expect , got ) <EOL> def test_empty_array_device ( self ) : <EOL> A = ( np . arange ( <NUM_LIT:0> , dtype = np . float64 ) + <NUM_LIT:1> ) <EOL> dA = cuda . to_device ( A ) <EOL> expect = A . sum ( ) <EOL> got = sum_reduce ( dA ) <EOL> self . assertEqual ( expect , got ) <EOL> def test_prod_reduce ( self ) : <EOL> prod_reduce = cuda . reduce ( lambda a , b : a * b ) <EOL> A = ( np . arange ( <NUM_LIT:64> , dtype = np . float64 ) + <NUM_LIT:1> ) <EOL> expect = A . prod ( ) <EOL> got = prod_reduce ( A , init = <NUM_LIT:1> ) <EOL> self . assertTrue ( np . allclose ( expect , got ) ) <EOL> def test_max_reduce ( self ) : <EOL> max_reduce = cuda . Reduce ( lambda a , b : max ( a , b ) ) <EOL> A = ( np . arange ( <NUM_LIT> , dtype = np . float64 ) + <NUM_LIT:1> ) <EOL> expect = A . max ( ) <EOL> got = max_reduce ( A , init = <NUM_LIT:0> ) <EOL> self . assertEqual ( expect , got ) <EOL> def test_non_identity_init ( self ) : <EOL> init = <NUM_LIT:3> <EOL> A = ( np . arange ( <NUM_LIT:10> , dtype = np . float64 ) + <NUM_LIT:1> ) <EOL> expect = A . sum ( ) + init <EOL> got = sum_reduce ( A , init = init ) <EOL> self . assertEqual ( expect , got ) <EOL> def test_result_on_device ( self ) : <EOL> A = ( np . arange ( <NUM_LIT:10> , dtype = np . float64 ) + <NUM_LIT:1> ) <EOL> got = cuda . to_device ( np . zeros ( <NUM_LIT:1> , dtype = np . float64 ) ) <EOL> expect = A . sum ( ) <EOL> res = sum_reduce ( A , res = got ) <EOL> self . assertIsNone ( res ) <EOL> self . assertEqual ( expect , got [ <NUM_LIT:0> ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
