MPI distributed programming -- 1.OpenMPI installation and basic use

Keywords: Programming sudo

1. OpenMPI installation (auto compile configuration)

The first method is to install sudo apt get install using a third-party source.

$ sudo apt-get install openmpi-bin

Query version information.

$ mpirun --version
mpirun (Open MPI) 1.10.2
$ mpiexec --version
mpiexec (OpenRTE) 1.10.2


2. OpenMPI installation (manual compilation and configuration)

The second method: use the official software package to install. First, Download Official openmpiv 4.0.1 installation package.

$ cd /home/joe/App/Openmpi
$ wget https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.1.tar.gz
$ ls
openmpi-4.0.1.tar.gz

Decompression.

$ tar -xzvf openmpi-4.0.1.tar.gz

Compile and install.

$ cd /home/joe/App/Openmpi/openmpi-4.0.1
$ ./configure --prefix=$HOME/App/Openmpi
$ make all
$ make install 

Delete the package and its extracted folder.

$ cd /home/joe/App/Openmpi
$ rm $HOME/App/Openmpi/openmpi-4.0.1.tar.gz
$ rm -r $HOME/App/Openmpi/openmpi-4.0.1

System environment variable configuration.

$ echo "export PATH=\$PATH:\$HOME/App/Openmpi/bin" >> $HOME/.bashrc
$ echo "export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:\$HOME/App/Openmpi/lib" >> $HOME/.bashrc

Query version.

$ /home/joe/App/Openmpi/bin
$ ./mpiexec --version
mpiexec (OpenRTE) 4.0.1
$ ./mpirun --version
mpirun (Open MPI) 4.0.1


3. test

Next, implement a "Hello World" of MPI multiprocess parallel programming.

Create a new hello.c file and write MPI code.

#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();
}

Compile hello.c to generate the Hello executable.

$ mpicc hello.c -o hello

Initialize the multiprocess parameters through mpirun to execute the hello file. It can be seen that in the state of multiprocess parallel programming, processes start at the same time and end in disorder.

$ mpirun -np 1 ./hello
Hello world from processor ubuntu00, rank 0 out of 1 processors
$ mpirun -np 2 ./hello
Hello world from processor ubuntu00, rank 1 out of 2 processors
Hello world from processor ubuntu00, rank 0 out of 2 processors
$ mpirun -np 3 ./hello
Hello world from processor ubuntu00, rank 2 out of 3 processors
Hello world from processor ubuntu00, rank 0 out of 3 processors
Hello world from processor ubuntu00, rank 1 out of 3 processors
$ mpirun -np 4 ./hello
Hello world from processor ubuntu00, rank 1 out of 4 processors
Hello world from processor ubuntu00, rank 2 out of 4 processors
Hello world from processor ubuntu00, rank 3 out of 4 processors
Hello world from processor ubuntu00, rank 0 out of 4 processors
$ mpirun -np 5 ./hello
Hello world from processor ubuntu00, rank 3 out of 5 processors
Hello world from processor ubuntu00, rank 4 out of 5 processors
Hello world from processor ubuntu00, rank 0 out of 5 processors
Hello world from processor ubuntu00, rank 1 out of 5 processors
Hello world from processor ubuntu00, rank 2 out of 5 processors

"Hello World" of Open MPI is finished.



Reference

[1. Official document of open MPI] https://www.open-mpi.org/software/ompi/v4.0/
[2. Open MPI v4.0.1 installation package] https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.1.tar.gz
[3. Open MPI installation tutorial] http://edu.itp.phys.ethz.ch/hs12/programming_techniques/openmpi.pdf
[4. Hello World in open MPI programming] https://mpitutorial.com/tutorials/mpi-hello-world/
[5. Open MPI programming official document] https://www.open-mpi.org/doc/

Posted by phpwannabe25 on Sun, 10 Nov 2019 06:26:50 -0800