C# 执行并行因式分解时发生溢出异常

C# 执行并行因式分解时发生溢出异常,c#,.net,overflow,task-parallel-library,C#,.net,Overflow,Task Parallel Library,我正在尝试编写一个不太智能的因式分解程序,并尝试使用TPL并行地完成它。然而,在Core2Duo机器上运行大约15分钟后,我得到了一个聚合异常,其中包含一个溢出异常。堆栈跟踪中的所有条目都是.NET framework的一部分,溢出不是来自我的代码。如果您能帮我弄清楚为什么会发生这种情况,我们将不胜感激 下面是注释代码,希望它足够简单,可以理解: class Program { static List<Tuple<BigInteger, int>> factors

我正在尝试编写一个不太智能的因式分解程序,并尝试使用TPL并行地完成它。然而,在Core2Duo机器上运行大约15分钟后,我得到了一个聚合异常,其中包含一个溢出异常。堆栈跟踪中的所有条目都是.NET framework的一部分,溢出不是来自我的代码。如果您能帮我弄清楚为什么会发生这种情况,我们将不胜感激

下面是注释代码,希望它足够简单,可以理解:

class Program
{
    static List<Tuple<BigInteger, int>> factors = new List<Tuple<BigInteger, int>>();

    static void Main(string[] args)
    {
        BigInteger theNumber = BigInteger.Parse(
            "653872562986528347561038675107510176501827650178351386656875178" +
            "568165317809518359617865178659815012571026531984659218451608845" +
            "719856107834513527");
        Stopwatch sw = new Stopwatch();
        bool isComposite = false;
        sw.Start();

        do
        {
            /* Print out the number we are currently working on. */
            Console.WriteLine(theNumber);

            /* Find a factor, stop when at least one is found
               (using the Any operator). */
            isComposite = Range(theNumber)
                          .AsParallel()
                          .Any(x => CheckAndStoreFactor(theNumber, x));

            /* Of the factors found, take the one with the lowest base. */
            var factor = factors.OrderBy(x => x.Item1).First();
            Console.WriteLine(factor);

            /* Divide the number by the factor. */
            theNumber = BigInteger.Divide(
                            theNumber, 
                            BigInteger.Pow(factor.Item1, factor.Item2));

            /* Clear the discovered factors cache, and keep looking. */
            factors.Clear();
        } while (isComposite);

        sw.Stop();
        Console.WriteLine(isComposite + " " + sw.Elapsed);
    }

    static IEnumerable<BigInteger> Range(BigInteger squareOfTarget)
    {
        BigInteger two = BigInteger.Parse("2");
        BigInteger element = BigInteger.Parse("3");
        while (element * element < squareOfTarget)
        {
            yield return element;
            element = BigInteger.Add(element, two);
        }
    }

