ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.28
Committed: Mon Dec 15 20:43:01 1997 UTC (26 years, 3 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.27: +6 -10 lines
Log Message:
eliminated OCCUPANCY variable and fixed a couple of new bugs

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 <signal.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16
17 /* the following must be consistent with rholo.h */
18 int NVARS = NRHVARS; /* total number of variables */
19
20 VARIABLE vv[] = RHVINIT; /* variable-value pairs */
21
22 char *progname; /* our program name */
23 char *hdkfile; /* holodeck file name */
24 char froot[256]; /* root file name */
25
26 int nowarn = 0; /* turn warnings off? */
27
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 */
39
40 long maxdisk; /* maximum file space (bytes) */
41
42 int rtargc = 1; /* rtrace command */
43 char *rtargv[128] = {"rtrace", NULL};
44
45 int orig_mode = -1; /* original file mode (-1 if unchanged) */
46
47 long nraysdone = 0L; /* number of rays done */
48 long npacksdone = 0L; /* number of packets done */
49
50 PACKET *freepacks; /* available packets */
51
52 char *sigerr[NSIG]; /* signal error messages */
53
54 extern time_t time();
55
56
57 main(argc, argv)
58 int argc;
59 char *argv[];
60 {
61 HDGRID hdg;
62 int i;
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]) {
69 case 'w': /* turn off warnings */
70 nowarn++;
71 break;
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;
81 ncprocs = atoi(argv[++i]);
82 break;
83 case 'o': /* output display */
84 if (i >= argc-2)
85 goto userr;
86 outdev = argv[++i];
87 break;
88 default:
89 goto userr;
90 }
91 /* get root file name */
92 rootname(froot, hdkfile=argv[i++]);
93 /* load variables? */
94 if (i < argc)
95 if (argv[i][0] != '-' && argv[i][0] != '+')
96 loadvars(argv[i]); /* load variables from file */
97
98 if (i >= argc || argv[i][0] == '+')
99 loadholo(); /* load existing holodeck */
100
101 while (++i < argc) /* get command line settings */
102 if (setvariable(argv[i], matchvar) < 0) {
103 sprintf(errmsg, "unknown variable: %s", argv[i]);
104 error(USER, errmsg);
105 }
106 /* check settings */
107 checkvalues();
108 /* load RIF if any */
109 getradfile();
110
111 if (hdlist[0] == NULL) { /* create new holodeck */
112 /* set defaults */
113 setdefaults(&hdg);
114 /* holodeck exists? */
115 if (!force && access(hdkfile, R_OK|W_OK) == 0)
116 error(USER,
117 "holodeck file exists -- use -f to overwrite");
118 /* create holodeck */
119 creatholo(&hdg);
120 } else /* else just set defaults */
121 setdefaults(NULL);
122 /* initialize */
123 initrholo();
124 /* main loop */
125 while (rholo())
126 ;
127 /* done */
128 quit(0);
129 userr:
130 fprintf(stderr,
131 "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
132 progname);
133 quit(1);
134 }
135
136
137 onsig(signo) /* fatal signal */
138 int signo;
139 {
140 static int gotsig = 0;
141
142 if (gotsig++) /* two signals and we're gone! */
143 _exit(signo);
144
145 alarm(60); /* allow 60 seconds to clean up */
146 signal(SIGALRM, SIG_DFL); /* make certain we do die */
147 eputs("signal - ");
148 eputs(sigerr[signo]);
149 eputs("\n");
150 quit(3);
151 }
152
153
154 sigdie(signo, msg) /* set fatal signal */
155 int signo;
156 char *msg;
157 {
158 if (signal(signo, onsig) == SIG_IGN)
159 signal(signo, SIG_IGN);
160 sigerr[signo] = msg;
161 }
162
163
164 int
165 resfmode(fd, mod) /* restrict open file access mode */
166 int fd, mod;
167 {
168 struct stat stbuf;
169 /* get original mode */
170 if (fstat(fd, &stbuf) < 0)
171 error(SYSTEM, "cannot stat open holodeck file");
172 mod &= stbuf.st_mode; /* always more restrictive */
173 if (mod == stbuf.st_mode)
174 return(-1); /* already set */
175 /* else change it */
176 if (fchmod(fd, mod) < 0) {
177 error(WARNING, "cannot change holodeck file access mode");
178 return(-1);
179 }
180 return(stbuf.st_mode); /* return original mode */
181 }
182
183
184 initrholo() /* get our holodeck running */
185 {
186 extern int global_packet();
187 register int i;
188 /* close holodeck on exec() */
189 fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
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. + .5;
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(es)");
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 (!vdef(OBSTRUCTIONS) || !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 (nprocs <= 0) {
276 idle = 1;
277 return(outdev != NULL);
278 }
279 /* check file size */
280 if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
281 error(WARNING, "file limit exceeded");
282 done_rtrace();
283 return(1); /* comes back */
284 }
285 /* check time */
286 if (endtime > 0 || reporttime > 0)
287 t = time(NULL);
288 if (endtime > 0 && t >= endtime) {
289 error(WARNING, "time limit exceeded");
290 done_rtrace();
291 return(1); /* comes back */
292 }
293 if (reporttime > 0 && t >= reporttime)
294 report(t);
295 idle = 0; /* get packets to process */
296 while (freepacks != NULL) {
297 p = freepacks; freepacks = p->next; p->next = NULL;
298 if (!next_packet(p)) {
299 p->next = freepacks; freepacks = p;
300 idle = 1;
301 break;
302 }
303 if (pl == NULL) pl = p;
304 else plend->next = p;
305 plend = p;
306 }
307 /* process packets */
308 done_packets(do_packets(pl));
309 return(1); /* and continue */
310 }
311
312
313 setdefaults(gp) /* set default values */
314 register HDGRID *gp;
315 {
316 extern char *atos();
317 register int i;
318 double len[3], maxlen, d;
319 char buf[64];
320
321 if (!vdef(SECTION)) {
322 sprintf(errmsg, "%s must be defined", vnam(SECTION));
323 error(USER, errmsg);
324 }
325 if (vdef(SECTION) > 1) {
326 sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
327 error(WARNING, errmsg);
328 }
329 if (!vdef(OCTREE)) {
330 if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
331 error(SYSTEM, "out of memory");
332 sprintf(vval(OCTREE), "%s.oct", froot);
333 vdef(OCTREE)++;
334 }
335 if (!vdef(VDIST)) {
336 vval(VDIST) = "F";
337 vdef(VDIST)++;
338 }
339 /* append rendering options */
340 if (vdef(RENDER))
341 rtargc += wordstring(rtargv+rtargc, vval(RENDER));
342
343 if (gp == NULL) /* already initialized? */
344 return;
345 /* set grid parameters */
346 gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
347 if (sscanf(vval(SECTION),
348 "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
349 &gp->orig[0], &gp->orig[1], &gp->orig[2],
350 &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
351 &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
352 &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
353 &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
354 badvalue(SECTION);
355 maxlen = 0.;
356 for (i = 0; i < 3; i++)
357 if ((len[i] = VLEN(gp->xv[i])) > maxlen)
358 maxlen = len[i];
359 if (!vdef(GRID)) {
360 sprintf(buf, "%.4f", maxlen/8.);
361 vval(GRID) = savqstr(buf);
362 vdef(GRID)++;
363 }
364 if ((d = vflt(GRID)) <= FTINY)
365 badvalue(GRID);
366 for (i = 0; i < 3; i++)
367 if (gp->grid[i] <= 0)
368 gp->grid[i] = len[i]/d + (1.-FTINY);
369 }
370
371
372 creatholo(gp) /* create a holodeck output file */
373 HDGRID *gp;
374 {
375 extern char VersionID[];
376 long endloc = 0;
377 int fd;
378 FILE *fp;
379 /* open & truncate file */
380 if ((fp = fopen(hdkfile, "w+")) == NULL) {
381 sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
382 error(SYSTEM, errmsg);
383 }
384 /* write information header */
385 newheader("RADIANCE", fp);
386 fprintf(fp, "SOFTWARE= %s\n", VersionID);
387 printvars(fp);
388 fputformat(HOLOFMT, fp);
389 fputc('\n', fp);
390 putw(HOLOMAGIC, fp); /* put magic number & terminus */
391 fwrite(&endloc, sizeof(long), 1, fp);
392 fd = dup(fileno(fp));
393 fclose(fp); /* flush and close stdio stream */
394 hdinit(fd, gp); /* allocate and initialize index */
395 }
396
397
398 headline(s) /* process information header line */
399 char *s;
400 {
401 extern char FMTSTR[];
402 register char *cp;
403 char fmt[32];
404
405 if (formatval(fmt, s)) {
406 if (strcmp(fmt, HOLOFMT)) {
407 sprintf(errmsg, "%s file \"%s\" has %s%s",
408 HOLOFMT, hdkfile, FMTSTR, fmt);
409 error(USER, errmsg);
410 }
411 return;
412 }
413 for (cp = s; *cp; cp++) /* take off any comments */
414 if (*cp == '#') {
415 *cp = '\0';
416 break;
417 }
418 setvariable(s, matchvar); /* don't flag errors */
419 }
420
421
422 loadholo() /* start loading a holodeck from fname */
423 {
424 extern long ftell();
425 FILE *fp;
426 int fd;
427 long fpos;
428 /* open holodeck file */
429 if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
430 sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
431 ncprocs>0 ? "appending" : "reading");
432 error(SYSTEM, errmsg);
433 }
434 /* load variables from header */
435 getheader(fp, headline, NULL);
436 /* check magic number */
437 if (getw(fp) != HOLOMAGIC) {
438 sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
439 hdkfile);
440 error(USER, errmsg);
441 }
442 fread(&fpos, sizeof(long), 1, fp);
443 if (fpos != 0)
444 error(WARNING, "ignoring multiple sections in holodeck file");
445 fpos = ftell(fp); /* get stdio position */
446 fd = dup(fileno(fp));
447 fclose(fp); /* done with stdio */
448 lseek(fd, fpos, 0); /* align system file pointer */
449 hdinit(fd, NULL); /* allocate and load index */
450 }
451
452
453 done_packets(pl) /* handle finished packets */
454 PACKET *pl;
455 {
456 static int n2flush = 0;
457 register PACKET *p;
458
459 while (pl != NULL) {
460 p = pl; pl = p->next; p->next = NULL;
461 if (p->nr > 0) { /* add to holodeck */
462 bcopy((char *)p->ra,
463 (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
464 p->nr*sizeof(RAYVAL));
465 if (outdev != NULL) /* display it */
466 disp_packet((PACKHEAD *)p);
467 if (hdcachesize <= 0) /* manual flushing */
468 n2flush += p->nr;
469 nraysdone += p->nr;
470 npacksdone++;
471 }
472 p->nr = 0; /* push onto free list */
473 p->next = freepacks;
474 freepacks = p;
475 }
476 if (n2flush > 512*RPACKSIZ*nprocs) {
477 hdflush(NULL); /* flush holodeck buffers */
478 n2flush = 0;
479 }
480 }
481
482
483 rootname(rn, fn) /* remove tail from end of fn */
484 register char *rn, *fn;
485 {
486 char *tp, *dp;
487
488 for (tp = NULL, dp = rn; *rn = *fn++; rn++)
489 if (*rn == '/')
490 dp = rn;
491 else if (*rn == '.')
492 tp = rn;
493 if (tp != NULL && tp > dp)
494 *tp = '\0';
495 }
496
497
498 badvalue(vc) /* report bad variable value and exit */
499 int vc;
500 {
501 sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
502 error(USER, errmsg);
503 }
504
505
506 eputs(s) /* put error message to stderr */
507 register char *s;
508 {
509 static int midline = 0;
510
511 if (!*s)
512 return;
513 if (!midline++) { /* prepend line with program name */
514 fputs(progname, stderr);
515 fputs(": ", stderr);
516 }
517 fputs(s, stderr);
518 if (s[strlen(s)-1] == '\n') {
519 fflush(stderr);
520 midline = 0;
521 }
522 }
523
524
525 wputs(s) /* put warning string to stderr */
526 char *s;
527 {
528 if (!nowarn)
529 eputs(s);
530 }
531
532
533 quit(ec) /* exit program gracefully */
534 int ec;
535 {
536 int status = 0;
537
538 if (hdlist[0] != NULL) { /* close holodeck */
539 if (nprocs > 0)
540 status = done_rtrace(); /* calls hdsync() */
541 if (ncprocs > 0 && vdef(REPORT)) {
542 long fsiz, fuse;
543 fsiz = hdfilen(hdlist[0]->fd);
544 fuse = hdfiluse(hdlist[0]->fd, 1);
545 fprintf(stderr,
546 "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
547 hdkfile, fsiz/(1024.*1024.),
548 100.*(fsiz-fuse)/fsiz);
549 }
550 }
551 if (orig_mode >= 0) /* reset holodeck access mode */
552 fchmod(hdlist[0]->fd, orig_mode);
553 if (outdev != NULL) /* close display */
554 disp_close();
555 exit(ec ? ec : status); /* exit */
556 }