首页 > 其他分享 >使用benchmark比较循环嵌套与strassen求解矩阵乘法的性能

使用benchmark比较循环嵌套与strassen求解矩阵乘法的性能

时间:2023-04-05 18:23:02浏览次数:42  
标签:matrix Rmatrix 嵌套 int auto benchmark ++ strassen Matrix

#include <benchmark/benchmark.h>

#include <iostream>
#include <random>
#include <vector>

using namespace std;

static const int n = 200;
static const int _lrange = 0;
static const int _rrange = 10;
static const int _iter = 1;

using Matrix = vector<vector<int>>;

auto matrix_mult(Matrix _Amatrix, Matrix _Bmatrix, int n) {
	Matrix _Rmatrix(n, vector<int>(n, 0));
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			for (int k = 0; k < n; k++)
				_Rmatrix[i][j] += _Amatrix[i][k] * _Bmatrix[k][j];
	return _Rmatrix;
}

Matrix operator+(Matrix _Amatrix, Matrix _Bmatrix) {
	int n = _Amatrix.size();
	Matrix _Rmatrix(n, vector<int>(n, 0));
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			_Rmatrix[i][j] = _Amatrix[i][j] + _Bmatrix[i][j];
	return _Rmatrix;
}
Matrix operator-(Matrix _Amatrix, Matrix _Bmatrix) {
	int n = _Amatrix.size();
	Matrix _Rmatrix(n, vector<int>(n, 0));
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			_Rmatrix[i][j] = _Amatrix[i][j] - _Bmatrix[i][j];
	return _Rmatrix;
}

Matrix slice_matrix(Matrix _matrix, int row, int col, int n) {
	Matrix _Rmatrix(n, vector<int>(n, 0));
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++) _Rmatrix[i][j] = _matrix[row + i][col + j];
	return _Rmatrix;
}

Matrix merge_matrix(Matrix _M11, Matrix _M12, Matrix _M21, Matrix _M22, int n) {
	Matrix _Rmatrix(n, vector<int>(n, 0));
	for (int i = 0; i < n / 2; i++)
		for (int j = 0; j < n / 2; j++) _Rmatrix[i][j] = _M11[i][j];
	for (int i = 0; i < n / 2; i++)
		for (int j = 0; j < n / 2; j++) _Rmatrix[i][n / 2 + j] = _M12[i][j];
	for (int i = 0; i < n / 2; i++)
		for (int j = 0; j < n / 2; j++) _Rmatrix[n / 2 + i][j] = _M21[i][j];
	for (int i = 0; i < n / 2; i++)
		for (int j = 0; j < n / 2; j++) _Rmatrix[n / 2 + i][n / 2 + j] = _M22[i][j];
	return _Rmatrix;
}

Matrix matrix_mult_strassen(Matrix _Amatrix, Matrix _Bmatrix, int n) {
	Matrix _Rmatrix(n, vector<int>(n, 0));
	if (n == 1) {
		_Rmatrix[0][0] = _Amatrix[0][0] * _Bmatrix[0][0];
		return _Rmatrix;
	}

	auto _A11 = slice_matrix(_Amatrix, 0, 0, n / 2);
	auto _A12 = slice_matrix(_Amatrix, 0, n / 2, n / 2);
	auto _A21 = slice_matrix(_Amatrix, n / 2, 0, n / 2);
	auto _A22 = slice_matrix(_Amatrix, n / 2, n / 2, n / 2);

	auto _B11 = slice_matrix(_Bmatrix, 0, 0, n / 2);
	auto _B12 = slice_matrix(_Bmatrix, 0, n / 2, n / 2);
	auto _B21 = slice_matrix(_Bmatrix, n / 2, 0, n / 2);
	auto _B22 = slice_matrix(_Bmatrix, n / 2, n / 2, n / 2);

	auto _S1 = _B12 - _B22;
	auto _S2 = _A11 + _A12;
	auto _S3 = _A21 + _A22;
	auto _S4 = _B21 - _B11;
	auto _S5 = _A11 + _A22;
	auto _S6 = _B11 + _B22;
	auto _S7 = _A12 - _A22;
	auto _S8 = _B21 + _B22;
	auto _S9 = _A11 - _A21;
	auto _S10 = _B11 + _B12;

	auto _P1 = matrix_mult_strassen(_A11, _S1, n / 2);
	auto _P2 = matrix_mult_strassen(_S2, _B22, n / 2);
	auto _P3 = matrix_mult_strassen(_S3, _B11, n / 2);
	auto _P4 = matrix_mult_strassen(_A22, _S4, n / 2);
	auto _P5 = matrix_mult_strassen(_S5, _S6, n / 2);
	auto _P6 = matrix_mult_strassen(_S7, _S8, n / 2);
	auto _P7 = matrix_mult_strassen(_S9, _S10, n / 2);

	auto _R11 = _P5 + _P4 - _P2 + _P6;
	auto _R12 = _P1 + _P2;
	auto _R21 = _P3 + _P4;
	auto _R22 = _P5 + _P1 - _P3 - _P7;

	_Rmatrix = merge_matrix(_R11, _R12, _R21, _R22, n);
	return _Rmatrix;
}

