-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseUseage.cpp
More file actions
43 lines (40 loc) · 1.48 KB
/
BaseUseage.cpp
File metadata and controls
43 lines (40 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cstdio>
#include <mpi.h>
#include <cstring>
const int MAX_STRING = 100;
int main(){
char greeting[MAX_STRING];
int comm_sz, 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){
//printf("Greetings from process %d of %d!\n", my_rank, comm_sz);
sprintf_s(greeting, "Working from process %d of %d!", my_rank, comm_sz);
MPI_Send(greeting, strlen(greeting)+1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
}//MPI_Send: msg_p, msg_size: +1 means '\0', msg_type, dest, tag, comm
else {
printf("Greetings from process %d of %d!\n", my_rank, comm_sz);
for (int q = 1; q < comm_sz; q++) {
MPI_Status status;
//use MPI_Status to store the source and tag of recved msg
MPI_Recv(greeting, MAX_STRING, MPI_CHAR, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
printf("%s\n", greeting);
//printf("%d %d\n", status.MPI_SOURCE, status.MPI_TAG);
int number;
MPI_Get_count(&status, MPI_CHAR, &number);
//use MPI_Get_count to get the count of recved msg
printf("%d\n", number);
}//MPI_Recv: msg_p, msg_size, msg_type, source, tag, comm, status_error
//ANY_SOURCE: break the dependency
}
MPI_Finalize();
return 0;
}
/*
* recv.comm = send.comm
* recv.tag = send.tag
* dest = r && src = q
* recv_type = send_type
* recv_size >= send_size
*/