您现在的位置是:首页 >技术杂谈 >Task Parallel Library in C#网站首页技术杂谈
Task Parallel Library in C#
文章目录
Task Parallel Library in C# with Examples (TPL in C#)
In this article, I am going to give you an overview of Parallel Programming and Task Parallel Library in C# with Examples. Please read our previous section articles where we discussed Asynchronous Programming in C#. The Task Parallel Library is also referred to as TPL in C#. At the end of this article, you will understand What Task Parallel Library is and why we need it in C# applications.
在本文中,我将通过示例向您概述c#中的并行编程和任务并行库。请阅读我们之前讨论c#异步编程的部分文章。任务并行库在c#中也被称为TPL。在本文的最后,您将了解什么是任务并行库,以及为什么在c#应用程序中需要它。
Introduction to Parallelism
It is time for us to start talking about parallelism. With parallelism, we can use our processor to perform several actions simultaneously. So, with parallelism, we have the opportunity to improve the speed of certain processes of our programs.
现在是时候开始讨论并行性了。通过并行,我们可以使用处理器同时执行多个操作。因此,通过并行,我们有机会提高程序某些进程的速度。
We will start this module by talking about what parallelism is. Later, we will see different tools for parallelization like Parallel.For, Parallel.Foreach and Parallel.Inoke. We will also talk about when not to use parallelism. We will also see concepts such as Atomic Methods, Thread safety, and race conditions. Then we will see mechanisms to integrate race conditions such as interlocked and locks. Finally, we’ll talk about PLINQ (Parallel LINQ)
我们将从讨论并行是什么开始本模块。稍后,我们将看到用于并行化的不同工具,如Parallel。因为,平行。Foreach和Parallel.Inoke。我们还将讨论何时不使用并行。我们还将了解原子方法、线程安全性和竞争条件等概念。然后,我们将看到集成竞争条件的机制,如互锁和锁。最后,我们将讨论PLINQ(并行LINQ)
What is Parallel Programming in C#?
Parallel Programming in C# helps us divide a task into different parts and work those parts simultaneously. An example might be that we have a set of credit cards and we want to process them simultaneously. Or if we have a set of images and we want to apply a series of filters to each one, we can do this by taking the advantage of parallelism.
c#中的并行编程帮助我们将任务划分为不同的部分,并同时处理这些部分。例如,我们有一组信用卡,我们希望同时处理它们。或者,如果我们有一组图像,我们想对每一张图像应用一系列过滤器,我们可以利用并行性来做到这一点。
The main benefit of parallelism is saving time. Time is saved by maximizing the use of computer resources. The idea is that if the computer allows the use of multi-threading, we can use these threads when we have a task to solve. So, instead of underusing our processor using a single thread, we can use as many threads as we can to speed up the processing of the task.
并行计算的主要优势是节约时间。这是通过最大化计算机资源的使用来实现的。其基本思想是,如果计算机支持多线程,当我们有一个需要解决的任务时,我们可以利用这些线程。因此,我们可以使用尽可能多的线程来加速任务的处理,而不是仅使用单个线程来浪费处理器的性能。
In general, an exception to the benefits of using parallelism using ASP.NET and ASP.NET Core since those scenarios are already parallelized. That is because every thread serves an HTTP request. And therefore, if you have one HTTP request that occupies several threads, then the Web server will have fewer resources to serve other HTTP requests.
一般来说,在使用ASP.NET和ASP.NET Core时,并行计算的优势存在一些例外,因为这些场景已经并行化了。这是因为每个线程都为一个HTTP请求提供服务。因此,如果您有一个占用多个线程的HTTP请求,则Web服务器将没有足够的资源来为其他HTTP请求提供服务。
In general, we use parallelism in CPU-bound operations, CPU bound operations are those operations whose resolution depends on the processor, not on services external to the application. Doing an arithmetic operation will be an example of a CPU-bound operation. Taking a set of images and applying filters and transformations through them will be another CPU-bound operation.
通常情况下,我们在 CPU-bound 操作中使用并行计算。CPU-bound 操作是指其处理时间取决于处理器,而不是取决于应用程序外部的服务。进行算术运算是 CPU-bound 操作的一个例子。将一组图像并对它们应用滤镜和变换也是 CPU-bound 操作的另一个例子。
It is these types of operations, those that are CPU bound, which are candidates for us to use parallelism. It is important to know that parallelism is not always beneficial in terms of performance, since using parallelism has a cost, so we must always make measurements to prove that the cost of parallelism is not greater than not using it. That is sometimes when we use parallelism, the result is a slower application. One reason for this is that when we want to parallelize is very small or doesn’t require enough work for justifying the use of parallelism.
The benefit of parallelism depends on the amount of work to be parallelized. So, Parallel Programming in C# is very important for systems that must process a huge amount of data. For example, on Facebook, approximately two hundred and fifty thousand photos are uploaded per minute. As you can imagine, it takes a lot of power to process such a high volume of information. However, the processors are not getting much faster because of the physical limitations. What is being done then mainly is to include more cores in the processors. In this way, we can take advantage of parallelism to accomplish more tasks in less time.
It is not recommended to occupy several threads for one HTTP request. If you have a long task to do, then it is recommended to use background services or some server technology.
Finally, parallelism shows benefits, if the computer where the program is running has parallelism capabilities. If you try to use parallelism on a computer that cannot use parallelism, then the code will run in sequential. That is, it will not throw any errors, but it will not be faster either. So, the objective is that not all computers can perform parallelism.
Modern processors are typically multicore. In windows, you can see if your computer is multicore by going to task manager, then selecting the performance tab, and having look at the CPU. Here, you can look at the number of available cores and also have a look at the logical processors. In my case, I have 4 cores and 8 logical processors as shown in the below image. This means that each core can do two operations simultaneously.
并行的主要好处是节省时间。通过最大限度地利用计算机资源来节省时间。思想是,如果计算机允许使用多线程,我们可以在解决任务时使用这些线程。因此,我们可以使用尽可能多的线程来加速任务处理,而不是使用单个线程浪费我们的处理器。
一般来说,在使用ASP.NET和ASP.NET Core时,并行使用的好处是一个例外,因为这些情况已经被并行化了。这是因为每个线程都为一个HTTP请求提供服务。因此,如果您有一个HTTP请求占用了多个线程,那么Web服务器将有更少的资源来服务其他HTTP请求。
一般来说,我们在CPU绑定操作中使用并行,CPU绑定操作是那些其解决方案取决于处理器而不是应用程序外部服务的操作。进行算术操作将是CPU绑定操作的示例。获取一组图像并对其应用过滤器和变换将是另一个CPU绑定操作。
这些类型的操作,即CPU绑定操作,是我们使用并行的候选操作。重要的是要知道,并行不总是有利于性能,因为使用并行是有成本的,因此我们必须始终进行测量来证明使用并行的成本不比不使用它更大。有时候,当我们使用并行时,结果是一个更慢的应用程序。一个原因是我们要并行的任务非常小或者不需要足够的工作来证明使用并行的必要性。
并行的好处取决于要并行化的工作量。因此,C#中的并行编程对于必须处理大量数据的系统非常重要。例如,在Facebook上,每分钟大约上传两十五万张照片。正如你所想象的那样,处理如此高的信息量需要大量的计算能力。但是,由于物理限制,处理器并没有变得更快。主要的做法是在处理器中增加更多的核心。通过这种方式,我们可以利用并行来在较短的时间内完成更多的任务。
不建议为一个HTTP请求占用多个线程。如果您有一个长时间的任务要完成,则建议使用后台服务或一些服务器技术。
最后,如果运行程序的计算机具有并行性能,则并行显示出好处。如果您尝试在不能使用并行性的计算机上使用并行性,则代码将按顺序运行。也就是说,它不会抛出任何错误,但也不会更快。因此,目标是并非所有计算机都可以执行并行操作。
现代处理器通常是多核心的。在 Windows 操作系统中,您可以通过打开任务管理器,然后选择性能选项卡来查看您的计算机是否是多核心的。在此处,您可以查看可用的核心数,以及逻辑处理器数。在我的电脑上,我有4个核心和8个逻辑处理器,这意味着每个核心可以同时执行两个操作。
In C#, we mainly use two tools to work with parallelism. They are as follows:
The Task Parallel Library (TPL)
Parallel LINQ (PLINQ)
The Task Parallel Library is a library that makes life easier for us. When we see parallelism in our programs that TPL (Task Parallel Library) abstracts the low-level detail of thread handling, allowing us to run programs that run parallel without having to work with these threads manually.
On the other hand, PLINQ or Parallel LINQ is an implementation of LINQ that allows us to work in parallel. For example, in LINQ, we can filter the elements of an array. Then with Parallel LINQ, we can filter the same array in parallel. This allows us to use the cores of our processor to perform the evaluations of the elements of the array simultaneously.
Why do we need Task Parallel Library in C#?
We can’t expect our sequential program to run faster on the new processors as we know the processor technology advances means the focus is on Multicore-processors. Today’s desktop typically has 4 cores but the latest experimental multi-core chips have up to 1000 cores.
So in simple words, we can say that the multicore processor machines are now becoming standard and the aim is to improve the performance by running a program on multiple processors in parallel. So by considering the above scenario, the .NET Framework 4 introduces Task Parallel Library (TPL) that makes it easier for developers to write parallel programs that target multi-core machines (automatically use multiple processors) which improves the performance.
Using the Task Parallel Library (TPL), we can express the parallelism in the existing sequential code, which means we can express the code as a Parallel task, which will be run concurrently on all the available processors.
在 C# 中,我们主要使用两个工具来处理并行性,它们是:
任务并行库(TPL)
并行 LINQ(PLINQ)
任务并行库是一个库,它为我们的工作提供了便利。当我们在程序中看到并行性时,TPL(任务并行库)抽象了线程处理的底层细节,允许我们在不必手动处理这些线程的情况下运行并行程序。
另一方面,PLINQ 或并行 LINQ 是 LINQ 的一种实现,它允许我们并行工作。例如,在 LINQ 中,我们可以过滤数组的元素。然后,在 Parallel LINQ 中,我们可以并行过滤同一个数组。这使我们能够使用处理器的核心来同时执行数组元素的评估。
为什么我们需要 C# 中的任务并行库?
我们不能指望我们的顺序程序在新的处理器上运行得更快,因为处理器技术的进步意味着重点在于多核处理器。今天的桌面通常有 4 个核心,但最新的实验性多核芯片具有高达 1000 个核心。
因此,简单来说,我们可以说多核处理器机器现在正在成为标准,目标是通过在多个处理器上并行运行程序来提高性能。因此,.NET Framework 4 引入了任务并行库(TPL),使开发人员能够编写针对多核机器(自动使用多个处理器)的并行程序,从而提高性能。
使用任务并行库(TPL),我们可以在现有的顺序代码中表达并行性,这意味着我们可以将代码表示为并行任务,该任务将在所有可用处理器上同时运行。
What is Parallel Programming in C#?
Parallel Programming in C# is a type of programming in which many calculations or the execution of processes are carried out simultaneously. The Points to Remember while working with Parallel Programming:
The Tasks must be independent.
The order of the execution does not matter
C# Supports Two Types of Parallelism:
Data Parallelism: In Data Parallelism, we have a collection of values and we want to use the same operation on each of the elements in the collection. The examples will be to filter the elements of an array in parallel or find the inverse of each matrix in a collection. This means each process does the same work on unique and independent pieces of data.
什么是C#中的并行编程?
C#中的并行编程是指同时进行多个计算或进程执行的一种编程类型。在处理并行编程时需要注意以下几点:
任务必须相互独立。
执行顺序无关紧要。
C#支持两种并行方式:
数据并行:在数据并行中,我们有一个值的集合,并且我们希望对集合中的每个元素使用相同的操作。例如,可以并行地过滤数组的元素或在集合中找到每个矩阵的逆。这意味着每个进程在独立的数据片上执行相同的任务。