谐波进展和c ++ openMP

时间:2012-11-14 21:46:26

标签: c++ parallel-processing openmp

我正在尝试使用opemMP制作并行版本的“Harmonic Progression Sum”问题。 但输出是相互不同的,取决于输入。 (并行和顺序)

程序:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <omp.h>
#include <time.h>

#define d 10    //Numbers of Digits (Example: 5 => 0,xxxxx)
#define n 1000  //Value of N (Example: 5 => 1/1 + 1/2 + 1/3 + 1/4 + 1/5)

using namespace std;

void HPSSeguencial(char* output) {
    long unsigned int digits[d + 11];

    for (int digit = 0; digit < d + 11; ++digit)
        digits[digit] = 0;

    for (int i = 1; i <= n; ++i) {
        long unsigned int remainder = 1;
        for (long unsigned int digit = 0; digit < d + 11 && remainder; ++digit) {
            long unsigned int div = remainder / i;
            long unsigned int mod = remainder % i;
            digits[digit] += div;
            remainder = mod * 10;
        }
    }


    for (int i = d + 11 - 1; i > 0; --i) {
        digits[i - 1] += digits[i] / 10;
        digits[i] %= 10;
    }
    if (digits[d + 1] >= 5) {
        ++digits[d];
    }


    for (int i = d; i > 0; --i) {
        digits[i - 1] += digits[i] / 10;
        digits[i] %= 10;
    }
    stringstream stringstreamA;
    stringstreamA << digits[0] << ",";


    for (int i = 1; i <= d; ++i) {
        stringstreamA << digits[i];
    }
    string stringA = stringstreamA.str();
    stringA.copy(output, stringA.size());
}

void HPSParallel(char* output) {
    long unsigned int digits[d + 11];

    for (int digit = 0; digit < d + 11; ++digit)
        digits[digit] = 0;

    int i;
    long unsigned int digit;
    long unsigned int remainder;
    #pragma omp parallel for private(i, remainder, digit)
    for (i = 1; i <= n; ++i) {
        remainder = 1; 
        for (digit = 0; digit < d + 11 && remainder; ++digit) {
            long unsigned int div = remainder / i;
            long unsigned int mod = remainder % i;
            digits[digit] += div;
            remainder = mod * 10;
        }
    }

    for (int i = d + 11 - 1; i > 0; --i) {
        digits[i - 1] += digits[i] / 10;
        digits[i] %= 10;
    }
    if (digits[d + 1] >= 5) {
        ++digits[d];
    }

    for (int i = d; i > 0; --i) {
        digits[i - 1] += digits[i] / 10;
        digits[i] %= 10;
    }
    stringstream stringstreamA;
    stringstreamA << digits[0] << ",";

    for (int i = 1; i <= d; ++i) {
        stringstreamA << digits[i];
    }
    string stringA = stringstreamA.str();
    stringA.copy(output, stringA.size());
}

int main() {
    //Sequential Method
    cout << "Sequential Method: " << endl;
    char outputSeguencial[d + 10];
    HPSSeguencial(outputSeguencial);
    cout << outputSeguencial << endl;

    //Cleaning vector
    string stringA = "";
    stringA.copy(outputSeguencial, stringA.size());

    //Parallel Method
    cout << "Parallel Method: " << endl;
    char outputParallel[d + 10];
    HPSParallel(outputParallel);
    cout << outputParallel << endl;

    system("PAUSE");
    return 0;
}

示例:

输入

#define d 10
#define n 1000

输出:

Sequential Method:
7,4854708606╠╠╠╠╠╠╠╠╠╠╠╠
Parallel Method:
6,6631705861╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ÇJ^

输入

#define d 12
#define n 7

输出:

Sequential Method:
2,592857142857╠╠╠╠╠╠╠╠╠╠╠╠╠╠ÀÂ♂ü─¨@
Parallel Method:
2,592857142857╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ÇJJ

此致

Pastecode

http://pastecode.org/index.php/view/62768285

2 个答案:

答案 0 :(得分:2)

更新digits数组时,您的线程会互相踩到脚趾。因此,一些新增内容会丢失,并且会出现虚假结果(不同运行中的结果不同,几乎可以肯定)。

您必须将写入同步到digits,例如与原子(或关键)部分:

// ... <snip>
#pragma omp parallel for private(i, remainder, digit)
for (i = 1; i <= n; ++i) {
    remainder = 1; 
    for (digit = 0; digit < d + 11 && remainder; ++digit) {
        long unsigned int div = remainder / i;
        long unsigned int mod = remainder % i;
        #pragma omp atomic     // <- HERE, could also be #pragma omp critical
        digits[digit] += div;
        remainder = mod * 10;
    }
}
// <snip> ...

这样一次只有一个线程可以更新数组。对于像这样的任务,这可能会使在多个线程中拆分任务的任何收益无效。

答案 1 :(得分:1)

正如Daniel Fischer指出的那样,您有写入冲突,但与使用omp critical部分相比,您可以更优雅地避免它,例如通过为每个线程提供它自己的digits副本,并在循环结束时将它们全部聚合。