    static bool CheckAndStoreFactor(BigInteger candidate, BigInteger factor)
    {
        BigInteger remainder, dividend = candidate;
        int exponent = 0;
        do
        {
            dividend = BigInteger.DivRem(dividend, factor, out remainder);
            if (remainder.IsZero)
            {
                exponent++;
            }
        } while (remainder.IsZero);
        if (exponent > 0)
        {
            lock (factors)
            {
                factors.Add(Tuple.Create(factor, exponent));
            }
        }
        return exponent > 0;
    }
}
类程序
{
静态列表因子=新列表();
静态void Main(字符串[]参数)
{
biginger theNumber=biginger.Parse(
"653872562986528347561038675107510176501827650178351386656875178" +
"568165317809518359617865178659815012571026531984659218451608845" +
"719856107834513527");
秒表sw=新秒表();
bool isComposite=false;
sw.Start();
做
{
/*打印出我们目前正在处理的号码*/
Console.WriteLine(编号);
/*找到一个因子,在至少找到一个因子时停止
(使用任意运算符)*/
isComposite=范围(编号)
.天冬酰胺()
.Any(x=>CheckAndStoreFactor(数字x));
/*在找到的因素中,选择基数最低的因素*/
var factor=factors.OrderBy(x=>x.Item1.First();
控制台写入线(因子);
/*将数字除以系数*/
number=BigInteger.Divide(
数字,
BigInteger.Pow(factor.Item1,factor.Item2));
/*清除已发现的因素缓存,并继续查找*/
因子;
}while(isComposite);
sw.Stop();
Console.WriteLine(isComposite+“”+sw.Passed);
}
静态IEnumerable范围(目标的BigInteger平方)
{
bigingertwo=biginger.Parse(“2”);
biginger元素=biginger.Parse(“3”);
while(元素*元素<目标的平方)
{
收益-收益要素;
element=BigInteger.Add(element,two);
}
}
静态布尔CheckAndStoreFactor(BigInteger候选,BigInteger因子)
{
大整数余数,被除数=候选数;
int指数=0;
做
{
股息=BigInteger.DivRem(股息、因子、余数);
if(余数为零)
{
指数++;
}
}while(余数为零);
如果(指数>0)
{
锁(系数)
{
factors.Add(Tuple.Create(factor,exponent));
}
}
返回指数>0;
}
}
下面是引发的异常:

Unhandled Exception: System.AggregateException: One or more errors occurred. ---
> System.OverflowException: Arithmetic operation resulted in an overflow.
   at System.Linq.Parallel.PartitionedDataSource`1.ContiguousChunkLazyEnumerator.MoveNext(T& currentElement, Int32& currentKey)
   at System.Linq.Parallel.AnyAllSearchOperator`1.AnyAllSearchOperatorEnumerator`1.MoveNext(Boolean& currentElement, Int32& currentKey)
   at System.Linq.Parallel.StopAndGoSpoolingTask`2.SpoolingWork()
   at System.Linq.Parallel.SpoolingTaskBase.Work()
   at System.Linq.Parallel.QueryTask.BaseWork(Object unused)
   at System.Linq.Parallel.QueryTask.<.cctor>b__0(Object o)
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
   at System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose)
   at System.Linq.Parallel.SpoolingTask.SpoolStopAndGo[TInputOutput,TIgnoreKey](QueryTaskGroupState groupState, PartitionedStream`2 partitions, SynchronousChannel`1[] channels, TaskScheduler taskScheduler)
   at System.Linq.Parallel.DefaultMergeHelper`2.System.Linq.Parallel.IMergeHelper<TInputOutput>.Execute()
   at System.Linq.Parallel.MergeExecutor`1.Execute[TKey](PartitionedStream`2 partitions, Boolean ignoreOutput, ParallelMergeOptions options, TaskScheduler taskScheduler, Boolean isOrdered, CancellationState cancellationState, Int32 queryId)

   at System.Linq.Parallel.PartitionedStreamMerger`1.Receive[TKey](PartitionedStream`2 partitionedStream)
   at System.Linq.Parallel.AnyAllSearchOperator`1.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream, IPartitionedStreamRecipient`1 recipient, BooleanpreferStriping, QuerySettings settings)
   at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream`2 inputStream)
   at System.Linq.Parallel.ScanQueryOperator`1.ScanEnumerableQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient)
   at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient)
   at System.Linq.Parallel.QueryOperator`1.GetOpenedEnumerator(Nullable`1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings)
   at System.Linq.Parallel.QueryOpeningEnumerator`1.OpenQuery()
   at System.Linq.Parallel.QueryOpeningEnumerator`1.MoveNext()
   at System.Linq.Parallel.AnyAllSearchOperator`1.Aggregate()
   at System.Linq.ParallelEnumerable.Any[TSource](ParallelQuery`1 source, Func`2 predicate)
   at PFact.Program.Main(String[] args) in d:\myprojects\PFact\PFact\Program.cs:line 34
未处理的异常:System.AggregateException:发生一个或多个错误---
>System.OverflowException:算术运算导致溢出。
位于System.Linq.Parallel.PartitionedDataSource`1.ContinguousChunklazyEnumerator.MoveNext(T¤tElement,Int32¤tKey)
位于System.Linq.Parallel.AnyAllSearchOperator`1.AnyAllSearchOperator分子`1.MoveNext(布尔值和当前元素、Int32和当前键)
在System.Linq.Parallel.StopAndGoSpoolingTask`2.SpoolingWork()中
在System.Linq.Parallel.SpoolingTaskBase.Work()中
at System.Linq.Parallel.QueryTask.BaseWork(对象未使用)
在System.Linq.Parallel.QueryTask.b__0(对象o)处
在System.Threading.Tasks.Task.InnerInvoke()中
在System.Threading.Tasks.Task.Execute()中
---内部异常堆栈跟踪的结束---
位于System.Linq.Parallel.QueryTaskGroupState.QueryEnd(布尔userInitiatedDispose)
在System.Linq.Parallel.SpoolingTask.SpoolStopAndGo[TInputOutput,TIgnoreKey](QueryTaskGroupState,groupState,PartitionedStream`2个分区,SynchronousChannel`1[]个通道,TaskScheduler TaskScheduler)
位于System.Linq.Parallel.DefaultMergeHelper`2.System.Linq.Parallel.IMergeHelper.Execute()
在System.Linq.Parallel.MergeExecutor`1.Execute[TKey](PartitionedStream`2个分区,Boolean ignoreOutput,ParallelMergeOptions选项,TaskScheduler TaskScheduler,Boolean isOrdered,CancellationState CancellationState,Int32 queryId)
在System.Linq.Parallel.partitionedstreammerge`1.Receive[TKey](PartitionedStream`2 PartitionedStream)
在System.Linq.Parallel.AnyAllSearchOperator`1.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream,iPPartitionedStreamRecipient`1 recipient,BooleanpreferStriping,QuerySettings设置)
在System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream`2 inputStream)
位于System.Linq.Parallel.ScanQueryOperator`1.ScanEnumerableQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1收件人)
在System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1个收件人)中
位于System.Linq.Parallel.QueryOperator`1.GetOpenedNumerator(可为null的`1合并选项、布尔抑制顺序、布尔前效果、查询设置查询设置)
在System.Linq.Parallel.QueryOpenEnumerator`1.OpenQuery()中
在System.Linq.Parallel.QueryOpeningEnumerator`1.MoveNext()中
位于System.Linq.Parallel.AnyAllSearchOperator`1.Aggregate()
位于System.Linq.ParallelEnumerable.Any[TSource](ParallelQuery`1 source,Func`2谓词)
位于d:\myprojects\PFact\PFact\Program.cs中的PFact.Program.Main(字符串[]args):第34行
任何帮助都将不胜感激

谢谢

编辑

在Simon的回答之后,我再次运行了代码,这次使用了
catch(aggregateeexception x)
子句,并检查了
InnerExceptions
集合中的所有元素。总共有2个元素(我假设每个执行线程有一个元素,因为我有2个)
int exponent = 0;
BigInteger exponent = 0;
at System.Linq.Parallel.PartitionedDataSource`1.
  ContiguousChunkLazyEnumerator.MoveNext(T& currentElement, Int32& currentKey)
at System.Linq.Parallel.AnyAllSearchOperator`1.
  AnyAllSearchOperatorEnumerator`1.MoveNext(Boolean& currentElement, Int32& currentKey)