我正在看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();
例外(很长)
System.AggregateException是 未处理的消息=一个或多个错误 发生了。来源= System.Core程序
堆栈跟踪: 在System.Linq.Parallel.QueryTaskGroupState.QueryEnd(布尔值 userInitiatedDispose) 在System.Linq.Parallel.SpoolingTask.SpoolStopAndGo [TInputOutput,TIgnoreKey](QueryTaskGroupState groupState,PartitionedStream2 partitions, SynchronousChannel
1 [] 渠道,TaskScheduler taskScheduler) 在System.Linq.Parallel.DefaultMergeHelper2.System.Linq.Parallel.IMergeHelper<TInputOutput>.Execute() at System.Linq.Parallel.MergeExecutor
1.Execute [TKey](PartitionedStream2 partitions, Boolean ignoreOutput, ParallelMergeOptions options, TaskScheduler taskScheduler, Boolean isOrdered, CancellationState cancellationState, Int32 queryId) at System.Linq.Parallel.PartitionedStreamMerger
1.Receive [TKey](PartitionedStream2 partitionedStream) at System.Linq.Parallel.InlinedAggregationOperator
3.WrapPartitionedStream [TKey](PartitionedStream {{1 }} 1 收件人,布尔值preferStriping, QuerySettings设置) 在System.Linq.Parallel.UnaryQueryOperator2 inputStream, IPartitionedStreamRecipient
2 的inputStream) 在System.Linq.Parallel.WhereQueryOperator2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream
2 的inputStream, IPartitionedStreamRecipient1.WrapPartitionedStream[TKey](PartitionedStream
2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive [TKEY的](PartitionedStream1 recipient, Boolean preferStriping, QuerySettings settings) at System.Linq.Parallel.UnaryQueryOperator
1.ScanEnumerableQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient2 inputStream) at System.Linq.Parallel.ScanQueryOperator
2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient1 recipient) at System.Linq.Parallel.UnaryQueryOperator
2.UnaryQueryOperatorResults .GivePartitionedStream(IPartitionedStreamRecipient1 recipient) at System.Linq.Parallel.UnaryQueryOperator
1.GetOpenedEnumerator(可空1 recipient) at System.Linq.Parallel.QueryOperator
1.OpenQuery() 在System.Linq.Parallel.QueryOpeningEnumerator1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings) at System.Linq.Parallel.QueryOpeningEnumerator
3.Aggregate() 在System.Linq.ParallelEnumerable.Sum(ParallelQuery1.MoveNext() at System.Linq.Parallel.IntSumAggregationOperator.InternalAggregate(Exception& singularExceptionToThrow) at System.Linq.Parallel.InlinedAggregationOperator
1.MoveNextCore(Int32&amp; currentElement) 在System.Linq.Parallel.InlinedAggregationOperatorEnumerator1 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.IntSumAggregationOperatorEnumerator
2.SpoolingWork() 在System.Linq.Parallel.SpoolingTaskBase.Work() 在System.Linq.Parallel.QueryTask.BaseWork(Object 没用过) 在System.Linq.Parallel.QueryTask。&lt; .cctor&gt; b__0(对象 O) 在System.Threading.Tasks.Task.InnerInvoke() 在System.Threading.Tasks.Task.Execute() 的InnerException:
答案 0 :(得分:5)
如果删除对AsParallel()的调用,则可以看到Enumerable.Sum抛出和OverflowException。将Sum()更改为Sum(x =&gt;(long)x)应该有所帮助。