2017-09-21 5 views
0

を実装しようと、私はOpenMPIの使用方法を学習しようとしていたときにセルの数を設定し、このサンプルコードに出くわしたは、なぜあなたはOpenMPIの

#include "mpi.h” 
int main(int argc, char **argv) 
{ 
    // Call MPI initialization 
    MPI_Init(&argc, &argv); 

    // Get my processor ID and number of processor 
    int myProcID; 
    int numProcs; 
    MPI_Comm_rank(MPI_COMM_WORLD, &myProcID); 
    MPI_Comm_size(MPI_COMM_WORLD, &numProcs); 

    // Set number of cells from command line argument 
    int numCells = atoi(argv[1]); 

<etc…> 

    // Find which piece of the integral this 
    // processor is responsible for 
    int numCellsPerProc = numCells/numProcs; 
    int myStart = myProcID * numCellsPerProc; 
    int myEnd = myStart + numCellsPerProc; 

    // Account for unequal load by making sure 
    // last processor has correct end 
    if (myProcID == numProcs - 1) myEnd = numCells; 
    // Loop over cells and compute integral 
    // using trapezoidal rule 
    double myResult = 0.0; 
    for (int i = myStart; i < myEnd; ++i) 
    { 
    double xL = xMin + i*dx; 
    double xR = xL + dx; 
    myResult += 0.5 * (myFunction(xL)+myFunction(xR)) * dx; 
    } 

    // Sum result across processors 
    double totalResult; 
    MPI_Reduce(&myResult, &totalResult, 1, MPI_DOUBLE, MPI_SUM, 
      0, MPI_COMM_WORLD); 

    // Print the result, but only from root processor 
    if (myProcID == 0) 
    { 
    std::cout << "result = "; 
    std::cout << std::fixed << std::setprecision(10) 
       << totalResult << std::endl; 
    } 

    // Call MPI_Finalize 
    MPI_Finalize(); 

    return 0; 
} 

<etc> 

ん、それはプロセッサの実際のアーキテクチャに来るとき私の無知を許し。サンプルコードでセル数を設定するのはなぜですか?私は、各プロセッサが一度に1つの仕事に責任があると思いましたか?
私は全くこれらの行を理解していない...

// Set number of cells from command line argument 
    int numCells = atoi(argv[1]); 

<etc…> 

    // Find which piece of the integral this 
    // processor is responsible for 
    int numCellsPerProc = numCells/numProcs; 
    int myStart = myProcID * numCellsPerProc; 
    int myEnd = myStart + numCellsPerProc 

答えて

1

これは、コマンドライン引数に依存します - あなたは、経由を指定することができ、OpenMPIの中で例えば(あなたはノードごとに必要がありますどのように多くの求人 - argv[1]-N、ノードあたりのジョブ数)。さらに、スレッドを生成してマルチコアプロセッサを利用することもできます。

実際には、
\int_0^cell myFunction(x) \mathrm{d}xの積分を計算しています。
[0,cell]の積分区間をnumProcs部分に分割すると、各ジョブはそれぞれの部分を計算し、最終的にはすべてが削減によって合計されます。

- 素敵なあなたに感謝本当の事を片付け

+0

(ワードcellは、この文脈では良い変数名ではありません)。 –

関連する問題