-xhost 标志到底是什么?
我无法理解与 icc
一起使用的 -xhost
标志的用途。
在 英特尔网站,它指出:
xHost、QxHost
告诉编译器生成指令 编译主机处理器上可用的最高指令集。
我不确定“最高指令集”是什么意思。
另外,我看到一些关于 SIMD
I am having trouble understanding the purpose of the -xhost
flag used with icc
.
On the intel website, it states:
xHost, QxHost
Tells the compiler to generate instructions for the
highest instruction set available on the compilation host processor.
I am not sure what is meant by "highest instruction set".
Also, I see something about SIMD here. If -xhost
can speed up your code, why would someone choose not to use this flag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-xhost
标志根据当前 CPU(即用于进行编译的计算机中的 CPU)的功能生成尽可能最佳的代码。“最高指令集”意味着编译器将自动打开与您的CPU支持的最高指令集相对应的代码生成标志。因此,如果您的 CPU 仅支持 SSE2,那么就仅支持 SSE2。如果支持 AVX2,则该选项将打开。无论您的 CPU 支持哪种最高指令集扩展,编译器都会生成针对该指令集扩展的代码。
当您想要构建代码以在构建代码的同一台计算机上运行时,通常会使用此选项。例如,当构建将在同一台计算机上运行的科学算法时,或者编译您自己的 Linux 内核时。
从技术上讲,生成的二进制文件将在任何支持至少与构建计算机相同的指令集扩展的计算机上运行,这就是文档谈论“编译主机处理器上可用的最高指令集”的原因”。
正如 Peter Cordes 在评论中已经指出的那样,ICC 的
-xhost
标志本质上等同于 GCC 和 Clang 的-march=native
标志。它们都告诉编译器自动打开与主机 CPU 功能相匹配的所有选项,为主机 CPU 生成可能的最佳二进制文件,但只要它将运行在其他 CPU 上因为他们具有同等或更高的能力。您可以通过查找计算机 CPU 的规格并将相应的 code-gen 选项添加到编译器命令行来执行与
-xhost
完全相同的操作。-xhost
只是为您做这件事,查找您的主机 CPU 支持的内容并自动启用这些标志,而无需您进行跑腿工作。所以,这是一个方便的功能;仅此而已。-xhost
标志确实可以通过利用某些指令集扩展来加速代码,但它也可以导致二进制文件根本无法工作(在不支持与构建计算机相同的指令集扩展的另一台计算机上)。也许这对你来说不是问题;在这种情况下,您肯定会打开-host
标志。但是,在许多情况下,我们软件开发人员正在构建二进制文件供其他人运行,在这种情况下,我们必须更加小心地确定要排除哪些 CPU。还值得注意的是,英特尔的编译器实际上可以生成具有动态调度支持的单个可执行文件,从而允许您支持两种不同的体系结构。有关更多详细信息,请参阅Sergey L. 对相关问题的回答。
The
-xhost
flag generates the most optimal code possible, based on the capabilities of your current CPU (that is, the one in the computer you're using to do the compilation).By "highest instruction set", it means that the compiler will automatically turn on the code-generation flags corresponding to the highest instruction set supported by your CPU. So, if your CPU only supports SSE2, then that's all that will be turned on. If it supports AVX2, then that option will be turned on. Whatever the highest instruction set extension that your CPU supports, the compiler will generate code targeting that instruction set extension.
This option is generally used when you want to build code to run on the same computer where you're building it. For example, when building a scientific algorithm that you'll run on the same computer, or when compiling your own Linux kernel.
Technically speaking, the generated binaries will run on any computer that supports at least the same instruction set extensions as the build computer, which is why the documentation talks about "the highest instruction set available on the compilation host processor".
As Peter Cordes already noted in a comment, ICC's
-xhost
flag is essentially equivalent to GCC and Clang's-march=native
flag. Both of them tell the compiler to automatically turn on all options that match what the host CPU is capable of, generating the most optimal binary possible for the host CPU, but which will run on other CPUs, as long as they have equal or higher capabilities.You can do exactly the same thing that
-xhost
is going to do by looking up the specifications for your computer's CPU and adding the corresponding code-gen options to the compiler command line.-xhost
just does it for you, looking up what your host CPU supports and enabling those flags automatically, without you having to do the legwork. So, it is a convenience feature; nothing more, nothing less.The
-xhost
flag can, indeed, speed up your code by taking advantage of certain instruction set extensions, but it can also result in a binary that won't work at all (on a different computer that doesn't support the same instruction set extensions as your build computer). Maybe that's not a problem for you; in that case, you'd definitely turn on the-host
flag. But, in many cases, we software developers are building binaries for other people to run, and in that case, we have to be a bit more careful about exactly which CPUs we want to exclude.It is also worth noting that Intel's compiler can actually generate a single executable with dynamic dispatching support that allows you to support two different architectures. See Sergey L.'s answer to a related question for more details.
添加@Cody Gray 的答案:有时您不想使用
-xhost
标志。在超级计算机集群上,您通常在“登录节点”上进行编译,并且代码在“计算节点”上执行。这两者的架构可能略有不同(有时:非常不同)。正确的策略是通过指示目标指令集的显式编译器标志告诉登录节点要编译哪种架构。无论如何,您都不要使用
xhost
标志,这可能会导致它在计算节点上无法执行。To add to the answer by @Cody Gray: sometimes you don't want to use the
-xhost
flag. On a supercomputer cluster you often do your compilation on a "login node" and your code executes on a "compute node". These two can have slightly (or sometimes: very) different architectures. The right strategy is to tell the login node for what architecture to compile through explicit compiler flags indicating that target instruction set.In any case, you don't use the
xhost
flag which might make it unexecutable on the compute node.