Introduction to multi-core CPU, multi-thread and parallel computing

Source: Internet
Author: User

Introduction to multi-core CPU, multi-thread and parallel computing

0. Preface

I have used MPI and C # thread pools, participated in competitions, and had some feelings. For nearly a year, I have been interested in multi-thread programming and have been paying attention to it. I decided to write an article, this is a summary of the knowledge. If something is wrong, you are welcome to correct me :)

1. CPU Development Trend

The number of cores is still increasing. According to Moore's Law, the performance improvement of a single core has serious bottlenecks, ordinary desktop PCs are expected to reach 24 cores (or 16 cores and 32 threads) by the end of 2017 and the beginning of 2018. How can we deal with this sudden increase in the number of cores? Programming must keep pace with the times. The author boldly predicts that the chip bus between the core of the CPU will be connected with four groups :), because the full connection is too complicated and the single bus is not powerful enough. It should also be a non-symmetric multi-core processor, which may contain several DSP processors or stream processors.

 

2. Differences between multithreading and parallel computing

(1) multithreading is not only used for parallel computing, but also has many beneficial functions.

In the single-core era, multithreading is widely used. At this time, multithreading is mostly used to reduce blocking (meaning similar

While (1)

{

If (flag = 1)

Break;

Sleep (1 );

}

This Code) will bring about idle CPU resources, note that there is no waste of CPU resources here, remove sleep (1) is pure waste.

When Will blocking happen? It generally waits for IO operations (disks, databases, networks, and so on ). At this time, if a single thread is used, the CPU will do nothing (nothing that has nothing to do with this program, because it doesn't make sense for me to execute other programs ), low Efficiency (for this program). For example, if an I/O operation takes 10 milliseconds, the CPU will be blocked for nearly 10 milliseconds. How a waste is it! You need to know that the CPU is several nanoseconds.

Therefore, this time-consuming IO operation is executed on behalf of a Thread. The function (CODE) that creates this Thread will not be blocked by the IO operation and will continue to do other things in this program, instead of waiting (or executing other programs ).

In this single-core era, the multi-thread blocking can also be called "concurrency", which is essentially different from parallelism. Concurrency is "pseudo-parallel", which seems to be parallel. In fact, a CPU is still executing everything, But switching is too fast for us to notice. For example, a UI-based program (as the saying goes), if you need to execute an event triggered by clicking a button for 10 seconds, the program will be suspended because the program is busy executing, there is no time to handle other user operations. If you assign the function triggered by this button to a thread and start the thread for execution, the program will not be suspended and continue other operations of the corresponding user. However, the thread mutex, synchronization, and deadlock issues Follow. For details, see the relevant literature.

Now is the multi-core era. The problem of thread mutex and synchronization is even more serious. In the single-core era, the concurrency is mostly calculated, and the multi-core era is actually quite different. Why? For more information, see references. In the past, the use of volatile variables can solve most problems. For example, multiple threads can access a Flag. If it is a single-core concurrency, there will be no problems (P.S. under what circumstances will the problem occur? There are multiple flags, or an array. At this time, this problem can only be solved by logical means. It doesn't matter if there are more than a few idling attempts. Don't have a fatal problem.) because there is only one CPU, at the same time, only one thread can access this flag, which is not the same in multi-core scenarios. Therefore, volatile alone cannot solve the problem. This requires a specific language, in the specific environment, the "semaphores", Mutex, Monitor, Lock, and so on, all of these classes operate on the "off-the-shelf" on the hardware to achieve the "primitive" effect, the effect of Access to the critical section is not explained in detail. You can refer to modern operating system.

(2) parallel computing can also be achieved through other means, while multithreading is only one of them.

Other means include: multi-process (this includes shared storage areas and distributed multi-host, and hybrid), command-level parallel.

ILP (command-level parallel execution) is called SMT (multiple threads at the same time) in x86 architecture. What corresponds to this in MIPS architecture is super scalar (excessive amount) and out-of-order execution, which are different from each other, however, in common, commands can be run concurrently. This is beyond the control of users and is not within the programming scope. Only limited optimizations can be made. These limited optimizations may only fall under the jurisdiction of the compiler, few users can do.

(3) typical languages suitable for Parallel Computing

Erlang and MPI: the two are languages, while the latter are C ++ and Fortran extension libraries. The results are the same. multi-process parallel computing is used, erlang is a shared storage area and MPI is a hybrid type.

C #. NET4.0: the new version 4.0 can use a small amount of code to implement a parallel For loop. Earlier versions can achieve the same function only with tedious code. This uses multiple threads for parallel computing. Both Java and C #3.5 have ThreadPool, which is also a good and easy-to-use multi-thread management class and can be used conveniently and efficiently.

CUDA, a newborn calf, has great development potential, but its application field is very limited at present. Currently, it can only use C language, and is not C99. It is relatively low-level and cannot use function pointers. I personally feel that this is due to the inherent hardware limitations (the average memory available for each core is small, and the communication time with the system memory is long). It is only applicable to scientific computing, static image processing, video encoding and decoding, other fields are not as good as high-end CPUs. After the GPU has an operating system, you can fully schedule GPU resources. In fact, multi-core CPU can also achieve good physical acceleration in the game.

Other languages... Well .. For future discussions.

 

3. Is there more threads, the better? When is multithreading necessary?

The more threads, the better. Thread switching also requires overhead. When you add a thread, the additional overhead is smaller than the blocking time that can be eliminated by the thread, this is a value for money.

Since Linux kernel 2.6, different threads are handed over to different cores for processing. Windows also supports this feature from NT.4.0.

When should I use multithreading? This can be discussed in four situations:

A. Multi-core CPU-computing-intensive tasks. We recommend that you use multiple threads to improve task execution efficiency, such as encryption and decryption, data compression and decompression (video, audio, and common data). Otherwise, only one core is fully loaded, while other cores are idle.

B. Single-core CPU-computing-intensive tasks. At this time, the task has consumed 100% of the CPU resources, so there is no need or no way to use multithreading to improve the computing efficiency. On the contrary, it is best to use multithreading for human-computer interaction, this prevents the user from being unable to operate the computer.

C. Single-core CPU--IO intensive tasks, the use of multithreading or for the convenience of human-computer interaction,

D. Multi-core CPU--IO-intensive tasks, not to mention, the same reason for documentary nuclear.

4. Skills/technologies that programmers need to master

(1) Reduce serial code to improve efficiency. This is nonsense.

(2) distribution of a single shared data: Copy multiple copies of one data so that different threads can access the data at the same time.

(3) Load Balancing: static and dynamic. For more information, see related documents.

This article permanently updates the link address:

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.