并行计算—OpenMP—统计素数的个数
2016-12-12 12:06
453 查看
//统计素数个数的并行算法和OpenMP并行编程。 #include "stdafx.h" #include <omp.h> #include <iostream> #include <time.h> #include <math.h> #include <stdlib.h> using namespace std; int isPrime(int lyy_num) //判断是否为素数 { int lyy_flag=1; int lyy_s=sqrt(lyy_num*1.0); for(int j=2;j<=lyy_s;j++) { if(lyy_num%j==0) { lyy_flag=0; break; } } return lyy_flag; } int _tmain(int argc, _TCHAR* argv[]) { omp_set_num_threads(2); int lyy_n,lyy_num=0; clock_t t1, t2; cin>>lyy_n; //并行 t1=clock(); #pragma omp parallel for reduction(+:lyy_num) //reduction方法实现并行 for(int i=2;i<=lyy_n;i++) lyy_num+=isPrime(i); t2=clock(); cout<<lyy_num<<endl; cout<<"parallel time="<<(t2-t1)<<endl; //串行 lyy_num=0; t1=clock(); for(int i=2;i<=lyy_n;i++) lyy_num+=isPrime(i); t2=clock(); cout<<lyy_num<<endl; cout<<"serial time="<<(t2-t1)<<endl; system("pause"); return 0; }
运行结果:
分析:
加速比计算:219/156=1.404
相关文章推荐
- 并行计算----5种方式实现判断是不是素数.Net,Win32,JavaRunnable,OpenMp,JavaThread
- 并行计算----5种方式实现判断是不是素数.Net,Win32,JavaRunnable,OpenMp,JavaThread
- OpenMP 线程化(Threading)基础(1)--并行计算简介
- MPI实现并行计算统计数据
- 并行计算实现判断一个数是不是素数--Win32和.Net两种方式结合
- 如何利用VS的代码优化和openmp并行计算提高程序运行速度
- (一)初步了解并行计算、OpenMP
- 完数的OpenMP并行程序设计-并行计算
- 并行计算实现判断一个数是不是素数--Win32和.Net两种方式结合
- 并行计算大作业之多边形相交(OpenMP、MPI、Java、Windows)
- OpenMP并行计算程序设计基础下载地址
- 关于高性能计算(并行计算)的知识记录汇总(OpenMP、OpenCL、CUDA、TBB)
- 并行计算之OpenMP入门简介
- 并行计算之OpenMP中的任务调度
- openmp并行计算
- 并行计算—OpenMP—临界区critical
- java 多线程并行计算之求素数(星星笔记)
- OpenMP并行计算PI的值
- 文章标题 并行计算 -- OpenMP简介和计算实例 (计算pi)
- 并行计算—OpenMP—负载均衡