static void BM_demo_1(benchmark::State& state) {
	for (auto _ : state) {
		state.PauseTiming();
		Matrix a_matrix, b_matrix;

		random_device rd;
		mt19937 gen(rd());
		uniform_int_distribution<int> dist(_lrange, _rrange);
		for (int i = 0; i < n; ++i) {
			vector<int> row;
			for (int j = 0; j < n; ++j) {
				row.push_back(dist(gen));
			}
			a_matrix.push_back(row);
		}
		for (int i = 0; i < n; ++i) {
			vector<int> row;
			for (int j = 0; j < n; ++j) {
				row.push_back(dist(gen));
			}
			b_matrix.push_back(row);
		}
		state.ResumeTiming();
		matrix_mult(a_matrix, b_matrix, n);
	}
}
BENCHMARK(BM_demo_1)->Iterations(_iter);

static void BM_demo_2(benchmark::State& state) {
	for (auto _ : state) {
		state.PauseTiming();
		Matrix a_matrix, b_matrix;

		random_device rd;
		mt19937 gen(rd());
		uniform_int_distribution<int> dist(_lrange, _rrange);
		for (int i = 0; i < n; ++i) {
			vector<int> row;
			for (int j = 0; j < n; ++j) {
				row.push_back(dist(gen));
			}
			a_matrix.push_back(row);
		}
		for (int i = 0; i < n; ++i) {
			vector<int> row;
			for (int j = 0; j < n; ++j) {
				row.push_back(dist(gen));
			}
			b_matrix.push_back(row);
		}
		state.ResumeTiming();
		matrix_mult_strassen(a_matrix, b_matrix, n);
	}
}
BENCHMARK(BM_demo_2)->Iterations(_iter);

BENCHMARK_MAIN();

标签:matrix,Rmatrix,嵌套,int,auto,benchmark,++,strassen,Matrix
From: https://www.cnblogs.com/fjnhyzCYL/p/17290237.html

相关文章

  • 使用benchmark比较分治法与归纳法求解最大子数组问题的性能
    #include<benchmark/benchmark.h>#include<algorithm>#include<deque>#include<functional>#include<iostream>#include<random>#include<string>#include<vector>usingnamespacestd;staticconstint......
  • Chrome 112 发布,删除 Chrome Apps、支持 CSS 嵌套
    时隔一个月时间,Google正式发布了Chrome112版本,该版本删除了ChromeApps、支持CSS嵌套、改进了<dialog>等。ChromeApps过去,ChromeApps是一种被视为向用户提供轻量级网站体验的方式。然而,它们从未像浏览器扩展或标准网站那样大受欢迎。为了跟上时代的变化,改善用......
  • 使用benchmark比较插入排序与归并排序性能
    #include<benchmark/benchmark.h>#include<algorithm>#include<deque>#include<functional>#include<iostream>#include<random>#include<string>#include<vector>usingnamespacestd;staticconstint......
  • android 解决ScrollView嵌套ListView的问题,不能全屏,全屏不能显示下面控件
    在开发中遇到ScrollView嵌套ListView的问题,最开始发出不能全屏,效果是这样的;但我想要的效果是这样的:下面看一下布局文件:<?xmlversion="1.0"encoding="utf-8"?><ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_p......
  • 项目一众筹网05_01_[树形结构开发]菜单维护-树形结构基础知识、自关联、zTree的介绍和
    树形结构开发]菜单维护文章目录树形结构开发]菜单维护01-菜单维护-树形结构基础知识-上==在数据库中怎么去表示树形关系====其实这就是自关联====我们怎么识别根节点==02-菜单维护-树形结构基础知识-下03-页面显示树形结构-后端-逆向工程==开发的细节:如何避免空指针异常:初始化==04-......
  • Go语言中本地包的嵌套调用方法
    最近学习区块链,在使用Go语言的过程中遇到本地包之间相互调用的问题,问题分为两个阶段:1.如何调用本地包(参考文章:https://blog.csdn.net/taoerchun/article/details/10482770......
  • uniapp app和H5网页的通信 app嵌套H5通过webview
    app发送信息给H5这个很简单,只要在网址上面携带参数就可以上面的例子是在app里面通过webview来嵌套网页,通过src的地址,可以携带参数,这样打开网页通过网址就可以获得传过来......
  • [FastAPI-31]嵌套注入
    fromtypingimportUnionfromfastapiimportDepends,FastAPIapp=FastAPI()'''嵌套注入-路径函数get_name需要的形参`username_or_nickname`有依赖条件,所以F......
  • 关于python编程中try...except的嵌套使用说明及注意事项
    今天笔者在写一个脚本时,情况比较复杂,在脚本中使用了try...except的嵌套,遇到了一些与预期不一样的结果于是笔者又研究了一下,try...except的嵌套使用,首先有一点是肯定的,那......
  • 嵌套 if 三次猜随机数
    '''定义一个数字1到10随机产生,通过3次判断来猜出数字'''importrandomnum=random.randint(1,10)print(num)ifint(input("第一次,猜猜数字是:"))!=num:p......