MPI is an important library for high-performance computing and parallel computing. If you want to run an MPI program, you must first configure it.
This tutorial is configured based on the development environment of VScode in the environment of win11
1. Download the compiler and MPI library
Mingw-w64:MinGW-w64 - for 32 and 64 bit Windows - Browse Files at SourceForge.net
You can use the installer at the bottom
After the installation is complete, open cmd and use the command gcc -v to view the installation version number. The version number appears to indicate that the installation is complete.
MicrosoftMPI:Download Microsoft MPI v10.1.2 from Official Microsoft Download Center
After clicking Download, select all two files to download, and then install
Use the set msmpi command in cmd to check whether the installation is complete. If the path is displayed, the installation is complete. Remember these paths
2. Configure the compiler and library
After installing the compiler, you must first configure the environment variables, you can directly search and open it in the search bar of win11
Click on the environment variable to enter the edit
Select path in the system variable, and click edit
Pick two blank lines to fill in the path
VScode needs to use these two plugins, C/C++ and Cunner
We directly set up the extension of Coderunner and edit it in setting.json
Comment out the original two lines, add two new lines, pay attention to replace with your own path, the replacement code is as follows
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt -fopenmp -l msmpi -L \"C:\\Program Files (x86)\\Microsoft SDKs\\MPI\\Lib\\x64\" -I \"C:\\Program Files (x86)\\Microsoft SDKs\\MPI\\Include\" && mpiexec -n 8 $fileNameWithoutExt",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -fopenmp -l msmpi -L \"C:\\Program Files (x86)\\Microsoft SDKs\\MPI\\Lib\\x64\" -I \"C:\\Program Files (x86)\\Microsoft SDKs\\MPI\\Include\" && mpiexec -n 8 $fileNameWithoutExt",
Then hold down ctrl+shift+p, search for C/C++, and find the edit configuration
Find Edit Configuration JSON on the left
Open the json file and add the include path of your mpi in the includepath
Use an absolute path, be careful to replace it with your installation path
3. Test
#include <stdio.h>
#include <mpi.h>
int main(){
MPI_Status status;
char string[]="xxxxx";
int myid;
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
if(myid==2)
MPI_Send("HELLO",5,MPI_CHAR,7,1234,MPI_COMM_WORLD);
if(myid==7){
MPI_Recv(string,5,MPI_CHAR,2,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
printf("Got %s from P%d, tag %d\n",string,status.MPI_SOURCE,status.MPI_TAG);
fflush(stdout);
}
MPI_Finalize();
}
A hello program test is prepared here
run successfully
4. Modify multi-threading
To modify the number of threads, you need to modify it in the setting.json file of Coderunner, and modify the suffixes of the two statements we just modified. There are 8 threads by default here.