ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.37
Committed: Fri Jan 16 11:45:43 1998 UTC (26 years, 3 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.36: +9 -7 lines
Log Message:
changed default behavior when GRID is unspecified

File Contents

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