zoukankan      html  css  js  c++  java
  • openmp 并行求完数

    
    
    // GetWanShu.cpp : 定义控制台应用程序的入口点。
    //
    
    
    
    #include "stdafx.h"
    #include "omp.h"
    #include <Windows.h>
    #include "time.h"
    //函数声明
    void getWanShuBySection();
    void getWanShu();
    void getWanShuByReduction();
    int _tmain(int argc, _TCHAR* argv[])
    {
    	//串行实现求完数
    	clock_t start_LiXingLe=clock();
    	 getWanShu();
    	 clock_t end_LiXingLe=clock();
    	 double chuanxingTime_LXL=end_LiXingLe-start_LiXingLe;
    	 printf("             串行时间为:%f
    ",chuanxingTime_LXL);
    	 //section 并行方法实现
    	 getWanShuBySection();
    	 //Reduction 并行方法实现
         getWanShuByReduction();
    
    
    	 //for 并行方法实现
    	 omp_set_num_threads(4);
    
    	 start_LiXingLe=clock();
    #pragma omp parallel
    	#pragma omp for
    		 
    				for (int n = 1; n <= 10000; n++)
    				{
    					  int  sum = 0;
    					
    					  for (int i = 1; i <= (n/2); i++)  //除到原来一半就可以了
    					  {
    						   if (n % i == 0)
    						   {
    							sum += i;
    						   }
    					  }
    					  if (sum == n)
    					  {
    						 printf ("%d
    ", sum);
    					  }
    				 }
    		 
    		 end_LiXingLe=clock();
    		  double parallelTime_LXL=end_LiXingLe-start_LiXingLe;
    		  printf("              for并行时间为 :%f
    ",parallelTime_LXL);
    		    printf("for加速比为:%f
    ",chuanxingTime_LXL/ parallelTime_LXL);
    
    	return 0;
    }
    void getWanShuBySection(){
    
    	 omp_set_num_threads(4);
    
    	clock_t start_LiXingLe=clock();
    #pragma omp parallel
    	{
    	#pragma omp sections
    		{
    		
    		#pragma omp section
    			for (int n = 1; n < 2500; n++)
    				{
    					  int  sum = 0;
    					  for (int i = 1; i <= (n/2); i++)  //除到原来一半就可以了
    					  {
    						   if (n % i == 0)
    						   {
    							sum += i;
    						   }
    					  }
    					  if (sum == n)
    					  {
    						 printf ("%d
    ", sum);
    					  }
    				 }
    			#pragma omp section
    			for (int n = 2500; n < 5000; n++)
    				{
    					  int  sum = 0;
    					  for (int i = 1; i <= (n/2); i++)  //除到原来一半就可以了
    					  {
    						   if (n % i == 0)
    						   {
    							sum += i;
    						   }
    					  }
    					  if (sum == n)
    					  {
    						 printf ("%d
    ", sum);
    					  }
    				 }
    			#pragma omp section
    			for (int n = 5000; n <= 7500; n++)
    				{
    					  int  sum = 0;
    					  for (int i = 1; i <= (n/2); i++)  //除到原来一半就可以了
    					  {
    						   if (n % i == 0)
    						   {
    							sum += i;
    						   }
    					  }
    					  if (sum == n)
    					  {
    						 printf ("%d
    ", sum);
    					  }
    				 }
    			#pragma omp section
    			for (int n = 7500; n <= 10000; n++)
    				{
    					  int  sum = 0;
    					  for (int i = 1; i <= (n/2); i++)  //除到原来一半就可以了
    					  {
    						   if (n % i == 0)
    						   {
    							sum += i;
    						   }
    					  }
    					  if (sum == n)
    					  {
    						 printf ("%d
    ", sum);
    					  }
    				 }
    		
    		
    		}
    	
    	}
    
    	clock_t end_LiXingLe=clock();
    	 printf("            section并行时间为 :%d
    ",end_LiXingLe-start_LiXingLe);
    
    
    }
    void getWanShu(){
    
    	for (int n = 1; n <= 10000; n++)
     {
    	  int  sum = 0;
    	  for (int i = 1; i <= (n/2); i++)  //除到原来一半就可以了
    	  {
    		   if (n % i == 0)
    		   {
    			sum += i;
    		   }
    	  }
    	  if (sum == n)
    	  {
    		 printf ("%d
    ", sum);
    	  }
     }
    
    }
    
    
    
    void getWanShuByReduction(){
    
    	 omp_set_num_threads(5);
    
    	clock_t start_LiXingLe=clock();
    		for (int n = 1; n <= 10000; n++)
    		 {
    			  int  sum = 0;
    			  #pragma omp parallel for reduction(+:sum)
    			  for (int i = 1; i <= (n/2); i++)  //除到原来一半就可以了
    			  {
    				   if (n % i == 0)
    				   {
    					sum += i;
    				   }
    			  }
    			  if (sum == n)
    			  {
    				 printf ("%d
    ", sum);
    			  }
    		 }
       clock_t end_LiXingLe=clock();
        printf("             reduction并行时间为 :%d
    ",end_LiXingLe-start_LiXingLe);
    
    }
    
    
    
    
    
    
    
    
    
    运行结果:
    
    






  • 相关阅读:
    TCP三次握手(建立连接)/四次挥手(关闭连接)
    STL
    Hadoop- 集群时间同步
    Hadoop- MapReduce在实际应用中常见的调优
    Hadoop- HDFS的Safemode
    Hadoop- 分布式资源管理YARN架构讲解
    Hadoop- Hadoop详解
    Linux- Linux自带定时调度Crontab使用详解
    Spark- Spark Yarn模式下跑yarn-client无法初始化SparkConext,Over usage of virtual memory
    Zeppelin- Linux下安装Zeppelin
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3375692.html
Copyright © 2011-2022 走看看