-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask.c
355 lines (271 loc) · 8.63 KB
/
task.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include <stdarg.h>
#include "task.h"
extern pool_t *pending_tasks;
extern pool_t *sig_ready_tasks;
extern pool_t *non_sig_ready_tasks;
extern pool_t *executing_tasks;
extern pool_t *finished_tasks;
pthread_mutex_t global_lock;
int cmp_tasks(void *args1, void *args2){
task_t *t1 = args1;
task_t *t2 = args2;
if(t1->task_id == t2->task_id)
return 1;
return 0;
}
// Task descriptor allocator
// I am just filling the descriptor's variables values.
task_t* new_task(void (*exec)(void *), void *args, unsigned int size_args ,int (*san)(void *, void *),
void *san_args, unsigned int san_size_args , unsigned char sig, unsigned int redo){
static int id = 0;
task_t *new = (task_t *) malloc (sizeof(task_t));
assert(new);
new->task_id = id++;
new->execution_id = -1;
new->execution_args = args;
new->execution = exec;
new->sanity_func = san;
new->sanity_args = san_args;
new->redo = redo;
new->executed_times = 0;
new->significance = sig;
new->num_in = 0;
new->inputs = NULL;
new->num_out = 0;
new->outputs = NULL;
new->my_group = NULL;
#ifdef DEPENDENCIES
#warning compilin with dependencies turned on
new->depend_on = create_pool(); // this can be commented out
new->dependent_tasks = create_pool(); // this can be commented out
new->dependencies = 0;
#endif
return new;
}
void print_id(void *args){
task_t *me = (task_t*) args ;
printf("%d ",me->task_id);
}
#ifdef DEPENDENCIES
void define_in_dependencies(task_t* task, int number, ...){
va_list args;
int i;
if(number == 0)
return;
task->inputs = (d_t*) malloc (sizeof(d_t)*number);
assert(task->inputs);
task->num_in = number;
va_start(args,number);
for(i = 0 ; i < number ; i++){
task->inputs[i].start = va_arg(args,void *);
}
for(i = 0 ; i < number ; i++){
task->inputs[i].size = va_arg(args,int);
}
// printf("Define Input dependencies of task %d\n",task->task_id);
// for(i = 0 ; i < number ; i++)
// printf("Addr : %p , size %d\n",task->inputs[i].start,task->inputs[i].size );
// printf("Define Input dependencies of task %d\n",task->task_id);
}
void define_out_dependencies(task_t* task, int number, ...){
va_list args;
int i;
if(number == 0)
return;
task->outputs = (d_t*) malloc (sizeof(d_t)*number);
assert(task->outputs);
task->num_out = number;
va_start(args,number);
for(i = 0 ; i < number ; i++){
task->outputs[i].start = va_arg(args,void *);
}
for(i = 0 ; i < number ; i++){
task->outputs[i].size = va_arg(args,int);
}
// printf("Define Ouput dependencies of task %d\n",task->task_id);
// for(i = 0 ; i < number ; i++)
// printf("Addr : %p , size %u\n",task->outputs[i].start,task->outputs[i].size );
// printf("~Define Ouput dependencies of task %d~\n",task->task_id);
}
void dependent(void *m1, void *m2){
task_t *task1 = (task_t *) m1;
task_t *task2 = (task_t *) m2;
unsigned long start1 ,end1 , start2,end2;
// printf("task id1 %d, task id2 %d\n",task1->task_id,task2->task_id);
int i,j;
for( i = 0 ; i < task1->num_in; i++){
start1 = (unsigned long ) task1->inputs[i].start;
end1 = start1 +task1->inputs[i].size;
for( j = 0 ; j < task2->num_out ; j++){
start2 = (unsigned long ) task2->outputs[j].start;
end2 = start2 +task2->outputs[j].size;
// printf("%ld --- %ld\n",start1,end1);
// printf("%ld --- %ld\n",start2,end2);
if(start1 <= start2 && end1 > start2 ){
add_pool_head(task1->depend_on,task2);
add_pool_head(task2->dependent_tasks,task1);
task1->dependencies++;
return;
}
else if(start1 > start2 && start1 < end2 ){
add_pool_head(task1->depend_on,task2);
add_pool_head(task2->dependent_tasks,task1);
task1->dependencies++;
return;
}
}
}
for( i = 0 ; i < task1->num_out; i++){
start1 =(unsigned long ) task1->outputs[i].start;
end1 = start1 +task1->outputs[i].size;
for( j = 0 ; j < task2->num_in ; j++){
start2 =(unsigned long ) task2->inputs[j].start;
end2 = start2 +task2->inputs[j].size;
if(start1<= start2 && end1 > start2 ){
add_pool_head(task1->depend_on,task2);
add_pool_head(task2->dependent_tasks,task1);
task1->dependencies++;
return;
}
else if(start1 > start2 && start1 < end2 ){
add_pool_head(task1->depend_on,task2);
add_pool_head(task2->dependent_tasks,task1);
task1->dependencies++;
return;
}
}
}
for( i = 0 ; i < task1->num_out; i++){
start1 = (unsigned long ) task1->outputs[i].start;
end1 = start1 +task1->outputs[i].size;
for( j = 0 ; j < task2->num_out ; j++){
start2 = (unsigned long ) task2->outputs[j].start;
end2 = start2 +task2->outputs[j].size;
if(start1<= start2 && end1 > start2 ){
add_pool_head(task1->depend_on,task2);
add_pool_head(task2->dependent_tasks,task1);
task1->dependencies++;
return;
}
else if(start1 > start2 && start1 < end2 ){
add_pool_head(task1->depend_on,task2);
add_pool_head(task2->dependent_tasks,task1);
task1->dependencies++;
return;
}
}
}
return;
}
void print_dependecies(task_t *task){
printf("%d ->( ",task->task_id);
exec_on_elem(task->depend_on,print_id);
printf(")\n");
}
void remove_dependency(void *removed, void *dependent){
task_t *rmv = (task_t *) removed;
task_t *dpnt = (task_t *) dependent;
delete_element(dpnt->depend_on,cmp_tasks,rmv);
dpnt->dependencies--;
if(dpnt->dependencies == 0){
delete_element(pending_tasks,cmp_tasks, dpnt);
if(dpnt->significance == 0)
add_pool_tail(non_sig_ready_tasks,dpnt);
else
add_pool_tail(sig_ready_tasks,dpnt);
}
return ;
}
#endif
void push_task(task_t *task, char *name){
group_t *group;
pool_t *temp;
// pthread_mutex_lock(&global_lock);
// Here I am pushing the task into the correct q
#ifdef DEPENDENCIES
exec_on_elem_targs(pending_tasks,dependent,task);
exec_on_elem_targs(sig_ready_tasks,dependent,task);
exec_on_elem_targs(non_sig_ready_tasks,dependent,task);
#endif
if(name == NULL){
pthread_mutex_lock(&pending_tasks->lock);
add_pool_head(pending_tasks,task);
pthread_mutex_unlock(&pending_tasks->lock);
return;
}
group = create_group(name);
task->my_group = group;
if(task->significance == 1)
group->total_sig_tasks++;
if(task->significance == 0)
group->total_non_sig_tasks++;
group->pending_num++;
if(task->significance == 0)
temp= non_sig_ready_tasks;
else
temp= sig_ready_tasks;
#ifdef DEPENDENCIES
if(task->dependencies == 0){
pthread_mutex_lock(&temp->lock);
add_pool_tail(temp,task);
pthread_mutex_unlock(&temp->lock);
}
else{
pthread_mutex_lock(&pending_tasks->lock);
add_pool_head(pending_tasks,task); // I am pushing it into the pending q.
pthread_mutex_unlock(&pending_tasks->lock);
}
#else
pthread_mutex_lock(&temp->lock);
add_pool_tail(temp,task);
pthread_mutex_unlock(&temp->lock);
#endif
pthread_mutex_lock(&group->pending_q->lock);
add_pool_head(group->pending_q,task);
pthread_mutex_unlock(&group->pending_q->lock);
#ifdef DEBUG
printf("Pushing Task %s%d\n",name,task->task_id);
#endif
// pthread_mutex_unlock(&global_lock);
}
void finished_task(task_t* task){
task_t *elem;
#ifdef DEBUG
printf("Finished Task %s%d\n",task->my_group->name,task->task_id);
#endif
pthread_mutex_lock(&task->my_group->executing_q->lock);
elem = delete_element(task->my_group->executing_q,cmp_tasks,task);
if(!elem)
printf("Something went wrong\n");
// Here I am pushing tasks in the head.
// When re-executing the entire group
// I will need to move the elemets from the tail
// of the finished_q to the head of the pending_q.
add_pool_head(task->my_group->finished_q, task);
task->my_group->executing_num--;
if(task->significance == 0)
task->my_group->finished_non_sig_num++;
else
task->my_group->finished_sig_num++;
// Lets move pending_tasks to the ready q
#ifdef DEPENDENCIES
exec_on_elem_targs(task->dependent_tasks,remove_dependency,task);
#endif
pthread_mutex_unlock(&task->my_group->executing_q->lock);
explicit_sync(task->my_group);
}
void move_q(task_t *task){
if(!task)
return;
pthread_mutex_lock(&task->my_group->pending_q->lock);
delete_element(task->my_group->pending_q,cmp_tasks,task);
pthread_mutex_unlock(&task->my_group->pending_q->lock);
pthread_mutex_lock(&task->my_group->executing_q->lock);
add_pool_head(task->my_group->executing_q,task);
task->my_group->executing_num++;
pthread_mutex_unlock(&task->my_group->executing_q->lock);
}