test_op.cpp 1019 Bytes
Newer Older
108 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <iostream>
#include <omp.h> 
using namespace std;



// 0.pragma
void fun1()
//定义6个线程,每个线程输出"test"
{
#pragma omp parallel num_threads(6)
	{
		cout << "test" << endl;
	}
	system("pause");
}

//1.pragma for
void fun2(int a)
{
#pragma omp parallel for num_threads(6)  
	for (int i = 0; i < 1200000000000; i++)
	{
		printf("OpenMP Test, 线程编号为: %d\n", omp_get_thread_num());
		a += i;
	}
	printf("a的值为%d", a);
	system("pause");
	
}
class blog
{
public:
	int test(int num_thread);
	
};
int blog::test(int num_thread)
{
	long sum = 0;
	int n = 1000;
	double factor = 1;
#pragma omp parallel for num_threads(num_thread) reduction(+:sum) private(factor)
	for (int i = 0; i < n; i++)
	{
		if (i % 2 == 0)
			factor = 1.0;
		else
		{
			factor = -1.0;
		};
		sum += factor / (2 * i + 1);
	}
	return sum;
}

//sections & section
//section语句是用在sections语句里用来将sections语句里的代码划分成几个不同的段,每段都并行执行。

long main()
{
	blog t;
	t.test(4);
	cout << "result" << t.test(4) << endl;

}