【发布时间】:2019-08-02 07:02:20
【问题描述】:
我正在尝试做分而治之的矩阵乘法,这样我就可以并行化它,但是我得到了一半的随机垃圾数和一半的 0,例如在 2x2 矩阵“[[15909360,0][15909360,0]]”上。这是我迄今为止基于the algorithm on Wikipedia 所拥有的,但我真的不知道从这里去哪里。我还没有将指针用于分区或线程。顺便说一句,这是家庭作业。
void partition(const std::vector<std::vector<IntElement> >& m, std::vector<std::vector<IntElement> >& m11, std::vector<std::vector<IntElement> >& m12,
std::vector<std::vector<IntElement> >& m21, std::vector<std::vector<IntElement> >& m22, int n){
for(int i=0;i<n/2;i++)
for(int j=0;j<n/2;j++){
m11[i][j] = m[i][j]; // top left
m12[i][j] = m[i][j + n / 2]; // top right
m21[i][j] = m[i + n / 2][j]; // bottom left
m22[i][j] = m[i + n / 2][j + n / 2]; // bottom right
}
};
void add(std::vector<std::vector<IntElement> >& C, std::vector<std::vector<IntElement> >& T, int n){
if(n==1){
C[0][0] += C[0][0] + T[0][0];
}
else{
std::vector<std::vector<IntElement> > c11(n/2, std::vector<IntElement>(n/2)), c12(n/2, std::vector<IntElement>(n/2)),
c21(n/2, std::vector<IntElement>(n/2)), c22(n/2, std::vector<IntElement>(n/2));
std::vector<std::vector<IntElement> > t11(n/2, std::vector<IntElement>(n/2)), t12(n/2, std::vector<IntElement>(n/2)),
t21(n/2, std::vector<IntElement>(n/2)), t22(n/2, std::vector<IntElement>(n/2));
partition(C, c11, c12, c21, c22, n);
partition(T, t11, t12, t21, t22, n);
add(c11, t11, n/2);
add(c12, t12, n/2);
add(c21, t21, n/2);
add(c22, t22, n/2);
}
};
void multiply(std::vector<std::vector<IntElement> >& C, const std::vector<std::vector<IntElement> >& A,
const std::vector<std::vector<IntElement> >& B, int n){
if(n==1)
C[0][0] += A[0][0] * B[0][0];
else{
std::vector<std::vector<IntElement> > T(n, std::vector<IntElement>(n));
std::vector<std::vector<IntElement> > a11(n/2, std::vector<IntElement>(n/2)), a12(n/2, std::vector<IntElement>(n/2)),
a21(n/2, std::vector<IntElement>(n/2)), a22(n/2, std::vector<IntElement>(n/2));
std::vector<std::vector<IntElement> > b11(n/2, std::vector<IntElement>(n/2)), b12(n/2, std::vector<IntElement>(n/2)),
b21(n/2, std::vector<IntElement>(n/2)), b22(n/2, std::vector<IntElement>(n/2));
std::vector<std::vector<IntElement> > c11(n/2, std::vector<IntElement>(n/2)), c12(n/2, std::vector<IntElement>(n/2)),
c21(n/2, std::vector<IntElement>(n/2)), c22(n/2, std::vector<IntElement>(n/2));
std::vector<std::vector<IntElement> > t11(n/2, std::vector<IntElement>(n/2)), t12(n/2, std::vector<IntElement>(n/2)),
t21(n/2, std::vector<IntElement>(n/2)), t22(n/2, std::vector<IntElement>(n/2));
partition(A, a11, a12, a21, a22, n);
partition(B, b11, b12, b21, b22, n);
partition(C, c11, c12, c21, c22, n);
partition(T, t11, t12, t21, t22, n);
multiply(c11, a11, b11, n/2);
multiply(c12, a11, b12, n/2);
multiply(c21, a21, b11, n/2);
multiply(c22, a21, b12, n/2);
multiply(t11, a12, b21, n/2);
multiply(t12, a12, b22, n/2);
multiply(t21, a22, b21, n/2);
multiply(t22, a22, b22, n/2);
add(C, T, n);
}
return;
};
SquareMatrix& SquareMatrix::operator*=(const SquareMatrix& m){
std::vector<std::vector<IntElement> > C(n, std::vector<IntElement>(n));
multiply(C, elements, m.elements, n);
elements = C;
return *this;
}
SquareMatrix operator*(const SquareMatrix& a, const SquareMatrix& b){
SquareMatrix c = a;
c *= b;
return c;
}
编辑:我改变了 C[0][0] += C[0][0] + T[0][0];在 add() 到 C[0][0] += T[0][0];我还做了一个 unpartition 函数,它基本上做相反的事情,并在相乘和相加后将分区放回 C 和 T:
void unpartition(std::vector<std::vector<IntElement> >& m,std::vector<std::vector<IntElement> >& m11, std::vector<std::vector<IntElement> >& m12,
std::vector<std::vector<IntElement> >& m21, std::vector<std::vector<IntElement> >& m22, int n){
for(int i=0;i<n/2;i++)
for(int j=0;j<n/2;j++){
m[i][j] = m11[i][j]; // top left
m[i][j + n / 2] = m12[i][j]; // top right
m[i + n / 2][j] = m21[i][j]; // bottom left
m[i + n / 2][j + n / 2] = m22[i][j]; // bottom right
}
}
在我修复了 IntElement 类的默认构造函数后,我的向量被正确初始化。
【问题讨论】:
-
你试过调试你的代码吗?也许在纸上手工完成步骤并与程序进行比较?
-
仅供参考:您可以通过在文件顶部使用
using elem_mat_t = std::vector<std::vector<IntElement> >;之类的矢量类型别名来使您的代码(对我们和您自己)更具可读性。 -
欢迎来到 Stack Overflow。请在minimal complete examples 上查看我们的页面;产生错误的最小、最简单的乘法示例是什么?
-
但乍一看,您似乎从未初始化那些本地向量
s T,a11,b11,c11,d11。它们不是零初始化的。 -
我建议您不要将向量的向量用作矩阵,因为它们会导致数据组织效率非常低。最好的方法可能是编写一个将数据存储在一维向量中的包装类。
标签: c++ matrix divide-and-conquer multiplication