<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 13.3 使用OpenMP并行化交叉編譯Windows二進制文件 **NOTE**:*此示例代碼可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-13/recipe-02 中找到,其中包含一個C++示例和Fortran示例。該示例在CMake 3.5版(或更高版本)中是有效的,并且已經在GNU/Linux、macOS和Windows上進行過測試。* 在這個示例中,我們將交叉編譯一個OpenMP并行化的Windows二進制文件。 ## 準備工作 我們將使用第3章第5節中的未修改的源代碼,示例代碼將所有自然數加到N (`example.cpp`): ```c++ #include <iostream> #include <omp.h> #include <string> int main(int argc, char *argv[]) { std::cout << "number of available processors: " << omp_get_num_procs() << std::endl; std::cout << "number of threads: " << omp_get_max_threads() << std::endl; auto n = std::stol(argv[1]); std::cout << "we will form sum of numbers from 1 to " << n << std::endl; // start timer auto t0 = omp_get_wtime(); auto s = 0LL; #pragma omp parallel for reduction(+ : s) for (auto i = 1; i <= n; i++) { s += i; } // stop timer auto t1 = omp_get_wtime(); std::cout << "sum: " << s << std::endl; std::cout << "elapsed wall clock time: " << t1 - t0 << " seconds" << std::endl; return 0; } ``` `CMakeLists.txt`檢測OpenMP并行環境方面基本沒有變化,除了有一個額外的安裝目標: ```cmake # set minimum cmake version cmake_minimum_required(VERSION 3.9 FATAL_ERROR) # project name and language project(recipe-02 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) include(GNUInstallDirs) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) find_package(OpenMP REQUIRED) add_executable(example example.cpp) target_link_libraries(example PUBLIC OpenMP::OpenMP_CXX ) install( TARGETS example DESTINATION ${CMAKE_INSTALL_BINDIR} ) ``` ## 具體實施 通過以下步驟,我們將設法交叉編譯一個OpenMP并行化的Windows可執行文件: 1. 創建一個包含`example.cpp`和`CMakeLists.txt`的目錄。 2. 我們將使用與之前例子相同的`toolchain.cmake`: ```cmake # the name of the target operating system set(CMAKE_SYSTEM_NAME Windows) # which compilers to use set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++) # adjust the default behaviour of the find commands: # search headers and libraries in the target environment set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) # search programs in the host environment set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) ``` 3. 將`CMAKE_CXX_COMPILER`設置為對應的編譯器(路徑)。 4. 然后,通過`CMAKE_TOOLCHAIN_FILE`指向工具鏈文件來配置代碼(本例中,使用了從源代碼構建的MXE編譯器): ```shell $ mkdir -p build $ cd build $ cmake -D CMAKE_TOOLCHAIN_FILE=toolchain.cmake .. -- The CXX compiler identification is GNU 5.4.0 -- Check for working CXX compiler: /home/user/mxe/usr/bin/i686-w64-mingw32.static-g++ -- Check for working CXX compiler: /home/user/mxe/usr/bin/i686-w64-mingw32.static-g++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found OpenMP_CXX: -fopenmp (found version "4.0") -- Found OpenMP: TRUE (found version "4.0") -- Configuring done -- Generating done -- Build files have been written to: /home/user/cmake-recipes/chapter-13/recipe-02/cxx-example/build ``` 5. 構建可執行文件: ```shell $ cmake --build . Scanning dependencies of target example [ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.obj [100%] Linking CXX executable bin/example.exe [100%] Built target example ``` 6. 將`example.exe`拷貝到Windows環境下。 7. Windows環境下,將看到如下的輸出: ```shell $ set OMP_NUM_THREADS=1 $ example.exe 1000000000 number of available processors: 2 number of threads: 1 we will form sum of numbers from 1 to 1000000000 sum: 500000000500000000 elapsed wall clock time: 2.641 seconds $ set OMP_NUM_THREADS=2 $ example.exe 1000000000 number of available processors: 2 number of threads: 2 we will form sum of numbers from 1 to 1000000000 sum: 500000000500000000 elapsed wall clock time: 1.328 seconds ``` 8. 正如我們所看到的,二進制文件可以在Windows上工作,而且由于OpenMP并行化,我們可以觀察到加速效果! ## 工作原理 我們已經成功地使用一個簡單的工具鏈進行交叉編譯了一個可執行文件,并可以在Windows平臺上并行執行。我們可以通過設置`OMP_NUM_THREADS`來指定OpenMP線程的數量。從一個線程到兩個線程,我們觀察到運行時從2.6秒減少到1.3秒。有關工具鏈文件的討論,請參閱前面的示例。 ## 更多信息 可以交叉編譯一組目標平臺(例如:Android),可以參考:https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html
                  <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>

                              哎呀哎呀视频在线观看