2010-06-29 57 views
0

我正在用Igor Ostrovsky來詮釋PLINQ PCD09演示文稿,並試圖瞭解我可以從我的CULV筆記本電腦中獲得什麼。PLINQ聚合異常我不明白

在一個我有一個奇怪的例外,我不知道這意味着什麼。我已經濃縮了代碼以獲得更好的概述。這是最後一個素數.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,PartitionedStream 2 partitions, SynchronousChannel 1 ] 通道的TaskScheduler的TaskScheduler) 在System.Linq.Parallel.DefaultMergeHelper 2.System.Linq.Parallel.IMergeHelper<TInputOutput>.Execute() at System.Linq.Parallel.MergeExecutor 1.Execute [TKEY的](PartitionedStream​​1.接收[TKEY的](PartitionedStream 2 partitionedStream) at System.Linq.Parallel.InlinedAggregationOperator 3.WrapPartitionedStream [TKEY的](PartitionedStream 收件人,布爾preferStriping , QuerySettings設置) System.Linq.Parallel.UnaryQueryOperator 的inputStream) 在System.Linq.Parallel.WhereQueryOperator 的inputStream, IPartitionedStreamRecipient 1 recipient, Boolean preferStriping, 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.UnaryQueryOperator 2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient 1 recipient) at System.Linq.Parallel.QueryOperator 1.GetOpenedEnumerator(可空1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings) at System.Linq.Parallel.QueryOpeningEnumerator 1.OpenQuery() 在System.Linq.Parallel.QueryOpeningEnumerator 1.MoveNext() at System.Linq.Parallel.IntSumAggregationOperator.InternalAggregate(Exception& singularExceptionToThrow) at System.Linq.Parallel.InlinedAggregationOperator 3.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.IntSumAggregationOperatorEnumerator 1.MoveNextCore(的Int32 & currentElement) 處System.Linq.Parallel.SpoolingTaskBase.Work System.Linq.Parallel.InlinedAggregationOperatorEnumerator 1.MoveNext(TIntermediate& currentElement, Int32& currentKey) at System.Linq.Parallel.StopAndGoSpoolingTask 2.SpoolingWork() () 在System.Linq.Parallel.QueryTask.BaseWork(對象 未使用) 在System.Linq.Parallel.QueryTask。 < .cctor> b__0(對象 O) 在System.Threading.Tasks.Task.InnerInvoke() 在System.Threading.Tasks.Task.Execute() 的InnerException:

回答

5

如果您刪除調用AsParallel()然後你可以看到Enumerable.Sum拋出和OverflowException。將Sum()更改爲Sum(x =>(long)x)應該有所幫助。

+0

你說得對。現在我有點驚訝地發現添加這個:var numbers = Enumerable.Range(1,8 * 1000 * 1000).Cast ();是不足夠的。內部Sum表示必須是int ..感謝您的幫助。 - – Moberg 2010-06-30 19:05:31

+0

儘管我沒有發生同樣的錯誤,但它的確幫助我解決了我的問題......所以我的+1。 – TravisWhidden 2013-02-08 22:57:29