Benutzer:MovGP0/Parallel/Aggregation
Zur Navigation springen
Zur Suche springen
MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
Parallel Aggregation[Bearbeiten | Quelltext bearbeiten]
Sum[Bearbeiten | Quelltext bearbeiten]
Multiset Union[Bearbeiten | Quelltext bearbeiten]
Parallel Loops for Aggregation[Bearbeiten | Quelltext bearbeiten]
Partitioner[Bearbeiten | Quelltext bearbeiten]double[] sequence = ...
var lockObject = new object();
var sum = 0.0d;
var rangePartitioner = Partitioner.Create(0, sequence.Length);
Parallel.ForEach(rangePartitioner,
() => 0.0d, // init
(range, loopState, initialValue) => // partial sums
{
double partialSum = initialValue;
for(var i = range.Item1; i < range.Item2; i++)
{
partialSum += Normalize(sequence[i]);
}
return partialSum;
},
localPartialSum => // sum of partial sums
{
lock(lockObject)
{
sum += localPartialSum;
}
});
return sum;
PLINQ Aggregation with Range[Bearbeiten | Quelltext bearbeiten]
int[] histogramm = MakeEmptyHistogram();
return ParallelEnumerable.Range(0, count).Aggregate(
// accumulator seed
() => new Tuple<int[], Random>(MakeEmptyHistogram(), new Random(SampleUtilities.MakeRandomSeed())),
// run simulation and add result to local accumulator
(localAccumulator, i) =>
{
var sample = localAccumulator.Item2.NextDouble();
if(sample > 0.0 && sample < 1.0)
{
var simulationResult = DoSimulation(sample, mean, stdDev);
int histogramBucket = (int)Math.Floor(simulationResult / BucketSize);
if(0 <= histogramBucket && histrogramBucket < TableSize)
{
localAccumulator.Item1[histogramBucket] += 1;
}
}
return localAccumulator;
},
// combine local results
(localAccumulator1, localAccumulator2) =>
{
var combinedHistograms = CombineHistograms(localAccumulator1.Item1, localAccumulator2.Item1);
return new Tuple<int[], Random>(combinedHistograms, null);
},
// global result
finalAccumulator => finalAccumulator.Item1;
);
|