<s> import pytest <EOL> import numpy <EOL> from numpy . testing import assert_allclose <EOL> from pyop2 import op2 <EOL> nelems = <NUM_LIT> <EOL> class TestGlobalReductions : <EOL> """<STR_LIT>""" <EOL> @ pytest . fixture ( scope = '<STR_LIT>' , params = [ ( nelems , nelems , nelems , nelems ) , <EOL> ( <NUM_LIT:0> , nelems , nelems , nelems ) , <EOL> ( nelems / <NUM_LIT:2> , nelems , nelems , nelems ) ] ) <EOL> def set ( cls , request ) : <EOL> return op2 . Set ( request . param , '<STR_LIT>' ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def dset ( cls , set ) : <EOL> return op2 . DataSet ( set , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def dset2 ( cls , set ) : <EOL> return op2 . DataSet ( set , <NUM_LIT:2> , '<STR_LIT>' ) <EOL> @ pytest . fixture <EOL> def d1 ( cls , dset ) : <EOL> return op2 . Dat ( dset , numpy . arange ( nelems ) + <NUM_LIT:1> , dtype = numpy . uint32 ) <EOL> @ pytest . fixture <EOL> def d2 ( cls , dset2 ) : <EOL> return op2 . Dat ( dset2 , numpy . arange ( <NUM_LIT:2> * nelems ) + <NUM_LIT:1> , dtype = numpy . uint32 ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def k1_write_to_dat ( cls ) : <EOL> k = """<STR_LIT>""" <EOL> return op2 . Kernel ( k , "<STR_LIT:k>" ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def k1_inc_to_global ( cls ) : <EOL> k = """<STR_LIT>""" <EOL> return op2 . Kernel ( k , "<STR_LIT:k>" ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def k1_min_to_global ( cls ) : <EOL> k = """<STR_LIT>""" <EOL> return op2 . Kernel ( k , "<STR_LIT:k>" ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def k2_min_to_global ( cls ) : <EOL> k = """<STR_LIT>""" <EOL> return op2 . Kernel ( k , "<STR_LIT:k>" ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def k1_max_to_global ( cls ) : <EOL> k = """<STR_LIT>""" <EOL> return op2 . Kernel ( k , "<STR_LIT:k>" ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def k2_max_to_global ( cls ) : <EOL> k = """<STR_LIT>""" <EOL> return op2 . Kernel ( k , "<STR_LIT:k>" ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def k2_write_to_dat ( cls , request ) : <EOL> k = """<STR_LIT>""" <EOL> return op2 . Kernel ( k , "<STR_LIT:k>" ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def k2_inc_to_global ( cls ) : <EOL> k = """<STR_LIT>""" <EOL> return op2 . Kernel ( k , "<STR_LIT:k>" ) <EOL> @ pytest . fixture <EOL> def duint32 ( cls , dset ) : <EOL> return op2 . Dat ( dset , [ <NUM_LIT:12> ] * nelems , numpy . uint32 , "<STR_LIT>" ) <EOL> @ pytest . fixture <EOL> def dint32 ( cls , dset ) : <EOL> return op2 . Dat ( dset , [ - <NUM_LIT:12> ] * nelems , numpy . int32 , "<STR_LIT>" ) <EOL> @ pytest . fixture <EOL> def dfloat32 ( cls , dset ) : <EOL> return op2 . Dat ( dset , [ - <NUM_LIT> ] * nelems , numpy . float32 , "<STR_LIT>" ) <EOL> @ pytest . fixture <EOL> def dfloat64 ( cls , dset ) : <EOL> return op2 . Dat ( dset , [ - <NUM_LIT> ] * nelems , numpy . float64 , "<STR_LIT>" ) <EOL> def test_direct_min_uint32 ( self , backend , set , duint32 ) : <EOL> kernel_min = """<STR_LIT>""" <EOL> g = op2 . Global ( <NUM_LIT:1> , <NUM_LIT:8> , numpy . uint32 , "<STR_LIT:g>" ) <EOL> op2 . par_loop ( op2 . Kernel ( kernel_min , "<STR_LIT>" ) , set , <EOL> duint32 ( op2 . READ ) , <EOL> g ( op2 . MIN ) ) <EOL> assert g . data [ <NUM_LIT:0> ] == <NUM_LIT:8> <EOL> def test_direct_min_int32 ( self , backend , set , dint32 ) : <EOL> kernel_min = """<STR_LIT>""" <EOL> g = op2 . Global ( <NUM_LIT:1> , <NUM_LIT:8> , numpy . int32 , "<STR_LIT:g>" ) <EOL> op2 . par_loop ( op2 . Kernel ( kernel_min , "<STR_LIT>" ) , set , <EOL> dint32 ( op2 . READ ) , <EOL> g ( op2 . MIN ) ) <EOL> assert g . data [ <NUM_LIT:0> ] == - <NUM_LIT:12> <EOL> def test_direct_max_int32 ( self , backend , set , dint32 ) : <EOL> kernel_max = """<STR_LIT>""" <EOL> g = op2 . Global ( <NUM_LIT:1> , - <NUM_LIT> , numpy . int32 , "<STR_LIT:g>" ) <EOL> op2 . par_loop ( op2 . Kernel ( kernel_max , "<STR_LIT>" ) , set , <EOL> dint32 ( op2 . READ ) , <EOL> g ( op2 . MAX ) ) <EOL> assert g . data [ <NUM_LIT:0> ] == - <NUM_LIT:12> <EOL> def test_direct_min_float ( self , backend , set , dfloat32 ) : <EOL> kernel_min = """<STR_LIT>""" <EOL> g = op2 . Global ( <NUM_LIT:1> , - <NUM_LIT> , numpy . float32 , "<STR_LIT:g>" ) <EOL> op2 . par_loop ( op2 . Kernel ( kernel_min , "<STR_LIT>" ) , set , <EOL> dfloat32 ( op2 . READ ) , <EOL> g ( op2 . MIN ) ) <EOL> assert_allclose ( g . data [ <NUM_LIT:0> ] , - <NUM_LIT> ) <EOL> def test_direct_max_float ( self , backend , set , dfloat32 ) : <EOL> kernel_max = """<STR_LIT>""" <EOL> g = op2 . Global ( <NUM_LIT:1> , - <NUM_LIT> , numpy . float32 , "<STR_LIT:g>" ) <EOL> op2 . par_loop ( op2 . Kernel ( kernel_max , "<STR_LIT>" ) , set , <EOL> dfloat32 ( op2 . READ ) , <EOL> g ( op2 . MAX ) ) <EOL> assert_allclose ( g . data [ <NUM_LIT:0> ] , - <NUM_LIT> ) <EOL> def test_direct_min_double ( self , backend , set , dfloat64 ) : <EOL> kernel_min = """<STR_LIT>""" <EOL> g = op2 . Global ( <NUM_LIT:1> , - <NUM_LIT> , numpy . float64 , "<STR_LIT:g>" ) <EOL> op2 . par_loop ( op2 . Kernel ( kernel_min , "<STR_LIT>" ) , set , <EOL> dfloat64 ( op2 . READ ) , <EOL> g ( op2 . MIN ) ) <EOL> assert_allclose ( g . data [ <NUM_LIT:0> ] , - <NUM_LIT> ) <EOL> def test_direct_max_double ( self , backend , set , dfloat64 ) : <EOL> kernel_max = """<STR_LIT>""" <EOL> g = op2 . Global ( <NUM_LIT:1> , - <NUM_LIT> , numpy . float64 , "<STR_LIT:g>" ) <EOL> op2 . par_loop ( op2 . Kernel ( kernel_max , "<STR_LIT>" ) , set , <EOL> dfloat64 ( op2 . READ ) , <EOL> g ( op2 . MAX ) ) <EOL> assert_allclose ( g . data [ <NUM_LIT:0> ] , - <NUM_LIT> ) <EOL> def test_1d_read ( self , backend , k1_write_to_dat , set , d1 ) : <EOL> g = op2 . Global ( <NUM_LIT:1> , <NUM_LIT:1> , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k1_write_to_dat , set , <EOL> d1 ( op2 . WRITE ) , <EOL> g ( op2 . READ ) ) <EOL> assert all ( d1 . data == g . data ) <EOL> def test_1d_read_no_init ( self , backend , k1_write_to_dat , set , d1 ) : <EOL> g = op2 . Global ( <NUM_LIT:1> , dtype = numpy . uint32 ) <EOL> d1 . data [ : ] = <NUM_LIT:100> <EOL> op2 . par_loop ( k1_write_to_dat , set , <EOL> d1 ( op2 . WRITE ) , <EOL> g ( op2 . READ ) ) <EOL> assert all ( g . data == <NUM_LIT:0> ) <EOL> assert all ( d1 . data == <NUM_LIT:0> ) <EOL> def test_2d_read ( self , backend , k2_write_to_dat , set , d1 ) : <EOL> g = op2 . Global ( <NUM_LIT:2> , ( <NUM_LIT:1> , <NUM_LIT:2> ) , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k2_write_to_dat , set , <EOL> d1 ( op2 . WRITE ) , <EOL> g ( op2 . READ ) ) <EOL> assert all ( d1 . data == g . data . sum ( ) ) <EOL> def test_1d_inc ( self , backend , k1_inc_to_global , set , d1 ) : <EOL> g = op2 . Global ( <NUM_LIT:1> , <NUM_LIT:0> , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k1_inc_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . INC ) ) <EOL> assert g . data == d1 . data . sum ( ) <EOL> def test_1d_inc_no_data ( self , backend , k1_inc_to_global , set , d1 ) : <EOL> g = op2 . Global ( <NUM_LIT:1> , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k1_inc_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . INC ) ) <EOL> assert g . data == d1 . data . sum ( ) <EOL> def test_1d_min_dat_is_min ( self , backend , k1_min_to_global , set , d1 ) : <EOL> val = d1 . data . min ( ) + <NUM_LIT:1> <EOL> g = op2 . Global ( <NUM_LIT:1> , val , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k1_min_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . MIN ) ) <EOL> assert g . data == d1 . data . min ( ) <EOL> def test_1d_min_global_is_min ( self , backend , k1_min_to_global , set , d1 ) : <EOL> d1 . data [ : ] += <NUM_LIT:10> <EOL> val = d1 . data . min ( ) - <NUM_LIT:1> <EOL> g = op2 . Global ( <NUM_LIT:1> , val , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k1_min_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . MIN ) ) <EOL> assert g . data == val <EOL> def test_1d_max_dat_is_max ( self , backend , k1_max_to_global , set , d1 ) : <EOL> val = d1 . data . max ( ) - <NUM_LIT:1> <EOL> g = op2 . Global ( <NUM_LIT:1> , val , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k1_max_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . MAX ) ) <EOL> assert g . data == d1 . data . max ( ) <EOL> def test_1d_max_global_is_max ( self , backend , k1_max_to_global , set , d1 ) : <EOL> val = d1 . data . max ( ) + <NUM_LIT:1> <EOL> g = op2 . Global ( <NUM_LIT:1> , val , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k1_max_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . MAX ) ) <EOL> assert g . data == val <EOL> def test_2d_inc ( self , backend , k2_inc_to_global , set , d2 ) : <EOL> g = op2 . Global ( <NUM_LIT:2> , ( <NUM_LIT:0> , <NUM_LIT:0> ) , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k2_inc_to_global , set , <EOL> d2 ( op2 . READ ) , <EOL> g ( op2 . INC ) ) <EOL> assert g . data [ <NUM_LIT:0> ] == d2 . data [ : , <NUM_LIT:0> ] . sum ( ) <EOL> assert g . data [ <NUM_LIT:1> ] == d2 . data [ : , <NUM_LIT:1> ] . sum ( ) <EOL> def test_2d_min_dat_is_min ( self , backend , k2_min_to_global , set , d2 ) : <EOL> val_0 = d2 . data [ : , <NUM_LIT:0> ] . min ( ) + <NUM_LIT:1> <EOL> val_1 = d2 . data [ : , <NUM_LIT:1> ] . min ( ) + <NUM_LIT:1> <EOL> g = op2 . Global ( <NUM_LIT:2> , ( val_0 , val_1 ) , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k2_min_to_global , set , <EOL> d2 ( op2 . READ ) , <EOL> g ( op2 . MIN ) ) <EOL> assert g . data [ <NUM_LIT:0> ] == d2 . data [ : , <NUM_LIT:0> ] . min ( ) <EOL> assert g . data [ <NUM_LIT:1> ] == d2 . data [ : , <NUM_LIT:1> ] . min ( ) <EOL> def test_2d_min_global_is_min ( self , backend , k2_min_to_global , set , d2 ) : <EOL> d2 . data [ : , <NUM_LIT:0> ] += <NUM_LIT:10> <EOL> d2 . data [ : , <NUM_LIT:1> ] += <NUM_LIT:10> <EOL> val_0 = d2 . data [ : , <NUM_LIT:0> ] . min ( ) - <NUM_LIT:1> <EOL> val_1 = d2 . data [ : , <NUM_LIT:1> ] . min ( ) - <NUM_LIT:1> <EOL> g = op2 . Global ( <NUM_LIT:2> , ( val_0 , val_1 ) , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k2_min_to_global , set , <EOL> d2 ( op2 . READ ) , <EOL> g ( op2 . MIN ) ) <EOL> assert g . data [ <NUM_LIT:0> ] == val_0 <EOL> assert g . data [ <NUM_LIT:1> ] == val_1 <EOL> def test_2d_max_dat_is_max ( self , backend , k2_max_to_global , set , d2 ) : <EOL> val_0 = d2 . data [ : , <NUM_LIT:0> ] . max ( ) - <NUM_LIT:1> <EOL> val_1 = d2 . data [ : , <NUM_LIT:1> ] . max ( ) - <NUM_LIT:1> <EOL> g = op2 . Global ( <NUM_LIT:2> , ( val_0 , val_1 ) , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k2_max_to_global , set , <EOL> d2 ( op2 . READ ) , <EOL> g ( op2 . MAX ) ) <EOL> assert g . data [ <NUM_LIT:0> ] == d2 . data [ : , <NUM_LIT:0> ] . max ( ) <EOL> assert g . data [ <NUM_LIT:1> ] == d2 . data [ : , <NUM_LIT:1> ] . max ( ) <EOL> def test_2d_max_global_is_max ( self , backend , k2_max_to_global , set , d2 ) : <EOL> max_val_0 = d2 . data [ : , <NUM_LIT:0> ] . max ( ) + <NUM_LIT:1> <EOL> max_val_1 = d2 . data [ : , <NUM_LIT:1> ] . max ( ) + <NUM_LIT:1> <EOL> g = op2 . Global ( <NUM_LIT:2> , ( max_val_0 , max_val_1 ) , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k2_max_to_global , set , <EOL> d2 ( op2 . READ ) , <EOL> g ( op2 . MAX ) ) <EOL> assert g . data [ <NUM_LIT:0> ] == max_val_0 <EOL> assert g . data [ <NUM_LIT:1> ] == max_val_1 <EOL> def test_1d_multi_inc_same_global ( self , backend , k1_inc_to_global , set , d1 ) : <EOL> g = op2 . Global ( <NUM_LIT:1> , <NUM_LIT:0> , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k1_inc_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . INC ) ) <EOL> assert g . data == d1 . data . sum ( ) <EOL> op2 . par_loop ( k1_inc_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . INC ) ) <EOL> assert g . data == d1 . data . sum ( ) * <NUM_LIT:2> <EOL> def test_1d_multi_inc_same_global_reset ( self , backend , k1_inc_to_global , set , d1 ) : <EOL> g = op2 . Global ( <NUM_LIT:1> , <NUM_LIT:0> , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k1_inc_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . INC ) ) <EOL> assert g . data == d1 . data . sum ( ) <EOL> g . data = <NUM_LIT:10> <EOL> op2 . par_loop ( k1_inc_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . INC ) ) <EOL> assert g . data == d1 . data . sum ( ) + <NUM_LIT:10> <EOL> def test_1d_multi_inc_diff_global ( self , backend , k1_inc_to_global , set , d1 ) : <EOL> g = op2 . Global ( <NUM_LIT:1> , <NUM_LIT:0> , dtype = numpy . uint32 ) <EOL> g2 = op2 . Global ( <NUM_LIT:1> , <NUM_LIT:10> , dtype = numpy . uint32 ) <EOL> op2 . par_loop ( k1_inc_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g ( op2 . INC ) ) <EOL> assert g . data == d1 . data . sum ( ) <EOL> op2 . par_loop ( k1_inc_to_global , set , <EOL> d1 ( op2 . READ ) , <EOL> g2 ( op2 . INC ) ) <EOL> assert g2 . data == d1 . data . sum ( ) + <NUM_LIT:10> <EOL> def test_globals_with_different_types ( self , backend , set ) : <EOL> g_uint32 = op2 . Global ( <NUM_LIT:1> , [ <NUM_LIT:0> ] , numpy . uint32 , "<STR_LIT>" ) <EOL> g_double = op2 . Global ( <NUM_LIT:1> , [ <NUM_LIT:0.0> ] , numpy . float64 , "<STR_LIT>" ) <EOL> k = """<STR_LIT>""" <EOL> op2 . par_loop ( op2 . Kernel ( k , "<STR_LIT:k>" ) , <EOL> set , <EOL> g_uint32 ( op2 . INC ) , <EOL> g_double ( op2 . INC ) ) <EOL> assert_allclose ( g_uint32 . data [ <NUM_LIT:0> ] , g_double . data [ <NUM_LIT:0> ] ) <EOL> assert g_uint32 . data [ <NUM_LIT:0> ] == set . size </s>
