Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

https://mpitutorial.com/tutorials/mpi-hello-world/

每个process的开始:MPI_Init之后
MPI_Init之后初始化的变量是每个process一个copy的,在local memory中

Hello world code examples

#include <mpi.h>
#include <stdio.h>

int main(int argc, char** argv) {
    // Initialize the MPI environment
    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
    printf("Hello world from processor %s, rank %d out of %d processors\n",
           processor_name, world_rank, world_size);

    // Finalize the MPI environment.
    MPI_Finalize();
}

MPI_Init

MPI environment must be initialized with:

MPI_Init(
    int* argc,
    char*** argv)

During MPI_Init, all of MPI’s global and internal variables are constructed. For example, a communicator is formed around all of the processes that were spawned, and unique ranks are assigned to each process.

编译运行

编译(把gcc换成mpicc即可,或者把g++替换成mpicxx

mpicc -o mpi_hello_world mpi_hello_world.c

运行(单节点)

mpirun -np 1 ./mpi-hello-world

MPI_Abort

int MPI_Abort(MPI_Comm comm, int errorcode)

comm
communicator of tasks to abort
errorcode
error code to return to invoking environment

Terminates all MPI processes associated with the communicator comm; in most systems (all to date), terminates all processes.

评论