| 9 |
|
*/ |
| 10 |
|
|
| 11 |
|
#include "rholo.h" |
| 12 |
< |
#include "paths.h" |
| 12 |
> |
#include "random.h" |
| 13 |
|
#include <signal.h> |
| 14 |
|
#include <sys/types.h> |
| 15 |
|
#include <sys/stat.h> |
| 21 |
|
|
| 22 |
|
char *progname; /* our program name */ |
| 23 |
|
char *hdkfile; /* holodeck file name */ |
| 24 |
< |
char froot[MAXPATH]; /* root file name */ |
| 24 |
> |
char froot[256]; /* root file name */ |
| 25 |
|
|
| 26 |
|
int nowarn = 0; /* turn warnings off? */ |
| 27 |
|
|
| 28 |
– |
double expval = 1.; /* global exposure value */ |
| 29 |
– |
|
| 28 |
|
int ncprocs = 0; /* desired number of compute processes */ |
| 29 |
|
|
| 30 |
|
char *outdev = NULL; /* output device name */ |
| 31 |
|
|
| 32 |
+ |
int readinp = 0; /* read commands from stdin */ |
| 33 |
+ |
|
| 34 |
+ |
int force = 0; /* allow overwrite of holodeck */ |
| 35 |
+ |
|
| 36 |
|
time_t starttime; /* time we got started */ |
| 37 |
|
time_t endtime; /* time we should end by */ |
| 38 |
|
time_t reporttime; /* time for next report */ |
| 60 |
|
{ |
| 61 |
|
HDGRID hdg; |
| 62 |
|
int i; |
| 61 |
– |
int force = 0; |
| 63 |
|
/* mark start time */ |
| 64 |
|
starttime = time(NULL); |
| 65 |
+ |
initurand(16384); /* initialize urand */ |
| 66 |
|
progname = argv[0]; /* get arguments */ |
| 67 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
| 68 |
|
switch (argv[i][1]) { |
| 72 |
|
case 'f': /* force overwrite */ |
| 73 |
|
force++; |
| 74 |
|
break; |
| 75 |
+ |
case 'i': /* read input from stdin */ |
| 76 |
+ |
readinp++; |
| 77 |
+ |
break; |
| 78 |
|
case 'n': /* compute processes */ |
| 79 |
|
if (i >= argc-2) |
| 80 |
|
goto userr; |
| 106 |
|
/* check settings */ |
| 107 |
|
checkvalues(); |
| 108 |
|
/* load RIF if any */ |
| 109 |
< |
if (vdef(RIF)) |
| 105 |
< |
getradfile(vval(RIF)); |
| 109 |
> |
getradfile(); |
| 110 |
|
/* set defaults */ |
| 111 |
|
setdefaults(&hdg); |
| 112 |
|
/* holodeck exists? */ |
| 117 |
|
creatholo(&hdg); |
| 118 |
|
} else { /* else load holodeck */ |
| 119 |
|
loadholo(); |
| 120 |
< |
if (vdef(RIF)) /* load RIF if any */ |
| 121 |
< |
getradfile(vval(RIF)); |
| 120 |
> |
/* check settings */ |
| 121 |
> |
checkvalues(); |
| 122 |
> |
/* load RIF if any */ |
| 123 |
> |
getradfile(); |
| 124 |
> |
/* set defaults */ |
| 125 |
> |
setdefaults(NULL); |
| 126 |
|
} |
| 127 |
|
/* initialize */ |
| 128 |
|
initrholo(); |
| 129 |
< |
/* run */ |
| 129 |
> |
/* main loop */ |
| 130 |
|
while (rholo()) |
| 131 |
|
; |
| 132 |
|
/* done */ |
| 190 |
|
{ |
| 191 |
|
extern int global_packet(); |
| 192 |
|
register int i; |
| 193 |
+ |
/* close holodeck on exec() */ |
| 194 |
+ |
fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC); |
| 195 |
|
|
| 196 |
|
if (outdev != NULL) /* open output device */ |
| 197 |
|
disp_open(outdev); |
| 206 |
|
if (!vdef(TIME) || vflt(TIME) <= FTINY) |
| 207 |
|
endtime = 0; |
| 208 |
|
else |
| 209 |
< |
endtime = starttime + vflt(TIME)*3600.; |
| 209 |
> |
endtime = starttime + vflt(TIME)*3600. + .5; |
| 210 |
|
/* set up memory cache */ |
| 211 |
|
if (outdev == NULL) |
| 212 |
|
hdcachesize = 0; /* manual flushing */ |
| 222 |
|
if (ncprocs > 0) { |
| 223 |
|
i = start_rtrace(); |
| 224 |
|
if (i < 1) |
| 225 |
< |
error(USER, "cannot start rtrace process"); |
| 225 |
> |
error(USER, "cannot start rtrace process(es)"); |
| 226 |
|
if (vdef(REPORT)) { /* make first report */ |
| 227 |
|
printargs(rtargc, rtargv, stderr); |
| 228 |
|
report(0); |
| 277 |
|
if (!disp_check(idle)) |
| 278 |
|
return(0); |
| 279 |
|
/* display only? */ |
| 280 |
< |
if (ncprocs <= 0) |
| 281 |
< |
return(1); |
| 280 |
> |
if (nprocs <= 0) |
| 281 |
> |
return(outdev != NULL); |
| 282 |
|
/* check file size */ |
| 283 |
|
if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { |
| 284 |
|
error(WARNING, "file limit exceeded"); |
| 285 |
< |
return(0); |
| 285 |
> |
done_rtrace(); |
| 286 |
> |
idle = 1; |
| 287 |
> |
return(1); /* comes back */ |
| 288 |
|
} |
| 289 |
|
/* check time */ |
| 290 |
|
if (endtime > 0 || reporttime > 0) |
| 291 |
|
t = time(NULL); |
| 292 |
|
if (endtime > 0 && t >= endtime) { |
| 293 |
|
error(WARNING, "time limit exceeded"); |
| 294 |
< |
return(0); |
| 294 |
> |
done_rtrace(); |
| 295 |
> |
idle = 1; |
| 296 |
> |
return(1); /* comes back */ |
| 297 |
|
} |
| 298 |
|
if (reporttime > 0 && t >= reporttime) |
| 299 |
|
report(t); |
| 302 |
|
p = freepacks; freepacks = p->next; p->next = NULL; |
| 303 |
|
if (!next_packet(p)) { |
| 304 |
|
p->next = freepacks; freepacks = p; |
| 305 |
+ |
idle = 1; |
| 306 |
|
break; |
| 307 |
|
} |
| 308 |
|
if (pl == NULL) pl = p; |
| 309 |
|
else plend->next = p; |
| 310 |
|
plend = p; |
| 311 |
|
} |
| 312 |
< |
idle = pl == NULL && freepacks != NULL; |
| 298 |
< |
/* are we out of stuff to do? */ |
| 299 |
< |
if (idle && outdev == NULL) |
| 300 |
< |
return(0); |
| 301 |
< |
/* else process packets */ |
| 312 |
> |
/* process packets */ |
| 313 |
|
done_packets(do_packets(pl)); |
| 314 |
|
return(1); /* and continue */ |
| 315 |
|
} |
| 316 |
|
|
| 317 |
|
|
| 307 |
– |
report(t) /* report progress so far */ |
| 308 |
– |
time_t t; |
| 309 |
– |
{ |
| 310 |
– |
if (t == 0) |
| 311 |
– |
t = time(NULL); |
| 312 |
– |
fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n", |
| 313 |
– |
progname, npacksdone, nraysdone, (t-starttime)/3600.); |
| 314 |
– |
fflush(stderr); |
| 315 |
– |
if (vdef(REPORT)) |
| 316 |
– |
reporttime = t + (time_t)(vflt(REPORT)*60.+.5); |
| 317 |
– |
} |
| 318 |
– |
|
| 319 |
– |
|
| 318 |
|
setdefaults(gp) /* set default values */ |
| 319 |
|
register HDGRID *gp; |
| 320 |
|
{ |
| 331 |
|
sprintf(errmsg, "ignoring all but first %s", vnam(SECTION)); |
| 332 |
|
error(WARNING, errmsg); |
| 333 |
|
} |
| 336 |
– |
if (sscanf(vval(SECTION), |
| 337 |
– |
"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", |
| 338 |
– |
&gp->orig[0], &gp->orig[1], &gp->orig[2], |
| 339 |
– |
&gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], |
| 340 |
– |
&gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], |
| 341 |
– |
&gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12) |
| 342 |
– |
badvalue(SECTION); |
| 343 |
– |
maxlen = 0.; |
| 344 |
– |
for (i = 0; i < 3; i++) |
| 345 |
– |
if ((len[i] = VLEN(gp->xv[i])) > maxlen) |
| 346 |
– |
maxlen = len[i]; |
| 347 |
– |
if (!vdef(GRID)) { |
| 348 |
– |
sprintf(buf, "%.4f", maxlen/8.); |
| 349 |
– |
vval(GRID) = savqstr(buf); |
| 350 |
– |
vdef(GRID)++; |
| 351 |
– |
} |
| 352 |
– |
if ((d = vflt(GRID)) <= FTINY) |
| 353 |
– |
badvalue(GRID); |
| 354 |
– |
for (i = 0; i < 3; i++) |
| 355 |
– |
gp->grid[i] = len[i]/d + (1.-FTINY); |
| 356 |
– |
if (!vdef(EXPOSURE)) { |
| 357 |
– |
sprintf(errmsg, "%s must be defined", vnam(EXPOSURE)); |
| 358 |
– |
error(USER, errmsg); |
| 359 |
– |
} |
| 360 |
– |
expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ? |
| 361 |
– |
pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE); |
| 334 |
|
if (!vdef(OCTREE)) { |
| 335 |
|
if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) |
| 336 |
|
error(SYSTEM, "out of memory"); |
| 341 |
|
vval(OBSTRUCTIONS) = "T"; |
| 342 |
|
vdef(OBSTRUCTIONS)++; |
| 343 |
|
} |
| 344 |
+ |
if (!vdef(VDIST)) { |
| 345 |
+ |
vval(VDIST) = "F"; |
| 346 |
+ |
vdef(VDIST)++; |
| 347 |
+ |
} |
| 348 |
|
if (!vdef(OCCUPANCY)) { |
| 349 |
|
vval(OCCUPANCY) = "U"; |
| 350 |
|
vdef(OCCUPANCY)++; |
| 352 |
|
/* append rendering options */ |
| 353 |
|
if (vdef(RENDER)) |
| 354 |
|
rtargc += wordstring(rtargv+rtargc, vval(RENDER)); |
| 355 |
+ |
|
| 356 |
+ |
if (gp == NULL) /* already initialized? */ |
| 357 |
+ |
return; |
| 358 |
+ |
/* set grid parameters */ |
| 359 |
+ |
gp->grid[0] = gp->grid[1] = gp->grid[2] = 0; |
| 360 |
+ |
if (sscanf(vval(SECTION), |
| 361 |
+ |
"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd", |
| 362 |
+ |
&gp->orig[0], &gp->orig[1], &gp->orig[2], |
| 363 |
+ |
&gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], |
| 364 |
+ |
&gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], |
| 365 |
+ |
&gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2], |
| 366 |
+ |
&gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12) |
| 367 |
+ |
badvalue(SECTION); |
| 368 |
+ |
maxlen = 0.; |
| 369 |
+ |
for (i = 0; i < 3; i++) |
| 370 |
+ |
if ((len[i] = VLEN(gp->xv[i])) > maxlen) |
| 371 |
+ |
maxlen = len[i]; |
| 372 |
+ |
if (!vdef(GRID)) { |
| 373 |
+ |
sprintf(buf, "%.4f", maxlen/8.); |
| 374 |
+ |
vval(GRID) = savqstr(buf); |
| 375 |
+ |
vdef(GRID)++; |
| 376 |
+ |
} |
| 377 |
+ |
if ((d = vflt(GRID)) <= FTINY) |
| 378 |
+ |
badvalue(GRID); |
| 379 |
+ |
for (i = 0; i < 3; i++) |
| 380 |
+ |
if (gp->grid[i] <= 0) |
| 381 |
+ |
gp->grid[i] = len[i]/d + (1.-FTINY); |
| 382 |
|
} |
| 383 |
|
|
| 384 |
|
|
| 486 |
|
p->next = freepacks; |
| 487 |
|
freepacks = p; |
| 488 |
|
} |
| 489 |
< |
if (n2flush > 512*RPACKSIZ*ncprocs) { |
| 489 |
> |
if (n2flush > 512*RPACKSIZ*nprocs) { |
| 490 |
|
hdflush(NULL); /* flush holodeck buffers */ |
| 491 |
|
n2flush = 0; |
| 492 |
|
} |
| 493 |
|
} |
| 494 |
|
|
| 495 |
|
|
| 493 |
– |
getradfile(rfargs) /* run rad and get needed variables */ |
| 494 |
– |
char *rfargs; |
| 495 |
– |
{ |
| 496 |
– |
static short mvar[] = {OCTREE,EXPOSURE,-1}; |
| 497 |
– |
static char tf1[] = TEMPLATE; |
| 498 |
– |
char tf2[64]; |
| 499 |
– |
char combuf[256]; |
| 500 |
– |
char *pippt; |
| 501 |
– |
register int i; |
| 502 |
– |
register char *cp; |
| 503 |
– |
/* create rad command */ |
| 504 |
– |
mktemp(tf1); |
| 505 |
– |
sprintf(tf2, "%s.rif", tf1); |
| 506 |
– |
sprintf(combuf, |
| 507 |
– |
"rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH", |
| 508 |
– |
rfargs, tf1); |
| 509 |
– |
cp = combuf; |
| 510 |
– |
while (*cp){ |
| 511 |
– |
if (*cp == '|') pippt = cp; |
| 512 |
– |
cp++; |
| 513 |
– |
} /* match unset variables */ |
| 514 |
– |
for (i = 0; mvar[i] >= 0; i++) |
| 515 |
– |
if (!vdef(mvar[i])) { |
| 516 |
– |
*cp++ = '|'; |
| 517 |
– |
strcpy(cp, vnam(mvar[i])); |
| 518 |
– |
while (*cp) cp++; |
| 519 |
– |
pippt = NULL; |
| 520 |
– |
} |
| 521 |
– |
if (pippt != NULL) |
| 522 |
– |
strcpy(pippt, "> /dev/null"); /* nothing to match */ |
| 523 |
– |
else |
| 524 |
– |
sprintf(cp, ")[ \t]*=' > %s", tf2); |
| 525 |
– |
if (system(combuf)) { |
| 526 |
– |
error(SYSTEM, "cannot execute rad command"); |
| 527 |
– |
unlink(tf2); /* clean up */ |
| 528 |
– |
unlink(tf1); |
| 529 |
– |
quit(1); |
| 530 |
– |
} |
| 531 |
– |
if (pippt == NULL) { |
| 532 |
– |
loadvars(tf2); /* load variables */ |
| 533 |
– |
unlink(tf2); |
| 534 |
– |
} |
| 535 |
– |
rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */ |
| 536 |
– |
unlink(tf1); /* clean up */ |
| 537 |
– |
} |
| 538 |
– |
|
| 539 |
– |
|
| 496 |
|
rootname(rn, fn) /* remove tail from end of fn */ |
| 497 |
|
register char *rn, *fn; |
| 498 |
|
{ |
| 499 |
|
char *tp, *dp; |
| 500 |
|
|
| 501 |
|
for (tp = NULL, dp = rn; *rn = *fn++; rn++) |
| 502 |
< |
if (ISDIRSEP(*rn)) |
| 502 |
> |
if (*rn == '/') |
| 503 |
|
dp = rn; |
| 504 |
|
else if (*rn == '.') |
| 505 |
|
tp = rn; |
| 549 |
|
int status = 0; |
| 550 |
|
|
| 551 |
|
if (hdlist[0] != NULL) { /* flush holodeck */ |
| 552 |
< |
if (ncprocs > 0) { |
| 553 |
< |
done_packets(flush_queue()); |
| 554 |
< |
status = end_rtrace(); /* close rtrace */ |
| 555 |
< |
hdflush(NULL); |
| 556 |
< |
if (vdef(REPORT)) { |
| 557 |
< |
long fsiz, fuse; |
| 558 |
< |
report(0); |
| 559 |
< |
fsiz = hdfilen(hdlist[0]->fd); |
| 604 |
< |
fuse = hdfiluse(hdlist[0]->fd, 1); |
| 605 |
< |
fprintf(stderr, |
| 552 |
> |
if (nprocs > 0) |
| 553 |
> |
status = done_rtrace(); |
| 554 |
> |
hdflush(NULL); |
| 555 |
> |
if (ncprocs > 0 && vdef(REPORT)) { |
| 556 |
> |
long fsiz, fuse; |
| 557 |
> |
fsiz = hdfilen(hdlist[0]->fd); |
| 558 |
> |
fuse = hdfiluse(hdlist[0]->fd, 1); |
| 559 |
> |
fprintf(stderr, |
| 560 |
|
"%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", |
| 561 |
< |
hdkfile, fsiz/(1024.*1024.), |
| 562 |
< |
100.*(fsiz-fuse)/fsiz); |
| 563 |
< |
} |
| 610 |
< |
} else |
| 611 |
< |
hdflush(NULL); |
| 561 |
> |
hdkfile, fsiz/(1024.*1024.), |
| 562 |
> |
100.*(fsiz-fuse)/fsiz); |
| 563 |
> |
} |
| 564 |
|
} |
| 565 |
|
if (orig_mode >= 0) /* reset holodeck access mode */ |
| 566 |
|
fchmod(hdlist[0]->fd, orig_mode); |