3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
8 // DoubleSumAggregationOperator.cs
10 // <OWNER>Microsoft</OWNER>
12 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
14 using System.Collections.Generic;
15 using System.Diagnostics.Contracts;
16 using System.Threading;
18 using System.Core; // for System.Core.SR
20 namespace System.Linq.Parallel
23 /// An inlined sum aggregation and its enumerator, for doubles.
25 internal sealed class DoubleSumAggregationOperator : InlinedAggregationOperator<double, double, double>
28 //---------------------------------------------------------------------------------------
29 // Constructs a new instance of a sum associative operator.
32 internal DoubleSumAggregationOperator(IEnumerable<double> child) : base(child)
36 //---------------------------------------------------------------------------------------
37 // Executes the entire query tree, and aggregates the intermediate results into the
38 // final result based on the binary operators and final reduction.
41 // The single result of aggregation.
44 protected override double InternalAggregate(ref Exception singularExceptionToThrow)
46 // Because the final reduction is typically much cheaper than the intermediate
47 // reductions over the individual partitions, and because each parallel partition
48 // will do a lot of work to produce a single output element, we prefer to turn off
49 // pipelining, and process the final reductions serially.
50 using (IEnumerator<double> enumerator = GetEnumerator(ParallelMergeOptions.FullyBuffered, true))
52 // We just reduce the elements in each output partition.
54 while (enumerator.MoveNext())
58 sum += enumerator.Current;
66 //---------------------------------------------------------------------------------------
67 // Creates an enumerator that is used internally for the final aggregation step.
70 protected override QueryOperatorEnumerator<double,int> CreateEnumerator<TKey>(
71 int index, int count, QueryOperatorEnumerator<double, TKey> source, object sharedData, CancellationToken cancellationToken)
73 return new DoubleSumAggregationOperatorEnumerator<TKey>(source, index, cancellationToken);
76 //---------------------------------------------------------------------------------------
77 // This enumerator type encapsulates the intermediary aggregation over the underlying
78 // (possibly partitioned) data source.
81 private class DoubleSumAggregationOperatorEnumerator<TKey> : InlinedAggregationOperatorEnumerator<double>
83 private readonly QueryOperatorEnumerator<double, TKey> m_source; // The source data.
85 //---------------------------------------------------------------------------------------
86 // Instantiates a new aggregation operator.
89 internal DoubleSumAggregationOperatorEnumerator(QueryOperatorEnumerator<double, TKey> source, int partitionIndex,
90 CancellationToken cancellationToken) :
91 base(partitionIndex, cancellationToken)
93 Contract.Assert(source != null);
97 //---------------------------------------------------------------------------------------
98 // Tallies up the sum of the underlying data source, walking the entire thing the first
99 // time MoveNext is called on this object.
102 protected override bool MoveNextCore(ref double currentElement)
104 double element = default(double);
105 TKey keyUnused = default(TKey);
107 QueryOperatorEnumerator<double, TKey> source = m_source;
108 if (source.MoveNext(ref element, ref keyUnused))
110 // We just scroll through the enumerator and accumulate the sum.
111 double tempSum = 0.0;
115 if ((i++ & CancellationState.POLL_INTERVAL) == 0)
116 CancellationState.ThrowIfCanceled(m_cancellationToken);
119 while (source.MoveNext(ref element, ref keyUnused));
121 // The sum has been calculated. Now just return.
122 currentElement = tempSum;
129 //---------------------------------------------------------------------------------------
130 // Dispose of resources associated with the underlying enumerator.
133 protected override void Dispose(bool disposing)
135 Contract.Assert(m_source != null);