当前位置:网站首页>Parallel MPI program delivery send message
Parallel MPI program delivery send message
2022-07-27 00:37:00 【Hula, hula, hula, Hula】
List of articles
Preface
Achieve the goal :
Each even numbered process sends a message to the odd numbered process on the right , And the change message is printed by the odd number process , For example, a total of 7 A process ,0 Send a message to 1 No. and by 1 Number printing ,2 Send a message to 3 No. and by 3 Number printing ,4 Send a message to 5 No. and by 5 Number printing ,6 No. will not send messages , Because there is no corresponding receiving process .
Then compile 、 misarrangement 、 perform , And summarize the learning experience
One 、 Experimental principle and tips
MPI The program must first #include <mpi.h>.
all MPI All related calls must be located in MPI_Init() and MPI_Finalize() Two MPI Between functions .MPI_Comm_size(MPI_COMM_WORLD,&comm_sz)MPI_Comm_rank(MPI_COMM_WORLD, &my_rank) Two MPI Function calls are used to obtain the number of processes in the parallel program and the sequence number of the current process (rank). A typical SPMD The structure is as follows :
if(my_rank == x)
do something;
else
do other things;
Two 、 Experiment source code
The code is as follows :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mpi.h>
const int MAX_STRING = 100;
int main(void) {
char greeting[MAX_STRING];
int comm_sz;
int my_rank;
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if (my_rank != 0) {
sprintf(greeting, "Greetings from process %d of %d!", my_rank, comm_sz);
MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR,0, 0, MPI_COMM_WORLD);
} else {
printf("Greetings from process %d of %d!\n", my_rank, comm_sz);
for (int q = 1; q < comm_sz; q++){
MPI_Recv(greeting, MAX_STRING, MPI_CHAR, q,0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("%s\n", greeting);
}
}
MPI_Finalize();
return 0;
}
emmm, This is still not very good. Roll it inside , Do you want to go again
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mpi.h>
const int MAX_STRING = 100;
int main(void) {
char greeting[MAX_STRING];
int comm_sz;
int my_rank;
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
int src, dst;
if(my_rank % 2 != 0)
src = my_rank - 1;
else
dst = my_rank + 1;
if (my_rank % 2 != 0) {
MPI_Recv(greeting, MAX_STRING, MPI_CHAR, src, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Message printed by process %d: %s\n", my_rank, greeting);
} else {
if(dst != comm_sz) {
sprintf(greeting, "Greetings from process %d of %d!", my_rank, comm_sz);
MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR, dst, 0, MPI_COMM_WORLD);
}
}
MPI_Finalize();
return 0;
}
3、 ... and 、 Experimental results



3、 ... and 、 Problems encountered in the experiment and solutions
1. Do not understand the compiled program , Don't use your own settings correctly comm_sz、my_rank Two variables , Cause program error .
resolvent : Read the relevant procedures , Understanding usage , At the same time, I keep trying and improving .
2. Send a message to the odd numbered process on the right of the process that implements each even numbered process , And the function of printing messages by odd numbered processes , No further thought , Just think through ifelse Statement to distinguish , Failure to consider the possible phenomenon of process idling leads to poor implementation of the effect .
resolvent : By increasing in advance else Statement will , Singular processes are distinguished from even processes , Then enter the task of sending and receiving in the judgment statement .
summary
Parallel programs need to pay attention to many points , It is recommended that beginners run it manually first to determine where they need to pay attention , Write pseudocode , And then re programming , The lesson of blood and tears , The reason why I wrote this , I hope it can help the latecomers .
边栏推荐
- V-viewer use
- Reduced dimension mean dot product matrix multiplicative norm probability normal distribution square loss
- 放图仓库-3(功能图像)
- 程序员必做50题
- 7_ Principal component analysis
- Three tier architecture simulation
- Find method of web page parsing by crawler
- Deploy yolov5 error reporting in pycharm
- Web middleware log analysis script 1.0 (shell script)
- Uni app learning (II)
猜你喜欢

Signal and system impulse response and step response

C language to find prime numbers, leap years and minimum common multiples and maximum common divisors

postman的使用

Reduced dimension mean dot product matrix multiplicative norm probability normal distribution square loss

C language shutdown applet

7_ Principal component analysis

哨兵2号(Sentinel-2)的下载及处理

Matlab simulation of image reconstruction using filtered back projection method

动态联编和静态联编、以及多态

Deep learning of parameter adjustment skills
随机推荐
[3. VIM operation]
Anaconda = > pycharm=> CUDA=> cudnn=> pytorch environment configuration
My first blog - confused junior
Arcgis和Cass实现断面展高程点
关于Thymeleaf的表达式
放图仓库-2(函数图像)
【4.6 中国剩余定理详解】
【AcWing第61场周赛】
Leetcode topic - binary tree chapter
2020-12-22 maximum common factor
Pyautogui usage example
Sliding window problem summary
[qt] attribute
In JS, the common writing methods and calling methods of functions - conventional writing, anonymous function writing, taking the method as an object, and adding methods to the object in the construct
2022_ SummerBlog_ 008
[4.9 detailed explanation of inclusion exclusion principle]
Configure deeplobcut 1 with your head covered
C language shutdown applet
The crawler parses the object of the web page. Element name method
Draw impact function