Table of Contents

Class ParallelProcessingHelper

Namespace
AiDotNet.Helpers
Assembly
AiDotNet.dll

Helper class for executing multiple tasks in parallel to improve performance.

public static class ParallelProcessingHelper
Inheritance
ParallelProcessingHelper
Inherited Members

Remarks

For Beginners: This class helps your AI models run faster by doing multiple calculations at the same time, similar to having multiple people working on different parts of a project simultaneously instead of one person doing everything sequentially.

Methods

ProcessTasksInParallel<T>(IEnumerable<Func<T>>, int?)

Executes multiple functions in parallel with controlled concurrency.

public static Task<List<T>> ProcessTasksInParallel<T>(IEnumerable<Func<T>> taskFunctions, int? maxDegreeOfParallelism = null)

Parameters

taskFunctions IEnumerable<Func<T>>

Collection of functions to execute in parallel.

maxDegreeOfParallelism int?

Maximum number of tasks to run simultaneously. If not specified, defaults to the number of processor cores.

Returns

Task<List<T>>

A list containing all the results from the executed functions.

Type Parameters

T

The type of result returned by each function.

Remarks

For Beginners: This method takes a collection of "work items" (functions) and runs them in parallel. It controls how many can run at once to prevent overloading your computer.

For example, if you have 100 data processing tasks but set maxDegreeOfParallelism to 4, only 4 will run at once. As each one finishes, a new one starts until all are complete.

This is useful for AI tasks like processing multiple datasets or training several models at the same time.

ProcessTasksInParallel<T>(IEnumerable<Task<T>>, int?)

Executes multiple pre-created tasks in parallel batches with controlled concurrency.

public static Task<List<T>> ProcessTasksInParallel<T>(IEnumerable<Task<T>> tasks, int? maxDegreeOfParallelism = null)

Parameters

tasks IEnumerable<Task<T>>

Collection of tasks to execute in parallel.

maxDegreeOfParallelism int?

Maximum number of tasks to run simultaneously in each batch. If not specified, defaults to the number of processor cores.

Returns

Task<List<T>>

A list containing all the results from the executed tasks.

Type Parameters

T

The type of result returned by each task.

Remarks

For Beginners: This method is similar to the one above but works with tasks that are already created. It processes these tasks in batches, with each batch containing a number of tasks equal to your processor count (or the value you specify).

Think of it like having a pile of homework assignments and deciding to complete them in batches of 4 at a time. You finish all 4 in the current batch before moving to the next batch.

This approach is useful when you already have a collection of tasks (like model training jobs) and want to process them efficiently without overwhelming your system.