-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
110 lines (94 loc) · 2.49 KB
/
main.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <mpi.h>
#include <sys/time.h>
#define MASTER 0
#define TAG 0
#define MSGSIZE 1000
#define MAX 25
using namespace std;
int main(int argc, char* argv[])
{
int my_rank, source, num_nodes, workers;
int buffer[MSGSIZE];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_nodes);
workers = num_nodes - 1;
// Build matrix to fit the number of workers, but won't work if workers are greater tand 1000
int **matrix;
matrix = new int *[workers];
for(int i = 0; i < workers; i++){
matrix[i] = new int[workers];
}
for(int i = 0, x = 0; i < workers; i++)
{
for(int j = 0; j < workers; j++, x++)
{
matrix[i][j] = x;
}
}
if (my_rank != MASTER) {
int send[1];
for(int i = workers - my_rank, x = 0; x < workers; i++, x++){
// Can't wander off the array
if(i >= workers)
i = 0;
// Can't talk to yourself
if((i + 1) == my_rank)
continue;
send[0] = matrix[my_rank-1][i];
// Send first
if(my_rank < (i + 1)){
MPI_Send(send, 1, MPI_INT, (i + 1), TAG, MPI_COMM_WORLD);
MPI_Recv(buffer, 1, MPI_INT, (i + 1), TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
matrix[my_rank-1][i]=buffer[0];
}
// Receive first
else{
MPI_Recv(buffer, 1, MPI_INT, (i + 1), TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
matrix[my_rank-1][i]=buffer[0];
MPI_Send(send, 1, MPI_INT, (i + 1), TAG, MPI_COMM_WORLD);
}
}
MPI_Send(matrix[my_rank-1], MSGSIZE, MPI_INT, MASTER, TAG, MPI_COMM_WORLD);
}
else {
//sleep(20);
printf ("Num_nodes: %d\n", num_nodes);
printf ("Hello from Master (process %d)!\n", my_rank);
cout << "Initial array:" << endl;
for(int i = 0, x = 0; i < workers; i++)
{
for(int j = 0; j < workers; j++, x++)
{
if(matrix[i][j] < 10)
cout << " ";
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
// Get results
for (source = 1; source < num_nodes; source++) {
MPI_Recv(buffer, MSGSIZE, MPI_INT, source, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for(int i = 0; i < workers; i++)
matrix[source-1][i] = buffer[i];
}
cout << "Transposed array:" << endl;
for(int i = 0, x = 0; i < workers; i++)
{
for(int j = 0; j < workers; j++, x++)
{
if(matrix[i][j] < 10)
cout << " ";
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
MPI_Finalize();
return 0;
}