本文介绍了C ++ \Cli Parallel ::用于线程局部变量 - 错误:参数太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用踏板局部变量实现我的第一个 Parallel :: For 循环,以得到循环的结果。我的代码基于Visual C ++ 2010,by W. Saumweber,D.Louis(German)。Ch。33,P.804)中列出的示例。

Trying to implement my first Parallel::For loop with a tread local variable to sum results of the loop. My code is based on an example listed in "Visual C++ 2010, by W. Saumweber, D. Louis (German). Ch. 33, P.804).

我遇到了在 Parallel :: For 调用中的语法错误的实现中,错误如下,从左到右:a)期望一个类型说明符,b)泛型类 System :: Func 的过多参数,c)指向成员的指针对受管理类无效,d)没有操作符&匹配这些操作数。

I get stuck in the implementation with syntax errors in the Parallel::For call. The errors are as follows, from left to right: a) expected a type specifier, b) too many arguments for generic class "System::Func", c) pointer to member is not valid for a managed class, d) no operator "&" matches these operands.

根据这本书,我创建了一个数据 List< DataStructure ^> numbers 的集合是由方法 computeSumScore 中执行的计算执行的,该方法由方法<$ c中的 Parallel :: For $ c> sumScore 。所有结果以方法 finalizeSumScore 使用锁加总。

In line with the book, I create a collection with data List<DataStructure^> numbers, which is subject to a calculation performed in method computeSumScore which is called by the Parallel::For routine in method sumScore. All results are summed in method finalizeSumScore using a lock.

下面我粘贴类的.cpp部分的完整代码,以显示我有的数据收集 numbers 可能看起来有点凌乱,

Below I paste the full code of the .cpp part of the class, to show what I have. The data collection "numbers" may look a bit messy, but that's due to organical growth of the program and me learning as I go along.

// constructor
DataCollection::DataCollection(Form1^ f1) // takes parameter of type Form1 to give acces to variables on Form1
{
    this->f1 = f1;
}

// initialize data set for parallel processing
void DataCollection::initNumbers(int cIdx)
{
    DataStructure^ number;
    numbers = gcnew List<DataStructure^>();

    for (int i = 0; i < f1->myGenome->nGenes; i++)
    {
        number = gcnew DataStructure();

        number->concentrationTF = f1->myOrgan->cellPtr[cIdx]->concTFA[i];
        number->stringA->AddRange(f1->myGenome->cStruct[i]->gString->GetRange(0, f1->myGenome->cChars));
        number->stringB->AddRange(f1->myGenome->cStruct[i]->pString);
        if (f1->myGenome->cStruct[i]->inhibitFunc)
            number->sign = -1;
        else
            number->sign = 1;
        numbers->Add(number);
    }
}

// parallel-for summation of scores
double DataCollection::sumScore()
{
    Parallel::For<double>(0, numbers->Count, gcnew Func<double>(this, &GenomeV2::DataCollection::initSumScore),
                                            gcnew Func<int, ParallelLoopState^, double, double>(this, &GenomeV2::DataCollection::computeSumScore),
                                            gcnew Action<double>(this, &GenomeV2::DataCollection::finalizeSumScore));
    return summation;
}

// returns start value
double DataCollection::initSumScore()
{
    return 0.0;
}

// perform sequence alignment calculation
double DataCollection::computeSumScore(int k, ParallelLoopState^ status, double tempVal)
{
    int nwScore;

    if (numbers[k]->concentrationTF > 0)
    {       
        nwScore = NeedlemanWunsch::computeGlobalSequenceAlignment(numbers[k]->stringA, numbers[k]->stringB);
        tempVal = Mapping::getLinIntMapValue(nwScore); // mapped value (0-1)

        tempVal = (double) numbers[k]->sign * tempVal * numbers[k]->concentrationTF;
    }
    else
        tempVal = 0.0;

    return tempVal;
}

// locked addition
void DataCollection::finalizeSumScore(double tempVal)
{
    Object^ myLock = gcnew Object();

    try
    {
        Monitor::Enter(myLock);
        summation += tempVal;
    }
    finally
    {
        Monitor::Exit(myLock);
    }
}

一旦这个问题解决了,我需要确保调用的函数(computeGlobalSequenceAlignment和getLinIntMapvalue)是线程安全的,并且程序不会在访问同一(静态)变量的多个胎面上停滞。

Once this problem is solved I need to ensure that the functions called (computeGlobalSequenceAlignment and getLinIntMapvalue) are thread safe and the program doesn't get stalled on multiple treads accessing the same (static) variables. But this needs to work first.

推荐答案

Hans Passant在评论中回答了我的问题(包括完整的方法名称,添加逗号)。但我不能把我的问题标记为回答,所以这个答案是关闭问题。

Hans Passant answered my question in the comments (include full method name, add comma). Yet I cannot mark my question as answered, so this answer is to close the question.

这篇关于C ++ \Cli Parallel ::用于线程局部变量 - 错误:参数太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 09:50