from timeit import default_timer as timer
import numpy

iterations = 10
m = 256 
n= 256 

grid1 = numpy.zeros((m+2,n+2), dtype='double')
grid2 = numpy.zeros((m+2,n+2), dtype='double')

grid1[1,1:n+1] = list(range(n))
grid1[1:m+1:,1] = list(range(m))

for k in range(iterations+1):
#    # start timer after a warmup iteration
    if k<1:
        t0 = timer()

    for j in range(1,m+1):
        for i in range(1,n+1):
            grid2[j][i] =0.5*grid1[j][i] + 0.5*( grid1[j-1][i]+
                                             grid1[j+1][i]+
                                             grid1[j][i-1]+
                                             grid1[j][i+1])

    tmp=grid2
    grid2=grid1
    grid1=tmp
t1 = timer()
pipeline_time = t1 - t0
avgtime = pipeline_time/iterations

print('Rate (MFlops/s): ',1.e-6*2*(m-1)*(n-1)/avgtime,' Avg time (s): ',avgtime)
print("Total time ", pipeline_time)
