[Turn] Talking about the multi-core CPU, multi-threaded, multi-process Talking about the multi-core CPU, multi-threaded, multi-process

On multi-core CPU, multi-threaded, multi-process

1.CPU trends

The core number is still more and more, according to Moore's Law, due to the single core performance have serious bottlenecks, ordinary desktop PC is expected to reach 24 core (16-core or 32 threads) in late 2017 early 2018, how do we face this sudden increase in the number of core? Programming also with the times. I venture to predict chip bus between the CPU core of each will be used :) 4 way set associative, because the whole connected too complex, but not enough to force a single bus. And it should asymmetric multi-core processor, which may be a few mixed stream processor or DSP processor.

 

2. Multi-threading and parallel computation of difference

(1) multi-threaded role not only as a parallel computing, he still has a lot of very useful role.

Still single nuclear age, there is a very wide range of multi-threaded applications, multi-threaded this time mostly used to reduce blocking (meaning similar

while(1)

{

if(flag==1)

break;

sleep(1);

}

This code) brings idle CPU resources, note there is no waste of CPU resources, remove the sleep (1) it is pure waste.

Blockage occurs at what time it? Usually wait for IO operations (disk, database, network, etc.). At this point if a single-threaded, CPU will run dry concrete deeds (things unrelated to the program are considered concrete deeds, because other programs did not perform a lot of sense to me), inefficient (for purposes of this program), such as a IO operation to take 10 milliseconds, CPU will be blocked close to 10 ms, which is what a waste ah! To know the number of the CPU is nanoseconds to live.

So this time-consuming IO operations on one thread Thread behalf to execute, create a function (the code) is not part of this thread is blocked IO operation, continue to do other things in the program, rather than waiting for dry (or to perform other programs).

Also in this era of single-core, multi-threaded eliminate this blocking action can also be called a "concurrency", which are parallel and essentially different of. Concurrency is a "pseudo-parallel", seemingly in parallel, or in fact a CPU executing everything, just switch too fast, we could not detect nothing. Based programs such as the UI (the saying goes, is a graphical interface), if you point a button to trigger the event need to be performed for 10 seconds, then the program will be suspended animation, because the program execution busy, no empty ignores other operating users; and If you put this button to trigger the function assigned to a thread, and then start the thread to execute, then the program will not be suspended animation, to continue operating the corresponding other users. However, the attendant is mutual exclusion and thread synchronization, deadlocks and other issues detailed in the relevant literature.

Now is the era of multicore, mutual exclusion and synchronization problem with this thread is more severe, mostly single-core era considered concurrent, multi-core era really very different, and why? For details, refer to the literature. I am here to explain briefly, before the use of volatile variables can solve most of the problems, such as multiple threads access a common flag Flag, if it is a single-core concurrency, basically no problem (PS under what circumstances it would be wrong ? flag have more, or an array, this time can only get this problem by means of logic, more than a few idle does not matter, not a fatal problem on the line), because there is only one CPU, while the flag can only access the there is a thread, multi-core situation is not the same, so the only volatile unlikely to solve the problem, which use the specific language, "semaphore" specific environment, Mutex, Monitor, Lock and so on, these classes are operating a "break off" on the hardware, to "primitive" effect, access to the critical section is not interrupted by the effect of concrete will not explain, readers can look at the "modern operating system."

(2) parallel computing can also be obtained by other means, multithreading is only one.

Other means include: multi-process (which in turn includes shared memory and distributed multi-machine, as well as the hybrid), instruction-level parallelism.

ILP (instruction level parallelism), x86 architecture was called SMT (simultaneous multithreading), in the MIPS architecture in the corresponding is super scalar (superscalar) and order execution, the two are different, but all can reach common ground ILP, which is not user controlled, does not belong to programming range, can only do a limited optimization, which may only be a limited fall within the scope of the optimization compiler jurisdiction, the user can do little.

(3) typically adapted for parallel computing languages

Erlang and MPI: two former language, which is C ++ and Fortran extensions, the effect is the same, the use of multiple processes for parallel computing, Erlang is a shared storage area, MPI is a hybrid.

C # .NET4.0: New version 4.0 can be implemented in parallel For loop with a small amount of code, previous versions required a very complicated code to achieve the same functionality. This is the use of multi-threaded parallel computing. Java and C # 3.5 has a thread pool (ThreadPool), is also a good multi-threaded Management very easy to use, convenient and efficient to use multiple threads.

CUDA, still a young bull, there is great potential for development, but for now its applications are limited. It can only use the C language, and not the C99, relatively low-level, you can not use a function pointer. This is due to the innate sense of personal hardware limitations (the average small core available memory, system memory and communication for a long time), applies only to do scientific calculations, static image processing, video encoding and decoding, in other areas, not as good as high-end CPU. After the operating system and other GPU, the GPU resources can be fully scheduled, the GPU can be used when the great God. Physical speed of the game, in fact, multi-core CPU can be well done.

other languages. . . Yep. . Reserved for future discussion.

 

3. Thread the better it? And when we need to use multiple threads?

Thread is not necessarily better, but also to the thread switching overhead, when you add a thread of time, increasing the overhead is less than the thread can eliminate the blocking time, Now that's value for money.

Since Linux 2.6 kernel, it will put different threads to different cores to deal with. Windows also supports this feature from the start NT.4.0.

When to use multithreading it? To discuss this sub-four cases:

a. Multicore CPU-- compute-intensive tasks. This time to make use of multiple threads, can improve the efficiency of task execution, such as encryption and decryption, compression and decompression of data (video, audio, normal data), or only the core of a fully loaded, while the other core is idle.

b. monocytes CPU-- compute-intensive tasks. At this mission has been to consume 100% CPU resources, there is no need and can not use multiple threads to improve the computational efficiency; the contrary, if to do human-computer interaction, it is best to use multiple threads, the user can not avoid computer operation.

c single-core CPU -. IO-intensive tasks, using multiple threads or for convenient human-computer interaction,

d Multi-core CPU -. IO-intensive tasks, which needless to say, the same time with a single-core reason.

 

4. programmer needs to have skills / technology

(1) reduction of the serial code to improve efficiency. This is nonsense.

(2) distribution of a single shared data: data replication to a lot of copies, so that different threads can be accessed simultaneously.

(3) Load balancing is divided into two kinds of static and dynamic. See specific literature.

Guess you like

Origin www.cnblogs.com/kira2will/p/11027351.html