マトリックスに対していくつかの反復を並列化しようとしています。
行列は 1D 配列に保存され、メモリ内に連続したデータを保持します。
// 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];
「データ」に数値が入力された後、次のようにマトリックスにインデックスを付けます。
// 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;
}
その後、列ごとに計算を行う必要があるため、すべてのスレッドに列を割り当てて行列の反復を並列化します。
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;
}
}
}
}
}
マトリックスセルが書き込まれるとキャッシュデータが無効になる「偽共有」の問題に遭遇したと思います。
どうすればこの問題を解決できますか?