尝试使用胎面局部变量来实现我的第一个Parallel::For循环,以对循环的结果进行求和。我的代码是基于W. Saumweber,D. Louis (德语),第33章,P.804中列出的一个例子。
由于Parallel::For调用中的语法错误,我被困在实现中。这些错误如下,从左到右: a)期望类型说明符,b)泛型类"System::Func“的参数太多,c)指向成员的指针对托管类无效,d)没有运算符"&”匹配这些操作数。
根据这本书,我使用数据List<DataStructure^> numbers创建了一个集合,该集合受方法computeSumScore中执行的计算的限制,该方法由方法sumScore中的Parallel::For例程调用。所有结果都在使用锁的方法finalizeSumScore中求和。
下面我粘贴类的.cpp部分的完整代码,以显示我拥有的内容。数据收集"numbers“可能看起来有点混乱,但这是由于程序的有机增长和我学习的过程中。
// 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)的函数是线程安全的,并且程序不会在访问相同(静态)变量的多个踏板上陷入停滞。但这必须先起作用。
希望你能帮我一把。
发布于 2014-05-25 08:32:21
Hans在注释中回答了我的问题(包括完整的方法名,添加逗号)。然而,我不能把我的问题标上答案,所以这个答案就是结束这个问题。
https://stackoverflow.com/questions/23845818
复制相似问题