概述
sections 的主要功能是将一个任务分成独立的几个section,每个section由不同的线程来处理。换句话说,section语句用在sections语句里,用来将sections语句里的代码划分成几个不同的段,每段都并行执行吗,sections指令和for指令一样,必须与parallel联合使用才会并行化,否则串行执行。
例子1.求素数的个数,程序如下:
// sushuo1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <omp.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <Windows.h>
#define NUM_THREADS 2
int _tmain(int argc, _TCHAR* argv[])
{
long start = 3,end = 10000000;
bool prime,prime1;
clock_t t1,t2;
int sum1 = 0,sum = 0;
t1 =clock();
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel sections reduction(+:sum)
{
#pragma omp section
{
for (int m= start; m<= end; m = m+4)
{
prime = true;
int k= (int)sqrt((double)m);
for(int i= 2; i<=k;i ++)
if ( m%i == 0)
{
prime = false;
break;
}
if (prime)
{
// printf("++++++++++++::: %dn",omp_get_thread_num());
// printf("parallel 素数 = %dn",m);
sum ++;
}
}
}
#pragma omp section
{
for (int m= start + 2; m<= end; m = m+4)
{
prime1 = true;
int k = (int)sqrt((double)m);
for(int i= 2; i<=k; i++)
if ( m % i == 0)
{
prime1 =false;
break;
}
if (prime1)
{
// printf("--------------- %dn",omp_get_thread_num());
// printf("parallel 素数 = %dn",m);
sum ++;
}
}
}
}
t2 = clock();
printf("parallel 素数sum = %dn",sum);
printf("parallel time=%dn",(t2-t1));
//---------------------------------------------------------
int i,k;
sum = 0;
t1 = clock();
for ( int m= start; m<= end; m = m+2)
{
prime = true;
k= (int)sqrt((double)m);
for(i= 2;i<=k;i++)
if (m%i ==0)
{
prime =false;
break;
}
if (prime)
{
// printf("serial 素数 = %dn",m);
sum ++;
}
}
t2 = clock();
printf("serial 素数sum = %dn",sum);
printf("serial time=%dn",(t2-t1));
system("pause");
return 0;
}
运行结果如下:
例子2.求和运算,程序如下:
// SectionsAdd.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "windows.h"
#include <time.h>
#include "omp.h"
#define NUM_THREADS 2
int _tmain(int argc, _TCHAR* argv[])
{
omp_set_num_threads(NUM_THREADS);
long long sum = 0;
long long temp = 0l;
clock_t t1 = clock();
#pragma omp parallel sections reduction(+:sum)
{
#pragma omp section
{
for (long i = 0; i<=1000000000; i= i+NUM_THREADS)
{
sum+=i;
}
}
#pragma omp section
{
for (long i = 1; i<=1000000000; i= i+NUM_THREADS)
{
sum +=i;
}
}
}
clock_t t2 = clock();
printf("sum=%lldn",sum);
printf("parallel time=%dn",(t2-t1));
sum = 0;
t1 = clock();
for (long i=1; i<= 1000000000;i++)
{
sum = sum + i;
}
t2 = clock();
printf("sum=%lldn",sum);
printf("serial time=%dn",(t2-t1));
system("pause");
return 0;
}
运行结果如下:
例子3.求积分(圆周率)运算,程序如下:
// sectionspi.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "windows.h"
#include <time.h>
#include "omp.h"
static long num_steps = 100000000;
double step;
#define NUM_THREADS 2
int _tmain(int argc, _TCHAR* argv[])
{
int i;
clock_t t1,t2;
double sum = 0 ,pi = 0.0;
step = 1.0/(double) num_steps;
omp_set_num_threads(NUM_THREADS);
t1 = clock();
#pragma omp parallel sections reduction(+:sum)
{
#pragma omp section
{
double x;
for (int i=omp_get_thread_num();i <num_steps;i+=NUM_THREADS)
{
x=(i+0.5)*step;
sum = sum + 4.0/(1.0 + x*x);
}
}
#pragma omp section
{
double x;
for (int i=omp_get_thread_num();i <num_steps;i+=NUM_THREADS)
{
x=(i+0.5)*step;
sum = sum + 4.0/(1.0 + x*x);
}
}
}
pi = step * sum;
t2 = clock();
printf("pi = %.15fn",pi);
printf("parallel time=%dn",(t2-t1));
t1 = clock();
sum =0;
double x;
for (int i=0;i <num_steps;i++)
{
x=(i+0.5)*step;
sum = sum + 4.0/(1.0 + x*x);
}
pi = step * sum;
t2 = clock();
printf("pi = %.15fn",pi);
printf("serial time=%dn",(t2-t1));
printf("星星笔记n");
system("pause");
return 0;
}
运行结果如下:
最后
以上就是笑点低楼房为你收集整理的OpenMP 之 sections 求素数、求和、求积分圆周率(星星笔记)的全部内容,希望文章能够帮你解决OpenMP 之 sections 求素数、求和、求积分圆周率(星星笔记)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复