PLINQ聚合异常我不明白

Mob*_*erg 0 c# exception plinq

我正在看Igor Ostrovsky的PLINQ PCD09演示文稿,想要试着看看我能从CULV笔记本电脑中得到什么.

在某一点上,我得到了一个奇怪的例外,我不确定它是什么意思.我已经浓缩了代码以获得更好的概述.它是导致异常的最后一个primes.Sum(),如果我使范围变小 - 8000 - 则不会抛出异常.有任何想法吗?

Func<int, bool> isprime = n => // ignore input checks for now
    {
        int sqr = Convert.ToInt32(Math.Ceiling(Math.Sqrt(n)));
        for (int i = 2; i < sqr; i++) if (n % i == 0) return false;
        return true;
    };

var numbers = Enumerable.Range(1, 8*1000*1000);
long counter = 0;
ParallelQuery<int> primes = numbers.AsParallel().Where(x => isprime(x));
counter = primes.Sum();
Run Code Online (Sandbox Code Playgroud)

例外(很长)

System.AggregateException未处理Message =发生一个或多个错误.Source = System.Core
StackTrace:System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose)at System.Linq.Parallel.SpoolingTask.SpoolStopAndGo [TInputOutput,TIgnoreKey](QueryTaskGroupState groupState,PartitionedStream 2 partitions, SynchronousChannel1 [] channels,TaskScheduler taskScheduler)at at System.Linq.Parallel.DefaultMergeHelper 2.System.Linq.Parallel.IMergeHelper<TInputOutput>.Execute() at System.Linq.Parallel.MergeExecutor1.Execute [TKey](PartitionedStream 2 partitions, Boolean ignoreOutput, ParallelMergeOptions options, TaskScheduler taskScheduler, Boolean isOrdered, CancellationState cancellationState, Int32 queryId) at System.Linq.Parallel.PartitionedStreamMerger1.Receive [TKey](PartitionedStream 2 partitionedStream) at System.Linq.Parallel.InlinedAggregationOperator3.WrapPartitionedStream [TKey](PartitionedStream 2 inputStream, IPartitionedStreamRecipient1收件人,Boolean preferStriping,QuerySettings设置)在System.Linq.Parallel.UnaryQueryOperator 2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream2 inputStream )在System.Linq.Parallel.WhereQueryOperator1.WrapPartitionedStream[TKey](PartitionedStream2的inputStream,IPartitionedStreamRecipient 1 recipient, Boolean preferStriping, QuerySettings settings) at System.Linq.Parallel.UnaryQueryOperator2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive [TKEY的](PartitionedStream 2 inputStream) at System.Linq.Parallel.ScanQueryOperator1.ScanEnumerableQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient 1 recipient) at System.Linq.Parallel.UnaryQueryOperator2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient 1 recipient) at System.Linq.Parallel.UnaryQueryOperator2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient 1 recipient) at System.Linq.Parallel.QueryOperator1.GetOpenedEnumerator(可空1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings) at System.Linq.Parallel.QueryOpeningEnumerator1.OpenQuery()在系统. Linq.Parallel.QueryOpeningEnumerator 1.MoveNext() at System.Linq.Parallel.IntSumAggregationOperator.InternalAggregate(Exception& singularExceptionToThrow) at System.Linq.Parallel.InlinedAggregationOperator3.Aggregate()在System.Linq.ParallelEnumerable.Sum(ParallelQuery 1 source) at ConsoleTest.TestClass.Test() in C:\Users\henrik\Documents\Visual Studio 2010\Projects\CSharp\ConsoleTest\ConsoleTest\TestClass.cs:line 23 at ConsoleTest.Program.Main(String[] args) in C:\Users\henrik\Documents\Visual Studio 2010\Projects\CSharp\ConsoleTest\ConsoleTest\Program.cs:line 20 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.OverflowException Message=Arithmetic operation resulted in an overflow. Source=System.Core StackTrace: at System.Linq.Parallel.IntSumAggregationOperator.IntSumAggregationOperatorEnumerator1.MoveNextCore(Int32¤tElement)System.Linq.Parallel.InlinedAggregationOperatorEnumerator1.MoveNext(TIntermediate& currentElement, Int32& currentKey) at System.Linq.Parallel.StopAndGoSpoolingTask2.SpoolingWork()位于System.Linq.Parallel.SpoolingTaskBase.Work(),System.Linq.Parallel.QueryTask.BaseWork(Object unused),位于System.Linq.Parallel.QueryTask.<.cctor> b__0(Object o)at System.Threading.Tasks.Task.Execute()中的System.Threading.Tasks.Task.InnerInvoke()InnerException:

des*_*sco 5

如果删除对AsParallel()的调用,则可以看到Enumerable.Sum抛出和OverflowException.将Sum()更改为Sum(x =>(long)x)应该有所帮助.