Je préparais la présentation PLINQ PCD09 d'Igor Ostrovsky, et je voulais essayer de voir ce que je pouvais sortir de mon ordinateur portable CULV. À un moment donné, j'ai eu une exception étrange et je ne suis pas sûr de ce que cela signifie. J'ai condensé le code pour une meilleure vue d'ensemble. C'est le dernier nombre.Sum() qui provoque l'exception et si je rends la plage small - 8000 - l'exception n'est pas levée. Des idées?PLINQ Exception d'agrégat Je ne comprends pas
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 (assez longue)
System.AggregateException était message = unhandled Une ou plusieurs erreurs se sont produites . Source = System.Core
StackTrace: à System.Linq.Parallel.QueryTaskGroupState.QueryEnd (Boolean userInitiatedDispose) à System.Linq.Parallel.SpoolingTask.SpoolStopAndGo [TInputOutput, TIgnoreKey] (QueryTaskGroupState groupState, PartitionedStream2 partitions, SynchronousChannel
1 [ ] canaux, 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.Mode réponse [TKey] (PartitionedStream2 partitionedStream) at System.Linq.Parallel.InlinedAggregationOperator
3.WrapPartitionedStream [TKey] (PartitionedStream2 inputStream, IPartitionedStreamRecipient
1 destinataire , Boolean preferStriping , Paramètres QuerySettings) à System.Linq.Parallel.UnaryQueryOperator fluxEntrée) à System.Linq.Parallel.WhereQueryOperator fluxEntrée, 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() à System.Linq.Parallel.QueryOpeningEnumerator1.MoveNext() at System.Linq.Parallel.IntSumAggregationOperator.InternalAggregate(Exception& singularExceptionToThrow) at System.Linq.Parallel.InlinedAggregationOperator
3.Aggregate() à 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) à System.Linq.Parallel.InlinedAggregationOperatorEnumerator1.MoveNext(TIntermediate& currentElement, Int32& currentKey) at System.Linq.Parallel.StopAndGoSpoolingTask
2.SpoolingWork() à System.Linq.Parallel.SpoolingTaskBase.Work() à System.Linq.Parallel.QueryTask.BaseWork (objet inutilisé) à System.Linq.Parallel.QueryTask. < .cctor> b__0 (Object o) à System.Threading.Tasks.Task.InnerInvoke() à System.Threading.Tasks.Task.Execute() InnerException:
Vous avez raison. Maintenant, je suis un peu surpris de savoir que l'ajout de ceci: var numbers = Enumerable.Range (1, 8 * 1000 * 1000) .Cast(); n'est pas assez. La représentation Sum interne doit être un int .. Merci pour votre aide. - –
Moberg
Même si je n'ai pas eu la même erreur, cela m'a aidé à résoudre mon problème ... donc +1 de ma part. – TravisWhidden