如果 X 和 Y 是两个向量且 nb 是整数,"X += nb; Y+= nb;" 是什么意思?
What does "X += nb; Y+= nb;" mean if X and Y are two vectors and nb is an integer?
论文第 8 页:
[使用超级块算法系列减少点积中的浮点误差 - Anthony M. Castaldo、R.Clint Whaley 和 Anthony T. Chronopouos][1]
有如下代码:
typedef double scalar;
typedef scalar *Vec;
scalar dotProd(Vec X, Vec Y, int nb)
{ int n = X.length;
int nblks = n / nb;
int nsblks = sqrt(nblks);
int blksInSblk = nblks / nsblks;
scalar dot = 0.0, sdot, cdot;
for (s=0; s < nsblks; s++)
{ sdot = 0.0;
for (b=0; b < blksInSblk; b++)
{
cdot = X[0] * Y[0];
for (i=1; i < nb; i++)
cdot += X[i] * Y[i];
sdot += cdot;
X += nb; Y += nb;
}
dot += sdot;
}
return dot;
}
我不明白这行伪代码:
X += nb; Y+= nb;
如果X
和Y
是两个向量,nb
是一个整数,那是什么意思?
附录:如果不使用指针,如何"increment" by nb
个元素?
int n = x.size();
int nblks = n / nb;
int nsblks = sqrt(nblks);
int blksInSblk = nblks / nsblks;
double dot = 0.0;
double sdot = 0.0;
double cdot = 0.0;
for(int s=0;s<nsblks;s++) {
std::cout << "iteration s= " << s << std::endl;
for(int b=0;b<blksInSblk;b++) {
std::cout << "iteration b= " << b << std::endl;
cdot += x(0) * y(0);
std::cout << "cdot += x(0) * y(0) = " << cdot << std::endl;
int pointingTo = 0;
for(int i=pointingTo;i<nb;i++) {
cdot += x(i) * y(i);
}
sdot += cdot;
// Increment the pointer to x by nb elements:
// x += nb;
pointingTo += nb;
// Increment the pointer to y by nb elements;
// y += nb
//}
}
dot += sdot;
}
如果您假设
,您显示的代码可以解释为 C 代码
typedef double scalar;
typedef scalar *Vec;
并在第一个循环之前的某处添加一行 int s, b, i;
。
在这种情况下,很明显 X += nb
将指向 X
的指针递增 nb
标量的大小。这与算法似乎正在做的事情是一致的:以大小 nb
的块而不是一次对所有点积求和。
论文第 8 页: [使用超级块算法系列减少点积中的浮点误差 - Anthony M. Castaldo、R.Clint Whaley 和 Anthony T. Chronopouos][1]
有如下代码:
typedef double scalar;
typedef scalar *Vec;
scalar dotProd(Vec X, Vec Y, int nb)
{ int n = X.length;
int nblks = n / nb;
int nsblks = sqrt(nblks);
int blksInSblk = nblks / nsblks;
scalar dot = 0.0, sdot, cdot;
for (s=0; s < nsblks; s++)
{ sdot = 0.0;
for (b=0; b < blksInSblk; b++)
{
cdot = X[0] * Y[0];
for (i=1; i < nb; i++)
cdot += X[i] * Y[i];
sdot += cdot;
X += nb; Y += nb;
}
dot += sdot;
}
return dot;
}
我不明白这行伪代码:
X += nb; Y+= nb;
如果X
和Y
是两个向量,nb
是一个整数,那是什么意思?
附录:如果不使用指针,如何"increment" by nb
个元素?
int n = x.size();
int nblks = n / nb;
int nsblks = sqrt(nblks);
int blksInSblk = nblks / nsblks;
double dot = 0.0;
double sdot = 0.0;
double cdot = 0.0;
for(int s=0;s<nsblks;s++) {
std::cout << "iteration s= " << s << std::endl;
for(int b=0;b<blksInSblk;b++) {
std::cout << "iteration b= " << b << std::endl;
cdot += x(0) * y(0);
std::cout << "cdot += x(0) * y(0) = " << cdot << std::endl;
int pointingTo = 0;
for(int i=pointingTo;i<nb;i++) {
cdot += x(i) * y(i);
}
sdot += cdot;
// Increment the pointer to x by nb elements:
// x += nb;
pointingTo += nb;
// Increment the pointer to y by nb elements;
// y += nb
//}
}
dot += sdot;
}
如果您假设
,您显示的代码可以解释为 C 代码typedef double scalar;
typedef scalar *Vec;
并在第一个循环之前的某处添加一行 int s, b, i;
。
在这种情况下,很明显 X += nb
将指向 X
的指针递增 nb
标量的大小。这与算法似乎正在做的事情是一致的:以大小 nb
的块而不是一次对所有点积求和。