ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.15
Committed: Tue Dec 2 12:29:17 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.14: +32 -31 lines
Log Message:
removed useless EXPOSURE variable
added setdefaults() call after loading holodeck

File Contents

# Content
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Radiance holodeck generation controller
9 */
10
11 #include "rholo.h"
12 #include "random.h"
13 #include "paths.h"
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17
18 /* the following must be consistent with rholo.h */
19 int NVARS = NRHVARS; /* total number of variables */
20
21 VARIABLE vv[] = RHVINIT; /* variable-value pairs */
22
23 char *progname; /* our program name */
24 char *hdkfile; /* holodeck file name */
25 char froot[MAXPATH]; /* root file name */
26
27 int nowarn = 0; /* turn warnings off? */
28
29 int ncprocs = 0; /* desired number of compute processes */
30
31 char *outdev = NULL; /* output device name */
32
33 time_t starttime; /* time we got started */
34 time_t endtime; /* time we should end by */
35 time_t reporttime; /* time for next report */
36
37 long maxdisk; /* maximum file space (bytes) */
38
39 int rtargc = 1; /* rtrace command */
40 char *rtargv[128] = {"rtrace", NULL};
41
42 int orig_mode = -1; /* original file mode (-1 if unchanged) */
43
44 long nraysdone = 0L; /* number of rays done */
45 long npacksdone = 0L; /* number of packets done */
46
47 PACKET *freepacks; /* available packets */
48
49 char *sigerr[NSIG]; /* signal error messages */
50
51 extern time_t time();
52
53
54 main(argc, argv)
55 int argc;
56 char *argv[];
57 {
58 HDGRID hdg;
59 int i;
60 int force = 0;
61 /* mark start time */
62 starttime = time(NULL);
63 initurand(10240); /* initialize urand */
64 progname = argv[0]; /* get arguments */
65 for (i = 1; i < argc && argv[i][0] == '-'; i++)
66 switch (argv[i][1]) {
67 case 'w': /* turn off warnings */
68 nowarn++;
69 break;
70 case 'f': /* force overwrite */
71 force++;
72 break;
73 case 'n': /* compute processes */
74 if (i >= argc-2)
75 goto userr;
76 ncprocs = atoi(argv[++i]);
77 break;
78 case 'o': /* output display */
79 if (i >= argc-2)
80 goto userr;
81 outdev = argv[++i];
82 break;
83 default:
84 goto userr;
85 }
86 /* do we have a job? */
87 if (outdev == NULL && ncprocs <= 0)
88 goto userr;
89 /* get root file name */
90 rootname(froot, hdkfile=argv[i++]);
91 /* load... */
92 if (i < argc) { /* variables */
93 loadvars(argv[i++]);
94 /* cmdline settings */
95 for ( ; i < argc; i++)
96 if (setvariable(argv[i], matchvar) < 0) {
97 sprintf(errmsg, "unknown variable: %s",
98 argv[i]);
99 error(USER, errmsg);
100 }
101 /* check settings */
102 checkvalues();
103 /* load RIF if any */
104 if (vdef(RIF))
105 getradfile(vval(RIF));
106 /* set defaults */
107 setdefaults(&hdg);
108 /* holodeck exists? */
109 if (!force && access(hdkfile, R_OK|W_OK) == 0)
110 error(USER,
111 "holodeck file exists -- use -f to overwrite");
112 /* create holodeck */
113 creatholo(&hdg);
114 } else { /* else load holodeck */
115 loadholo();
116 /* check settings */
117 checkvalues();
118 /* load RIF if any */
119 if (vdef(RIF))
120 getradfile(vval(RIF));
121 /* set defaults */
122 setdefaults(NULL);
123 }
124 /* initialize */
125 initrholo();
126 /* main loop */
127 while (rholo())
128 ;
129 /* done */
130 quit(0);
131 userr:
132 fprintf(stderr,
133 "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n",
134 progname);
135 quit(1);
136 }
137
138
139 onsig(signo) /* fatal signal */
140 int signo;
141 {
142 static int gotsig = 0;
143
144 if (gotsig++) /* two signals and we're gone! */
145 _exit(signo);
146
147 alarm(30); /* allow 30 seconds to clean up */
148 signal(SIGALRM, SIG_DFL); /* make certain we do die */
149 eputs("signal - ");
150 eputs(sigerr[signo]);
151 eputs("\n");
152 quit(3);
153 }
154
155
156 sigdie(signo, msg) /* set fatal signal */
157 int signo;
158 char *msg;
159 {
160 if (signal(signo, onsig) == SIG_IGN)
161 signal(signo, SIG_IGN);
162 sigerr[signo] = msg;
163 }
164
165
166 int
167 resfmode(fd, mod) /* restrict open file access mode */
168 int fd, mod;
169 {
170 struct stat stbuf;
171 /* get original mode */
172 if (fstat(fd, &stbuf) < 0)
173 error(SYSTEM, "cannot stat open holodeck file");
174 mod &= stbuf.st_mode; /* always more restrictive */
175 if (mod == stbuf.st_mode)
176 return(-1); /* already set */
177 /* else change it */
178 if (fchmod(fd, mod) < 0) {
179 error(WARNING, "cannot change holodeck file access mode");
180 return(-1);
181 }
182 return(stbuf.st_mode); /* return original mode */
183 }
184
185
186 initrholo() /* get our holodeck running */
187 {
188 extern int global_packet();
189 register int i;
190
191 if (outdev != NULL) /* open output device */
192 disp_open(outdev);
193 else if (ncprocs > 0) /* else use global ray feed */
194 init_global();
195 /* record disk space limit */
196 if (!vdef(DISKSPACE))
197 maxdisk = 0;
198 else
199 maxdisk = 1024.*1024.*vflt(DISKSPACE);
200 /* record end time */
201 if (!vdef(TIME) || vflt(TIME) <= FTINY)
202 endtime = 0;
203 else
204 endtime = starttime + vflt(TIME)*3600.;
205 /* set up memory cache */
206 if (outdev == NULL)
207 hdcachesize = 0; /* manual flushing */
208 else if (vdef(CACHE))
209 hdcachesize = 1024.*1024.*vflt(CACHE);
210 /* open report file */
211 if (vdef(REPORT)) {
212 register char *s = sskip2(vval(REPORT), 1);
213 if (*s && freopen(s, "a", stderr) == NULL)
214 quit(2);
215 }
216 /* start rtrace */
217 if (ncprocs > 0) {
218 i = start_rtrace();
219 if (i < 1)
220 error(USER, "cannot start rtrace process");
221 if (vdef(REPORT)) { /* make first report */
222 printargs(rtargc, rtargv, stderr);
223 report(0);
224 }
225 /* allocate packets */
226 freepacks = (PACKET *)bmalloc(i*sizeof(PACKET));
227 if (freepacks == NULL)
228 goto memerr;
229 freepacks[--i].nr = 0;
230 freepacks[i].next = NULL;
231 if (!vbool(OBSTRUCTIONS)) {
232 freepacks[i].offset = (float *)bmalloc(
233 RPACKSIZ*sizeof(float)*(i+1) );
234 if (freepacks[i].offset == NULL)
235 goto memerr;
236 } else
237 freepacks[i].offset = NULL;
238 while (i--) {
239 freepacks[i].nr = 0;
240 freepacks[i].offset = freepacks[i+1].offset == NULL ?
241 NULL : freepacks[i+1].offset+RPACKSIZ ;
242 freepacks[i].next = &freepacks[i+1];
243 }
244 }
245 /* set up signal handling */
246 sigdie(SIGINT, "Interrupt");
247 sigdie(SIGHUP, "Hangup");
248 sigdie(SIGTERM, "Terminate");
249 sigdie(SIGPIPE, "Broken pipe");
250 sigdie(SIGALRM, "Alarm clock");
251 #ifdef SIGXCPU
252 sigdie(SIGXCPU, "CPU limit exceeded");
253 sigdie(SIGXFSZ, "File size exceeded");
254 #endif
255 /* protect holodeck file */
256 orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
257 return;
258 memerr:
259 error(SYSTEM, "out of memory in initrholo");
260 }
261
262
263 rholo() /* holodeck main loop */
264 {
265 static int idle = 1;
266 PACKET *pl = NULL, *plend;
267 register PACKET *p;
268 time_t t;
269 long l;
270
271 if (outdev != NULL) /* check display */
272 if (!disp_check(idle))
273 return(0);
274 /* display only? */
275 if (ncprocs <= 0)
276 return(1);
277 /* check file size */
278 if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
279 error(WARNING, "file limit exceeded");
280 return(0);
281 }
282 /* check time */
283 if (endtime > 0 || reporttime > 0)
284 t = time(NULL);
285 if (endtime > 0 && t >= endtime) {
286 error(WARNING, "time limit exceeded");
287 return(0);
288 }
289 if (reporttime > 0 && t >= reporttime)
290 report(t);
291 /* get packets to process */
292 while (freepacks != NULL) {
293 p = freepacks; freepacks = p->next; p->next = NULL;
294 if (!next_packet(p)) {
295 p->next = freepacks; freepacks = p;
296 break;
297 }
298 if (pl == NULL) pl = p;
299 else plend->next = p;
300 plend = p;
301 }
302 idle = pl == NULL && freepacks != NULL;
303 /* are we out of stuff to do? */
304 if (idle && outdev == NULL)
305 return(0);
306 /* else process packets */
307 done_packets(do_packets(pl));
308 return(1); /* and continue */
309 }
310
311
312 report(t) /* report progress so far */
313 time_t t;
314 {
315 if (t == 0)
316 t = time(NULL);
317 fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
318 progname, npacksdone, nraysdone, (t-starttime)/3600.);
319 fflush(stderr);
320 if (vdef(REPORT))
321 reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
322 }
323
324
325 setdefaults(gp) /* set default values */
326 register HDGRID *gp;
327 {
328 extern char *atos();
329 register int i;
330 double len[3], maxlen, d;
331 char buf[64];
332
333 if (!vdef(SECTION)) {
334 sprintf(errmsg, "%s must be defined", vnam(SECTION));
335 error(USER, errmsg);
336 }
337 if (vdef(SECTION) > 1) {
338 sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
339 error(WARNING, errmsg);
340 }
341 if (!vdef(OCTREE)) {
342 if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
343 error(SYSTEM, "out of memory");
344 sprintf(vval(OCTREE), "%s.oct", froot);
345 vdef(OCTREE)++;
346 }
347 if (!vdef(OBSTRUCTIONS)) {
348 vval(OBSTRUCTIONS) = "T";
349 vdef(OBSTRUCTIONS)++;
350 }
351 if (!vdef(VDIST)) {
352 vval(VDIST) = "F";
353 vdef(VDIST)++;
354 }
355 if (!vdef(OCCUPANCY)) {
356 vval(OCCUPANCY) = "U";
357 vdef(OCCUPANCY)++;
358 }
359 /* append rendering options */
360 if (vdef(RENDER))
361 rtargc += wordstring(rtargv+rtargc, vval(RENDER));
362
363 if (gp == NULL) /* already initialized? */
364 return;
365 /* set grid parameters */
366 if (sscanf(vval(SECTION),
367 "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
368 &gp->orig[0], &gp->orig[1], &gp->orig[2],
369 &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
370 &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
371 &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
372 badvalue(SECTION);
373 maxlen = 0.;
374 for (i = 0; i < 3; i++)
375 if ((len[i] = VLEN(gp->xv[i])) > maxlen)
376 maxlen = len[i];
377 if (!vdef(GRID)) {
378 sprintf(buf, "%.4f", maxlen/8.);
379 vval(GRID) = savqstr(buf);
380 vdef(GRID)++;
381 }
382 if ((d = vflt(GRID)) <= FTINY)
383 badvalue(GRID);
384 for (i = 0; i < 3; i++)
385 gp->grid[i] = len[i]/d + (1.-FTINY);
386 }
387
388
389 creatholo(gp) /* create a holodeck output file */
390 HDGRID *gp;
391 {
392 extern char VersionID[];
393 long endloc = 0;
394 int fd;
395 FILE *fp;
396 /* open & truncate file */
397 if ((fp = fopen(hdkfile, "w+")) == NULL) {
398 sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
399 error(SYSTEM, errmsg);
400 }
401 /* write information header */
402 newheader("RADIANCE", fp);
403 fprintf(fp, "SOFTWARE= %s\n", VersionID);
404 printvars(fp);
405 fputformat(HOLOFMT, fp);
406 fputc('\n', fp);
407 putw(HOLOMAGIC, fp); /* put magic number & terminus */
408 fwrite(&endloc, sizeof(long), 1, fp);
409 fd = dup(fileno(fp));
410 fclose(fp); /* flush and close stdio stream */
411 hdinit(fd, gp); /* allocate and initialize index */
412 }
413
414
415 headline(s) /* process information header line */
416 char *s;
417 {
418 extern char FMTSTR[];
419 register char *cp;
420 char fmt[32];
421
422 if (formatval(fmt, s)) {
423 if (strcmp(fmt, HOLOFMT)) {
424 sprintf(errmsg, "%s file \"%s\" has %s%s",
425 HOLOFMT, hdkfile, FMTSTR, fmt);
426 error(USER, errmsg);
427 }
428 return;
429 }
430 for (cp = s; *cp; cp++) /* take off any comments */
431 if (*cp == '#') {
432 *cp = '\0';
433 break;
434 }
435 setvariable(s, matchvar); /* don't flag errors */
436 }
437
438
439 loadholo() /* start loading a holodeck from fname */
440 {
441 extern long ftell();
442 FILE *fp;
443 int fd;
444 long fpos;
445 /* open holodeck file */
446 if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
447 sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
448 ncprocs>0 ? "appending" : "reading");
449 error(SYSTEM, errmsg);
450 }
451 /* load variables from header */
452 getheader(fp, headline, NULL);
453 /* check magic number */
454 if (getw(fp) != HOLOMAGIC) {
455 sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
456 hdkfile);
457 error(USER, errmsg);
458 }
459 fread(&fpos, sizeof(long), 1, fp);
460 if (fpos != 0)
461 error(WARNING, "ignoring multiple sections in holodeck file");
462 fpos = ftell(fp); /* get stdio position */
463 fd = dup(fileno(fp));
464 fclose(fp); /* done with stdio */
465 lseek(fd, fpos, 0); /* align system file pointer */
466 hdinit(fd, NULL); /* allocate and load index */
467 }
468
469
470 done_packets(pl) /* handle finished packets */
471 PACKET *pl;
472 {
473 static int n2flush = 0;
474 register PACKET *p;
475
476 while (pl != NULL) {
477 p = pl; pl = p->next; p->next = NULL;
478 if (p->nr > 0) { /* add to holodeck */
479 bcopy((char *)p->ra,
480 (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
481 p->nr*sizeof(RAYVAL));
482 if (outdev != NULL) /* display it */
483 disp_packet((PACKHEAD *)p);
484 if (hdcachesize <= 0) /* manual flushing */
485 n2flush += p->nr;
486 nraysdone += p->nr;
487 npacksdone++;
488 }
489 p->nr = 0; /* push onto free list */
490 p->next = freepacks;
491 freepacks = p;
492 }
493 if (n2flush > 512*RPACKSIZ*ncprocs) {
494 hdflush(NULL); /* flush holodeck buffers */
495 n2flush = 0;
496 }
497 }
498
499
500 getradfile(rfargs) /* run rad and get needed variables */
501 char *rfargs;
502 {
503 static short mvar[] = {OCTREE,-1};
504 static char tf1[] = TEMPLATE;
505 char tf2[64];
506 char combuf[256];
507 char *pippt;
508 register int i;
509 register char *cp;
510 /* create rad command */
511 mktemp(tf1);
512 sprintf(tf2, "%s.rif", tf1);
513 sprintf(combuf,
514 "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
515 rfargs, tf1);
516 cp = combuf;
517 while (*cp){
518 if (*cp == '|') pippt = cp;
519 cp++;
520 } /* match unset variables */
521 for (i = 0; mvar[i] >= 0; i++)
522 if (!vdef(mvar[i])) {
523 *cp++ = '|';
524 strcpy(cp, vnam(mvar[i]));
525 while (*cp) cp++;
526 pippt = NULL;
527 }
528 if (pippt != NULL)
529 strcpy(pippt, "> /dev/null"); /* nothing to match */
530 else
531 sprintf(cp, ")[ \t]*=' > %s", tf2);
532 if (system(combuf)) {
533 error(SYSTEM, "cannot execute rad command");
534 unlink(tf2); /* clean up */
535 unlink(tf1);
536 quit(1);
537 }
538 if (pippt == NULL) {
539 loadvars(tf2); /* load variables */
540 unlink(tf2);
541 }
542 rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
543 unlink(tf1); /* clean up */
544 }
545
546
547 rootname(rn, fn) /* remove tail from end of fn */
548 register char *rn, *fn;
549 {
550 char *tp, *dp;
551
552 for (tp = NULL, dp = rn; *rn = *fn++; rn++)
553 if (ISDIRSEP(*rn))
554 dp = rn;
555 else if (*rn == '.')
556 tp = rn;
557 if (tp != NULL && tp > dp)
558 *tp = '\0';
559 }
560
561
562 badvalue(vc) /* report bad variable value and exit */
563 int vc;
564 {
565 sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
566 error(USER, errmsg);
567 }
568
569
570 eputs(s) /* put error message to stderr */
571 register char *s;
572 {
573 static int midline = 0;
574
575 if (!*s)
576 return;
577 if (!midline++) { /* prepend line with program name */
578 fputs(progname, stderr);
579 fputs(": ", stderr);
580 }
581 fputs(s, stderr);
582 if (s[strlen(s)-1] == '\n') {
583 fflush(stderr);
584 midline = 0;
585 }
586 }
587
588
589 wputs(s) /* put warning string to stderr */
590 char *s;
591 {
592 if (!nowarn)
593 eputs(s);
594 }
595
596
597 quit(ec) /* exit program gracefully */
598 int ec;
599 {
600 int status = 0;
601
602 if (hdlist[0] != NULL) { /* flush holodeck */
603 if (ncprocs > 0) {
604 done_packets(flush_queue());
605 status = end_rtrace(); /* close rtrace */
606 hdflush(NULL);
607 if (vdef(REPORT)) {
608 long fsiz, fuse;
609 report(0);
610 fsiz = hdfilen(hdlist[0]->fd);
611 fuse = hdfiluse(hdlist[0]->fd, 1);
612 fprintf(stderr,
613 "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
614 hdkfile, fsiz/(1024.*1024.),
615 100.*(fsiz-fuse)/fsiz);
616 }
617 } else
618 hdflush(NULL);
619 }
620 if (orig_mode >= 0) /* reset holodeck access mode */
621 fchmod(hdlist[0]->fd, orig_mode);
622 if (outdev != NULL) /* close display */
623 disp_close();
624 exit(ec ? ec : status); /* exit */
625 }