0

我正在观看 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 未处理 Message=发生一个或多个错误。Source=System.Core
StackTrace:在 System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose) 在 System.Linq.Parallel.SpoolingTask.SpoolStopAndGo[TInputOutput,TIgnoreKey](QueryTaskGroupState groupState, PartitionedStream 2 partitions, SynchronousChannel1[] channels, TaskScheduler taskScheduler) 在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 接收者,布尔值 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.GetOpenedQuery 1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings) at System.Linq.Parallel.QueryOpeningEnumerator()) 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& currentElement) 在 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(对象未使用) 在 System.Linq.Parallel.QueryTask.<.cctor>b__0(Object o) 在System.Threading.Tasks.Task.InnerInvoke() 在 System.Threading.Tasks.Task.Execute() InnerException:

4

1 回答 1

5

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

于 2010-06-29T11:21:06.243 回答