C ++避免"错误共享"在OpenMP中

时间:2016-01-19 09:24:38

标签: c++ matrix openmp false-sharing

我试图在矩阵上并行化一些迭代。

矩阵保存在一维数组中,以便在内存中包含连续数据:

// 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;
                    }
                }
            }
        }
    }

我认为我遇到了错误分享&#34;写入矩阵单元时缓存数据无效的问题。

我该如何解决这个问题?

0 个答案:

没有答案