// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading.Tasks; namespace CommunityToolkit.HighPerformance.Helpers; /// /// Helpers to work with parallel code in a highly optimized manner. /// public static partial class ParallelHelper { /// /// Executes a specified action in an optimized parallel loop over the input data. /// /// The type of items to iterate over. /// The type of action (implementing of ) to invoke over each item. /// The input representing the data to process. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ForEach(ReadOnlyMemory memory) where TAction : struct, IInAction { ForEach(memory, default(TAction), 1); } /// /// Executes a specified action in an optimized parallel loop over the input data. /// /// The type of items to iterate over. /// The type of action (implementing of ) to invoke over each item. /// The input representing the data to process. /// /// The minimum number of actions to run per individual thread. Set to 1 if all invocations /// should be parallelized, or to a greater number if each individual invocation is fast /// enough that it is more efficient to set a lower bound per each running thread. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ForEach(ReadOnlyMemory memory, int minimumActionsPerThread) where TAction : struct, IInAction { ForEach(memory, default(TAction), minimumActionsPerThread); } /// /// Executes a specified action in an optimized parallel loop over the input data. /// /// The type of items to iterate over. /// The type of action (implementing of ) to invoke over each item. /// The input representing the data to process. /// The instance representing the action to invoke. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ForEach(ReadOnlyMemory memory, in TAction action) where TAction : struct, IInAction { ForEach(memory, action, 1); } /// /// Executes a specified action in an optimized parallel loop over the input data. /// /// The type of items to iterate over. /// The type of action (implementing of ) to invoke over each item. /// The input representing the data to process. /// The instance representing the action to invoke. /// /// The minimum number of actions to run per individual thread. Set to 1 if all invocations /// should be parallelized, or to a greater number if each individual invocation is fast /// enough that it is more efficient to set a lower bound per each running thread. /// public static void ForEach(ReadOnlyMemory memory, in TAction action, int minimumActionsPerThread) where TAction : struct, IInAction { if (minimumActionsPerThread <= 0) { ThrowArgumentOutOfRangeExceptionForInvalidMinimumActionsPerThread(); } if (memory.IsEmpty) { return; } int maxBatches = 1 + ((memory.Length - 1) / minimumActionsPerThread); int cores = Environment.ProcessorCount; int numBatches = Math.Min(maxBatches, cores); // Skip the parallel invocation when a single batch is needed if (numBatches == 1) { foreach (TItem? item in memory.Span) { Unsafe.AsRef(action).Invoke(item); } return; } int batchSize = 1 + ((memory.Length - 1) / numBatches); InActionInvoker actionInvoker = new(batchSize, memory, action); // Run the batched operations in parallel _ = Parallel.For( 0, numBatches, new ParallelOptions { MaxDegreeOfParallelism = numBatches }, actionInvoker.Invoke); } // Wrapping struct acting as explicit closure to execute the processing batches private readonly struct InActionInvoker where TAction : struct, IInAction { private readonly int batchSize; private readonly ReadOnlyMemory memory; private readonly TAction action; [MethodImpl(MethodImplOptions.AggressiveInlining)] public InActionInvoker( int batchSize, ReadOnlyMemory memory, in TAction action) { this.batchSize = batchSize; this.memory = memory; this.action = action; } /// /// Processes the batch of actions at a specified index /// /// The index of the batch to process public void Invoke(int i) { int low = i * this.batchSize; int high = low + this.batchSize; int end = Math.Min(high, this.memory.Length); ref TItem r0 = ref MemoryMarshal.GetReference(this.memory.Span); ref TItem rStart = ref Unsafe.Add(ref r0, low); ref TItem rEnd = ref Unsafe.Add(ref r0, end); while (Unsafe.IsAddressLessThan(ref rStart, ref rEnd)) { Unsafe.AsRef(this.action).Invoke(in rStart); rStart = ref Unsafe.Add(ref rStart, 1); } } } } /// /// A contract for actions being executed on items of a specific type, with readonly access. /// /// The type of items to process. /// If the method is small enough, it is highly recommended to mark it with . public interface IInAction { /// /// Executes the action on a specified item. /// /// The current item to process. void Invoke(in T item); }