-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacierz.c
More file actions
190 lines (170 loc) · 4.82 KB
/
macierz.c
File metadata and controls
190 lines (170 loc) · 4.82 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "cacti.h"
#define UNUSED(x) (void)(x)
#define MSG_GC 1
#define MSG_WC 2
#define MSG_COMPUTE 3
#define MSG_CLEANUP 4
typedef struct cell {
int mills;
int x;
} cell_t;
actor_id_t actor0;
actor_id_t* actors;
int completes = 0;
cell_t** matrix;
int* sums;
long no_cols;
int no_rows;
message_t msgGoDie = {
.message_type = MSG_GODIE
};
message_t msgSpawn = {
.message_type = MSG_SPAWN
};
message_t msgCleanUp = {
.message_type = MSG_CLEANUP
};
typedef struct info {
long col;
actor_id_t right;
} info_t;
typedef struct compInfo {
int row;
int res;
} comp_info_t;
void getCol(void** info, size_t size, void* left);
void writeCol(void** info, size_t size, void* col);
void compute(void** info, size_t size, void* comp_info);
void hello(void** info, size_t size, void* right);
void cleanUp(void** info, size_t size, void* dummy);
void cleanUp(void** info, size_t size, void* dummy) {
UNUSED(size);
UNUSED(dummy);
free(*info);
send_message(actor_id_self(), msgGoDie);
}
void getCol (void** info, size_t size, void* left) {
UNUSED(size);
message_t msgWC = {
.message_type = MSG_WC,
.nbytes = sizeof(int),
.data = (void*) ((*(info_t**)info)->col-1)
};
send_message((actor_id_t)left, msgWC);
};
void writeCol(void** info, size_t size, void* col) {
UNUSED(size);
(*(info_t**)info)->col = (intptr_t)col;
actors[(intptr_t)col] = actor_id_self();
if((intptr_t) col > 0) {
send_message(actor_id_self(), msgSpawn);
} else {
message_t msgCompute = {
.message_type = MSG_COMPUTE,
};
send_message(actor_id_self(), msgCompute);
}
}
void hello(void** info, size_t size, void* right){
UNUSED(size);
*info = malloc(sizeof(info_t));
if(actor_id_self() != actor0) {
(*(info_t **) info)->right = (actor_id_t) right;
message_t msgGC = {
.message_type = MSG_GC,
.nbytes = sizeof(actor_id_t),
.data = (void *) actor_id_self()
};
send_message((actor_id_t)right, msgGC);
} else {
message_t msgWC = {
.message_type = MSG_WC,
.nbytes = sizeof(int),
.data = (void*) (no_cols-1)
};
send_message(actor_id_self(), msgWC);
}
}
void compute(void** info, size_t size, void* comp_info) {
UNUSED(size);
long col = (*(info_t**) info)->col;
actor_id_t right = (*(info_t**) info)->right;
if(no_cols == 1) {
for (int r = 0; r < no_rows; r++) {
usleep(1000*matrix[r][col].mills);
sums[r] = matrix[r][col].x;
}
send_message(actor_id_self(), msgCleanUp);
return;
}
if(col == 0) {
for(int r = 0;r<no_rows;r++) {
comp_info_t *comp_info2 = malloc(sizeof(comp_info_t));
comp_info2->row=r;
usleep(1000*matrix[r][col].mills);
comp_info2->res=matrix[r][col].x;
message_t msgCompute = {
.message_type = MSG_COMPUTE,
.nbytes = sizeof(void*),
.data = comp_info2
};
send_message(right,msgCompute);
}
return;
}
int row = ((comp_info_t*)comp_info)->row;
int res = ((comp_info_t*)comp_info)->res;
usleep(1000*matrix[row][col].mills);
res+= matrix[row][col].x;
if(col == no_cols -1) {
sums[row] = res;
free(comp_info);
completes++;
if(completes == no_rows){
for(int i=0;i<no_cols;i++){
send_message(actors[i], msgCleanUp);
}
}
} else {
((comp_info_t*)comp_info)->res = res;
message_t msgCompute = {
.message_type = MSG_COMPUTE,
.nbytes = sizeof(void*),
.data = comp_info
};
send_message(right, msgCompute);
}
}
act_t act[5] = {&hello, &getCol, &writeCol, &compute, &cleanUp};
role_t role = {
.nprompts = 5,
.prompts = act
};
int main() {
scanf("%d", &no_rows);
scanf("%ld", &no_cols);
matrix = malloc(no_rows * sizeof(cell_t));
sums = malloc(no_rows * sizeof(int));
actors = malloc(no_cols * sizeof(actor_id_t));
for(int row =0;row<no_rows;row++){
matrix[row] = malloc(no_cols * sizeof(cell_t));
for(int col=0;col<no_cols;col++) {
scanf("%d", &matrix[row][col].x);
scanf("%d", &matrix[row][col].mills);
}
}
msgSpawn.data = &role;
actor_system_create(&actor0, &role);
actor_system_join(actor0);
for(int i=0;i<no_rows;i++)
printf("%d\n", sums[i]);
for(int row=0;row<no_rows;row++)
free(matrix[row]);
free(matrix);
free(actors);
free(sums);
return 0;
}