C++\Cli Parallel::For with thread local variable - Error: too many arguments

扶醉桌前 提交于 2019-12-08 04:02:34

问题


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).

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.

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.

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);
    }
}

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.

Hope you can help me out.


回答1:


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.



来源:https://stackoverflow.com/questions/23845818/c-cli-parallelfor-with-thread-local-variable-error-too-many-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!