-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp_test.c
205 lines (166 loc) · 4.15 KB
/
mp_test.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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "thread.h"
#include "cpu_affinity.h"
#include "uthread.h"
__attribute__ ((noinline)) int dribble(int ncount) {
float neg, a1, a2, a3, a4;
a1 = 0;
a2 = 0;
a3 = 0;
a4 = 0;
neg = -1;
for (int i = 1; i < ncount; i++) {
a1 += neg + (float)i * 1.0/((2.0*(float)i)-1) + a2;
a2 += neg + (float)i * 1.0/((2.0*(float)i)-1) + a3;
a3 += neg + (float)i * 1.0/((2.0*(float)i)-1) + a4;
a4 += neg + (float)i * 1.0/((2.0*(float)i)-1) + a1;
neg = -i % 100 - a1;
}
return (int) a1 + 1;
}
void task(int idx, int fdwrite) {
// printf(1, "writing from %d\r\n", idx);
int out = dribble(1e7);
write(fdwrite, &out, 1);
}
void *thread_task(int* out) {
// printf(1, "writing from %d\r\n", *out);
int out_ = dribble(1e7);
*out = out_;
exit();
}
void *thread_task_with_affinity(int* out) {
cpu_set_t affinity;
CPU_ZERO(&affinity);
CPU_SET(0, &affinity);
sched_setaffinity(&affinity);
return thread_task(out);
}
int sequential(int ntasks) {
int fds[2];
pipe(fds);
int buf = 0;
int out = 0;
for (int idx = 0; idx < ntasks; idx++) {
task(idx, fds[1]);
}
for (int idx = 0; idx < ntasks; idx++) {
read(fds[0], &buf, 1);
out += buf;
}
return out;
}
int withprocesses(int ntasks) {
int fds[2];
pipe(fds);
int buf = 0;
int out = 0;
for (int idx = 0; idx < ntasks; idx++) {
if (fork() == 0) {
close(fds[0]);
task(idx, fds[1]);
exit();
}
}
for (int idx = 0; idx < ntasks; idx++) {
if (wait() == -1) {
printf(1, "We have only %d/%d loyal children\n", idx + 1, ntasks);
break;
}
read(fds[0], &buf, 1);
out += buf;
}
return out;
}
int withthreads(int ntasks) {
Thread** threads = (Thread**) malloc(sizeof(Thread*) * ntasks);
int* out = (int*) malloc((sizeof(int)) * ntasks);
for (int i = 0; i < ntasks; ++i) {
threads[i] = thread_create(thread_task, &out[i]);
}
for (int i = 0; i < ntasks; ++i) {
thread_join(threads[i]);
}
int res = 0;
for (int i = 0; i < ntasks; ++i) {
res += out[i];
}
return res;
}
int withconcurency(int ntasks) {
Thread** threads = (Thread**) malloc(sizeof(Thread*) * ntasks);
int* out = (int*) malloc((sizeof(int)) * ntasks);
for (int i = 0; i < ntasks; ++i) {
threads[i] = thread_create(thread_task_with_affinity, &out[i]);
}
for (int i = 0; i < ntasks; ++i) {
thread_join(threads[i]);
}
int res = 0;
for (int i = 0; i < ntasks; ++i) {
res += out[i];
}
return res;
}
void test(UThreadManager *utm, Task* t, int id) {
printf(1, "test is started, thread id = %d\n", id);
uthread_yield(utm, t);
printf(1, "test is working, thread id = %d\n", id);
}
int main() {
// int starttime, duration;
// int ntasks = 2;
//
// {
// printf(1, "-- With threading :)\n");
//
// starttime = uptime();
// int threaded_out = withthreads(ntasks);
// duration = uptime() - starttime;
//
// printf(1, "result = %d in %d time units\r\n", threaded_out, duration);
// }
//
// {
// printf(1, "-- With concurrency on 1 CPU :)\n");
//
// starttime = uptime();
// int threaded_out = withconcurency(ntasks);
// duration = uptime() - starttime;
//
// printf(1, "result = %d in %d time units\r\n", threaded_out, duration);
// }
//
// {
// printf(1, "-- With multiprocessing :)\n");
//
// starttime = uptime();
// int multi_out = withprocesses(ntasks);
// duration = uptime() - starttime;
//
// printf(1, "result = %d in %d time units\r\n", multi_out, duration);
// }
//
// {
// printf(1, "-- Sequential :(\n");
//
// starttime = uptime();
// int seq_out = sequential(ntasks);
// duration = uptime() - starttime;
//
// printf(1, "result = %d in %d time units\r\n", seq_out, duration);
// }
printf(1, "Started successfully\n");
int n_workers = 1;
int n_tasks = 2;
UThreadManager *utm = uthread_create(n_workers, n_tasks);
printf(1, "utm.n_run_tasks: %d\n", utm->n_run_tasks);
uthread_add_task(utm, test, (void*) 0);
uthread_add_task(utm, test, (void*) 1);
uthread_join(utm);
printf(1, "utm.n_run_tasks: %d\n", utm->n_run_tasks);
printf(1, "Finished successfully\n");
exit();
}