ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 2.5 检测处理器指令集 **NOTE**:*此示例代码可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-02/recipe-05 中找到,包含一个C++示例。该示例在CMake 3.10版(或更高版本)中是有效的,并且已经在GNU/Linux、macOS和Windows上进行过测试。* 本示例中,我们将讨论如何在CMake的帮助下检测主机处理器支持的指令集。这个功能是较新版本添加到CMake中的,需要CMake 3.10或更高版本。检测到的主机系统信息,可用于设置相应的编译器标志,或实现可选的源代码编译,或根据主机系统生成源代码。本示例中,我们的目标是检测主机系统信息,使用预处理器定义将其传递给`C++`源代码,并将信息打印到输出中。 ## 准备工作 我们是`C++`源码(`processor-info.cpp`)如下所示: ```c++ #include "config.h" #include <cstdlib> #include <iostream> int main() { std::cout << "Number of logical cores: " << NUMBER_OF_LOGICAL_CORES << std::endl; std::cout << "Number of physical cores: " << NUMBER_OF_PHYSICAL_CORES << std::endl; std::cout << "Total virtual memory in megabytes: " << TOTAL_VIRTUAL_MEMORY << std::endl; std::cout << "Available virtual memory in megabytes: " << AVAILABLE_VIRTUAL_MEMORY << std::endl; std::cout << "Total physical memory in megabytes: " << TOTAL_PHYSICAL_MEMORY << std::endl; std::cout << "Available physical memory in megabytes: " << AVAILABLE_PHYSICAL_MEMORY << std::endl; std::cout << "Processor is 64Bit: " << IS_64BIT << std::endl; std::cout << "Processor has floating point unit: " << HAS_FPU << std::endl; std::cout << "Processor supports MMX instructions: " << HAS_MMX << std::endl; std::cout << "Processor supports Ext. MMX instructions: " << HAS_MMX_PLUS << std::endl; std::cout << "Processor supports SSE instructions: " << HAS_SSE << std::endl; std::cout << "Processor supports SSE2 instructions: " << HAS_SSE2 << std::endl; std::cout << "Processor supports SSE FP instructions: " << HAS_SSE_FP << std::endl; std::cout << "Processor supports SSE MMX instructions: " << HAS_SSE_MMX << std::endl; std::cout << "Processor supports 3DNow instructions: " << HAS_AMD_3DNOW << std::endl; std::cout << "Processor supports 3DNow+ instructions: " << HAS_AMD_3DNOW_PLUS << std::endl; std::cout << "IA64 processor emulating x86 : " << HAS_IA64 << std::endl; std::cout << "OS name: " << OS_NAME << std::endl; std::cout << "OS sub-type: " << OS_RELEASE << std::endl; std::cout << "OS build ID: " << OS_VERSION << std::endl; std::cout << "OS platform: " << OS_PLATFORM << std::endl; return EXIT_SUCCESS; } ``` 其包含`config.h`头文件,我们将使用`config.h.in`生成这个文件。`config.h.in`如下: ```c++ #pragma once #define NUMBER_OF_LOGICAL_CORES @_NUMBER_OF_LOGICAL_CORES@ #define NUMBER_OF_PHYSICAL_CORES @_NUMBER_OF_PHYSICAL_CORES@ #define TOTAL_VIRTUAL_MEMORY @_TOTAL_VIRTUAL_MEMORY@ #define AVAILABLE_VIRTUAL_MEMORY @_AVAILABLE_VIRTUAL_MEMORY@ #define TOTAL_PHYSICAL_MEMORY @_TOTAL_PHYSICAL_MEMORY@ #define AVAILABLE_PHYSICAL_MEMORY @_AVAILABLE_PHYSICAL_MEMORY@ #define IS_64BIT @_IS_64BIT@ #define HAS_FPU @_HAS_FPU@ #define HAS_MMX @_HAS_MMX@ #define HAS_MMX_PLUS @_HAS_MMX_PLUS@ #define HAS_SSE @_HAS_SSE@ #define HAS_SSE2 @_HAS_SSE2@ #define HAS_SSE_FP @_HAS_SSE_FP@ #define HAS_SSE_MMX @_HAS_SSE_MMX@ #define HAS_AMD_3DNOW @_HAS_AMD_3DNOW@ #define HAS_AMD_3DNOW_PLUS @_HAS_AMD_3DNOW_PLUS@ #define HAS_IA64 @_HAS_IA64@ #define OS_NAME "@_OS_NAME@" #define OS_RELEASE "@_OS_RELEASE@" #define OS_VERSION "@_OS_VERSION@" #define OS_PLATFORM "@_OS_PLATFORM@" ``` ## 如何实施 我们将使用CMake为平台填充`config.h`中的定义,并将示例源文件编译为可执行文件: 1. 首先,我们定义了CMake最低版本、项目名称和项目语言: ```cmake cmake_minimum_required(VERSION 3.10 FATAL_ERROR) project(recipe-05 CXX) ``` 2. 然后,定义目标可执行文件及其源文件,并包括目录: ```cmake add_executable(processor-info "") target_sources(processor-info PRIVATE processor-info.cpp ) target_include_directories(processor-info PRIVATE ${PROJECT_BINARY_DIR} ) ``` 3. 继续查询主机系统的信息,获取一些关键字: ```cmake foreach(key IN ITEMS NUMBER_OF_LOGICAL_CORES NUMBER_OF_PHYSICAL_CORES TOTAL_VIRTUAL_MEMORY AVAILABLE_VIRTUAL_MEMORY TOTAL_PHYSICAL_MEMORY AVAILABLE_PHYSICAL_MEMORY IS_64BIT HAS_FPU HAS_MMX HAS_MMX_PLUS HAS_SSE HAS_SSE2 HAS_SSE_FP HAS_SSE_MMX HAS_AMD_3DNOW HAS_AMD_3DNOW_PLUS HAS_IA64 OS_NAME OS_RELEASE OS_VERSION OS_PLATFORM ) cmake_host_system_information(RESULT _${key} QUERY ${key}) endforeach() ``` 4. 定义了相应的变量后,配置`config.h`: ```cmake configure_file(config.h.in config.h @ONLY) ``` 5. 现在准备好配置、构建和测试项目: ```shell $ mkdir -p build $ cd build $ cmake .. $ cmake --build . $ ./processor-info Number of logical cores: 4 Number of physical cores: 2 Total virtual memory in megabytes: 15258 Available virtual memory in megabytes: 14678 Total physical memory in megabytes: 7858 Available physical memory in megabytes: 4072 Processor is 64Bit: 1 Processor has floating point unit: 1 Processor supports MMX instructions: 1 Processor supports Ext. MMX instructions: 0 Processor supports SSE instructions: 1 Processor supports SSE2 instructions: 1 Processor supports SSE FP instructions: 0 Processor supports SSE MMX instructions: 0 Processor supports 3DNow instructions: 0 Processor supports 3DNow+ instructions: 0 IA64 processor emulating x86 : 0 OS name: Linux OS sub-type: 4.16.7-1-ARCH OS build ID: #1 SMP PREEMPT Wed May 2 21:12:36 UTC 2018 OS platform: x86_64 ``` 6. 输出会随着处理器的不同而变化。 ## 工作原理 `CMakeLists.txt`中的`foreach`循环会查询多个键值,并定义相应的变量。此示例的核心函数是`cmake_host_system_information`,它查询运行CMake的主机系统的系统信息。本例中,我们对每个键使用了一个函数调用。然后,使用这些变量来配置`config.h.in`中的占位符,输入并生成`config.h`。此配置使用`configure_file`命令完成。最后,`config.h`包含在`processor-info.cpp`中。编译后,它将把值打印到屏幕上。我们将在第5章(配置时和构建时操作)和第6章(生成源代码)中重新讨论这种方法。 ## 更多信息 对于更细粒度的处理器指令集检测,请考虑以下模块: https://github.com/VcDevel/Vc/blob/master/cmake/OptimizeForArchitecture.cmake 。有时候,构建代码的主机可能与运行代码的主机不一样。在计算集群中,登录节点的体系结构可能与计算节点上的体系结构不同。解决此问题的一种方法是,将配置和编译作为计算步骤,提交并部署到相应计算节点上。