分布式计算之母 - MPI

B站影视 2025-01-15 19:04 3

摘要:#include #include int main(int argc, char** argv) {// Initialize the MPI environment. The two arguments to MPI Ini

# 下载 OpenMPI 最新稳定版本curl -O https://download.open-MPI.org/release/open-mpi/v5.0/openmpi-5.0.6.tar.gz# 解压tar -zxvf openmpi-5.0.6.tar.gz# 编译安装cd openmpi-5.0.6mkdir $HOME/openmpi./configure --prefix=$HOME/openmpimake -j 24 all # 这里指定了用24个线程并行编译make installecho -e '\nexport LD_LIBRARY_PATH=$HOME/openmpi/lib:$LD_LIBRARY_PATH\n' >> ~/.bashrcecho -e '\nexport PATH=$HOME/openmpi/bin:$PATH\n' >> ~/.bashrcsource $HOME/.bashrc# 删除原安装包/目录rm -rf openmpi-5.0.6/which mpiccwhich mpirunmpirun --version

mpi_hello_world.c:

#include #include 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 processesint world_size;MPI_Comm_size(MPI_COMM_WORLD, &world_size);// Get the rank of the processint world_rank;MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);// Get the name of the processorchar processor_name[MPI_MAX_PROCESSOR_NAME];int name_len;MPI_Get_processor_name(processor_name, &name_len);// Print off a hello world messageprintf("Hello world from processor %s, rank %d out of %d processors\n",processor_name, world_rank, world_size);// Finalize the MPI environment. No more MPI calls can be made after thisMPI_Finalize;

编译 mpi_hello_world.c:

mpicc -o mpi_hello_world mpi_hello_world.c

在两节点上分布式并行运行 mpi_hello_world:

来源:贺友胜

相关推荐