Set up MPI parallel computing environment and calculate pi value [windows and Ubuntu]


Build MPI parallel computing environment, calculate pi value by using the formula for calculating pi, and use the number of multi threads n respectively Calculation results of 100, 1000 and 10000.

The following is the analysis of the specific installation process and experimental results, analyzing the impact of n value on pi accuracy and the impact of the number of parallel processes on computing speed.

1, Choose to build mpi environment on Linux, Ubuntu or Windows

The following is how to build the windows environment. The Ubuntu method is in the third point

First of all

Install visual studio
Official website download address: https://visualstudio.microsoft.com/zh-hans/vs/


After downloading, double-click the exe file. Select the necessary tools


Then wait for the installation

If there is an error, you can refer to the official error correction document https://docs.microsoft.com/zh-cn/troubleshoot/visualstudio/general/visual-studio-2022-unsupported-operating-systems

PS: linux or people who don't use VS can go to https://www.open-mpi.org/software/ompi/v4.1/ Download the corresponding mpich version, so you don't need VS

What's more

mpi installation started: https://www.microsoft.com/en-us/download/details.aspx?id=57467


Run the following two files


In this way, the mpi installation is completed (the above figure is only the SDK, and the mpi figure is the same as the above figure)

2, VS configuration mpi

reference resources http://t.zoukankan.com/CheeseIce-p-10626345.html

3, Calculate pi value

VS C++ HelloWord calculator application on the official website https://docs.microsoft.com/zh-cn/cpp/get-started/tutorial-console-cpp?view=msvc-160
PS: you can look at this first and then calculate the PI value

The following uses the Ubuntu method

(1) Construction of basic compilation environment

In order to compile MPI code normally, you need to install the compilation environment of C, C + + and Fortran.

By default, Ubuntu does not provide a compilation environment for these languages, so it needs to be installed manually. If you install these compilation environments separately, it is very troublesome. Fortunately, the build essential tool provides many software packages related to compilation, including compilers such as gcc/g++/gfortran, libc6 Dev and other necessary libraries and other tools. Therefore, we only need to install build essential through the package manager.

The - y option of the apt get command agrees with all default selections during the default installation.

sudo apt-get install -y build-essential

After downloading and installing, you can type the following command to observe the gcc version information.

gcc -v

(2) MPICH installation

The software package contains mpich, which can be downloaded and installed directly through apt get.

sudo apt-get install -y mpich

Observe mpicc version information

mpicc -v

(3) The first mpi program: pi value calculation

Switch to user home directory

cd ~

Create a new code file with vim

vim mpi.c

After vim enters, it defaults to control mode. Press the letter i to enter the editing mode and write the MPI program code.

#include<mpi.h>
#include<stdio.h>
#include<math.h>
#pragma comment(lib,"mpi.lib")
 
#define N1 100
#define N2 1000
#define N3 10000 //n uses * * 100, 1000 and 10000 * * calculation results respectively

void PI(int N){
	double local = 0, pi, w, temp;
	int i, rank, size,k;
	w = 1.0 / N;
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	MPI_Status status;
	double t1, t2;
	t1 = MPI_Wtime();
	for (i = rank; i < N; i = i + size)
	{		
		temp = (i + 0.5)*w;
		local = 4.0 / (1.0 + temp*temp) + local;
	}	
	if ((rank!=0))
		
		MPI_Send(&local, 1, MPI_DOUBLE, 0, rank, MPI_COMM_WORLD);
	
	if (rank == 0)
	for (i = 1; i < size;i++)
	{
		MPI_Recv(&temp, 1, MPI_DOUBLE,i, i, MPI_COMM_WORLD, &status);
		local += temp;
	}
	if (rank == 0)
		pi = local;
	
	t2 = MPI_Wtime();
	//t = t2 - t1;
	if (rank == 0)
	{
		printf("pi is %lf\n", pi*w);
		switch(N){
			case 100: printf("n = 100, time is %lf\n", t2-t1);break;
			case 1000: printf("n = 1000, time is %lf\n", t2-t1);break;
			case 10000: printf("n = 10000, time is %lf\n", t2-t1);break;
		}	
	}
	MPI_Finalize();
}

int main(int argc, char **argv)
{
	int i;
	MPI_Init(&argc, &argv);
	PI(N1);
	return 0;
}

Press esc to enter the control mode, enter: wq save and exit the file.

Use mpicc command to compile MPI code file. Mpicc is a command to compile and link MPI programs written in C.

mpicc pi.c -o pi.o

Run the program with the mpirun command. Where the - np option specifies the number of processors. Mpirun is the startup script of MPI program. It can simplify the startup program of job, shield different features as much as possible, and provide users with a general concept of MPI parallel machine.

mpirun -np 8 ./pi.o


Change to N2=1000 and run again


N3=10000

Tags: C++ Multithreading

Posted on Wed, 01 Dec 2021 03:44:30 -0500 by Orkan