openMP makes your cpp parallel grogramming more easy

Recently, I want to learn some cpp implementations of the statistical learning algorithms, kNN, k-Means, etc. There are so many implementations enlightened me.
What suprised me, is I find a line saying #include <omp.h>, in github repository aditya1601/kmeans-clustering-cpp. After some search, I found it's a header for OpenMP, which is a multi-threading implementation API.
By applying OpenMP, you just need to add a #gragma omp parallel ... line to parallelize your code.

test code

void Test(int n)
{
	for (int i = 0; i < 10000; ++i) {
		//do nothing, just waste time
	}
	stringstream ss;
	ss << this_thread::get_id() << "   ";
	ss << n << "  :  \n";
	printf(ss.str().c_str());
}

int main(int argc, char* argv[])
{
#pragma omp parallel for
	for (int i = 0; i < 100; ++i)
		Test(i);
	system("pause");
	return 0;
}

reference

aditya1601/kmeans-clustering-cpp: A C++ implementation of simple k-means clustering algorithm.
https://github.com/aditya1601/kmeans-clustering-cpp
OpenMP - Wikipedia
https://en.wikipedia.org/wiki/OpenMP
OpenMP教程——从0开始一小时写出并行程序! - 知乎
https://zhuanlan.zhihu.com/p/397670985
ヾ(*)))欢迎回来~
http://home.ustc.edu.cn/~shaojiemike/posts/openmpreductions/

posted on 2022-06-20 01:57  yusisc  阅读(30)  评论(0编辑  收藏  举报

导航