private static void RunConcatTestCore(bool usePipelining, bool useParallelRange, int leftSize, int rightSize) { string method = string.Format("RunConcatTest1(usePipelining={0}, useParallelRange={1}, leftSize={2}, rightSize={3})", usePipelining, useParallelRange, leftSize, rightSize); int[] leftData = new int[leftSize]; for (int i = 0; i < leftSize; i++) { leftData[i] = i; } int[] rightData = new int[rightSize]; for (int i = 0; i < rightSize; i++) { rightData[i] = i; } ParallelQuery <int> q = useParallelRange ? ParallelEnumerable.Range(0, leftSize) : leftData.AsParallel(); IEnumerable <int> r = usePipelining ? (IEnumerable <int>)q.AsOrdered().Concat(rightData.AsParallel()) : (IEnumerable <int>)q.AsOrdered().Concat(rightData.AsParallel()).ToList(); int cnt = 0; foreach (int x in r) { if (cnt < leftSize) { if (x != leftData[cnt]) { Assert.True(false, string.Format(method + " > FAILED. Expected element {0} to == {1} (from left)); got {2} instead", cnt, leftData[cnt], x)); } } else { if (x != rightData[cnt - leftSize]) { Assert.True(false, string.Format(method + " > FAILED. Expected element {0} to == {1} (from right)); got {2} instead", cnt, rightData[cnt - leftSize], x)); } } cnt++; } if (!(cnt == leftSize + rightSize)) { Assert.True(false, string.Format(method + " > FAILED. Expect: {0}, real: {1}", leftSize + rightSize, cnt)); } }
/// <summary>This is the method to opt into Parallel LINQ.</summary> /// <typeparam name="TSource">Specifies the type of elements provided to the query.</typeparam> /// <param name="source">The source query.</param> /// <param name="parallelOptions">The options to use for query processing.</param> /// <returns>The source as a ParallelQuery to bind to ParallelEnumerable extension methods.</returns> public static ParallelQuery <TSource> AsParallel <TSource>(this IEnumerable <TSource> source, ParallelLinqOptions parallelOptions) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (parallelOptions.TaskScheduler != null && parallelOptions.TaskScheduler != TaskScheduler.Default) { throw new ArgumentException("Parallel LINQ only supports the default TaskScheduler."); } ParallelQuery <TSource> source1 = source.AsParallel(); if (parallelOptions.Ordered) { source1 = source1.AsOrdered(); } if (parallelOptions.CancellationToken.CanBeCanceled) { source1 = source1.WithCancellation(parallelOptions.CancellationToken); } if (parallelOptions.MaxDegreeOfParallelism >= 1) { source1 = source1.WithDegreeOfParallelism(parallelOptions.MaxDegreeOfParallelism); } if (parallelOptions.ExecutionMode != ParallelExecutionMode.Default) { source1 = source1.WithExecutionMode(parallelOptions.ExecutionMode); } if (parallelOptions.MergeOptions != ParallelMergeOptions.Default) { source1 = source1.WithMergeOptions(parallelOptions.MergeOptions); } return(source1); }
public static void Union_SourceMultiple(ParallelQuery <int> leftQuery, int leftCount, ParallelQuery <int> rightQuery, int rightCount, int count) { int seen = 0; Assert.All(leftQuery.AsOrdered().Union(rightQuery.AsOrdered()), x => Assert.Equal(seen++, x)); Assert.Equal(count, seen); }
public ParallelQuery <CsvMappingResult <TEntity> > Parse(IEnumerable <Row> csvData) { if (csvData == null) { throw new ArgumentNullException(nameof(csvData)); } ParallelQuery <Row> query = csvData .Skip(options.SkipHeader ? 1 : 0) .AsParallel(); // If you want to get the same order as in the CSV file, this option needs to be set: if (options.KeepOrder) { query = query.AsOrdered(); } query = query .WithDegreeOfParallelism(options.DegreeOfParallelism) .Where(row => !string.IsNullOrWhiteSpace(row.Data)); // Ignore Lines, that start with a comment character: if (!string.IsNullOrWhiteSpace(options.CommentCharacter)) { query = query.Where(line => !line.Data.StartsWith(options.CommentCharacter)); } return(query .Select(line => new TokenizedRow(line.Index, options.Tokenizer.Tokenize(line.Data))) .Select(fields => mapping.Map(fields))); }
// // Left operator factory methods // internal static ParallelQuery <int> MakeCast(bool orderPreserved) { object[] a = Enumerable.Range(0, 100).Cast <object>().ToArray(); ParallelQuery <object> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Cast <int>()); }
internal static ParallelQuery <int> MakeOrderByThenBy(bool orderPreserved) { int[] a = Enumerable.Range(0, 100).Reverse().ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.OrderBy(i => i % 5).ThenBy(i => - i)); }
internal static ParallelQuery <int> MakeGroupJoin(bool orderPreserved) { int[] a = Enumerable.Range(0, 100).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.GroupJoin(a.AsParallel(), i => i, i => i, (i, e) => e.FirstOrDefault())); }
internal static ParallelQuery <int> MakeGroupBy(bool orderPreserved) { int[] a = Enumerable.Range(0, 1000).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.GroupBy(i => i % 100, i => i).Select(g => g.Key)); }
internal static ParallelQuery <int> MakeSelect(bool orderPreserved) { int[] a = Enumerable.Range(-99, 100).Reverse().ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Select(i => - i)); }
internal static ParallelQuery <int> MakeSkipWhile(bool orderPreserved) { int[] a = Enumerable.Range(-10, 110).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.SkipWhile(i => i < 0)); }
internal static ParallelQuery <int> MakeZip(bool orderPreserved) { int[] a = Enumerable.Range(0, 100).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Zip(a.AsParallel(), (i, j) => (i + j) / 2)); }
internal static ParallelQuery <int> MakeWhere(bool orderPreserved) { int[] a = Enumerable.Range(-10, 120).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Where(i => (i >= 0 && i < 100))); }
internal static ParallelQuery <int> MakeWhereIndexed(bool orderPreserved) { int[] a = Enumerable.Range(-10, 120).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Where((i, j) => (j >= 10 && j < 110))); }
internal static ParallelQuery <int> MakeTakeWhileIndexed(bool orderPreserved) { int[] a = Enumerable.Range(0, 110).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.TakeWhile((i, j) => j < 100)); }
internal static ParallelQuery <int> MakeDefaultIfEmpty(bool orderPreserved) { int[] a = Enumerable.Range(1, 100).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.DefaultIfEmpty()); }
internal static ParallelQuery <int> MakeJoin(bool orderPreserved) { int[] a = Enumerable.Range(0, 100).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Join(a.AsParallel(), i => i, i => i, (i, j) => i)); }
internal static ParallelQuery <int> MakeUnion(bool orderPreserved) { int[] a = Enumerable.Range(0, 80).ToArray(); int[] b = Enumerable.Range(20, 80).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Union(b.AsParallel())); }
internal static ParallelQuery <int> MakeExcept(bool orderPreserved) { int[] a = Enumerable.Range(-5, 110).ToArray(); int[] b = { -100, -50, -5, -4, -3, -2, -1, 101, 102, 103, 104, 105, 106, 180 }; ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Except(b.AsParallel())); }
internal static ParallelQuery <int> MakeSelectMany(bool orderPreserved) { int[] a = Enumerable.Range(0, 10).ToArray(); int[] b = Enumerable.Range(0, 10).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.SelectMany(i => b, (i, j) => (10 * i + j))); }
internal static ParallelQuery <int> MakeConcat(bool orderPreserved) { int[] a = Enumerable.Range(1, 35).ToArray(); int[] b = Enumerable.Range(36, 65).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Concat(b.AsParallel())); }
internal static ParallelQuery <int> MakeIntersect(bool orderPreserved) { int[] a = Enumerable.Range(-99, 200).ToArray(); int[] b = Enumerable.Range(0, 200).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Intersect(b.AsParallel())); }
internal static ParallelQuery <int> MakeOfType(bool orderPreserved) { object[] a = Enumerable.Range(0, 50) .Cast <object>() .Concat(Enumerable.Repeat <object>(null, 50)) .Concat(Enumerable.Range(50, 50).Cast <object>()).ToArray(); ParallelQuery <object> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.OfType <int>()); }
public static void Union_SecondOrdered_SourceMultiple(ParallelQuery <int> leftQuery, int leftCount, ParallelQuery <int> rightQuery, int rightCount, int count) { IntegerRangeSet seenUnordered = new IntegerRangeSet(0, leftCount); int seen = leftCount; foreach (int i in leftQuery.Union(rightQuery.AsOrdered())) { if (i >= leftCount) { Assert.Equal(seen++, i); } else { seenUnordered.Add(i); } } Assert.Equal(count, seen); seenUnordered.AssertComplete(); }
internal static ParallelQuery <int> MakeDistinct(bool orderPreserved) { List <int> list = new List <int>(); for (int i = 1; i <= 100; i++) { list.Add(i); list.Add(i); list.Add(i); } int[] a = list.Concat(list).Concat(list).ToArray(); ParallelQuery <int> ipe = a.AsParallel(); if (orderPreserved) { ipe = ipe.AsOrdered(); } return(ipe.Distinct()); }
public static void Union_SourceMultiple(ParallelQuery<int> leftQuery, int leftCount, ParallelQuery<int> rightQuery, int rightCount, int count) { int seen = 0; Assert.All(leftQuery.AsOrdered().Union(rightQuery.AsOrdered()), x => Assert.Equal(seen++, x)); Assert.Equal(count, seen); }
public static void Union_SecondOrdered_SourceMultiple(ParallelQuery<int> leftQuery, int leftCount, ParallelQuery<int> rightQuery, int rightCount, int count) { IntegerRangeSet seenUnordered = new IntegerRangeSet(0, leftCount); int seen = leftCount; foreach (int i in leftQuery.Union(rightQuery.AsOrdered())) { if (i >= leftCount) { Assert.Equal(seen++, i); } else { seenUnordered.Add(i); } } Assert.Equal(count, seen); seenUnordered.AssertComplete(); }