- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要编写矩阵- vector 和矩阵-矩阵乘法函数,但我无法理解 SSE 命令。
矩阵和 vector 的维度总是 4 的倍数。
我设法编写了如下所示的 vector - vector 乘法函数:
void vector_multiplication_SSE(float* m, float* n, float* result, unsigned const int size)
{
int i;
__declspec(align(16))__m128 *p_m = (__m128*)m;
__declspec(align(16))__m128 *p_n = (__m128*)n;
__declspec(align(16))__m128 *p_result = (__m128*)result;
for (i = 0; i < size / 4; ++i)
p_result[i] = _mm_mul_ps(p_m[i], p_n[i]);
// print the result
for (int i = 0; i < size; ++i)
{
if (i % 4 == 0) cout << endl;
cout << result[i] << '\t';
}
}
现在我正在尝试实现矩阵 vector 乘法。
这是我目前所拥有的:
void multiply_matrix_by_vector_SSE(float* m, float* v, float* result, unsigned const int vector_dims)
{
int i, j;
__declspec(align(16))__m128 *p_m = (__m128*)m;
__declspec(align(16))__m128 *p_v = (__m128*)v;
__declspec(align(16))__m128 *p_result = (__m128*)result;
for (i = 0; i < vector_dims; i += 4)
{
__m128 tmp = _mm_load_ps(&result[i]);
__m128 p_m_tmp = _mm_load_ps(&m[i]);
tmp = _mm_add_ps(tmp, _mm_mul_ps(tmp, p_m_tmp));
_mm_store_ps(&result[i], tmp);
// another for loop here?
}
// print the result
for (int i = 0; i < vector_dims; ++i)
{
if (i % 4 == 0) cout << endl;
cout << result[i] << '\t';
}
}
这个函数看起来完全错误。我的意思是不仅它不能正常工作,而且我似乎也在朝着错误的方向前进。
谁能帮我实现 vector -矩阵和矩阵-矩阵乘法?我非常感谢一些示例代码和非常详细的解释
这是我的第 2 次尝试:
它因 Access reading violation
异常而失败,但仍然感觉更接近
void multiply_matrix_by_vector_SSE(float* m, float* v, float* result, unsigned const int vector_dims)
{
int i, j;
__declspec(align(16))__m128 *p_m = (__m128*)m;
__declspec(align(16))__m128 *p_v = (__m128*)v;
__declspec(align(16))__m128 *p_result = (__m128*)result;
for (i = 0; i < vector_dims; ++i)
{
p_result[i] = _mm_mul_ps(_mm_load_ps(&m[i]), _mm_load_ps1(&v[i]));
}
// print the result
for (int i = 0; i < vector_dims; ++i)
{
if (i % 4 == 0) cout << endl;
cout << result[i] << '\t';
}
}
void multiply_matrix_by_vector_SSE(float* m, float* v, float* result, unsigned const int vector_dims)
{
int i, j;
__declspec(align(16))__m128 *p_m = (__m128*)m;
__declspec(align(16))__m128 *p_v = (__m128*)v;
__declspec(align(16))__m128 *p_result = (__m128*)result;
for (i = 0; i < vector_dims; ++i)
{
for (j = 0; j < vector_dims * vector_dims / 4; ++j)
{
p_result[i] = _mm_mul_ps(p_v[i], p_m[j]);
}
}
for (int i = 0; i < vector_dims; ++i)
{
if (i % 4 == 0) cout << endl;
cout << result[i] << '\t';
}
cout << endl;
}
最佳答案
没有任何技巧或任何东西,矩阵 vector 乘法只是 vector 和矩阵行之间的一堆点积。您的代码实际上没有那种结构。实际上将其写为点积(未测试):
for (int row = 0; row < nrows; ++row) {
__m128 acc = _mm_setzero_ps();
// I'm just going to assume the number of columns is a multiple of 4
for (int col = 0; col < ncols; col += 4) {
__m128 vec = _mm_load_ps(&v[col]);
// don't forget it's a matrix, do 2d addressing
__m128 mat = _mm_load_ps(&m[col + ncols * row]);
acc = _mm_add_ps(acc, _mm_mul_ps(mat, vec));
}
// now we have 4 floats in acc and they have to be summed
// can use two horizontal adds for this, they kind of suck but this
// isn't the inner loop anyway.
acc = _mm_hadd_ps(acc, acc);
acc = _mm_hadd_ps(acc, acc);
// store result, which is a single float
_mm_store_ss(&result[row], acc);
}
有一些明显的技巧,例如一次处理多行、重用 vector 的负载以及创建多个独立的依赖链以便更好地利用吞吐量(见下文)。还有一个非常简单的技巧是将 FMA 用于 mul/add 组合,但支持还没有那么广泛(2015 年还没有,但现在在 2020 年已经相当普遍)。
您可以从中构建矩阵-矩阵乘法(如果您更改结果所在的位置),但这不是最佳选择(详见下文)。
一次取四行(未测试):
for (int row = 0; row < nrows; row += 4) {
__m128 acc0 = _mm_setzero_ps();
__m128 acc1 = _mm_setzero_ps();
__m128 acc2 = _mm_setzero_ps();
__m128 acc3 = _mm_setzero_ps();
for (int col = 0; col < ncols; col += 4) {
__m128 vec = _mm_load_ps(&v[col]);
__m128 mat0 = _mm_load_ps(&m[col + ncols * row]);
__m128 mat1 = _mm_load_ps(&m[col + ncols * (row + 1)]);
__m128 mat2 = _mm_load_ps(&m[col + ncols * (row + 2)]);
__m128 mat3 = _mm_load_ps(&m[col + ncols * (row + 3)]);
acc0 = _mm_add_ps(acc0, _mm_mul_ps(mat0, vec));
acc1 = _mm_add_ps(acc1, _mm_mul_ps(mat1, vec));
acc2 = _mm_add_ps(acc2, _mm_mul_ps(mat2, vec));
acc3 = _mm_add_ps(acc3, _mm_mul_ps(mat3, vec));
}
acc0 = _mm_hadd_ps(acc0, acc1);
acc2 = _mm_hadd_ps(acc2, acc3);
acc0 = _mm_hadd_ps(acc0, acc2);
_mm_store_ps(&result[row], acc0);
}
现在每 4 个 FMA 只有 5 个负载,而在未展开行的版本中,每 1 个 FMA 有 2 个负载。还有 4 个独立的 FMA,或者没有 FMA 收缩的加/乘对,无论哪种方式,它都增加了流水线/同时执行的潜力。实际上您可能想要展开更多,例如 Skylake 每个周期可以启动 2 个独立的 FMA,它们需要 4 个周期才能完成,因此要完全占用两个 FMA 单元,您需要 8 个独立的 FMA。作为奖励,对于水平求和,这 3 个水平加法最终的结果相对较好。
不同的数据布局最初似乎是一个缺点,不再可能简单地从矩阵和 vector 中进行 vector 加载并将它们相乘(这会将第一个矩阵的一个微小的行 vector 乘以一个微小的 < em>row 第二个矩阵的 vector ,这是错误的)。但是全矩阵-矩阵乘法可以利用这样一个事实,即它本质上是将一个矩阵乘以许多独立的 vector ,它充满了独立的工作要做。水平和也可以很容易地避免。所以实际上它比矩阵 vector 乘法更方便。
关键是从矩阵 A 中取出一个小列 vector 和从矩阵 B 中取出一个小行 vector ,并将它们相乘成一个小矩阵。与您习惯的做法相比,这听起来可能是相反的,但这样做对于 SIMD 效果更好,因为计算始终保持独立且无水平操作。
例如(未测试,假设矩阵的维度可以被展开因子整除,需要 x64 否则它会用完寄存器)
for (size_t i = 0; i < mat1rows; i += 4) {
for (size_t j = 0; j < mat2cols; j += 8) {
float* mat1ptr = &mat1[i * mat1cols];
__m256 sumA_1, sumB_1, sumA_2, sumB_2, sumA_3, sumB_3, sumA_4, sumB_4;
sumA_1 = _mm_setzero_ps();
sumB_1 = _mm_setzero_ps();
sumA_2 = _mm_setzero_ps();
sumB_2 = _mm_setzero_ps();
sumA_3 = _mm_setzero_ps();
sumB_3 = _mm_setzero_ps();
sumA_4 = _mm_setzero_ps();
sumB_4 = _mm_setzero_ps();
for (size_t k = 0; k < mat2rows; ++k) {
auto bc_mat1_1 = _mm_set1_ps(mat1ptr[0]);
auto vecA_mat2 = _mm_load_ps(mat2 + m2idx);
auto vecB_mat2 = _mm_load_ps(mat2 + m2idx + 4);
sumA_1 = _mm_add_ps(_mm_mul_ps(bc_mat1_1, vecA_mat2), sumA_1);
sumB_1 = _mm_add_ps(_mm_mul_ps(bc_mat1_1, vecB_mat2), sumB_1);
auto bc_mat1_2 = _mm_set1_ps(mat1ptr[N]);
sumA_2 = _mm_add_ps(_mm_mul_ps(bc_mat1_2, vecA_mat2), sumA_2);
sumB_2 = _mm_add_ps(_mm_mul_ps(bc_mat1_2, vecB_mat2), sumB_2);
auto bc_mat1_3 = _mm_set1_ps(mat1ptr[N * 2]);
sumA_3 = _mm_add_ps(_mm_mul_ps(bc_mat1_3, vecA_mat2), sumA_3);
sumB_3 = _mm_add_ps(_mm_mul_ps(bc_mat1_3, vecB_mat2), sumB_3);
auto bc_mat1_4 = _mm_set1_ps(mat1ptr[N * 3]);
sumA_4 = _mm_add_ps(_mm_mul_ps(bc_mat1_4, vecA_mat2), sumA_4);
sumB_4 = _mm_add_ps(_mm_mul_ps(bc_mat1_4, vecB_mat2), sumB_4);
m2idx += 8;
mat1ptr++;
}
_mm_store_ps(&result[i * mat2cols + j], sumA_1);
_mm_store_ps(&result[i * mat2cols + j + 4], sumB_1);
_mm_store_ps(&result[(i + 1) * mat2cols + j], sumA_2);
_mm_store_ps(&result[(i + 1) * mat2cols + j + 4], sumB_2);
_mm_store_ps(&result[(i + 2) * mat2cols + j], sumA_3);
_mm_store_ps(&result[(i + 2) * mat2cols + j + 4], sumB_3);
_mm_store_ps(&result[(i + 3) * mat2cols + j], sumA_4);
_mm_store_ps(&result[(i + 3) * mat2cols + j + 4], sumB_4);
}
}
该代码的要点是很容易安排计算非常 SIMD 友好,有很多独立的算法来饱和浮点单元,同时使用相对较少的负载(否则可能成为瓶颈,甚至抛开它们可能会错过 L1 缓存,只是因为它们太多了)。
您甚至可以使用此代码,但它无法与英特尔 MKL 竞争。特别是对于中型或大型矩阵,平铺非常重要。将它升级到 AVX 很容易。它根本不适合小矩阵,例如乘以两个 4x4 矩阵见 Efficient 4x4 matrix multiplication .
关于c++ - 使用 SSE 的矩阵 vector 和矩阵矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33811888/
我有这个析构函数,它在运行时产生错误“vector 迭代器不可取消引用”。 gridMatrix 是一个 std::vector * > * > * > * > 我添加了 typename 和 typ
我有一个 vector 的 vector ,比方说 std::vector > my2dArray; 现在我想要一个 vector ,其中包含 my2dArray 中 vector 的大小。手动这看起
假设我有一些 vector :v1、v2、v3 假设我还有一个 vector 来保存这些 vList = {v1, v2, v3} 如果我同步了 (vList),这是否意味着 v1、v2 和 v3 也
我正在创建一个 char 的二维 vector 数组作为类变量,但我在将 vector 添加到 vector 数组中时遇到了麻烦。 我正在使用 C++ 11 标准运行 gcc。 我尝试使用 vecto
如何修改 Vec基于 Vec 中某项的信息没有对向量的不可变和可变引用? 我已尝试创建一个最小示例来演示我的特定问题。在我的真实代码中,Builder struct 已经是其他答案提出的中间结构。具体
这个问题在这里已经有了答案: What is the idiomatic Rust way to copy/clone a vector in a parameterized function? (
在我的程序中,我有一个整数 vector 的 vector 。现在我想从 vector 的 vector 中取出一个 vector 并在另一个 vector 容器中对其进行操作,但是我得到了错误...
我得到一个vector>数据由 OpenCV 提供。由于某些原因(例如偏移/缩放),我需要转换数据 Point至Point2f 。我怎样才能做到这一点? 例如: std::vector > conto
我有一个函数,该函数应使用来自字符串类型的给定 vector vector 中的某些元素初始化来自字符串类型的空 vector vector 。我的语法看起来像这样 std::vector> extr
我得到一个vector>数据由 OpenCV 提供。由于某些原因(例如偏移/缩放),我需要转换数据 Point至Point2f 。我怎样才能做到这一点? 例如: std::vector > conto
这里有很多类似的问题,但我没有真正找到任何可以特别回答我的问题的问题。 我有一个 vector 的 vector 作为类的属性。另一个属性是 bucket_count。我想将 vector 的 vec
如果我像这样创建一个 vector 的 vector : std::vector> myVectorOfVectors; 然后用一些东西填充它: std::vector myVector1; myVe
我正在用 C++ 编写自定义 vector 类。我对这样的代码有疑问: vector vec; vec.push_back(one); vec.push_back(two);
这是我发布的问题 c++ program for reading an unknown size csv file (filled only with floats) with constant (b
vector> a; for (int i=0;i v(i+1); iota(v.begin(),v.end(),1); a.push_back(v); } a.erase(a.beg
也许已经晚了,但我不明白为什么我会得到一个超出此代码范围的 vector 下标: int m = 3; int n = 2; std::vector> path(m, std::vector(n, 0
这个问题真的很奇怪,我似乎找不到任何导致它的原因。 所以这里有一个赋值运算符重载函数,鸟类和哺乳动物都是 vector 。 (下面是类) const Register& Register::opera
我怎么去 std::vector> 只是 std::vector> ?有真正有效的方法吗? 最佳答案 我会做这样的事情: #include #include int main() { //
我正在尝试将这些 vector 中的一些数据写入文本文件。当我运行代码时,它返回运行时错误。 Category、Product、Cart、Customer和Address都是struct 包含每个 g
显然它会因您使用的编译器而异,但我很好奇执行 vector> 时的性能问题与 vector*> ,尤其是在 C++ 中。具体来说: 假设您的外部 vector 已满,您想要开始将元素插入到第一个内部
我是一名优秀的程序员,十分优秀!