简体   繁体   English

具有单核处理器的多线程代码和具有多核处理器的单线程代码

[英]Multi-thread code with single-core processor and single-thread code with multi-core processor

I'm new to multi-threaded programming. 我是多线程编程的新手。 I have been reading some articles, but two main points I'm not completely sure about. 我一直在阅读一些文章,但是有两点我不确定。

  1. If I have a single-thread code (sequential), and I run it on multi-core processor. 如果我有一个单线程代码(顺序),并且在多核处理器上运行它。 Will the OS try to divide the thread into multiple threads (while taking care of dependencies) to take advantage of the muli-core processor? 操作系统是否会尝试将线程划分为多个线程(同时照顾依赖性)以利用多核处理器?
  2. If I have a multi-thread code, and I run it on single-core processor. 如果我有一个多线程代码,并且在单核处理器上运行它。 Will the OS make time-sharing between different threads (the same way it does with multiple processes)? 操作系统会在不同线程之间进行分时 (与多个进程相同)吗?

1) No 1)没有

If an application makes use of, for example, the Intel maths libraries and has been compiled with the right switches, routines like FFTs will at runtime be split out into separate threads matching the number of cores in the machine. 例如,如果某个应用程序利用了英特尔数学库并且已使用正确的开关进行了编译,则诸如FFT之类的例程将在运行时拆分为与计算机内核数量匹配的单独线程。 Your source code remains 'single threaded', but the library is creating and destroying threads behind your back. 您的源代码仍然是“单线程”,但是该库正在背后创建和销毁线程。

Similarly some compilers (eh Intel's icc, Sun's C compiler) may turn some loops into separate threads, each tackling a share of the iterations. 同样,某些编译器(例如Intel的icc,Sun的C编译器)可能会将某些循环转换为单独的线程,每个循环负责一部分迭代。 Again the source code looks single threaded, but the compiler generates threaded code on your behalf. 同样,源代码看起来是单线程的,但是编译器会代您生成线程代码。 It's a bit like automatically applying some OpenMP to your source code. 这有点像自动将一些OpenMP应用于源代码。

OSes cannot second guess what an application is going to do, so they cannot intervene like this. 操作系统无法再猜测应用程序将要做什么,因此它们无法像这样进行干预。 Libraries and compilers know what is about to happen, so they can. 库和编译器知道将要发生的事情,因此可以。

Libraries and compiler tricks like this have been developed so as to make it easy for programmers to extract higher performance from 'single' threaded code. 已经开发了类似的库和编译器技巧,以使程序员可以轻松地从“单个”线程代码中提取更高的性能。 Intel started adding features like that to their maths library around about the same time they started heading towards multi-core CPUs. 大约在他们开始转向多核CPU的同时,英特尔就开始在其数学库中添加类似的功能。 The idea was to create (from the programmer's point of view) the impression of better 'single' thread performance, whilst the speed was actually being delivered by multiple cores. 这个想法是(从程序员的角度)创建更好的“单”线程性能的印象,而速度实际上是由多个内核提供的。 Similarly with Sun when they started doing multi-processor computers. 当他们开始使用多处理器计算机时,与Sun类似。

And with everyone more or less giving up on making significant improvements to the performance of a single core, this is the only way ahead. 随着每个人或多或少都放弃对单个内核性能的重大改进,这是前进的唯一方法。

2) Yes. 2)是的。 How else would it do it? 还能怎么做?

  1. No, the operating system has not enough information to do that. 否,操作系统没有足够的信息来执行此操作。 In parallelization you need to consider the dependencies between operations. 在并行化中,您需要考虑操作之间的依赖关系。 Some compiler try to do that, they have more information about the intent of the code. 一些编译器尝试这样做,他们具有有关代码意图的更多信息。 But even they often fail to do that effectively. 但是,即使他们经常不能有效地做到这一点。

  2. Yes, for example the Linux scheduler does not even distinguish between threads and processes . 是的,例如Linux调度程序甚至不区分线程和进程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM