Skip to content

Instantly share code, notes, and snippets.

@sld
Created September 24, 2012 21:02
Show Gist options
  • Save sld/3778358 to your computer and use it in GitHub Desktop.
Save sld/3778358 to your computer and use it in GitHub Desktop.
Diagonal ( zigzag ) matrix traverse OpenMP example
void print_matrix( int **matrix, int count )
{
for( int i = 0; i < count; i++ )
{
printf("\n");
for( int j = 0; j < count; j++ )
printf(" %d ", matrix[i][j]);
}
printf("\n");
}
void matrix_change(int **square_matrix, int count)
{
int i_max = count - 1;
for ( int d = 0; d < 2*count - 1; d++ )
{
if ((count - 1 - d) >= 0 )
{
#pragma omp parallel for
for(int i = 0; i <= d; i++ )
{
/* i + j = const - Invariant */
int j = d - i;
if ((i-2) >= 0 && (j-2) >= 0)
{
square_matrix[i][j] = square_matrix[i-2][j] + square_matrix[i][j-2];
}
}
}
else
{
int i_min = abs(2*count - d - 1 - i_max);
#pragma omp parallel for
for(int i = i_max; i > i_min; i-- )
{
int j = d - i;
if ((i-2) >= 0 && (j-2) >= 0)
{
square_matrix[i][j] = square_matrix[i-2][j] + square_matrix[i][j-2];
}
}
}
}
print_matrix( square_matrix, count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment