<s> from random import random <EOL> from time import time <EOL> from arrayfire import ( array , randu ) <EOL> import arrayfire as af <EOL> import sys <EOL> try : <EOL> frange = xrange <EOL> except NameError : <EOL> frange = range <EOL> def calc_pi_device ( samples ) : <EOL> x = randu ( samples ) <EOL> y = randu ( samples ) <EOL> return <NUM_LIT:4> * af . sum ( ( x * x + y * y ) < <NUM_LIT:1> ) / samples <EOL> def in_circle ( x , y ) : <EOL> return ( x * x + y * y ) < <NUM_LIT:1> <EOL> def calc_pi_host ( samples ) : <EOL> count = sum ( <NUM_LIT:1> for k in frange ( samples ) if in_circle ( random ( ) , random ( ) ) ) <EOL> return <NUM_LIT:4> * float ( count ) / samples <EOL> def bench ( calc_pi , samples = <NUM_LIT> , iters = <NUM_LIT> ) : <EOL> func_name = calc_pi . __name__ [ <NUM_LIT:8> : ] <EOL> print ( "<STR_LIT>" % ( func_name , samples / <NUM_LIT> , calc_pi ( samples ) ) ) <EOL> start = time ( ) <EOL> for k in frange ( iters ) : <EOL> calc_pi ( samples ) <EOL> end = time ( ) <EOL> print ( "<STR_LIT>" % ( <NUM_LIT:1000> * ( end - start ) / iters ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if ( len ( sys . argv ) > <NUM_LIT:1> ) : <EOL> af . set_device ( int ( sys . argv [ <NUM_LIT:1> ] ) ) <EOL> af . info ( ) <EOL> bench ( calc_pi_device ) <EOL> bench ( calc_pi_host ) </s>
