9 |
|
#include <ctype.h> |
10 |
|
|
11 |
|
#include "platform.h" |
12 |
+ |
#include "rtprocess.h" |
13 |
|
#include "mkillum.h" |
14 |
|
#include "random.h" |
15 |
|
|
32 |
|
char *myrtopts[] = { "-I-", "-i-", "-ld-", "-ov", "-h-", |
33 |
|
"-fff", "-y", "0", NULL }; |
34 |
|
|
35 |
< |
struct rtproc rt; /* our rtrace process */ |
35 |
> |
struct rtproc rt0; /* head of rtrace process list */ |
36 |
|
|
37 |
|
struct illum_args thisillum = { /* our illum and default values */ |
38 |
|
0, |
50 |
|
|
51 |
|
FUN ofun[NUMOTYPE] = INIT_OTYPE; /* object types */ |
52 |
|
|
53 |
+ |
char persistfn[] = "pfXXXXXX"; /* persist file name */ |
54 |
+ |
|
55 |
|
int gargc; /* global argc */ |
56 |
|
char **gargv; /* global argv */ |
57 |
|
#define progname gargv[0] |
61 |
|
|
62 |
|
int warnings = 1; /* print warnings? */ |
63 |
|
|
64 |
< |
void init(void); |
64 |
> |
void init(int np); |
65 |
|
void filter(register FILE *infp, char *name); |
66 |
|
void xoptions(char *s, char *nm); |
67 |
|
void printopts(void); |
75 |
|
char *argv[] |
76 |
|
) |
77 |
|
{ |
78 |
+ |
int nprocs = 1; |
79 |
|
char *rtpath; |
80 |
|
FILE *fp; |
81 |
|
register int i; |
82 |
|
/* set global arguments */ |
83 |
|
gargv = argv; |
84 |
+ |
/* check for -n option */ |
85 |
+ |
if (!strcmp(argv[1], "-n")) { |
86 |
+ |
nprocs = atoi(argv[2]); |
87 |
+ |
if (nprocs <= 0) |
88 |
+ |
error(USER, "illegal number of processes"); |
89 |
+ |
i = 3; |
90 |
+ |
} else |
91 |
+ |
i = 1; |
92 |
|
/* set up rtrace command */ |
93 |
< |
for (i = 1; i < argc; i++) { |
93 |
> |
for ( ; i < argc; i++) { |
94 |
|
if (argv[i][0] == '<' && argv[i][1] == '\0') |
95 |
|
break; |
96 |
|
rtargv[rtargc++] = argv[i]; |
114 |
|
} |
115 |
|
} |
116 |
|
gargc = i; |
117 |
< |
rtargc--; |
117 |
> |
if (!strcmp(rtargv[--rtargc], "-defaults")) |
118 |
> |
nprocs = 0; |
119 |
> |
if (nprocs > 1) { /* add persist file if parallel invocation */ |
120 |
> |
rtargv[rtargc++] = "-PP"; |
121 |
> |
rtargv[rtargc++] = mktemp(persistfn); |
122 |
> |
} |
123 |
> |
/* add "mandatory" rtrace options */ |
124 |
|
for (i = 0; myrtopts[i] != NULL; i++) |
125 |
|
rtargv[rtargc++] = myrtopts[i]; |
126 |
+ |
/* finally, put back final argument */ |
127 |
|
rtargv[rtargc++] = argv[gargc-1]; |
128 |
|
rtargv[rtargc] = NULL; |
129 |
< |
/* just asking for defaults? */ |
111 |
< |
if (!strcmp(argv[gargc-1], "-defaults")) { |
129 |
> |
if (!nprocs) { /* just asking for defaults? */ |
130 |
|
printopts(); fflush(stdout); |
131 |
|
rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); |
132 |
|
if (rtpath == NULL) { |
135 |
|
exit(1); |
136 |
|
} |
137 |
|
execv(rtpath, rtargv); |
138 |
< |
perror(rtpath); |
138 |
> |
perror(rtpath); /* execv() should not return */ |
139 |
|
exit(1); |
140 |
|
} |
141 |
|
if (gargc < 2 || argv[gargc-1][0] == '-') |
142 |
|
error(USER, "missing octree argument"); |
143 |
|
/* else initialize and run our calculation */ |
144 |
< |
init(); |
144 |
> |
init(nprocs); |
145 |
|
if (gargc+1 < argc) |
146 |
|
for (i = gargc+1; i < argc; i++) { |
147 |
|
if ((fp = fopen(argv[i], "r")) == NULL) { |
154 |
|
} |
155 |
|
else |
156 |
|
filter(stdin, "standard input"); |
157 |
< |
return 0; |
157 |
> |
quit(0); |
158 |
|
} |
159 |
|
|
160 |
+ |
static void |
161 |
+ |
killpersist(void) /* kill persistent rtrace process */ |
162 |
+ |
{ |
163 |
+ |
FILE *fp = fopen(persistfn, "r"); |
164 |
+ |
int pid; |
165 |
|
|
166 |
+ |
if (fp == NULL) |
167 |
+ |
return; |
168 |
+ |
if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0) |
169 |
+ |
unlink(persistfn); |
170 |
+ |
fclose(fp); |
171 |
+ |
} |
172 |
+ |
|
173 |
|
void |
174 |
< |
quit(status) /* exit with status */ |
145 |
< |
int status; |
174 |
> |
quit(int status) /* exit with status */ |
175 |
|
{ |
176 |
+ |
struct rtproc *rtp; |
177 |
|
int rtstat; |
178 |
|
|
179 |
< |
rtstat = close_process(&(rt.pd)); |
180 |
< |
if (status == 0) { |
181 |
< |
if (rtstat < 0) |
182 |
< |
error(WARNING, |
183 |
< |
"unknown return status from rtrace process"); |
184 |
< |
else |
185 |
< |
status = rtstat; |
179 |
> |
if (rt0.next != NULL) /* terminate persistent rtrace */ |
180 |
> |
killpersist(); |
181 |
> |
/* clean up rtrace process(es) */ |
182 |
> |
for (rtp = &rt0; rtp != NULL; rtp = rtp->next) { |
183 |
> |
rtstat = close_process(&rtp->pd); |
184 |
> |
if (status == 0) { |
185 |
> |
if (rtstat < 0) |
186 |
> |
error(WARNING, |
187 |
> |
"unknown return status from rtrace process"); |
188 |
> |
else |
189 |
> |
status = rtstat; |
190 |
> |
} |
191 |
|
} |
192 |
|
exit(status); |
193 |
|
} |
194 |
|
|
195 |
|
void |
196 |
< |
init(void) /* start rtrace and set up buffers */ |
196 |
> |
init(int np) /* start rtrace and set up buffers */ |
197 |
|
{ |
198 |
< |
extern int o_face(), o_sphere(), o_ring(); |
198 |
> |
struct rtproc *rtp; |
199 |
> |
int i; |
200 |
|
int maxbytes; |
201 |
|
/* set up object functions */ |
202 |
|
ofun[OBJ_FACE].funp = o_face; |
206 |
|
#ifdef SIGPIPE /* not present on Windows */ |
207 |
|
signal(SIGPIPE, quit); |
208 |
|
#endif |
209 |
< |
/* start rtrace process */ |
210 |
< |
errno = 0; |
211 |
< |
maxbytes = open_process(&(rt.pd), rtargv); |
212 |
< |
if (maxbytes == 0) { |
213 |
< |
eputs(rtargv[0]); |
214 |
< |
eputs(": command not found\n"); |
215 |
< |
exit(1); |
209 |
> |
rtp = &rt0; /* start rtrace process(es) */ |
210 |
> |
for (i = 0; i++ < np; ) { |
211 |
> |
errno = 0; |
212 |
> |
maxbytes = open_process(&rtp->pd, rtargv); |
213 |
> |
if (maxbytes == 0) { |
214 |
> |
eputs(rtargv[0]); |
215 |
> |
eputs(": command not found\n"); |
216 |
> |
exit(1); |
217 |
> |
} |
218 |
> |
if (maxbytes < 0) |
219 |
> |
error(SYSTEM, "cannot start rtrace process"); |
220 |
> |
if (!i && np > 1) |
221 |
> |
sleep(2); /* wait for persist file */ |
222 |
> |
rtp->bsiz = maxbytes/(6*sizeof(float)); |
223 |
> |
rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--); |
224 |
> |
rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *)); |
225 |
> |
if (rtp->buf == NULL || rtp->dest == NULL) |
226 |
> |
error(SYSTEM, "out of memory in init"); |
227 |
> |
rtp->nrays = 0; |
228 |
> |
if (i == np) /* last process? */ |
229 |
> |
break; |
230 |
> |
rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc)); |
231 |
> |
if (rtp->next == NULL) |
232 |
> |
error(SYSTEM, "out of memory in init"); |
233 |
> |
rtp = rtp->next; |
234 |
|
} |
235 |
< |
if (maxbytes < 0) |
182 |
< |
error(SYSTEM, "cannot start rtrace process"); |
183 |
< |
rt.bsiz = maxbytes/(6*sizeof(float)); |
184 |
< |
rt.buf = (float *)malloc(6*sizeof(float)*rt.bsiz--); |
185 |
< |
rt.dest = (float **)malloc(sizeof(float *)*rt.bsiz); |
186 |
< |
if (rt.buf == NULL || rt.dest == NULL) |
187 |
< |
error(SYSTEM, "out of memory in init"); |
188 |
< |
rt.nrays = 0; |
235 |
> |
rtp->next = NULL; |
236 |
|
/* set up urand */ |
237 |
|
initurand(16384); |
238 |
|
} |
553 |
|
checkhead(); |
554 |
|
/* process object */ |
555 |
|
if (doit) |
556 |
< |
(*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt, nm); |
556 |
> |
(*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm); |
557 |
|
else |
558 |
|
printobj(thisillum.altmat, &thisobj); |
559 |
|
/* free arguments */ |