<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??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 。有時候,構建代碼的主機可能與運行代碼的主機不一樣。在計算集群中,登錄節點的體系結構可能與計算節點上的體系結構不同。解決此問題的一種方法是,將配置和編譯作為計算步驟,提交并部署到相應計算節點上。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看