-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mpi.C
57 lines (44 loc) · 1.7 KB
/
test_mpi.C
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "mpi.h"
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
printf("Entered Code\n");
cout << argc << endl;
int numtasks, rank, dest, source, rc, count, tag=0;
char inmsg, outmsg='x';
MPI_Status Stat; // required variable for receive routines
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("Setup Done\n");
cout << "procs = " << numtasks << endl;
cout << "rank = " << rank << endl;
// task 0 sends to task 1 and waits to receive a return message
if (rank == 0) {
dest = 1;
source = 1;
cout << "Entered 'if' 1 with rank = " << rank << endl;
MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
cout << "Sent MPI Message 1" << endl;
MPI_Recv(&inmsg, 1, MPI_CHAR, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &Stat);
cout << "Recieved MPI Message 1" << endl;
}
// task 1 waits for task 0 message then returns a message
else if (rank == 1) {
dest = 0;
source = 0;
cout << "Entered 'if' 2 with rank = " << rank << endl;
MPI_Recv(&inmsg, 1, MPI_CHAR, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &Stat);
cout << "Reieved MPI Message 2" << endl;
MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
cout << "Sent MPI Message 2" << endl;
}
printf("Ran Parallel Code\n");
// query receive Stat variable and print message details
// MPI_Get_count(&Stat, MPI_CHAR, &count);
// printf("Task %d: Received %d char(s) from task %d with tag %d \n", rank, count, Stat.MPI_SOURCE, Stat.MPI_TAG);
MPI_Finalize();
printf("Fin\n");
return 0;
}