为什么在尝试实施 OpenMPI 时设置单元数
Why do you set number of cells when trying to implement 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>
请原谅我对处理器实际架构的无知。为什么示例代码设置单元格数?我以为每个处理器作为一个整体一次负责一项工作?
我根本看不懂这些台词...
// 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
这取决于命令行参数——argv[1]
——每个节点将有多少个作业(例如,在 OpenMPI 中,您可以通过 -N
指定每个节点的作业数节点)。此外,您可以生成线程以使用多核处理器。
实际上,您正在计算积分
。
您将积分区间 拆分为 numProcs
个部分,因此每个作业计算他的部分,最后全部归纳为还原。
(单词 cell
- 在这种情况下不是一个好的变量名)
我正在尝试学习如何使用 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>
请原谅我对处理器实际架构的无知。为什么示例代码设置单元格数?我以为每个处理器作为一个整体一次负责一项工作?
我根本看不懂这些台词...
// 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
这取决于命令行参数——argv[1]
——每个节点将有多少个作业(例如,在 OpenMPI 中,您可以通过 -N
指定每个节点的作业数节点)。此外,您可以生成线程以使用多核处理器。
实际上,您正在计算积分
。
您将积分区间 拆分为 numProcs
个部分,因此每个作业计算他的部分,最后全部归纳为还原。
(单词 cell
- 在这种情况下不是一个好的变量名)