相关疑难解决方法(0)

使用按位AND和popcount而不是实际int或float的大(0,1)矩阵乘法?

对于乘法大二进制矩阵(10Kx20K),我通常要做的是将矩阵转换为浮点数并执行浮点矩阵乘法,因为整数矩阵乘法非常慢(请看这里).

但这一次,我需要执行超过数十万次这样的乘法运算,甚至平均事情上的毫秒级性能提升.


我想要一个intfloat矩阵作为结果,因为产品可能有非0或1的元素.输入矩阵元素都是0或1,因此它们可以存储为单个位.

在行向量和列向量之间的内积中(为了产生输出矩阵的一个元素),乘法简化为按位AND.添加仍然是添加,但我们可以添加具有填充计数功能的位,而不是单独循环它们.

一些其他布尔/二进制矩阵函数或位而不是计数它们,产生位矩阵结果,但这不是我需要的.


下面是一个示例代码,显示将问题形成为std::bitset, AND并且count操作比矩阵乘法更快.

#include <iostream>
using std::cout; using std::endl;
#include <vector>
    using std::vector;
#include <chrono>
#include <Eigen/Dense>
    using Eigen::Map; using Eigen::Matrix; using Eigen::MatrixXf;
#include <random>
    using std::random_device; using std::mt19937; using std::uniform_int_distribution;
#include <bitset>
    using std::bitset;

using std::floor;

const int NROW = 1000;
const int NCOL = 20000;

const float DENSITY = 0.4;
const float DENOMINATOR = 10.0 - (10*DENSITY);

void fill_random(vector<float>& vec) { …
Run Code Online (Sandbox Code Playgroud)

c++ sse avx bitset matrix-multiplication

8
推荐指数
1
解决办法
1145
查看次数

标签 统计

avx ×1

bitset ×1

c++ ×1

matrix-multiplication ×1

sse ×1