/ / PLINQ Excepción agregada que no entiendo - c #, excepción, plinq

PLINQ Excepción de agregado que no entiendo - c #, excepción, plinq

Estaba haciendo la presentación de PLINQ PCD09 por Igor Ostrovsky, y quería intentar ver qué podía sacar de mi computadora portátil CULV.

En un punto obtuve una extraña excepción y no estoyseguro lo que significa. He condensado el código para una mejor visión general. Son los últimos números primos.Sum () que causa la excepción y si el rango es pequeño (8000), no se lanza la excepción. ¿Alguna idea?

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();

Excepción (bastante larga)

System.AggregateException era Mensaje no manejado = Uno o más errores ocurrió. Fuente = System.Core
StackTrace: en System.Linq.Parallel.QueryTaskGroupState.QueryEnd (Boolean userInitiatedDispose) en System.Linq.Parallel.SpoolingTask.SpoolStopAndGo [TInputOutput, TIgnoreKey] (QueryTaskGroupState groupState, PartitionedStream2 partitions, SynchronousChannel1 [] canales, TaskScheduler taskScheduler) en System.Linq.Parallel.DefaultMergeHelper2.System.Linq.Parallel.IMergeHelper<TInputOutput>.Execute() at System.Linq.Parallel.MergeExecutor1.Ejecute [TKey] (PartitionedStream2 partitions, Boolean ignoreOutput, ParallelMergeOptions options, TaskScheduler taskScheduler, Boolean isOrdered, CancellationState cancellationState, Int32 queryId) at System.Linq.Parallel.PartitionedStreamMerger1.Reciba [TKey] (PartitionedStream)2 partitionedStream) at System.Linq.Parallel.InlinedAggregationOperator3.WrapPartitionedStream [TKey] (PartitionedStream2 inputStream, IPartitionedStreamRecipient1 destinatario, preferStriping booleano, Ajustes de configuración de consulta) en System.Linq.Parallel.UnaryQueryOperator2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream2 flujo de entrada) en System.Linq.Parallel.WhereQueryOperator1.WrapPartitionedStream[TKey](PartitionedStream2 flujo de entrada, IPartitionedStreamRecipient1 recipient, Boolean preferStriping, QuerySettings settings) at System.Linq.Parallel.UnaryQueryOperator2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive [TKey] (PartitionedStream2 inputStream) at System.Linq.Parallel.ScanQueryOperator1.ScanEnumerableQueryOperatorResults.GivePartitionedStream (IPartitionedStreamRecipient1 recipient) at System.Linq.Parallel.UnaryQueryOperator2.UnaryQueryOperatorResults.GivePartitionedStream (IPartitionedStreamRecipient1 recipient) at System.Linq.Parallel.UnaryQueryOperator2.UnaryQueryOperatorResults.GivePartitionedStream (IPartitionedStreamRecipient1 recipient) at System.Linq.Parallel.QueryOperator1.GetOpenedEnumerator (Nullable1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings) at System.Linq.Parallel.QueryOpeningEnumerator1.OpenQuery () en System.Linq.Parallel.QueryOpeningEnumerator1.MoveNext() at System.Linq.Parallel.IntSumAggregationOperator.InternalAggregate(Exception& singularExceptionToThrow) at System.Linq.Parallel.InlinedAggregationOperator3. Agregado () en System.Linq.ParallelEnumerable.Sum (ParallelQuery1 source) at ConsoleTest.TestClass.Test() in C:UsershenrikDocumentsVisual Studio 2010ProjectsCSharpConsoleTestConsoleTestTestClass.cs:line 23 at ConsoleTest.Program.Main(String[] args) in C:UsershenrikDocumentsVisual Studio 2010ProjectsCSharpConsoleTestConsoleTestProgram.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 & elemento actual en System.Linq.Parallel.InlinedAggregationOperatorEnumerator1.MoveNext(TIntermediate& currentElement, Int32& currentKey) at System.Linq.Parallel.StopAndGoSpoolingTask2.SpoolingWork () en System.Linq.Parallel.SpoolingTaskBase.Work () en System.Linq.Parallel.QueryTask.BaseWork (Object no usado) en System.Linq.Parallel.QueryTask. <. cctor> b__0 (Object o) en System.Threading.Tasks.Task.InnerInvoke () en System.Threading.Tasks.Task.Execute () InnerException:

Respuestas

5 para la respuesta № 1

Si elimina la llamada a AsParallel (), puede ver que Enumerable.Sum lanza y OverflowException. Cambiar Suma () a Suma (x => (largo) x) debería ayudar.