Exemplo n.º 1
0
        /// <summary>
        /// 简单map reduce 适合分组统计
        /// </summary>
        public static IDictionary <T, int> MR <T, K>(this IEnumerable <T> input, Func <T, K> keySelector, Func <int, bool> mrFilter = default(Func <int, bool>), ParallelLinqOptions options = default(ParallelLinqOptions))
        {
            var tmpMap = input.AsParallel().ToLookup(e => e, keySelector);

            if (options == default(ParallelLinqOptions))
            {
                options = new ParallelLinqOptions();
            }

            if (mrFilter != default(Func <int, bool>))
            {
                return
                    (tmpMap
                     .AsParallel(options)
                     .Where(e => mrFilter(e.Count()))
                     .ToDictionary(e => e.Key, e => e.AsParallel().Count())
                     .AsParallel(options)
                     .OrderBy(e => e.Value)
                     .ToDictionary(e => e.Key, e => e.Value));
            }
            return
                (tmpMap
                 .AsParallel(options)
                 .ToDictionary(e => e.Key, e => e.AsParallel().Count())
                 .AsParallel(options)
                 .OrderBy(e => e.Value)
                 .ToDictionary(e => e.Key, e => e.Value));
        }
Exemplo n.º 2
0
        /// <summary>
        /// <paramref name="options"/>에 따라 ParallelQuery{TSource} 를 빌드합니다.
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static ParallelQuery <TSource> AsParallel <TSource>(this IEnumerable <TSource> source, ParallelOptions options)
        {
            source.ShouldNotBeNull("source");
            options = options ?? ParallelTool.DefaultParallelOptions;

            return(AsParallel(source, ParallelLinqOptions.CreateFrom(options)));
        }
Exemplo n.º 3
0
        /// <summary>
        /// <paramref name="options"/>에 따라 ParallelQuery{TSource} 를 빌드합니다.
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static ParallelQuery <TSource> AsParallel <TSource>(this IEnumerable <TSource> source, ParallelLinqOptions options = null)
        {
            source.ShouldNotBeNull("source");
            options = options ?? ParallelLinqOptions.CreateFrom(ParallelTool.DefaultParallelOptions);

            if (options.TaskScheduler != null && options.TaskScheduler != TaskScheduler.Default)
            {
                throw new InvalidOperationException("Parallel LINQ 는 기본 TaskScheduler만 지원합니다. " +
                                                    "options.TaskScheduler는 TaskScheduler.Default가 아닙니다.");
            }

            if (IsDebugEnabled)
            {
                log.Debug("병렬 옵션을 기준으로 PLINQ로 변환합니다...  options=[{0}]", options.ObjectToString());
            }

            var result = source.AsParallel();

            if (options.Ordered)
            {
                result = result.AsOrdered();
            }

            if (options.CancellationToken.CanBeCanceled)
            {
                result = result.WithCancellation(options.CancellationToken);
            }

            if (options.MaxDegreeOfParallelism >= 1)
            {
                result = result.WithDegreeOfParallelism(options.MaxDegreeOfParallelism);
            }

            if (options.ExecutionMode != ParallelExecutionMode.Default)
            {
                result = result.WithExecutionMode(options.ExecutionMode);
            }

            if (options.MergeOptions != ParallelMergeOptions.Default)
            {
                result = result.WithMergeOptions(options.MergeOptions);
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <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("source");
            }

            // Validate unsupported options
            if (parallelOptions.TaskScheduler != null && parallelOptions.TaskScheduler != TaskScheduler.Default)
            {
                throw new ArgumentException("Parallel LINQ only supports the default TaskScheduler.");
            }

            // First convert to PLINQ
            var result = source.AsParallel();

            // Then apply all of the options as requested...
            if (parallelOptions.Ordered)
            {
                result = result.AsOrdered();
            }
            if (parallelOptions.CancellationToken.CanBeCanceled)
            {
                result = result.WithCancellation(parallelOptions.CancellationToken);
            }
            if (parallelOptions.MaxDegreeOfParallelism >= 1)
            {
                result = result.WithDegreeOfParallelism(parallelOptions.MaxDegreeOfParallelism);
            }
            if (parallelOptions.ExecutionMode != ParallelExecutionMode.Default)
            {
                result = result.WithExecutionMode(parallelOptions.ExecutionMode);
            }
            if (parallelOptions.MergeOptions != ParallelMergeOptions.Default)
            {
                result = result.WithMergeOptions(parallelOptions.MergeOptions);
            }
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 简单map reduce 适合分组统计 ,PLINQ延迟执行
        /// </summary>
        public static IDictionary <T, int> LazyMR <T, K>(this IEnumerable <T> input, Func <T, K> keySelector, Func <int, bool> mrFilter = default(Func <int, bool>), ParallelLinqOptions options = default(ParallelLinqOptions))
        {
            var tmpMap = input.AsParallel().ToLookup(e => e, keySelector);

            if (options == default(ParallelLinqOptions))
            {
                options = new ParallelLinqOptions();
            }

            if (mrFilter != default(Func <int, bool>))
            {
                return
                    ((from IGrouping <T, int> wordMap in tmpMap.AsParallel(options)
                      where mrFilter(wordMap.Count())
                      select new { Word = wordMap.Key, Count = wordMap.AsParallel().Count() }).ToDictionary(e => e.Word, e => e.Count));
            }
            return
                ((from IGrouping <T, int> wordMap in tmpMap.AsParallel(options)
                  select new { Word = wordMap.Key, Count = wordMap.AsParallel().Count() })
                 .ToDictionary(e => e.Word, e => e.Count));
        }