PLINQ Aggregate exception I don't understand
I was waching the PLINQ PCD09 presentation by Igor Ostrovsky, and wanted to try to see what I could get out of my CULV laptop.
At a point I got a strange exception and I'm not sure what it means. I've condensed the c开发者_开发问答ode for better overview. It is the last primes.Sum() that causes the exception and if I make the range small - 8000 - the exception is not thrown. Any ideas?
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();
Exception (quite long)
System.AggregateException was unhandled Message=One or more errors occurred. Source=System.Core
StackTrace: at System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose) at System.Linq.Parallel.SpoolingTask.SpoolStopAndGo[TInputOutput,TIgnoreKey](QueryTaskGroupState groupState, PartitionedStream2 partitions, SynchronousChannel
1[] channels, TaskScheduler taskScheduler) at 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](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.WhereQueryOperator1.WrapPartitionedStream[TKey](PartitionedStream
2 inputStream, IPartitionedStreamRecipient1 recipient, Boolean preferStriping, QuerySettings settings) at System.Linq.Parallel.UnaryQueryOperator
2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream2 inputStream) at System.Linq.Parallel.ScanQueryOperator
1.ScanEnumerableQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient1 recipient) at System.Linq.Parallel.UnaryQueryOperator
2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient1 recipient) at System.Linq.Parallel.UnaryQueryOperator
2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient1 recipient) at System.Linq.Parallel.QueryOperator
1.GetOpenedEnumerator(Nullable1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings) at System.Linq.Parallel.QueryOpeningEnumerator
1.OpenQuery() at System.Linq.Parallel.QueryOpeningEnumerator1.MoveNext() at System.Linq.Parallel.IntSumAggregationOperator.InternalAggregate(Exception& singularExceptionToThrow) at System.Linq.Parallel.InlinedAggregationOperator
3.Aggregate() at System.Linq.ParallelEnumerable.Sum(ParallelQuery1 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) at System.Linq.Parallel.InlinedAggregationOperatorEnumerator1.MoveNext(TIntermediate& 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() InnerException:
If you remove call to AsParallel() then you can see that Enumerable.Sum throws and OverflowException. Changing Sum() to Sum(x => (long)x) should help.
精彩评论