Three minute overview of Microsoft task parallel library TPL

introduction

As the saying goes, a programmer who doesn't want to fly a plane is not a good father; As an old bird of Microsoft technology stack, he has always regarded the way of code cleanliness as a classic. Excellent programmers regard elegant and high-performance code as their face.

Today, let's talk about my right NET parallel programming library, the understanding of Task Parallel Library, is at full power and ready to squeeze the CPU.

The truth of dual core cpu gif

Technical background

Hardware thread and software thread

Multi core processor has more than one physical core: the physical core is a real independent processing unit, and multiple physical cores enable multiple instructions to run in parallel at the same time.

Hardware thread is also called logical kernel. A physical kernel may use hyper threading technology to provide multiple hardware threads, so a hardware thread does not represent a physical kernel. The program passes environment What processorcount gets is the logical kernel (my machine is i5-5300U virtual 4-core). Each running program in Windows is a process, and each process will create and run one or more threads. These threads are called software threads. Hardware threads are like a swimming lane, and software threads are the people swimming in them.

Parallel scene

. The task parallel library (TPL) introduced by. NET dynamically expands the concurrency and uses all available processors in the most effective way.

In addition, TPL supports partition work, ThreadPool based scheduling, asynchronous operation cancellation, and state management.

Through TPL, focus and let the program complete your business tasks, while maximizing program performance.

TPL supports data parallelism, task parallelism and pipelined Dataflow at the same time

1. Data parallelism: there is a large amount of data to be processed, and the same operation must be performed on each data; 2. Task parallelism: run different operations concurrently through tasks; 3. Pipeline: a combination of task parallelism and data parallelism (System.Threading.Tasks.Dataflow component library needs to be introduced)
Of which 1 and 3 are already in Above After the demonstration, this article will talk about data parallelism and task parallelism.

Programming practice

1. Data parallelism

Find the number of prime numbers within 100000

Above [shared memory concurrency model], the code can be optimized as follows:

Each thread independently calculates the prime sum generated by the iteration within the thread, and finally sums several sums.

using System;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;

/// <summary>
///The number of 100000 prime numbers is calculated by using Parallel programming library
/// </summary>
namespace Paralleler
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            ShareMemory();
            sw.Stop();
            Console.WriteLine($"The optimized shared memory concurrency model takes time:{sw.Elapsed}");
        }

        static void ShareMemory()
        {
            var sum = 0;
            Parallel.For(1, 100000 + 1, () => 0, (x, state, local) =>
            {
                var f = true;
                if (x == 1)
                    f = false;
                for (int i = 2; i <= x / 2; i++)
                {
                    if (x % i == 0)  //If it is divided by any number of [2,x/2], it is not a prime number
                        f = false;
                }
                if (f == true)
                    local++;
                return local;
            },
                 local =>
                 {
                     Interlocked.Add(ref sum, local);
                 }
               );
            Console.WriteLine($"1-100000 The number of endoplasmic numbers is{sum}");
        }
    }
}
Parameter 1,2 Represents the object to be operated by data parallel;
Parameter 3 localInit Indicates the initial value of the iteration within a thread, which will be used as parameter 4 body The third parameter of the delegate is only used for the first time in the thread;

Parameter 4 body Represents the execution body that each iteration needs to go through, Here, threads are used as the unit to process iterations;

Parameter 5localFinally calculates the output of each thread again. The input parameter is the output of parameter 4.

2. Parallel tasks

The simplest way to run many methods in Parallel is to use the Invoke method of the Parallel class, which accepts an Action parameter group

void  System.Threading.Tasks.Parallel.Invoke(WatchMovie, HaveDinner, ReadBook, WriteBlog);

This code creates a delegate to each method.

There is no specific execution order

Parallel. The invoke method will not return until all four methods are completed. It requires at least four hardware threads to run these four methods concurrently.


However, it is not guaranteed that the four methods can be started and run at the same time. If one or more kernels are busy, the underlying scheduling logic may delay the initialization and execution of some methods.

Catch exceptions that occur in parallel loops

When a delegate in a parallel iteration throws an exception and this exception is not captured in the delegate, it becomes a set of exceptions, the new System.. Aggregateexception is responsible for handling this set of exceptions.

Keywords: ASP.NET

Added by sfc12345 on Fri, 31 Dec 2021 06:00:29 +0200