zoukankan      html  css  js  c++  java
  • 使用并行区域方法进行求和

    // OpenMP1.cpp : 定义控制台应用程序的入口点。
    
    // 使用并行区域方法进行求和
    
    #include "stdafx.h"
    #include <time.h>
    #include <windows.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 sumtmp[NUM_THREADS];
        clock_t t1 = clock();
    
        #pragma omp parallel
        {
            long i;
            long id = omp_get_thread_num();
            long long temp = 0l;
    
            for(i = id; i <= 1000000000; i = i + NUM_THREADS) {
                temp += i;
            }
            sumtmp[id] = temp;
        }
    
        for(long i = 0; i < NUM_THREADS; i++) {
            sum += sumtmp[i];
        }
    
        clock_t t2 = clock();
        printf("sum = %lld
    ", sum);
        printf("parallel time = %d
    ", t2 - t1);
    
    
        sum = 0;
        t1 = clock();
        for(long i = 1; i <= 1000000000; i += 1) {
            sum = sum + i;
        }
        t2 = clock();
        printf("sum = %lld
    ", sum);
        printf("serial time = %d
    ", (t2 - t1));
        system("pause");
    
        return 0;
    }
    

      

  • 相关阅读:
    51nod 1179 最大的最大公约数 (数论)
    POJ 3685 二分套二分
    POJ 3045 贪心
    LIC
    HDU 1029 Ignatius and the Princess IV
    HDU 1024 Max Sum Plus Plus
    HDU 2389 Rain on your Parade
    HDU 2819 Swap
    HDU 1281 棋盘游戏
    HDU 1083 Courses
  • 原文地址:https://www.cnblogs.com/mjn1/p/10893666.html
Copyright © 2011-2022 走看看