0

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

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

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

我认为我遇到了“错误共享”问题,其中写入矩阵单元时缓存数据无效。

我怎么解决这个问题?

4

0 回答 0