I’m trying to make a parallel version of “Harmonic Progression Sum” problem using opemMP.
But the output are differents each other depending on the inputs. (Parallel and Sequential)
Program:
#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;
}
Examples:
Input:
#define d 10
#define n 1000
Output:
Sequential Method:
7,4854708606╠╠╠╠╠╠╠╠╠╠╠╠
Parallel Method:
6,6631705861╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ÇJ^
Input:
#define d 12
#define n 7
Output:
Sequential Method:
2,592857142857╠╠╠╠╠╠╠╠╠╠╠╠╠╠ÀÂ♂ü─¨@
Parallel Method:
2,592857142857╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ÇJJ
Regards
Pastecode
Your threads step on each other’s toes when updating the
digitsarray. Hence some additions are lost, and you get bogus results (different results in different runs, almost certainly).You must synchronise the writes to
digits, e.g. with an atomic (or critical) section:so that only one thread at a time can update the array. For a task like this, that would probably nullify any gains of splitting the task in several threads, though.