1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
7 // FloatSumAggregationOperator.cs
9 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
11 using System.Collections.Generic;
12 using System.Diagnostics;
13 using System.Threading;
15 namespace System.Linq.Parallel
18 /// An inlined sum aggregation and its enumerator, for floats.
20 internal sealed class FloatSumAggregationOperator : InlinedAggregationOperator<float, double, float>
22 //---------------------------------------------------------------------------------------
23 // Constructs a new instance of a sum associative operator.
26 internal FloatSumAggregationOperator(IEnumerable<float> child) : base(child)
30 //---------------------------------------------------------------------------------------
31 // Executes the entire query tree, and aggregates the intermediate results into the
32 // final result based on the binary operators and final reduction.
35 // The single result of aggregation.
38 protected override float InternalAggregate(ref Exception singularExceptionToThrow)
40 // Because the final reduction is typically much cheaper than the intermediate
41 // reductions over the individual partitions, and because each parallel partition
42 // will do a lot of work to produce a single output element, we prefer to turn off
43 // pipelining, and process the final reductions serially.
44 using (IEnumerator<double> enumerator = GetEnumerator(ParallelMergeOptions.FullyBuffered, true))
46 // We just reduce the elements in each output partition.
48 while (enumerator.MoveNext())
50 sum += enumerator.Current;
57 //---------------------------------------------------------------------------------------
58 // Creates an enumerator that is used internally for the final aggregation step.
61 protected override QueryOperatorEnumerator<double, int> CreateEnumerator<TKey>(
62 int index, int count, QueryOperatorEnumerator<float, TKey> source, object sharedData,
63 CancellationToken cancellationToken)
65 return new FloatSumAggregationOperatorEnumerator<TKey>(source, index, cancellationToken);
68 //---------------------------------------------------------------------------------------
69 // This enumerator type encapsulates the intermediary aggregation over the underlying
70 // (possibly partitioned) data source.
73 private class FloatSumAggregationOperatorEnumerator<TKey> : InlinedAggregationOperatorEnumerator<double>
75 private readonly QueryOperatorEnumerator<float, TKey> _source; // The source data.
77 //---------------------------------------------------------------------------------------
78 // Instantiates a new aggregation operator.
81 internal FloatSumAggregationOperatorEnumerator(QueryOperatorEnumerator<float, TKey> source, int partitionIndex,
82 CancellationToken cancellationToken) :
83 base(partitionIndex, cancellationToken)
85 Debug.Assert(source != null);
89 //---------------------------------------------------------------------------------------
90 // Tallies up the sum of the underlying data source, walking the entire thing the first
91 // time MoveNext is called on this object.
94 protected override bool MoveNextCore(ref double currentElement)
96 float element = default(float);
97 TKey keyUnused = default(TKey);
99 QueryOperatorEnumerator<float, TKey> source = _source;
100 if (source.MoveNext(ref element, ref keyUnused))
102 // We just scroll through the enumerator and accumulate the sum.
103 double tempSum = 0.0f;
107 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
108 CancellationState.ThrowIfCanceled(_cancellationToken);
112 while (source.MoveNext(ref element, ref keyUnused));
114 // The sum has been calculated. Now just return.
115 currentElement = tempSum;
122 //---------------------------------------------------------------------------------------
123 // Dispose of resources associated with the underlying enumerator.
126 protected override void Dispose(bool disposing)
128 Debug.Assert(_source != null);