当前位置:网站首页>How to use parallel programming to improve task execution efficiency
How to use parallel programming to improve task execution efficiency
2022-07-21 00:43:00 【biyusr】
《.NET Concurrency becomes reality 》 Journal entry : Parallel programming Parallel
Manual catalogue :
One 、 Preface
Two 、 Task parallel library (TPL) Introduction to
3、 ... and 、Parallel.Invoke Use
Four 、Parallel.For Use
5、 ... and 、Parallel.ForEach+Partitioner Use
6、 ... and 、 Specify the maximum parallelism MaxDegreeOfParallelism
7、 ... and 、 Exit the loop and catch exceptions
8、 ... and 、 References
One 、 Preface
background : In the Internet of things scenario , Due to the large data throughput , The conventional Task Asynchronous execution has obvious performance bottlenecks , Then by reference Riccardo Terrel( Ricardo Dian· Terrell ) Writing , Translated by teacher Ye Weimin 《.NET Concurrent programming practice 》, Used Parallel Parallel programming , as well as Comparator Partitioner, Combine the two It improves the speed of device data binding and data update , Also achieved right CPU The performance of is quite extreme .
Mengxin record , The boss should be more upright !
Skip the concept , Arrive directly at the use case —> 5、 ... and 、Parallel.ForEach+Partitioner Use
l The principle of parallel programming
stay 《.net Concurrent programming practice 》( Hereinafter referred to as 《 actual combat 》) Parallel programming is explained in this way —— Perform multiple tasks at the same time .
From a developer's perspective , When we consider these problems ,“ Can my program perform multiple operations at the same time ?” or “ How can my program solve a problem faster ” We'll think of parallel . Parallelism refers to the simultaneous execution of multiple tasks on different kernels , To speed up the application , This requires hardware support ( Multicore ), And parallelism can only be implemented in multi-core devices , It is a means to improve program performance and throughput .
l There is a simple distinction between parallel and concurrent programming
1、 Concurrent programming handles multiple operations at once , No hardware support required ( Use one or more cores ).
2、 Parallel programming in multiple CPU Or performing multiple operations simultaneously on multiple cores . All parallel programs are concurrent , Simultaneous running , But not all concurrency is parallel . The reason is that parallelism can only be implemented on multi-core devices .
3、 Multitasking executes multiple threads from different processes at the same time . Multitasking does not necessarily mean parallel execution , Only when using multiple CPU Or multiple cores to achieve parallel execution .
l Why do we need to use parallel programming
《 actual combat 》 For different programs CPU A comparison is made between the use of resources :
《 actual combat 》 I think , Running an application on a multi-core computer without considering concurrency , Is a waste of computer productivity , Because the application can only use part of the available computing power in the process of sequential processing , In this case, any CPU The performance counter will find that only one kernel is running fast , May be 100%, Other kernels are underutilized or idle , In the picture above 8 Kernel computer , Running non parallel programs means that the overall utilization of resources may be less than 15%.
l There are two ways to use parallel programming
1、 Task parallel library (TPL), This is the only way used in this article
2、 parallel LINQ(PLINQ)—》 Official documents go directly to :https://docs.microsoft.com/zh-cn/dotnet/standard/parallel-programming/introduction-to-plinq
Two 、 Task parallel library (TPL) Parallel of Introduce
.Net Framework4 New Task Parallel Library( Task parallel library ,TPL), It supports data parallelism 、 Task parallelism and pipeline .
When parallel loops run ,TPL The data source will be partitioned according to the built-in partition algorithm ( Or you can customize a partition algorithm ) Divide the data into disjoint subsets , then , Select threads from the thread pool to process these data subsets in parallel , Each thread is responsible for processing only one subset of data . Backstage , Task scheduler will partition tasks according to system resources and workload . If possible , The scheduler redistributes work between multiple threads and processors when the workload becomes unbalanced .
On any code ( Including cycle ) When parallelizing , An important goal is to use as many processors as possible , But don't over parallelize to the extent that the overhead of row processing consumes any performance advantages . such as : For nested loops , Only external loops will be parallelized , The reason is that there is not much work to be done in the internal loop . The combination of a small amount of work and poor cache impact may cause the performance of nested parallel loops to degrade .
Because the loop body runs in parallel , The partition of the iteration range is based on the number of logical cores available 、 Partition size and other factors change dynamically , Therefore, the order of iteration execution cannot be guaranteed .
TPL Introduced System.Threading.Tasks , The main class is Task, This class represents an asynchronous concurrent operation , However, we do not have to use Task Class , have access to Parallel Static class . It provides Parallel.Invoke, Parallel.For,Parallel.Forecah Three methods , Here are the introduction 3 A simple example of a method , Each method has multiple overloads , You can check the source code by yourself
3、 ... and 、Parallel.Invoke Use
static void Main()
{
try
{
Parallel.Invoke(
BasicAction,// Param #0 - Static methods
() =>// Param #1 - lambda expression
{
Console.WriteLine(" The cook cooks , Thread={0}", Thread.CurrentThread.ManagedThreadId);
},
delegate ()// Param #2 - entrust
{
Console.WriteLine(" In the delegation method , Thread={0}", Thread.CurrentThread.ManagedThreadId);
}
);
}
// In this case, an exception is not expected , But if an exception is still thrown in the task ,
// It will be packed in AggregateException in , And propagate to the main thread .
catch (AggregateException e)
{
Console.WriteLine(" The catching \n{0}", e.InnerException.ToString());
}
}
static void BasicAction()
{
Console.WriteLine(" Migrant workers work , Thread={0}", Thread.CurrentThread.ManagedThreadId);
}
annotation :
l This method can be used to perform a set of operations that may be performed in parallel .
l The sequence of operations is not guaranteed , Or whether to perform operations in parallel .
l This method will not return until each provided operation has been completed , Whether it is due to normal termination or abnormal termination .
Four 、Parallel.For Use
Let's start with a simple insert , To compare parallel for Loop and serial for Speed of cycle .
Here because Parallel.For There is also a performance consumption when allocating tasks to processors , The speed increase is not obvious .
Let's take a look Parallel.For One of the overloads of
var list = new List<int>() { 10, 20, 30, 40 };
var options = new ParallelOptions();
var total = 0;
var result = Parallel.For(0, list.Count, () =>
{
Console.WriteLine("------------ thead --------------");
return 1;
},
(i, loop, j) =>
{
Console.WriteLine("------------ body --------------");
Console.WriteLine("i=" + list[i] + " j=" + j);
return list[i];
},
(b) =>
{
Console.WriteLine("------------ tfoot --------------");
Interlocked.Add(ref total, b);
Console.WriteLine("total=" + total);
});
Console.WriteLine("iscompleted:" + result.IsCompleted);
Console.Read();
annotation :
l Because the execution sequence is not guaranteed in parallel tasks , And multiple tasks may try to update at the same time total Variable , So it's used here Interlocked.Add perform , To ensure that it is performed as an atomic operation .
5、 ... and 、Parallel.ForEach+Partitioner Of combination Use
Partitioner Comparator :
First, let's look at the source code of the partition , See how it partitions the data source :
Partitioner.Create If only the starting and ending index position of the specified data source , Partition creation is mainly based on the number of logical cores (PlatformHelper.ProcessorCount) Decisive .
In most cases ,TPL The load balancing mechanisms used behind the scenes are very efficient , For example, we don't use partitions , Directly load balance the data source in parallel , Please see the case —> 6、 ... and 、 Specify the maximum parallelism .
Of course, we can also customize the partition size , Now let's enter the actual development environment , The current experimental computer is 6 nucleus 12 Thread processor
annotation :
dataList —> Data source of real-time data
Index —> Total number of data sources , It is assumed here that 1W Data
rangesize—> Block size , From this we can calculate 10000/12+1=834(+1 In order to adapt to the situation that may not be eliminated )
Partitioner.Create(0,Index,rangesize) —> The partitioner puts the data source 0-1W Pieces of data are divided into 12 Data blocks , Each piece is 834 strip , Of course, the last piece is not 834 Data
Hit the breakpoint and you can see range.Item2-range.Item1=834, The blocks have been divided , Then there is the parallel processing of business code .
An example is posted here , Paste available :
int index = 10000;
var rangesize = (int)(index / Environment.ProcessorCount) + 1;
var rangePartitioner = Partitioner.Create(1, index, rangesize);
System.Threading.Tasks.Parallel.ForEach(rangePartitioner, range =>
{
#region Business code
#endregion
});
6、 ... and 、 Specify the maximum parallelism MaxDegreeOfParallelism
Refer to blog post :Parallel.ForEach And MaxDegreeOfParallelism
https://www.cnblogs.com/QinQouShui/p/12134232.html
System.Threading.Tasks.Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 12 }, range =>
{
#region Business code
#endregion
});
this Parallel.ForEach No partition is used , It's about using TPL Parallel for load balancing .
The source code of this overload is :
7、 ... and 、 Exit the loop and catch exceptions
And serial operation break Different ,ParallelLoopState Two methods are provided to stop Parallel.For and Parallel.ForEach Implementation .
public class ParallelLoopState
{
// Gets whether any iteration of the loop has thrown an unhandled exception for the corresponding iteration .
public bool IsExceptional { get; }
// Gets whether any iteration of the loop has been called ParallelLoopState.Stop().
public bool IsStopped { get; }
// To get in Parallel Cyclic invocation ParallelLoopState.Break() The lowest loop iteration of .
public long? LowestBreakIteration { get; }
// Gets whether the current iteration of the loop should exit based on the request made by this iteration or other iterations .
public bool ShouldExitCurrentIteration { get; }
// notice Parallel Loop the current iteration ” after ” Other iterations of don't need to run .
public void Break();
// notice Parallel Loop the current iteration “ outside ” All other iterations of don't need to run .
public void Stop();
}
l Break: Used to inform Parallel Loop the current iteration “ after ” Other iterations of don't need to run . for example , From 0 To 1000 Parallel iterative for loop , If in the first place 100 Iteration call Break(), Less than 100 All iterations of will still run ( Even if you haven't started processing ), And finish processing before exiting the loop . from 101 To 1000 Iterations that have not been started in will be abandoned . For long-running iterations that are already executing ,Break() It will correspond to the iteration that has been run but not finished ParallelLoopResult Structural LowestBreakIteration Property is set to call Bread() Index of iteration items .
l Stop:Stop() Used to inform Parallel Loop the current iteration “ outside ” All other iterations of don't need to run , Whether they are above or below the current iteration . For long-running iterations that are already executing , You can check IsStopped attribute , It is observed that true Exit ahead of time .Stop Usually used in search based algorithms , After finding a result, there is no need to perform any other iterations .( For example, when watching videos or comics, it automatically matches the server with the fastest response )
var loopresult = System.Threading.Tasks.Parallel.ForEach(rangePartitioner, range =>
{
#region Business code
loopState.Stop();
#endregion
});
When the delegate called in the parallel iteration throws an exception , This exception is not caught in the delegate when , It will become a group of exceptions , new System.AggregateException Be responsible for handling this group of exceptions .
try
{
System.Threading.Tasks.Parallel.ForEach(rangePartitioner, range =>
{
#region Business code
#endregion
});
}
Catch(AggregateException ex)
{
foreach (var innerEx in ex.InnerExceptions)
{
Console.WriteLine(innerEx.ToString());
}
}
8、 ... and 、 References
l 《.net Concurrent programming practice 》
l Official documents 《.NET Parallel programming in 》https://docs.microsoft.com/zh-cn/dotnet/standard/parallel-programming/
l Blog Garden 《.Net Advanced tutorial of parallel programming --Parallel》https://www.cnblogs.com/stoneniqiu/p/4857021.html
l Blog Garden 《8 Days play concurrent 》
https://www.cnblogs.com/huangxincheng/category/368987.html
l 《 Asynchronous programming :.NET4.X Data parallelism 》
https://www.cnblogs.com/heyuquan/archive/2013/03/13/parallel-for-foreach-invoke.html
l Blog Garden 《Parallel.ForEach And MaxDegreeOfParallelism》
https://www.cnblogs.com/QinQouShui/p/12134232.html
end
边栏推荐
- Matlab tutorial_ Summary of guoyanfu's notes at National Taiwan University (with video materials attached)
- 10、gin快速入门
- Chapter 8: monkey climbing N-level hierarchical recursion, finding the Zeng sequence of the simplest true fraction of the denominator [a, b], Fibonacci sequence recursion solution, Fibonacci sequence
- DBeaver的操作日志
- 央视新闻《南京开餐饮手撕定额发票》新闻频道_人民网
- 央视新闻《重庆开餐饮手撕定额发票》新闻频道_人民网
- 央视新闻《上海开住宿手撕定额发票》新闻频道_人民网
- What is the reason why the easycvr video Plaza device list cannot be scrolled and loaded?
- 央视新闻《成都开餐饮手撕定额发票》新闻频道_人民网
- 全局事件总线概述
猜你喜欢
开发者必读:2022年移动应用运营增长洞察白皮书
Developers must read: 2022 mobile application operation growth insight white paper
Operation log of dbeaver
Apipost :一款值得使用的利器
"Collection of Architects"
[try to hack] SQL injection less7 (into outfile and Boolean blind annotation)
解锁高评分 | eBay 深耕用户体验,优化大屏幕设备应用
Cve-2014-6271 "broken shell" vulnerability
10. Gin quick start
软件测试知识库+1,5款顶级自动化测试工具推荐和使用分析
随机推荐
Internet of things communication protocols: mqtt, COAP, nb-iot, RFID, Bluetooth, NFC
Codeworks 5 questions per day (average 1500) - day 20
Overwintering samples of game companies: has going to sea and boutique become a new growth point?
Create the future and enjoy extraordinary. Opengauss Developer Day 2022 was successfully held
Win11暂存文件夹是什么?Win11在线升级暂存文件夹在哪
Changjiang Dayong, director of opengauss community: opengauss cooperates with industry innovation to build an open source database root community
NET问答: C# 中是否有最高效的方式对大文件做 checksum ?
How to get asp Net core current startup address?
Developers must read: 2022 mobile application operation growth insight white paper
Learun, open source, one Net web quick open
CCTV news "Chengdu opens catering quota invoice by hand" news channel_ People's network
Weex demining 1:data this
Multi device data acquisition of cpolar application examples
如何运用并行编程Parallel提升任务执行效率
Web性能测试需求分析,具体应该怎么做?
央视新闻《上海开住宿手撕定额发票》新闻频道_人民网
Typeof and keyof
Re understand the life world and ourselves
10. Gin quick start
Cpolar application example helps shipping customers Telecommuting