我试图使用Parallel For循环来帮助我加速处理器密集型计算,然后将这些计算添加到我可以在for循环完成后访问的线程安全列表中,以便我可以访问数据。我按照https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-parallel-for-loop-with-thread-local-variables
上的示例进行了操作我在粗线显示编译时错误,因为我是多线程的新手,如果你能指出我所犯的任何错误,我将不胜感激,所以我可以学习从这个错误。
public static async Task Test()
{
Vector<double> vectorArrayBuy = null;
Vector<double> vectorArraySell = null;
ConcurrentBag<DailyStockData> query;
query = new ConcurrentBag<DailyStockData>();
ConcurrentBag<MultipleRegressionInfo> listMRInfo = new ConcurrentBag<MultipleRegressionInfo>();
Calculations calcTemp = new Calculations();
**Parallel.For<ConcurrentBag<MultipleRegressionInfo>>(0, 200, (listMRInfo) = new ConcurrentBag<MultipleRegressionInfo>(), (j, loop, listMRInfoLocal) =>**
{
int k = Convert.ToInt32(j);
Calculations calc = new Calculations(query, k);
var targetValueBuy = calc.ListCalculationData.Select(i => i.MRTargetValueBuy).ToList();
var targetValueSell = calc.ListCalculationData.Select(i => i.MRTargetValueSell).ToList();
vectorArrayBuy = CreateVector.Dense(targetValueBuy.ToArray());
vectorArraySell = CreateVector.Dense(targetValueSell.ToArray());
var name = calc.ListCalculationData.First();
ConcurrentBag<double> value;
value = new ConcurrentBag<double>(calc.ListCalculationData.Select(i => i.WilliamsR));
MultipleRegressionInfo r1 = Rn(value, vectorArrayBuy, nameof(name.WilliamsR), k, calc);
listMRInfoLocal.Add(r1);
calcTemp = calc;
return listMRInfoLocal;
},
(variable) => listMRInfo = variable
);
listMRInfo = new ConcurrentBag<MultipleRegressionInfo>(listMRInfo.OrderByDescending(i => i.RSquared).DistinctBy(i => i.ValueName).ToList()); // trying to access this data after parallel for loop completes
public class DailyStockData
{
public DailyStockData();
public int ID { get; set; }
public string Symbol { get; set; }
public string Market { get; set; }
public DateTime Date { get; set; }
public decimal Open { get; set; }
public decimal High { get; set; }
public decimal Low { get; set; }
public decimal Close { get; set; }
public decimal AdjustedClose { get; set; }
public long Volume { get; set; }
}
public class CalculationData
{
public CalculationData(CalculationData calcData)
{
Date = calcData.Date;
Open = calcData.Open;
High = calcData.High;
Low = calcData.Low;
Close = calcData.Close;
AdjustedClose = calcData.AdjustedClose;
Volume = calcData.Volume;
WilliamsR = calcData.WilliamsR;
}
public CalculationData() { }
public DateTime Date { get; set; }
public double Open { get; set; }
public double High { get; set; }
public double Low { get; set; }
public double Close { get; set; }
public double AdjustedClose { get; set; }
public double Volume { get; set; }
public double WilliamsR { get; set; }
}
答案 0 :(得分:2)
看起来你正试图使用Parallel.For
函数的更复杂的重载而不是你需要的。 listMRinfo
是并发类,因此在for循环的每次迭代中直接访问此变量是安全的。
Parallel.For<ConcurrentBag<MultipleRegressionInfo>>(0, 200, (index) =>
{
// ...
listMRInfo.Add(r1);
});
另一方面,如果没有进行某种锁定,则不应在循环的每次迭代中更新对calcTemp
的引用。即使使用锁定,我也不相信你应该存储来自循环的一个迭代的值。这是一个 parallel foreach,所以在循环完成后,你无法保证 calcTemp
的值来自哪个迭代。
答案 1 :(得分:0)
你的问题在这里:
... (listMRInfo) = new ConcurrentBag<MultipleRegressionInfo>() ...
没有Parallel.For
的重载,其第三个参数接受T
,最佳匹配是这一个:
Parallel.For<TLocal> Method (Int32, Int32, Func<TLocal>, Func<Int32, ParallelLoopState, TLocal, TLocal>, Action<TLocal>)
并且,正如错误所述,TLocal
无法隐式转换为Func<TLocal>
。由于该参数没有任何意义,因为已经分配了listMRInfo
,所以您应该这样做:
Parallel.For<ConcurrentBag<MultipleRegressionInfo>>(0, 200, (j, loop, listMRInfoLocal) =>
{
...
}