internal override IEnumerable <T> GetSequential() { if (source != null) { return(source); } return(WrapHelper.Wrap(customPartitioner.GetPartitions(1))[0]); }
private void ApplySpaceAndWrappingOperations( FormattingContext context, TokenStream tokenStream, TokenPairWithOperations[] tokenOperations, OperationApplier applier, CancellationToken cancellationToken) { using (Logger.LogBlock(FunctionId.Formatting_ApplySpaceAndLine, cancellationToken)) { // go through each token pairs and apply operations. operations don't need to be applied in order var partitioner = new Partitioner(context, tokenStream, tokenOperations); // always create task 1 more than current processor count var partitions = partitioner.GetPartitions(this.TaskExecutor == TaskExecutor.Synchronous ? 1 : Environment.ProcessorCount + 1); var tasks = new List <Task>( partitions.Select( partition => this.TaskExecutor.StartNew( () => { cancellationToken.ThrowIfCancellationRequested(); partition.Do(operationPair => ApplySpaceAndWrappingOperationsBody(context, tokenStream, operationPair, applier, cancellationToken)); }, cancellationToken))); Task.WaitAll(tasks.ToArray(), cancellationToken); } }
/// <include file='../../_Doc/mscorlib.xml' path='doc/members/member[@name="M:System.Threading.Tasks.Parallel.ForEach``1(System.Collections.Concurrent.Partitioner{``0},System.Threading.Tasks.ParallelOptions,System.Action{``0,System.Threading.Tasks.ParallelLoopState})"]/*' /> public static ParallelLoopResult ForEach <TSource>( Partitioner <TSource> source, ParallelOptions parallelOptions, Action <TSource, ParallelLoopState> body) { var loopState = new ParallelLoopState(); foreach (var partition in source.GetPartitions(1)) { while (partition.MoveNext()) { if (loopState.ShouldExitCurrentIteration) { return new ParallelLoopResult { IsCompleted = false, LowestBreakIteration = null } } ; body(partition.Current, loopState); } } return(new ParallelLoopResult { IsCompleted = true }); } #endregion }
internal override IList <IEnumerable <T> > GetEnumerables(QueryOptions options) { if (customPartitioner != null) { return(WrapHelper.Wrap(customPartitioner.GetPartitions(options.PartitionCount))); } Partitioner <T> partitioner = (options.UseStrip) ? ParallelPartitioner.CreateForStrips(source, 1) : ParallelPartitioner.CreateBest(source); return(WrapHelper.Wrap(partitioner.GetPartitions(options.PartitionCount))); }
private static void ParseLogsSequential(Partitioner <string> logsPartitioner, out ConcurrentBag <string> stackTraces, out long workingTime) { var sequentialTimer = Stopwatch.StartNew(); var sequentialStackTraces = new ConcurrentBag <string>(); var enumerator = logsPartitioner.GetPartitions(1).Single(); while (enumerator.MoveNext()) { ProcessLine(enumerator.Current, ref sequentialStackTraces); } stackTraces = sequentialStackTraces; workingTime = sequentialTimer.ElapsedMilliseconds; }
private void ApplySpaceAndWrappingOperations( FormattingContext context, TokenPairWithOperations[] tokenOperations, OperationApplier applier, CancellationToken cancellationToken) { using (Logger.LogBlock(FunctionId.Formatting_ApplySpaceAndLine, cancellationToken)) { // go through each token pairs and apply operations. operations don't need to be applied in order var partitioner = new Partitioner(context, tokenOperations); // always create task 1 more than current processor count var partitions = partitioner.GetPartitions(partitionCount: 1, cancellationToken); foreach (var partition in partitions) { cancellationToken.ThrowIfCancellationRequested(); partition.Do(operationPair => ApplySpaceAndWrappingOperationsBody(context, operationPair, applier, cancellationToken)); } } }
internal override void GivePartitionedStream(IPartitionedStreamRecipient <TElement> recipient) { Contract.Assert(m_settings.DegreeOfParallelism.HasValue); int partitionCount = m_settings.DegreeOfParallelism.Value; OrderablePartitioner <TElement> orderablePartitioner = m_partitioner as OrderablePartitioner <TElement>; // If the partitioner is not orderable, it will yield zeros as order keys. The order index state // is irrelevant. OrdinalIndexState indexState = (orderablePartitioner != null) ? GetOrdinalIndexState(orderablePartitioner) : OrdinalIndexState.Shuffled; PartitionedStream <TElement, int> partitions = new PartitionedStream <TElement, int>( partitionCount, Util.GetDefaultComparer <int>(), indexState); if (orderablePartitioner != null) { IList <IEnumerator <KeyValuePair <long, TElement> > > partitionerPartitions = orderablePartitioner.GetOrderablePartitions(partitionCount); if (partitionerPartitions == null) { throw new InvalidOperationException(SR.GetString(SR.PartitionerQueryOperator_NullPartitionList)); } if (partitionerPartitions.Count != partitionCount) { throw new InvalidOperationException(SR.GetString(SR.PartitionerQueryOperator_WrongNumberOfPartitions)); } for (int i = 0; i < partitionCount; i++) { IEnumerator <KeyValuePair <long, TElement> > partition = partitionerPartitions[i]; if (partition == null) { throw new InvalidOperationException(SR.GetString(SR.PartitionerQueryOperator_NullPartition)); } partitions[i] = new OrderablePartitionerEnumerator(partition); } } else { IList <IEnumerator <TElement> > partitionerPartitions = m_partitioner.GetPartitions(partitionCount); if (partitionerPartitions == null) { throw new InvalidOperationException(SR.GetString(SR.PartitionerQueryOperator_NullPartitionList)); } if (partitionerPartitions.Count != partitionCount) { throw new InvalidOperationException(SR.GetString(SR.PartitionerQueryOperator_WrongNumberOfPartitions)); } for (int i = 0; i < partitionCount; i++) { IEnumerator <TElement> partition = partitionerPartitions[i]; if (partition == null) { throw new InvalidOperationException(SR.GetString(SR.PartitionerQueryOperator_NullPartition)); } partitions[i] = new PartitionerEnumerator(partition); } } recipient.Receive <int>(partitions); }