我有一个使用OpenMP和C ++编写的代码,我想知道其中有多少是串行和并行的。
我读了一些加速比定律和公式,但是我不知道如何计算并行代码和串行代码的百分比
并且如果有人可以引导我使用一些免费软件和网站来帮助我得出1-8核的加速比
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <omp.h>
using namespace std;
int userInput(int num) {
const int len = num;
return len;
}
void main() {
long double dataSetOneSum = 0, dataSetTwoSum = 0, multiplicationSum = 0, setOnePowSum = 0, setTwoPowSum = 0;
long double correlationCoefficient;
int i;
int num;
cout << "enter size of data sets: " << endl;
cin >> num;
int length = userInput(num);
double wtime;
long double* dataSetOne = new long double[length];
long double* dataSetTwo = new long double[length];
long double* dataSetMultiplication = new long double[length];
long double* dataSetOnePow = new long double[length];
long double* dataSetTwoPow = new long double[length];
srand(time(0));
omp_set_num_threads(3);
wtime = omp_get_wtime();
#pragma omp parallel for
for (i = 0; i < length; i++) {
dataSetOne[i] = 1 + (rand() % 99);
cout << "thread num: " << omp_get_thread_num() << endl;
}
#pragma omp parallel for
for (i = 0; i < length; i++) {
dataSetOneSum = dataSetOneSum + dataSetOne[i];
//cout << "thread num: " << omp_get_thread_num() << endl;
}
cout << "the sum of the first data set :" << fixed << dataSetOneSum << endl;
#pragma omp parallel for
for (i = 0; i < length; i++) {
dataSetTwo[i] = 1 + (rand() % 99);
//cout << "thread num: " << omp_get_thread_num() << endl;
}
#pragma omp parallel for
for (i = 0; i < length; i++) {
dataSetTwoSum = dataSetTwoSum + dataSetTwo[i];
//cout << "thread num: " << omp_get_thread_num() << endl;
}
cout << "the sum of the second data set :" << dataSetTwoSum << endl;;
#pragma omp parallel for
for (i = 0;i < length;i++) {
dataSetMultiplication[i] = dataSetOne[i] * dataSetTwo[i];
multiplicationSum = multiplicationSum + dataSetMultiplication[i];
//cout << "thread num: " << omp_get_thread_num() << endl;
}
cout << "the sum of the two data sets multiplied : " << multiplicationSum << endl;
#pragma omp parallel for
for (i = 0;i < length;i++) {
dataSetOnePow[i] = pow(dataSetOne[i], 2);
setOnePowSum = setOnePowSum + dataSetOnePow[i];
//cout << "thread num: " << omp_get_thread_num() << endl;
}
cout << "the sum of the first data set numbers raised to the power of 2 : " << setOnePowSum << endl;
#pragma omp parallel for
for (i = 0;i < length;i++) {
dataSetTwoPow[i] = pow(dataSetTwo[i], 2);
setTwoPowSum = setTwoPowSum + dataSetTwoPow[i];
//cout << "thread num: " << omp_get_thread_num() << endl;
}
cout << "the sum of the second data set numbers raised to the power of 2 : " << setTwoPowSum << endl;
correlationCoefficient = ((length * multiplicationSum) - (dataSetOneSum * dataSetTwoSum)) / (sqrt((length * setOnePowSum) - (pow(dataSetOneSum, 2))) * sqrt((length * setTwoPowSum) - (pow(dataSetTwoSum, 2))));
cout << "the correlation = " << correlationCoefficient << endl;
if (correlationCoefficient > 0) {
cout << "the correlation between data sets is: " << correlationCoefficient << ", data sets is positively correlated " << endl;
}
else if (correlationCoefficient == 0) {
cout << "the correlation between datasets is: " << correlationCoefficient << ", data sets is independent" << endl;
}
else if (correlationCoefficient < 0) {
cout << "the correlation between data sets is: " << correlationCoefficient << ", data sets is negatively correlated " << endl;
}
wtime = omp_get_wtime() - wtime;
cout << " Elapsed wall clock time = " << wtime << "\n";
system("pause");
}