I'm trying to parallelize some iterations over a matrix.
The matrix is saved in a 1D array to have contiguous data in memory:
// array that contains all the elems of dense matrix
char* data;
//set of pointers to the matrix rows indexed by the subarrays of 'data'
char ** dense = NULL;
dense = new char*[m_rows];
data = new char[m_cols*m_rows];
After 'data' has been populated with numbers, I index the matrix in that way:
// index every row of DENSE with a subarray of DATA
char* index = data;
for(int i = 0; i < m_rows; i++)
{
dense[i] = index;
// index now points to the next row
index += m_cols;
}
After that, I parallelize the iteration over the matrix assigning a column to every thread because I have to make computations column by column.
int th_id;
#pragma omp parallel for private(i, th_id) schedule(static)
for(j=0;j<m_cols;++j)
{
for(i=0;i<m_rows;++i)
{
if(dense[i][j] == 1)
{
if(i!=m_rows-1)
{
if(dense[i+1][j] == 0)
{
dense[i][j] = 0;
dense[i+1][j] = 1;
i++;
}
}
else
{
if(dense[0][j] == 0)
{
dense[i][j] = 0;
dense[0][j] = 1;
}
}
}
}
}
I think that I came across the "False sharing" problem in which cache data are invalidated when a matrix cell is written.
How can I solve this problem?