From b6562f14dadfcc308dd3015c1524908b51871e40 Mon Sep 17 00:00:00 2001 From: Marcus Cuda Date: Thu, 20 Aug 2009 22:45:53 +0800 Subject: [PATCH] Parellel: added missing 'wait for threads' to complete code - not sure how the tests passed before. there might be a problem with the tests Signed-off-by: Marcus Cuda --- src/Numerics/Threading/Parallel.cs | 4 +- .../ThreadingTests/ParallelForEachTests.cs | 40 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Numerics/Threading/Parallel.cs b/src/Numerics/Threading/Parallel.cs index 83ed7d5e..fff725e7 100644 --- a/src/Numerics/Threading/Parallel.cs +++ b/src/Numerics/Threading/Parallel.cs @@ -171,8 +171,8 @@ namespace MathNet.Numerics.Threading }); ThreadQueue.Enqueue(task); - - maxBlockSize = Math.Min(Control.MaximumBlockSize, maxBlockSize * scalingFactor); + tasks.Add(task); + maxBlockSize = Math.Min(Control.MaximumBlockSize, maxBlockSize * scalingFactor); } WaitForTasksToComplete(tasks.ToArray()); diff --git a/src/UnitTests/ThreadingTests/ParallelForEachTests.cs b/src/UnitTests/ThreadingTests/ParallelForEachTests.cs index 26c36a74..cb00ed0c 100644 --- a/src/UnitTests/ThreadingTests/ParallelForEachTests.cs +++ b/src/UnitTests/ThreadingTests/ParallelForEachTests.cs @@ -38,19 +38,19 @@ namespace MathNet.Numerics.UnitTests.ThreadingTests public class ParallelForEachTests { [Test, ApartmentState(ApartmentState.MTA)] - [Column( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 101)] + [Column( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1001)] public void ParallelForEachInvokesEveryItemOnceMTAOnePerCore(int count) { + // ensure One-Per-Core + ThreadQueue.Start(Environment.ProcessorCount); + var items = new double[count]; var pairs = new List>(); for (var i = 0; i < items.Length; i++) { - items[i] = i; pairs.Add(new KeyValuePair(i, i)); } - - // ensure One-Per-Core - ThreadQueue.Start(Environment.ProcessorCount); + Parallel.ForEach(pairs, pair => items[pair.Key] = pair.Value ); Parallel.ForEach(pairs, pair => @@ -66,19 +66,19 @@ namespace MathNet.Numerics.UnitTests.ThreadingTests } [Test, ApartmentState(ApartmentState.STA)] - [Column(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 101)] + [Column(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1001)] public void ParallelForEachInvokesEveryItemOnceSTAOnePerCore(int count) { + // ensure One-Per-Core + ThreadQueue.Start(Environment.ProcessorCount); + var items = new double[count]; var pairs = new List>(); for (var i = 0; i < items.Length; i++) { - items[i] = i; pairs.Add(new KeyValuePair(i, i)); } - - // ensure One-Per-Core - ThreadQueue.Start(Environment.ProcessorCount); + Parallel.ForEach(pairs, pair => items[pair.Key] = pair.Value); Parallel.ForEach(pairs, pair => @@ -94,19 +94,19 @@ namespace MathNet.Numerics.UnitTests.ThreadingTests } [Test, ApartmentState(ApartmentState.MTA)] - [Column(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 101)] + [Column(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1001)] public void ParallelForEachInvokesEveryItemOnceMTATwoPerCore(int count) { + // ensure Two-Per-Core + ThreadQueue.Start(2 * Environment.ProcessorCount); + var items = new double[count]; var pairs = new List>(); for (var i = 0; i < items.Length; i++) { - items[i] = i; pairs.Add(new KeyValuePair(i, i)); } - - // ensure Two-Per-Core - ThreadQueue.Start(2 * Environment.ProcessorCount); + Parallel.ForEach(pairs, pair => items[pair.Key] = pair.Value); Parallel.ForEach(pairs, pair => @@ -122,19 +122,19 @@ namespace MathNet.Numerics.UnitTests.ThreadingTests } [Test, ApartmentState(ApartmentState.STA)] - [Column(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 101)] + [Column(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1001)] public void ParallelForEachInvokesEveryItemOnceSTATwoPerCore(int count) { + // ensure Two-Per-Core + ThreadQueue.Start(2 * Environment.ProcessorCount); + var items = new double[count]; var pairs = new List>(); for (var i = 0; i < items.Length; i++) { - items[i] = i; pairs.Add(new KeyValuePair(i, i)); } - - // ensure Two-Per-Core - ThreadQueue.Start(2 * Environment.ProcessorCount); + Parallel.ForEach(pairs, pair => items[pair.Key] = pair.Value); Parallel.ForEach(pairs, pair =>