c++多线程学习(OpenMP、tbb)

目录

OpenMP案例一:

OpenMP案例二:

tbb案例一:

tbb案例二:


OpenMP案例一:

1.设置OpenMP支持

c++多线程学习(OpenMP、tbb)_第1张图片

 2.代码

#include 
#include "omp.h"

using namespace std;

int main() {
	omp_set_num_threads(4);

#pragma omp parallel
	{
		//cout << "hello ,threads" << omp_get_thread_num() << endl;//cout打印格式混乱

		printf("I am Thread %d\n", omp_get_thread_num());
	}

	system("pause");
	return 0;
}

 3.代码结果

c++多线程学习(OpenMP、tbb)_第2张图片

OpenMP案例二:

1.并行化代码:

#include 
#include "omp.h"

using namespace std;
#include   
int main() {
	double dur;
	clock_t startTime, endTime;
	startTime = clock();
#pragma omp parallel for num_threads(8)
	
	for (int i = 0; i < 10000; ++i) {
		printf("i=%d,i am thread %d\n", i, omp_get_num_threads());
	}

	endTime = clock();
	dur = (double)(endTime - startTime);
	printf("Use Time:%f\n", (dur / CLOCKS_PER_SEC));
	system("pause");
	return 0;
}

2.并行化代码结果

 c++多线程学习(OpenMP、tbb)_第3张图片

tbb案例一:

tbb库安装就不说了,网上多的是

1.tbb操作代码和结果

#include 
#include "omp.h"

using namespace std;
#include   
int main() {
	clock_t start, end;
	int k=0;
	double dur;
	start = clock();

	for (int i = 0; i < 200000000; ++i) {
		k+= i;
		k -= i * 0.9;
		k += i * 0.7;
		k -= i * 0.5;
	}
	end = clock();
	dur = (double)(end - start);
	printf("k=%d\n", k);
	printf("Use Time:%f\n", (dur / CLOCKS_PER_SEC));

	system("pause");
	return 0;
}

c++多线程学习(OpenMP、tbb)_第4张图片

如果替换tbb并行模块,再运行耗时是9秒。

2.tbb代码和结果

tbb中并行要使用安全的vector才可以,否则会出现报错

#include 

#include 
#include 
#include 
using namespace std;
#include   
int main() {
	clock_t start, end;
	int k=0;
	double dur;
	start = clock();
	tbb::concurrent_vector vec;
	tbb::parallel_for(0, 20000000, [&](int i) {
		vec.emplace_back(i);
	});

	//for (int i = 0; i < 200000000; ++i) {
	//	k+= i;
	//	k -= i * 0.9;
	//	k += i * 0.7;
	//	k -= i * 0.5;
	//}
	end = clock();
	dur = (double)(end - start);
	printf("k=%d\n", k);
	printf("Use Time:%f\n", (dur / CLOCKS_PER_SEC));

	for (auto& data : vec) {
		std::cout << data << " ";
	}
	system("pause");
	return 0;
}

结果,vec中是按顺序存储的

tbb案例二:

tbb对加速前后,参考文章

为什么我的tbb比串行还慢?-CSDN社区

#include 

#include 
#include 
#include 
using namespace std;
#include   
int main() {
	clock_t start, end;
	int k=0;
	double dur;
	start = clock();
	tbb::concurrent_vector vec;
	//tbb::parallel_for(0, 200000, [&](int i) {
	//	k+= i;
	//	k -= i * 0.9;
	//	
	//	vec.emplace_back(i);
	//	vec.emplace_back(i*0);
	//	vec.emplace_back(i*0);
	//	k += i * 0.7;
	//	k -= i * 0.5;

	//	for (int j = 0; j < 99999; ++j) {
	//		k += 0.0001;
	//	}

	//});

	for (int i = 0; i < 200000; ++i) {
		k += i;
		k -= i * 0.9;

		vec.emplace_back(i);
		vec.emplace_back(i * 0);
		vec.emplace_back(i * 0);
		k += i * 0.7;
		k -= i * 0.5;
		for (int j = 0; j < 99999; ++j) {
			k += 0.0001;
		}
	}
	end = clock();
	dur = (double)(end - start);
	printf("k=%d\n", k);
	printf("Use Time:%f\n", (dur / CLOCKS_PER_SEC));

	//for (auto& data : vec) {
	//	std::cout << data << " ";
	//}
	system("pause");
	return 0;
}

两个循环,一个tbb一个非。注释打开替换即可。tbb时间11秒,非tbb73秒。

你可能感兴趣的:(图像算法与c++,c++,开发语言)