concurrency::parallel_for 计算两个方形矩阵的乘积

摘自MSDN:点击打开链接

// compile with: /EHsc
#include <windows.h>
#include <ppl.h>
#include <iostream>
#include <random>

using namespace concurrency;
using namespace std;

// Calls the provided work function and returns the number of milliseconds 
// that it takes to call that function.
template <class Function>
__int64 time_call(Function&& f)
{
	__int64 begin = GetTickCount();
	f();
	return GetTickCount() - begin;
}

// Creates a square matrix with the given number of rows and columns.
double** create_matrix(size_t size);

// Frees the memory that was allocated for the given square matrix.
void destroy_matrix(double** m, size_t size);

// Initializes the given square matrix with values that are generated
// by the given generator function.
template <class Generator>
double** initialize_matrix(double** m, size_t size, Generator& gen);

// Computes the product of two square matrices.
//传统的串行方式
void matrix_multiply(double** m1, double** m2, double** result, size_t size)
{
	for (size_t i = 0; i < size; i++)
	{
		for (size_t j = 0; j < size; j++)
		{
			double temp = 0;
			for (int k = 0; k < size; k++)
			{
				temp += m1[i][k] * m2[k][j];
			}
			result[i][j] = temp;
		}
	}
}

// Computes the product of two square matrices in parallel.
//这里仅并行化外层循环,这是因为该循环执行的工作足够多,可以从并行处理的开销中受益。 
//如果并行化内层循环,则将不会获得性能上的提升,这是因为内层循环执行的少量工作不能抵消并行处理的开销。
//因此,仅并行化外部循环是在大多数系统上最大程度地发挥并发优势的最佳方式。
void parallel_matrix_multiply(double** m1, double** m2, double** result, size_t size)
{
	parallel_for(size_t(0), size, [&](size_t i)
	{
		for (size_t j = 0; j < size; j++)
		{
			double temp = 0;
			for (int k = 0; k < size; k++)
			{
				temp += m1[i][k] * m2[k][j];
			}
			result[i][j] = temp;
		}
	});
}

int main()
{
	// The number of rows and columns in each matrix.
	// TODO: Change this value to experiment with serial 
	// versus parallel performance. 
	const size_t size = 750;

	// Create a random number generator.
	mt19937 gen(42);

	// Create and initialize the input matrices and the matrix that
	// holds the result.
	double** m1 = initialize_matrix(create_matrix(size), size, gen);
	double** m2 = initialize_matrix(create_matrix(size), size, gen);
	double** result = create_matrix(size);

	// Print to the console the time it takes to multiply the 
	// matrices serially.
	wcout << L"serial: " << time_call([&] {
		matrix_multiply(m1, m2, result, size);
	}) << endl;

	// Print to the console the time it takes to multiply the 
	// matrices in parallel.
	wcout << L"parallel: " << time_call([&] {
		parallel_matrix_multiply(m1, m2, result, size);
	}) << endl;

	// Free the memory that was allocated for the matrices.
	destroy_matrix(m1, size);
	destroy_matrix(m2, size);
	destroy_matrix(result, size);
}

// Creates a square matrix with the given number of rows and columns.
double** create_matrix(size_t size)
{
	double** m = new double*[size];
	for (size_t i = 0; i < size; ++i)
	{
		m[i] = new double[size];
	}
	return m;
}

// Frees the memory that was allocated for the given square matrix.
void destroy_matrix(double** m, size_t size)
{
	for (size_t i = 0; i < size; ++i)
	{
		delete[] m[i];
	}
	delete m;
}

// Initializes the given square matrix with values that are generated
// by the given generator function.
template <class Generator>
double** initialize_matrix(double** m, size_t size, Generator& gen)
{
	for (size_t i = 0; i < size; ++i)
	{
		for (size_t j = 0; j < size; ++j)
		{
			m[i][j] = static_cast<double>(gen());
		}
	}
	return m;
}
编译器:vs2013 update5

运行环境:i7-4790U、8GRAM

x64 debug模式下运行结果:

serial: 5850
parallel: 1404
请按任意键继续. . .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值