<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 3.6 檢測MPI的并行環境 **NOTE**:*此示例代碼可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-03/recipe-06 中找到,包含一個C++和一個C的示例。該示例在CMake 3.9版(或更高版本)中是有效的,并且已經在GNU/Linux、macOS和Windows上進行過測試。https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-03/recipe-06 中也有一個適用于CMake 3.5的C示例。* 消息傳遞接口(Message Passing Interface, MPI),可以作為OpenMP(共享內存并行方式)的補充,它也是分布式系統上并行程序的實際標準。盡管,最新的MPI實現也允許共享內存并行,但高性能計算中的一種典型方法就是,在計算節點上OpenMP與MPI結合使用。MPI標準的實施包括: 1. 運行時庫 2. 頭文件和Fortran 90模塊 3. 編譯器的包裝器,用來調用編譯器,使用額外的參數來構建MPI庫,以處理目錄和庫。通常,包裝器`mpic++/mpiCC/mpicxx `用于C++,`mpicc`用于C,`mpifort`用于Fortran。 4. 啟動MPI:應該啟動程序,以編譯代碼的并行執行。它的名稱依賴于實現,可以使用這幾個命令啟動:`mpirun`、`mpiexec`或`orterun`。 本示例,將展示如何在系統上找到合適的MPI實現,從而編譯一個簡單的“Hello, World”MPI例程。 ## 準備工作 示例代碼(`hello-mpi.cpp`,可從http://www.mpitutorial.com 下載)將在本示例中進行編譯,它將初始化MPI庫,讓每個進程打印其名稱: ```c++ #include <iostream> #include <mpi.h> int main(int argc, char **argv) { // Initialize the MPI environment. The two arguments to MPI Init are not // currently used by MPI implementations, but are there in case future // implementations might need the arguments. MPI_Init(NULL, NULL); // Get the number of processes int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); // Get the rank of the process int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); // Get the name of the processor char processor_name[MPI_MAX_PROCESSOR_NAME]; int name_len; MPI_Get_processor_name(processor_name, &name_len); // Print off a hello world message std::cout << "Hello world from processor " << processor_name << ", rank " << world_rank << " out of " << world_size << " processors" << std::endl; // Finalize the MPI environment. No more MPI calls can be made after this MPI_Finalize(); } ``` ## 具體實施 這個示例中,我們先查找MPI實現:庫、頭文件、編譯器包裝器和啟動器。為此,我們將用到`FindMPI.cmake`標準CMake模塊: 1. 首先,定義了CMake最低版本、項目名稱、支持的語言和語言標準: ```cmake cmake_minimum_required(VERSION 3.9 FATAL_ERROR) project(recipe-06 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) ``` 2. 然后,調用`find_package`來定位MPI: ```cmake find_package(MPI REQUIRED) ``` 3. 與前面的配置類似,定義了可執行文件的的名稱和相關源碼,并鏈接到目標: ```cmake add_executable(hello-mpi hello-mpi.cpp) target_link_libraries(hello-mpi PUBLIC MPI::MPI_CXX ) ``` 4. 配置和構建可執行文件: ```shell $ mkdir -p build $ cd build $ cmake .. # -D CMAKE_CXX_COMPILER=mpicxx C++例子中可加,加與不加對于構建結果沒有影響╭(╯^╰)╮ -- ... -- Found MPI_CXX: /usr/lib/openmpi/libmpi_cxx.so (found version "3.1") -- Found MPI: TRUE (found version "3.1") -- ... $ cmake --build . ``` 5. 為了并行執行這個程序,我們使用`mpirun`啟動器(本例中,啟動了兩個任務): ```shell $ mpirun -np 2 ./hello-mpi Hello world from processor larry, rank 1 out of 2 processors Hello world from processor larry, rank 0 out of 2 processors ``` ## 工作原理 請記住,編譯包裝器是對MPI庫編譯器的封裝。底層實現中,將會調用相同的編譯器,并使用額外的參數(如成功構建并行程序所需的頭文件包含路徑和庫)來擴充它。 編譯和鏈接源文件時,包裝器用了哪些標志?我們可以使用`--showme`選項來查看。要找出編譯器的標志,我們可以這樣使用: ```shell $ mpicxx --showme:compile -pthread ``` 為了找出鏈接器標志,我們可以這樣: ```shell $ mpicxx --showme:link -pthread -Wl,-rpath -Wl,/usr/lib/openmpi -Wl,--enable-new-dtags -L/usr/lib/openmpi -lmpi_cxx -lmpi ``` 與之前的OpenMP配置類似,我們發現到MPI的鏈接非常簡單,這要歸功于`FindMPI`模塊提供的目標: 正如在前面的配方中所討論的,對于CMake版本低于3.9,需要更多的工作量: ```cmake add_executable(hello-mpi hello-mpi.c) target_compile_options(hello-mpi PUBLIC ${MPI_CXX_COMPILE_FLAGS} ) target_include_directories(hello-mpi PUBLIC ${MPI_CXX_INCLUDE_PATH} ) target_link_libraries(hello-mpi PUBLIC ${MPI_CXX_LIBRARIES} ) ``` 本示例中,我們討論了C++項目。其中的參數和方法對于C或Fortran項目同樣有效。
                  <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>

                              哎呀哎呀视频在线观看