使用一个线程完整代码的C ++ OpenMp

时间:2019-03-24 17:01:59

标签: c++ parallel-processing openmp

我正在使用OpenMp和C ++进行一个小项目(我的第一个项目) 该代码用于计算两个数据集的相关系数(我正在学习数据挖掘并且喜欢做我使用代码学习的内容)

我首先像往常一样编写了代码
然后尝试使其并行,但速度变慢,我认为它使用的线程不止一个
这是我第一次尝试并行
所以我不知道我是否错过了什么
我正在使用Visual Studio Community 2015

#include <iostream>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <omp.h>

using namespace std;

void main() {

    vector <long double> dataSetOne, dataSetTwo, dataSetMultiplication, dataSetOnePow, dataSetTwoPow;
    long double dataSetOneSum = 0, dataSetTwoSum = 0, multiplicationSum = 0, setOnePowSum = 0, setTwoPowSum = 0;
    double correlationCoefficient = 0;

    long int i;
    long int length;


    cout << "Enter length for the Data Sets: " << endl;
    cin >> length;



    #pragma omp parallel 
    {   
    #pragma omp for 
        for (i = 1; i <= length; i++) {
            dataSetOne.push_back(i);
        }
    #pragma omp for 
        for (i = 0; i < dataSetOne.size(); i++) {
            dataSetOneSum += dataSetOne[i];
        }

    }
    cout << "The sum of the first Data Set is: " << fixed << dataSetOneSum << endl << endl;


#pragma omp parallel
    {
    #pragma omp for private(i, dataSetTwo)
        for (i = length + 1; i <= length * 2; i++) {
            dataSetTwo.push_back(i);
        }
    #pragma omp for private(i, dataSetTwo)
        for (i = 0; i < dataSetTwo.size(); i++) {
            dataSetTwoSum += dataSetTwo[i];
        }


    }
    cout << "The sum of the second Data Set: " << dataSetTwoSum << endl << endl;
#pragma omp parallel for private(i,dataSetMultiplication,multiplicationSum)
    for (i = 0; i < length; i++) {
        dataSetMultiplication.push_back(dataSetOne[i] * dataSetTwo[i]);
        multiplicationSum += dataSetMultiplication[i];
    }

    cout << "Multiplication sum of the two Data Sets: " << multiplicationSum << endl << endl;
#pragma omp parallel for private(i,dataSetOnePow,setOnePowSum)
    for (i = 0; i < length; i++) {
        dataSetOnePow.push_back(pow(dataSetOne[i], 2));
        setOnePowSum += dataSetOnePow[i];
    }

    cout << "Sum of the first Data Set elements raised to the power of 2: " << setOnePowSum << endl << endl;
#pragma omp parallel for private(i,dataSetTwoPow,setTwoPowSum)
    for (i = 0; i < length; i++) {
        dataSetTwoPow.push_back(pow(dataSetTwo[i], 2));
        setTwoPowSum += dataSetTwoPow[i];
    }

    cout << "Sum of the second Data Set elements raised to the power of 2: " << setTwoPowSum << endl << endl;
#pragma omp parallel
    {
        correlationCoefficient = ((length * multiplicationSum) - (dataSetOneSum * dataSetTwoSum)) / (sqrt((length * setOnePowSum - pow(dataSetOneSum, 2))) * (length * setTwoPowSum) - (pow(dataSetTwoSum, 2)));
    }

    cout << "Correlation Coefficient: " << correlationCoefficient << endl;


    if (correlationCoefficient > 0) {
        cout << "the Correlation between Data Sets is: " << correlationCoefficient << " the relation between Data Sets is positively Correlated" << endl;
    }
    else if (correlationCoefficient == 0) {
        cout << " the Correlation between Data Sets is: " << correlationCoefficient << " the relation between Data Sets is Independent" << endl;
    }
    else if (correlationCoefficient < 0) {
        cout << " the Correlation between Data Sets is: " << correlationCoefficient << " the relation between Data Sets is negatively Correlated" << endl;
    }

    system("pause");
}

1 个答案:

答案 0 :(得分:0)

您有很多开销b / c,您使用的是许多不同的并行区域,而不是一个区域,在该区域中,您要做一些与#pragma omp single不平行的事情。这意味着,一个线程将不执行每次破坏和创建新线程的工作,而将依次执行,而其他线程则在等待。

仅当连续区域很大时,您才可能要使用多个平行区域。您没有所有这些线程闲置。