C++多线程并行计算的示例

示例:计算[1,10^{9}]范围内自然数的平方根之和

CPU: i7-7820HQ,8核

1.使用std::package_task和std::future

#include <thread>
#include <future>
#include <numeric>
#include <iostream>
#include <vector>
#include <chrono>


double accumulate(int min, int max)
{
	double sum = 0;
	for (int i = min; i <= max; ++i)
	{
		sum += sqrt(i);
	}
	return sum;
}


double concurrent_task(int min, int max)
{
	//每个线程的执行结果存放在容器中
	std::vector<std::future<double>> results;

	unsigned concurrent_count = std::thread::hardware_concurrency();
	min = 0;
	for (int i = 0; i < concurrent_count; i++)
	{
		std::packaged_task<double(int, int)> task(accumulate); //产生一个未就绪的共享状态
		results.push_back(task.get_future());

		int range = max / concurrent_count * (i + 1); //任务平均分配到各个线程中
		std::thread t(std::move(task), min, range); //通过新线程执行任务
		t.detach();

		min = range + 1;
	}

	std::cout << "threads create finish" << std::endl;
	double sum = 0;
	for (auto& r : results) {
		sum += r.get(); // 通过future获取每个任务的结果,即获取共享状态
	}
	return sum;
}

int main()
{
	auto start_time = std::chrono::steady_clock::now();

	double r = concurrent_task(1, 10e8);

	auto end_time = std::chrono::steady_clock::now();

	auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();

	std::cout << "Concurrent task finish, " << ms << " ms consumed, Result: " << r << std::endl;

	return 0;
}

耗时结果如下:

 

2.std::async和std::future

#include <thread>
#include <future>
#include <numeric>
#include <iostream>
#include <vector>
#include <chrono>


double accumulate(int min, int max)
{
	double sum = 0;
	for (int i = min; i <= max; ++i)
	{
		sum += sqrt(i);
	}
	return sum;
}

double concurrent_async(int min, int max)
{
	std::vector<std::future<double>> results; 
	unsigned concurrent_count = std::thread::hardware_concurrency();
	double sum = 0;
	min = 0;
	for (int i = 0; i < concurrent_count; i++)
	{
		int range = max / concurrent_count * (i + 1);
		auto f = std::async(std::launch::async, accumulate, min, range);
		results.push_back(std::move(f));
		min = range + 1;
	}
	
	int size = results.size();
	for (int i = 0; i < size; ++i)
	{
		//如果选择异步执行策略,调用get时,如果异步执行没有结束,get会阻塞当前调用线程,
		//直到异步执行结束并获得结果,如果异步执行已经结束,不等待获取执行结果;
		//如果选择同步执行策略,只有当调用get函数时,同步调用才真正执行,这也被称为函数调用被延迟
		sum += results[i].get();
	}
	return sum;
}

int main()
{
	std::cout << "waiting for the result..." << std::endl;

	auto start_time = std::chrono::steady_clock::now();

	double r = concurrent_async(1, 10e8);

	auto end_time = std::chrono::steady_clock::now();

	auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();

	std::cout << "Concurrent task finish, " << ms << " ms consumed, Result: " << r << std::endl;

	return 0;
}

耗时结果如下:

3. OpenMP算法

#include <iostream>
#include <chrono>

using namespace std;

double calculate(int min, int max) {
	double sum = 0.0;
#pragma omp parallel for reduction(+:sum)
	for (int i = min; i <= max; ++i)
	{
		sum += sqrt(i);
	}
	return sum;
}

int main() 
{
	std::cout << "waiting for the result..." << std::endl;

	auto time1 = chrono::steady_clock::now();
	auto sum = calculate(1, 10e8);
	auto time2 = chrono::steady_clock::now();
	auto duration = chrono::duration_cast<chrono::milliseconds>(time2 - time1).count();
	cout << "Concurrent task finish, " << duration  << "ms consumed, Result: " << sum << endl;
}

耗时结果如下:

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要加速warpAffine C++例程,可以使用多线程并行计算。具体步骤如下: 1. 将输入图像分割成多个小块,每个小块都可以在单独的线程上处理。 2. 创建线程池,以便可以同时运行多个线程。 3. 在每个线程上运行warpAffine函数,以处理对应的小块。 4. 等待所有线程完成,然后将小块重新组合成最终输出图像。 下面是一个示例代码,演示如何使用多线程并行计算来加速warpAffine C++例程: ```c++ #include <opencv2/opencv.hpp> #include <iostream> #include <thread> #include <vector> using namespace cv; using namespace std; void warpAffineThread(Mat src, Mat dst, Mat M, Rect roi) { Mat tmp; warpAffine(src(roi), tmp, M, roi.size()); tmp.copyTo(dst(roi)); } void warpAffineParallel(Mat src, Mat dst, Mat M, int numThreads) { vector<thread> threads(numThreads); vector<Rect> rois(numThreads); int heightPerThread = src.rows / numThreads; int remainder = src.rows % numThreads; int y = 0; for (int i = 0; i < numThreads; i++) { int height = heightPerThread + (i < remainder ? 1 : 0); rois[i] = Rect(0, y, src.cols, height); y += height; threads[i] = thread(warpAffineThread, src, dst, M, rois[i]); } for (int i = 0; i < numThreads; i++) { threads[i].join(); } } int main() { Mat src = imread("input.jpg"); Mat dst(src.size(), src.type()); Mat M = getRotationMatrix2D(Point2f(src.cols / 2, src.rows / 2), 30, 1); int numThreads = thread::hardware_concurrency(); warpAffineParallel(src, dst, M, numThreads); imwrite("output.jpg", dst); return 0; } ``` 在上面的代码中,warpAffineParallel函数将输入图像分割成多个小块,并在每个小块上启动一个线程来运行warpAffineThread函数。然后,该函数等待所有线程完成,并将小块重新组合成最终输出图像。请注意,该代码使用了C++11的标准库,因此需要使用编译器的C++11支持。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值