C语言 制作 -j4 或 -j8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15289250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
make -j4 or -j8
提问by Veger
I have 4 processors and am compiling processor hungry application, I read that using make with the -j4 switch was recommended for OpenCV, should I rather use -j8 and what is the advantage of making for multiple processors?
我有 4 个处理器,正在编译需要处理器的应用程序,我读到 OpenCV 建议使用带有 -j4 开关的 make,我应该使用 -j8 吗?为多个处理器制作的优势是什么?
回答by MadScientist
The answers above are all mostly correct. However, the details are a bit misleading. For example, there's no need to add an extra job for a "managing thread" (note: makeis not actually multithreaded). makenever counts itself as a job for the purposes of -j, so, as Huygens says above, if you say -j5you'll get 5 compile jobs running, not 4 plus make.
上面的答案大部分都是正确的。然而,细节有点误导。例如,不需要为“管理线程”添加额外的作业(注意:make实际上不是多线程的)。 make永远不会将自己视为一项工作-j,因此,正如惠更斯在上面所说的,如果您说-j5将运行 5 个编译作业,而不是 4 个加上 make。
The reason most people use [number of cores] + [some padding] has nothing to do with makeor what it needs, but rather with the nature of the compiler. A compiler is really just a very complicated text translation tool: it reads in text in one form and converts it to "text" (binary) in another form. A lot of this (especially as your language gets more complex, like C++), requires a lot of CPU. But it also requires a lot of disk I/O. Disk I/O is slow, so while one compiler is waiting for some data from the disk, the kernel schedules other jobs to run. That is why you can usefully have more than the number of cores compiles running at the same time.
大多数人使用 [核心数] + [一些填充] 的原因与make它需要什么无关,而是与编译器的性质有关。编译器实际上只是一个非常复杂的文本翻译工具:它以一种形式读入文本并将其转换为另一种形式的“文本”(二进制)。其中很多(尤其是当您的语言变得更复杂时,例如 C++)需要大量 CPU。但它也需要大量的磁盘 I/O。磁盘 I/O 很慢,因此当一个编译器正在等待来自磁盘的一些数据时,内核会安排其他作业运行。这就是为什么您可以有效地拥有多于同时运行的内核数量的原因。
Exactly how large you can get -jbefore you start seeing diminishing returns (your builds actually start going slower, at some point, with more -j) depends completely on your hardware, the kinds of builds you're doing, etc. The only way to know for sure is experimentation.
-j在您开始看到收益递减(您的构建实际上开始变慢,在某些时候,有更多-j)之前,您究竟能获得多大的规模完全取决于您的硬件、您正在执行的构建类型等。唯一的了解方式当然是实验。
However, [number of cores]+[a few] is typically a good approximation.
然而,[核心数]+[少数]通常是一个很好的近似值。
回答by Veger
As you say the -jflag tells make that it is allowed to spawn the provided amount of 'threads'. Ideally each thread is executed on its own core/CPU, so your multi-core/CPU environment is used to its fullest.
正如您所说,该-j标志告诉 make 允许生成提供的“线程”数量。理想情况下,每个线程都在其自己的内核/CPU 上执行,因此您的多核/CPU 环境可以得到充分利用。
makeitself does not compile the source files. This is done by a compiler (gcc). The Makefile (input for make) contains a set of targets. Each target has a set of dependencies (on other targets) and rules how to build the target. makereads the Makefile(s) and manages all targets, dependencies and build rules. Besides compiling source files you can use maketo perform any task that can be described by shell commands.
make本身不编译源文件。这是由编译器 (gcc) 完成的。Makefile(用于 的输入make)包含一组目标。每个目标都有一组依赖项(对其他目标)和规则如何构建目标。make读取 Makefile(s) 并管理所有目标、依赖项和构建规则。除了编译源文件之外,您还可以make用来执行可由 shell 命令描述的任何任务。
If you set the allowed number of threads too high, it is not possible to schedule each thread on its own core. Additional scheduling (context) switches are required to let all threads execute. This additional resource usage obviously result in lower performance.
如果您将允许的线程数设置得太高,则不可能在其自己的核心上调度每个线程。需要额外的调度(上下文)开关才能让所有线程执行。这种额外的资源使用显然会导致性能下降。
There are multiple rules-of-thumb, but I guess that setting to total amount to <number of cores> + 1is the most common. The idea behind this is that all cores have their own thread and there is one additional managing thread that handles the targets and which is next to be build.
有多种经验法则,但我想设置为 total amount to<number of cores> + 1是最常见的。这背后的想法是所有内核都有自己的线程,并且有一个额外的管理线程来处理目标,接下来要构建。
回答by Huygens
The -joption is only use to speed up application build, it determines how many jobs makecan spawn for the build. You can either set -j<nb core>or even higher -j<nb-core * 1.5>so that compilation can happen in parallel.
该-j选项仅用于加速应用程序构建,它确定make可以为构建生成多少作业。您可以设置-j<nb core>甚至更高,-j<nb-core * 1.5>以便编译可以并行进行。
It has no impact on the compiled code.
它对编译后的代码没有影响。
For a 4 core system, you could try make -j6. If make can run parallel builds, it will launch up to 6 simultaneous compilation process (e.g. 6 calls to gcc).
对于 4 核系统,您可以尝试make -j6. 如果 make 可以运行并行构建,它将启动最多 6 个同时编译过程(例如 6 个对 gcc 的调用)。
回答by SF.
One CPU per thread plus one manager/loader. Since a thread that does disk operations is technically almost idle from CPU point of view, add one to the total number of cores.
每个线程一个 CPU 加上一个管理器/加载器。由于从 CPU 的角度来看,执行磁盘操作的线程在技术上几乎处于空闲状态,因此在内核总数中加一个。
If the CPU is using hyperthreading, you can safely count each core as two cores and double the number of threads, so a quad core Intel Core i7 should get -j9 (eight virtual cores plus manager.) On a quad core AMD use -j5
如果 CPU 使用超线程,您可以安全地将每个内核视为两个内核并将线程数加倍,因此四核 Intel Core i7 应获得 -j9(八个虚拟内核加上管理器。)在四核 AMD 上使用 -j5

