Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenMP任务&;数据环境_C++_Task_Openmp - Fatal编程技术网

C++ OpenMP任务&;数据环境

C++ OpenMP任务&;数据环境,c++,task,openmp,C++,Task,Openmp,我正在尝试为我的C++/OpenMP程序使用任务构造: #pragma omp parallel { typename ClusterNormal<DIM>::VectorMean ResultMeanThread; ResultMeanThread.setZero(); #pragma omp single for(list<unsigned int>::const_iterator it=IDLe

我正在尝试为我的C++/OpenMP程序使用任务构造:

#pragma omp parallel
    {
        typename ClusterNormal<DIM>::VectorMean ResultMeanThread;
        ResultMeanThread.setZero();
        #pragma omp single
        for(list<unsigned int>::const_iterator it=IDLeft.cbegin(); it!=IDLeft.cend(); it++)
        {
            #pragma omp task
            {
                ResultMeanThread += Data[*it];
            }
        }
}
#pragma omp并行
{
typename ClusterNormal::VectoMean ResultMeanThread;
ResultMeanThread.setZero();
#布拉格omp单曲
for(list::const_迭代器it=IDLeft.cbegin();it!=IDLeft.cend();it++)
{
#pragma-omp任务
{
ResultMeanThread+=数据[*it];
}
}
}
此代码计算IDLeft中指示的数据元素的一些
向量平均值的总和(不介意它们是什么,但它们定义了运算符+)

每个线程都用全零初始化
vectomean
。我的问题是,在for循环之后,
resultmeanshread
仍然由全零组成

执行任务时,计算的总和是正确的,但在任务执行后,
resultmeanshread
始终被重新初始化为零


我怎样才能修好它?由于列表的原因,我正在使用任务,但我的代码不起作用。

我发现问题在于
resultmeanshread
的声明类似于私有变量。 我尝试了这段代码,声明了一个类似共享变量的
resultmeanshread
向量(向量的长度是线程的长度),因此每个线程只访问向量的一个元素(没有竞争条件)

在前面的代码中,由于
task
构造,每个
resultmeanshread
都为零。每次执行
任务
,都会将私有变量设置为其初始值。我必须使用
任务
构造,因为
列表

代码如下:

vector<typename ClusterNormal<DIM>::VectorMean> ResultMeanThread;
typename ClusterNormal<DIM>::VectorMean ResultMeanFinal;

//here i set initial values to zero, and number of vector elements equal to total number of threads

#pragma omp parallel
{
    #pragma omp single
    for(list<unsigned int>::const_iterator it=IDLeft.cbegin(); it!=IDLeft.cend(); it++)
    {
        #pragma omp task
        {
            ResultMeanThread[omp_get_thread_num()] += Data[*it];
        }
    }
    #pragma omp taskwait

    // Final sum
    #pragma omp critical
    {
        ResultMeanFinal+=ResultMeanThread[omp_get_thread_num()];
    }
}
vector resultmeanshread;
typename ClusterNormal::VectoMean ResultMeanFinal;
//这里我将初始值设置为零,向量元素的数量等于线程的总数
#pragma-omp并行
{
#布拉格omp单曲
for(list::const_迭代器it=IDLeft.cbegin();it!=IDLeft.cend();it++)
{
#pragma-omp任务
{
结果平均线程[omp_get_thread_num()]+=Data[*it];
}
}
#pragma omp taskwait
//最终金额
#pragma-omp-critical
{
ResultMeanFinal+=ResultMeanThread[omp_get_thread_num()];
}
}

不应该在并行区域之前声明
resultmeanshread
并标记为共享变量吗?另外,将
+=
操作并行应用于
VectorMean
类型的对象是否安全?我已经为每个线程创建了一个私有的
ResultMeanThread
。我认为使用带共享变量的+=运算符不是一个好主意,因为竞争条件……在这段代码之后,有一个带有单个构造函数的
ResultmeanThread
的和。你能告诉我以后会发生什么吗?这个程序用于统计计算,函数simlpy计算一些向量的平均值。所以我有像输出一样的全零向量