-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsp.c
74 lines (63 loc) · 1.28 KB
/
tsp.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
#include <u.h>
#include <libc.h>
void
usage(char *argv0)
{
sysfatal("usage:\n\t%s size datafile cpu [cpu ...]", argv0);
}
unsigned long long
factorial(unsigned long long n)
{
if (n > 1)
return n * factorial(n-1);
return 1;
}
void
main(int argc, char **argv)
{
int ncpu;
unsigned long long npaths;
unsigned long long parts;
int i, r, d;
char start[64];
char chunksize[64];
char **buf;
int **pfds;
double shortest;
double f;
if (argc < 4)
usage(argv[0]);
ncpu = argc - 3;
npaths = factorial(atoll(argv[1]));
parts = npaths / ncpu;
pfds = malloc(ncpu * sizeof(int*));
shortest = 1000000000.0;
for (i = 0; i < ncpu; i++) {
pfds[i] = malloc(2 * sizeof(int));
pipe(pfds[i]);
close(1);
dup(pfds[i][1], 1);
r = fork();
if (r == -1)
sysfatal("fork: %r");
if (r == 0) {
snprint(chunksize, 63, "%llud", parts);
snprint(start, 63, "%llud", parts * i);
execl("/bin/rcpu", "rcpu", "-h", argv[i+3], "-c", "worker", argv[1], argv[2], chunksize, start, nil);
}
}
buf = malloc(sizeof(char*) * ncpu);
d = 0;
for (i = 0; i < ncpu; i++) {
buf[i] = malloc(256 * sizeof(char));
r = read(pfds[i][0], buf[i], 255);
buf[i][r] = '\0';
f = strtod(buf[i], nil);
if (f < shortest) {
shortest = f;
d = i;
}
waitpid();
}
fprint(2, "%s\n", buf[d]);
}