Lock competition in multi-core programming

Source: Internet
Author: User

Lock competition in multi-core programming 

Related article links: Load Balancing difficulties in multi-core programming several difficulties in multi-core programming and their countermeasures (problem 1) OpenMP parallel programming (2) OpenMP parallel programming (1) fast sorting efficiency on dual-core CPU the previous article explains several difficulties in multi-core programming and their countermeasures (Challenge 1) the article mentioned that the lock competition will make serialization increase with the number of CPU cores. This article will conduct an in-depth analysis on the lock competition of multi-core programming. For the sake of simplification, Let's first look at a simple situation. Suppose there are four equivalent tasks that start and run at the same time. Assume that each task has a lock protection operation at the beginning, and the time consumed is 1, the time consumed for other parts of each task is 25. Figure 1 shows the running status of these tasks after they are started and run. The lock competition of the peer-to-peer tasks is medium. It can be seen that the 1st tasks are directly executed till the end of the competition without waiting in the middle, 2nd tasks waited for one time unit, 3rd tasks waited for two time units, and 3rd tasks waited for three time units. In this way, three CPUs are waiting for 6 time units in total. If all the tasks in OpenMP are waiting at the same point until all the tasks are executed and then run downward, the total running time will be 29 units of time in the same way as the fourth task, and the acceleration factor is: (1 + 4 × 25) /29 = 3.48 even if the average time of 4 tasks is 27.5, the acceleration coefficient is 101/27. 5 = 3.67 if the acceleration coefficient is calculated according to amerda's law, in the above application, the serial time is 1, and the total time of parallel processing is converted to 100 time units after serial processing, if it runs on a 4-core CPU, the acceleration coefficient is P/(1 + (1 + (PM) * f) = 4/(1 + (4-1) * 1/101) = 404/104 = 3.88 this creates a strange problem. After the lock is used, the acceleration coefficient is inferior to the acceleration coefficient calculated by amerda's law, let alone the acceleration coefficient calculated using the Gustafson law. In fact, we can promote the lock competition of the above four tasks to a more general situation, assuming that the serialization time with lock protection is 1, the running time of the parallelization part on single-core CPU is t, and the number of CPU cores is P. When the p pairs run simultaneously, the total waiting time caused by lock competition is: 1 + 2 +... + P = p * (PM)/2 the most time-consuming task time is: P + T/P. If the time used by the most time-consuming task is used as the parallel running time, acceleration factor is as follows S (p) = (t + 1)/(p + t/p) = p * (t + 1)/(p * p + t) (The acceleration coefficient formula under lock competition)This formula indicates that, in the case of lock competition, if the number of cores is fixed, the larger the parallelization part, the larger the acceleration coefficient. When the parallel time is fixed, the larger the number of CPU cores, the smaller the acceleration factor. Calculate several practical examples to illustrate the effect of the above formula: make T = 100, P = 4, acceleration coefficient = 4 × (100 + 1)/(4*4 + 100) = 3.48 orders T = 100, P = 16, acceleration coefficient = 16 × (100 + 1)/(16*16 + 100) = 4.54 orders T = 100, P = 64, acceleration coefficient = 64 × (100 + 1)/(64*64 + 100) = 1.54 T = 100, P = 128, acceleration coefficient = 128 × (100 + 1) /(128*128 + 100) = 0.78 from the above calculation, we can see that when the number of cores reaches a certain value, the acceleration coefficient not only decreases but also decreases, and the number of cores increases to 128, the acceleration factor is only 0.78, not as fast as running on a single-core CPU. In the above example, the serial code caused by lock protection is called when the task is started. In fact, the serial code of lock protection called elsewhere in the Peer task is also the same. The lock competition of peer-to-peer tasks is very common in actual situations. For example, for server software, each client usually processes tasks in a peering manner. If a lock is used in this case, it is easy to cause the above-mentioned acceleration coefficient to decrease with the increase of the number of CPU cores. Previously, server software was generally run on Dual-CPU or quad-CPU machines, so the lock competition caused a low acceleration factor. after entering the multi-core era, as the number of CPU cores increases, this problem will become very serious, so the multi-core era poses new challenges to program design. The previous multi-task Programming thought may not work on multi-core programming. Therefore, it is impractical to simply think that multi-core programming is equivalent to the previous multi-task programming or parallel computing. In the article about the difficulties of serialization, some countermeasures are proposed, however, those countermeasures will not be available until the industry continues to work hard. Of course, because the multi-core CPUs currently on the market are still dual-core and quad-core, it may take several years to wait until the CPU with more than 16 cores enters the market, we believe that the industry will be able to find a better solution to the lock competition issues in the above peer-to-peer tasks in the next few years.   Introduction: Zhou weiming is a freelancer and has been engaged in the software industry for more than 10 years. At present, we focus mainly on software testing, multi-core programming, software design, and other basic aspects. I have written the book "data structures and algorithms under multi-tasking". I am writing the book "software testing practices" and plan to write a multi-core programming book in the near future. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.