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=1 つ以上のエラーが発生しました。Source=System.Core
StackTrace: System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose) で System.Linq.Parallel.SpoolingTask.SpoolStopAndGo[TInputOutput,TIgnoreKey](QueryTaskGroupState groupState, PartitionedStream 2 partitions, SynchronousChannel1[] チャネル, 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.WhereQueryOperator で1.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 2.UnaryQueryOperatorResults.GivePartitionedQueryStream 1 recipient) at System.Linq.Parallel.UnaryQueryOperator(IPartitionedStreamRecipient1.OpenNum . Linq.Parallel.QueryOpeningEnumerator System.Linq.ParallelEnumerable.Sum(ParallelQuery の 3.Aggregate() System.Linq.Parallel.InlinedAggregationOperatorEnumerator の1.MoveNextCore(Int32& currentElement)1 recipient) at System.Linq.Parallel.QueryOperator1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings) at System.Linq.Parallel.QueryOpeningEnumerator1.MoveNext() at System.Linq.Parallel.IntSumAggregationOperator.InternalAggregate(Exception& singularExceptionToThrow) at System.Linq.Parallel.InlinedAggregationOperator1 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.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(オブジェクト 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 に答える