how to parallelise OpenMP iterative loop?
I am trying to parallelise an algorithm which does some operation on a matrix (lets call it blurring for simplicity sake). Once this operation has been done, it finds the biggest change between the old and new matrix (max of absolute difference between old and new matrix on a per element basis). If this maximum difference is above some threshold, then do another iteration of the matrix operation.

So my main program has the following loop:

    converged = 0;
    for( i = 1; i <= iteration_limit; i++ ){
		max_diff = update( &data_grid );
	
		if( max_diff < tol ) {
			converged = 1;
			break;
		}
	}

`update( &data_grid )` then calls the actual implementation of the blurring algorithm. The blurring algorithm then iterates over the matrix, it is this loop that I am trying to parallelise:

	for( i = 0; i < width; i++ ) {
		for( j = 0; j <= height; j++ ) {
			g->data[ update ][ i ][ j ] = 
			ONE_QUARTER * ( 
						 g->data[ update ][ i + 1 ][ j     ] +
						 g->data[ update ][ i - 1 ][ j     ] +
						 g->data[ update ][ i     ][ j + 1 ] +
						 g->data[ update ][ i     ][ j - 1 ] +
						 );
			diff = fabs( g->data[ old ][ i ][ j ] - g->data[ update ][ i ][ j ] );
			maxdiff = maxdiff > diff ? maxdiff : diff;
		}
	}


I could just stick a parallel region inside `update(&data_grid)` but that would mean threads would be created and destroyed on each iteration which I am trying to avoid.:

	#pragma omp parallel for private(i, j, diff, maxdg) shared(width, height, update, g, dg, chunksize) default(none) schedule(static, chunksize)

I have 2 copies of the grid and write the new answer in the "other one" on every iteration by switching `old` and `update` between `0` and `1`.
how to parallelise OpenMP iterative loop?
===

I am trying to parallelise an algorithm which does some operation on a matrix (lets call it blurring for simplicity sake). Once this operation has been done, it finds the biggest change between the old and new matrix (max of absolute difference between old and new matrix on a per element basis). If this maximum difference is above some threshold, then do another iteration of the matrix operation.

So my main program has the following loop:

    converged = 0;
    for( i = 1; i <= iteration_limit; i++ ){
		max_diff = update( &data_grid );
	
		if( max_diff < tol ) {
			converged = 1;
			break;
		}
	}

`update( &data_grid )` then calls the actual implementation of the blurring algorithm. The blurring algorithm then iterates over the matrix, it is this loop that I am trying to parallelise:

	for( i = 0; i < width; i++ ) {
		for( j = 0; j <= height; j++ ) {
			g->data[ update ][ i ][ j ] = 
			ONE_QUARTER * ( 
						 g->data[ update ][ i + 1 ][ j     ] +
						 g->data[ update ][ i - 1 ][ j     ] +
						 g->data[ update ][ i     ][ j + 1 ] +
						 g->data[ update ][ i     ][ j - 1 ] +
						 );
			diff = fabs( g->data[ old ][ i ][ j ] - g->data[ update ][ i ][ j ] );
			maxdiff = maxdiff > diff ? maxdiff : diff;
		}
	}


I could just stick a parallel region inside `update(&data_grid)` but that would mean threads would be created and destroyed on each iteration which I am trying to avoid.:

	#pragma omp parallel for private(i, j, diff, maxdg) shared(width, height, update, g, dg, chunksize) default(none) schedule(static, chunksize)

I have 2 copies of the grid and write the new answer in the "other one" on every iteration by switching `old` and `update` between `0` and `1`.