I have written a code for finite difference scheme using Jacobi iteration. The code works, but i am not sure how can i insert a stopping criteria that terminates the iteration when the error is below a threshold value. Below are excerpts of my code:

Host Code:

for(i=1;i<=21777;i++)

{

JacobiIteration<<<dimGrid, dimBlock>>>(du,nSize);

}

Kernel:

**global** void JacobiIteration(float* u,int n)

{

int x=threadIdx.x+blockIdx.x*BLOCK_SIZE;
int y=threadIdx.y+blockIdx.y*BLOCK_SIZE;

if(x>0&&x<n-1&&y>0&&y<n-1)

{

u[y+x*n]=0.25*(u[y+(x+1)*n]+u[(x-1) n+y]+u[(xn)+y+1]+u[(x*n)+y-1]);

}

}

Thanks in advanced.