-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
148 lines (126 loc) · 3.26 KB
/
main.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BACK 3
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
// do tablicy T
#define FREE 0
#define PROCESSED 1
#define BURIED 2
#define MSG_LENGTH 4
#define TAG 10
void sendAllInU(int *data, int size, int *U, int sender, int lamport)
{
int i = 0;
int req;
for (i = 0; i < size; i++)
if ((U[i] == FREE || U[i] == PROCESSED)&& i != sender)
{
data[3] = lamport;
MPI_Isend( data, MSG_LENGTH, MPI_INT, i, TAG, MPI_COMM_WORLD, &req);
lamport++;
}
}
void broadcast(int *data, int size, int sender, int lamport)
{
int i = 0;
int req;
for (i = 0; i < size; i++)
if (i!= sender)
{
data[3] = lamport;
MPI_Isend( data, MSG_LENGTH, MPI_INT, i, TAG, MPI_COMM_WORLD, &req);
lamport++;
}
}
int ready(int *table, int size)
{
int i = 0;
for (i; i < size; i++)
if (table[i] != 2)
return 0;
return 1;
}
int readyU(int *table, int size)
{
int i = 0;
for (i; i < size; i++)
if (table[i] == 0)
return 0;
return 1;
}
int main(int argc, char **argv)
{
srand(time(NULL));
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Status status;
// lista procesów ubiegających się o trupa
int U[size];
// tablica trupów - założyłem 100
int T[100] = {0};
// zegar lamporta
int C = 0;
int i = 0;
for (i = 0; i < size; i++)
U[i] = 0;
while(!ready(T, 100))
{
int trup_id = rand() % 100;
printf("rank %d: chce %d", rank, trup_id);
int message[MSG_LENGTH] = { rank, PROCESSED, trup_id, C };
sendAllInU(message, size, U, rank, C);
U[rank] = 1;
T[trup_id] = 1;
int status = 0;
do
{
int response[4];
MPI_Recv( &response, MSG_LENGTH, MPI_INT, MPI_ANY_SOURCE, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
C = MAX(C, response[3]);
printf("rank %d: dostałem od %d, trup_id: %d, C=%d\n", rank, response[0], response[2], response[3]);
if ((response[2] == trup_id) && (response[0] < rank))
{
status = 1;
break;
}
else if (response[1] == BURIED)
{
U[response[0]] = -1;
T[response[2]] = BURIED;
}
else if (response[1] == BACK)
{
U[response[0]] = 0;
}
else
{
T[response[2]] = PROCESSED;
U[response[0]] = 1;
}
}
while (!readyU(U,size));
if (status)
continue;
C++;
int request[4] = {
rank, BURIED, trup_id, C
};
broadcast(request, size, rank, C);
sleep(2);
int flag;
MPI_Request req;
MPI_Status stat;
int response[4];
do
{
MPI_Irecv( &response, MSG_LENGTH, MPI_INT, MPI_ANY_SOURCE, TAG, MPI_COMM_WORLD, &req);
MPI_Test(&req, &flag, &stat);
}
while (flag);
}
MPI_Finalize();
}