public void multi_threaded_locale_access_should_be_okay() { ParallelEnumerable.Range(0, 9999) .Select(i => new Name("nl")).ToList(); }
private static IEnumerable <Labeled <Func <int, int, IEnumerable <int> > > > Expanders() { yield return(Labeled.Label("Array", (Func <int, int, IEnumerable <int> >)((start, count) => Enumerable.Range(start * count, count).ToArray()))); yield return(Labeled.Label("Enumerable.Range", (Func <int, int, IEnumerable <int> >)((start, count) => Enumerable.Range(start * count, count)))); yield return(Labeled.Label("ParallelEnumerable.Range", (Func <int, int, IEnumerable <int> >)((start, count) => ParallelEnumerable.Range(start * count, count).AsOrdered().Select(x => x)))); }
public static void Union_ArgumentNullException() { Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <int>)null).Union(ParallelEnumerable.Range(0, 1))); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Range(0, 1).Union(null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <int>)null).Union(ParallelEnumerable.Range(0, 1), EqualityComparer <int> .Default)); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Range(0, 1).Union(null, EqualityComparer <int> .Default)); }
public static int getPrimeCount(int min, int count) { return(ParallelEnumerable.Range(min, count).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0))); }
public static void Intersect_ArgumentNullException() { AssertExtensions.Throws <ArgumentNullException>("first", () => ((ParallelQuery <int>)null).Intersect(ParallelEnumerable.Range(0, 1))); AssertExtensions.Throws <ArgumentNullException>("second", () => ParallelEnumerable.Range(0, 1).Intersect(null)); AssertExtensions.Throws <ArgumentNullException>("first", () => ((ParallelQuery <int>)null).Intersect(ParallelEnumerable.Range(0, 1), EqualityComparer <int> .Default)); AssertExtensions.Throws <ArgumentNullException>("second", () => ParallelEnumerable.Range(0, 1).Intersect(null, EqualityComparer <int> .Default)); }
public static void Zip_ArgumentNullException() { Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <int>)null).Zip(ParallelEnumerable.Range(0, 1), (x, y) => x)); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Range(0, 1).Zip(null, (Func <int, int, int>)((x, y) => x))); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Range(0, 1).Zip(ParallelEnumerable.Range(0, 1), (Func <int, int, int>)null)); }
public static void ForAll_AggregateException() { AssertThrows.Wrapped <DeliberateTestException>(() => ParallelEnumerable.Range(0, 1).ForAll(x => { throw new DeliberateTestException(); })); AssertThrows.Wrapped <DeliberateTestException>(() => ParallelEnumerable.Range(0, 1).Select((Func <int, int>)(x => { throw new DeliberateTestException(); })).ForAll(x => { })); }
public static void Concat_NotSupportedException() { #pragma warning disable 618 Assert.Throws <NotSupportedException>(() => ParallelEnumerable.Range(0, 1).Concat(Enumerable.Range(0, 1))); #pragma warning restore 618 }
public static void Concat_ArgumentNullException() { AssertExtensions.Throws <ArgumentNullException>("first", () => ((ParallelQuery <int>)null).Concat(ParallelEnumerable.Range(0, 1))); AssertExtensions.Throws <ArgumentNullException>("second", () => ParallelEnumerable.Range(0, 1).Concat(null)); }
public static void SequenceEqual_OperationCanceledException() { AssertThrows.EventuallyCanceled((source, canceler) => source.OrderBy(x => x).SequenceEqual(ParallelEnumerable.Range(0, 128).AsOrdered(), new CancelingEqualityComparer <int>(canceler))); AssertThrows.EventuallyCanceled((source, canceler) => ParallelEnumerable.Range(0, 128).AsOrdered().SequenceEqual(source.OrderBy(x => x), new CancelingEqualityComparer <int>(canceler))); }
public static void SequenceEqual_AggregateException_Wraps_OperationCanceledException() { AssertThrows.OtherTokenCanceled((source, canceler) => source.OrderBy(x => x).SequenceEqual(ParallelEnumerable.Range(0, 128).AsOrdered(), new CancelingEqualityComparer <int>(canceler))); AssertThrows.OtherTokenCanceled((source, canceler) => ParallelEnumerable.Range(0, 128).AsOrdered().SequenceEqual(source.OrderBy(x => x), new CancelingEqualityComparer <int>(canceler))); AssertThrows.SameTokenNotCanceled((source, canceler) => source.OrderBy(x => x).SequenceEqual(ParallelEnumerable.Range(0, 128).AsOrdered(), new CancelingEqualityComparer <int>(canceler))); AssertThrows.SameTokenNotCanceled((source, canceler) => ParallelEnumerable.Range(0, 128).AsOrdered().SequenceEqual(source.OrderBy(x => x), new CancelingEqualityComparer <int>(canceler))); }
public static void RunPlinqModesTests() { // I would assume that this gets the number of processors (ie. Environment.ProcessorCount) // but since we are trying to exclude dependencies that aren't part of the contract, we // can't use System.Runtime.Extensions. So we had to remove this check. // if (SchedulingProxy.GetDefaultDegreeOfParallelism() == 1) // { // Console.WriteLine(" - Test does not apply to the DOP=1 case."); // return true; // } Action <ParallelExecutionMode, Verifier>[] hardQueries = { (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Select(x => verifier.Verify(x)).Where(x => true).TakeWhile((x, i) => true).ToArray(), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Select(x => verifier.Verify(x)).Where(x => true).TakeWhile((x, i) => true).Iterate(), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Where(x => true).Select(x => verifier.Verify(x)).ElementAt(5), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Where(x => true).Select((x, i) => verifier.Verify(x)).Iterate(), }; Action <ParallelExecutionMode, Verifier>[] easyQueries = { (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .TakeWhile(x => true).Select(x => verifier.Verify(x)).ToArray(), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .TakeWhile(x => true).Select(x => verifier.Verify(x)).Iterate(), (mode, verifier) => Enumerable.Range(0, 1000).ToArray().AsParallel() .Select(x => verifier.Verify(x)).Take(100).WithExecutionMode(mode).ToArray(), (mode, verifier) => Enumerable.Range(0, 1000).ToArray().AsParallel().WithExecutionMode(mode) .Take(100).Select(x => verifier.Verify(x)).Iterate(), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Select(x => verifier.Verify(x)).ElementAt(5), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Select(x => verifier.Verify(x)).SelectMany((x, i) => Enumerable.Repeat(1, 2)).Iterate(), (mode, verifier) => Enumerable.Range(0, 1000).AsParallel().WithExecutionMode(mode) .Select(x => verifier.Verify(x)).SelectMany((x, i) => Enumerable.Repeat(1, 2)).Iterate(), (mode, verifier) => Enumerable.Range(0, 1000).AsParallel().WithExecutionMode(mode).AsUnordered() .Select(x => verifier.Verify(x)).Select((x, i) => x).Iterate(), (mode, verifier) => Enumerable.Range(0, 1000).AsParallel().WithExecutionMode(mode).AsUnordered().Where(x => true).Select(x => verifier.Verify(x)).First(), (mode, verifier) => Enumerable.Range(0, 1000).AsParallel().WithExecutionMode(mode) .Select(x => verifier.Verify(x)).OrderBy(x => x).ToArray(), (mode, verifier) => Enumerable.Range(0, 1000).AsParallel().WithExecutionMode(mode) .Select(x => verifier.Verify(x)).OrderBy(x => x).Iterate(), (mode, verifier) => Enumerable.Range(0, 1000).AsParallel().AsOrdered().WithExecutionMode(mode) .Where(x => true).Select(x => verifier.Verify(x)) .Concat(Enumerable.Range(0, 1000).AsParallel().AsOrdered().Where(x => true)) .ToList(), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Where(x => true).Select(x => verifier.Verify(x)).Take(100).ToArray(), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Where(x => true).Select(x => verifier.Verify(x)).Take(100).Iterate(), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Select(x => verifier.Verify(x)).TakeWhile(x => true).ToArray(), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Select(x => verifier.Verify(x)).TakeWhile(x => true).Iterate(), (mode, verifier) => ParallelEnumerable.Range(0, 1000) .OrderBy(x => x).Select(x => verifier.Verify(x)).WithExecutionMode(mode).ElementAt(5), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .OrderBy(x => x).Select((x, i) => verifier.Verify(x)).Iterate(), (mode, verifier) => ParallelEnumerable.Range(0, 1000).WithExecutionMode(mode) .Where(x => true).Select(x => verifier.Verify(x)).OrderBy(x => x).Take(10000).Iterate(), }; // Verify that all queries in 'easyQueries' run in parallel in default mode for (int i = 0; i < easyQueries.Length; i++) { Verifier verifier = new ParVerifier(); easyQueries[i].Invoke(ParallelExecutionMode.Default, verifier); if (!verifier.Passed) { Assert.True(false, string.Format("Easy query {0} expected to run in parallel in default mode", i)); } } // Verify that all queries in 'easyQueries' always run in forced mode for (int i = 0; i < easyQueries.Length; i++) { Verifier verifier = new ParVerifier(); easyQueries[i].Invoke(ParallelExecutionMode.ForceParallelism, verifier); if (!verifier.Passed) { Assert.True(false, string.Format("Easy query {0} expected to run in parallel in force-parallelism mode", i)); } } // Verify that all queries in 'easyQueries' always run in forced mode for (int i = 0; i < hardQueries.Length; i++) { Verifier verifier = new ParVerifier(); hardQueries[i].Invoke(ParallelExecutionMode.ForceParallelism, verifier); if (!verifier.Passed) { Assert.True(false, string.Format("Hard query {0} expected to run in parallel in force-parallelism mode", i)); } } }
static void Main(string[] args) { var customers = new [] { new Customer { ID = 1, FirstName = "Sandeep", LastName = "Ramani" }, new Customer { ID = 2, FirstName = "Dharmik", LastName = "Chotaliya" }, new Customer { ID = 3, FirstName = "Nisar", LastName = "Kalia" }, new Customer { ID = 4, FirstName = "Ravi", LastName = "Mapara" }, new Customer { ID = 5, FirstName = "Hardik", LastName = "Mistry" }, new Customer { ID = 6, FirstName = "Sandy", LastName = "Ramani" }, new Customer { ID = 7, FirstName = "Jigar", LastName = "Shah" }, new Customer { ID = 8, FirstName = "Kaushal", LastName = "Parik" }, new Customer { ID = 9, FirstName = "Abhishek", LastName = "Swarnker" }, new Customer { ID = 10, FirstName = "Sanket", LastName = "Patel" }, new Customer { ID = 11, FirstName = "Dinesh", LastName = "Prajapati" }, new Customer { ID = 12, FirstName = "Jayesh", LastName = "Patel" }, new Customer { ID = 13, FirstName = "Nimesh", LastName = "Mishra" }, new Customer { ID = 14, FirstName = "Shiva", LastName = "Reddy" }, new Customer { ID = 15, FirstName = "Jasmin", LastName = "Malviya" }, new Customer { ID = 16, FirstName = "Haresh", LastName = "Bhanderi" }, new Customer { ID = 17, FirstName = "Ankit", LastName = "Ramani" }, new Customer { ID = 18, FirstName = "Sanket", LastName = "Shah" }, new Customer { ID = 19, FirstName = "Amit", LastName = "Shah" }, new Customer { ID = 20, FirstName = "Nilesh", LastName = "Soni" } }; var results = from c in customers where c.FirstName.StartsWith("San") select c; //使用Parallel并行查询 var resultsUserParallel = from c in customers.AsParallel() where c.FirstName.StartsWith("San") select c; //对并行查询的结果排序,AsOrdered影响性能,最好不用 var resultsUserAsOrdered = from c in customers.AsParallel().AsOrdered() where c.FirstName.StartsWith("San") select c; //强制使用Parallel并行查询,如果让AsParallel自己决定的话 //有可能它会选择原来的Linq的执行方式 var resultsForceParallelism = from c in customers.AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism) where c.FirstName.StartsWith("San") select c; //确定并行执行使用CPU的最多的数量 var resultsDegreeOfParallelism = from c in customers.AsParallel().WithDegreeOfParallelism(2) where c.FirstName.StartsWith("San") select c; //随机产生一个有序的数 IEnumerable <int> evens = ((ParallelQuery <int>)ParallelEnumerable.Range(0, 50000)) .Where(i => i % 2 == 0) .Select(i => i).ToList(); //使用并行产生重复的数字ParallelQuery<TResult> int sum = ParallelEnumerable.Repeat(2, 50000).Select(i => i).Sum(); Console.WriteLine(sum); Console.ReadKey(); }
public static int GetPrimesCount(int start, int count) { return(ParallelEnumerable.Range(start, count).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0))); }
public Task <int> GetPrimesCountAsync(int start, int count) { return(Task.Run(() => ParallelEnumerable.Range(start, count).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0)))); }
public static void Concat_UnionSources_PrematureMerges() { const int ElementCount = 2048; ParallelQuery <int> leftQuery = ParallelEnumerable.Range(0, ElementCount / 4).Union(ParallelEnumerable.Range(ElementCount / 4, ElementCount / 4)); ParallelQuery <int> rightQuery = ParallelEnumerable.Range(2 * ElementCount / 4, ElementCount / 4).Union(ParallelEnumerable.Range(3 * ElementCount / 4, ElementCount / 4)); var results = new HashSet <int>(leftQuery.Concat(rightQuery)); Assert.Equal(ElementCount, results.Count); }
public static void Zip_NotSupportedException() { #pragma warning disable 618 Assert.Throws <NotSupportedException>(() => ParallelEnumerable.Range(0, 1).Zip(Enumerable.Range(0, 1), (x, y) => x)); #pragma warning restore 618 }
public static void Single_AggregateException() { AssertThrows.Wrapped <DeliberateTestException>(() => ParallelEnumerable.Range(0, 1).Single(x => { throw new DeliberateTestException(); })); AssertThrows.Wrapped <DeliberateTestException>(() => ParallelEnumerable.Range(0, 1).SingleOrDefault(x => { throw new DeliberateTestException(); })); }
public static void SequenceEqual_AggregateException(Labeled <Operation> source, Labeled <Operation> operation) { // Sequence equal double wraps queries that throw. ThrowsWrapped(() => operation.Item(DefaultStart, DefaultSize, source.Item).SequenceEqual(ParallelEnumerable.Range(DefaultStart, DefaultSize).AsOrdered())); ThrowsWrapped(() => ParallelEnumerable.Range(DefaultStart, DefaultSize).AsOrdered().SequenceEqual(operation.Item(DefaultStart, DefaultSize, source.Item))); }
public static void Single_AllMatch(int count) { Assert.Throws <InvalidOperationException>(() => ParallelEnumerable.Range(0, count).Single(x => true)); }
public static void ForAll_ArgumentNullException() { AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <int>)null).ForAll(x => { })); AssertExtensions.Throws <ArgumentNullException>("action", () => ParallelEnumerable.Range(0, 1).ForAll(null)); }
public static void WithExecutionMode_Multiple(ParallelExecutionMode first, ParallelExecutionMode second) { Assert.Throws <InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithExecutionMode(first).WithExecutionMode(second)); }
int GetPrimesCount(int start, int count) { var items = ParallelEnumerable.Range(start, count).Count(item => Enumerable.Range(2, (int)Math.Sqrt(item) - 1).All(i => item % i > 0)); return(items); }
public static void DegreeOfParallelism_Multiple() { Assert.Throws <InvalidOperationException>(() => ParallelEnumerable.Range(0, 2).WithDegreeOfParallelism(2).WithDegreeOfParallelism(2)); }
private static ParallelQuery <int> GenerateMD5InputData() { return(ParallelEnumerable.Range(1, NUM_MD5_HASHES)); }
private static void RunScenario(Scenario scenario, int threadCount) { Console.WriteLine("Executing Scenario: '{0}' with: '{1}' threads over: '{2:N0}' iterations.", scenario, threadCount, ITERATION_COUNT); Func <string> idGenerator; switch (scenario) { case Scenario.Unsafe: idGenerator = IDGeneratorService.GetNextIDUnsafe; break; case Scenario.WithLocking: idGenerator = IDGeneratorService.GetNextIDWithLocking; break; case Scenario.ThreadLocal: idGenerator = IDGeneratorService.GetNextIDThreadLocal; break; case Scenario.ThreadStatic: idGenerator = IDGeneratorService.GetNextIDThreadStatic; break; case Scenario.NewBuffer: idGenerator = IDGeneratorService.GetNextIDNewBuffer; break; case Scenario.SpinLock: idGenerator = IDGeneratorService.GetNextIDSpinLock; break; #if NETCOREAPP2_1 case Scenario.Span: idGenerator = IDGeneratorService.GetNextIDSpan; break; #endif default: throw new ArgumentOutOfRangeException(nameof(scenario), scenario, null); } var sw = Stopwatch.StartNew(); ParallelEnumerable .Range(1, ITERATION_COUNT) .WithDegreeOfParallelism(threadCount) .WithExecutionMode(ParallelExecutionMode.ForceParallelism) .ForAll(_ => idGenerator()); sw.Stop(); using (var process = Process.GetCurrentProcess()) { Console.WriteLine(" - Execution time: {0}\r\n - Gen-0: {1}, Gen-1: {2}, Gen-2: {3}\r\n - Peak WrkSet: {4:N0}", sw.Elapsed.ToString(), GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2), process.PeakWorkingSet64); } }
// Should not get the same setting from both operands. public static void Union_NoDuplicateSettings() { CancellationToken t = new CancellationTokenSource().Token; Assert.Throws <InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithCancellation(t).Union(ParallelEnumerable.Range(0, 1).WithCancellation(t))); Assert.Throws <InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithDegreeOfParallelism(1).Union(ParallelEnumerable.Range(0, 1).WithDegreeOfParallelism(1))); Assert.Throws <InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithExecutionMode(ParallelExecutionMode.Default).Union(ParallelEnumerable.Range(0, 1).WithExecutionMode(ParallelExecutionMode.Default))); Assert.Throws <InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithMergeOptions(ParallelMergeOptions.Default).Union(ParallelEnumerable.Range(0, 1).WithMergeOptions(ParallelMergeOptions.Default))); }
public static void Join_ArgumentNullException() { AssertExtensions.Throws <ArgumentNullException>("outer", () => ((ParallelQuery <int>)null).Join(ParallelEnumerable.Range(0, 1), i => i, i => i, (i, j) => i)); AssertExtensions.Throws <ArgumentNullException>("inner", () => ParallelEnumerable.Range(0, 1).Join((ParallelQuery <int>)null, i => i, i => i, (i, j) => i)); AssertExtensions.Throws <ArgumentNullException>("outerKeySelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), (Func <int, int>)null, i => i, (i, j) => i)); AssertExtensions.Throws <ArgumentNullException>("innerKeySelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), i => i, (Func <int, int>)null, (i, j) => i)); AssertExtensions.Throws <ArgumentNullException>("resultSelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), i => i, i => i, (Func <int, int, int>)null)); AssertExtensions.Throws <ArgumentNullException>("outer", () => ((ParallelQuery <int>)null).Join(ParallelEnumerable.Range(0, 1), i => i, i => i, (i, j) => i, EqualityComparer <int> .Default)); AssertExtensions.Throws <ArgumentNullException>("inner", () => ParallelEnumerable.Range(0, 1).Join((ParallelQuery <int>)null, i => i, i => i, (i, j) => i, EqualityComparer <int> .Default)); AssertExtensions.Throws <ArgumentNullException>("outerKeySelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), (Func <int, int>)null, i => i, (i, j) => i, EqualityComparer <int> .Default)); AssertExtensions.Throws <ArgumentNullException>("innerKeySelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), i => i, (Func <int, int>)null, (i, j) => i, EqualityComparer <int> .Default)); AssertExtensions.Throws <ArgumentNullException>("resultSelector", () => ParallelEnumerable.Range(0, 1).Join(ParallelEnumerable.Range(0, 1), i => i, i => i, (Func <int, int, int>)null, EqualityComparer <int> .Default)); }
public static void WithMergeOptions_Multiple(ParallelMergeOptions first, ParallelMergeOptions second) { Assert.Throws <InvalidOperationException>(() => ParallelEnumerable.Range(0, 1).WithMergeOptions(first).WithMergeOptions(second)); }
public static void Sum_Long_SomeNull(int count) { Assert.Equal(Functions.SumRange(0, count / 2), ParallelEnumerable.Range(0, count).Select(x => x < count / 2 ? (long?)x : null).Sum()); Assert.Equal(-Functions.SumRange(0, count / 2), ParallelEnumerable.Range(0, count).Sum(x => x < count / 2 ? -(long?)x : null)); }