ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
(Generate patch)

Comparing ray/src/hd/rholo.c (file contents):
Revision 3.28 by gregl, Mon Dec 15 20:43:01 1997 UTC vs.
Revision 3.55 by schorsch, Sun Jun 8 12:03:10 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Radiance holodeck generation controller
6   */
# Line 11 | Line 8 | static char SCCSid[] = "$SunId$ SGI";
8   #include "rholo.h"
9   #include "random.h"
10   #include <signal.h>
14 #include <sys/types.h>
11   #include <sys/stat.h>
12  
13 + #ifndef FRAGWARN
14 + #define FRAGWARN        20              /* fragmentation for warning (%) */
15 + #endif
16 + #ifndef MAXQTIME
17 + #define MAXQTIME        5               /* target maximum seconds in queue */
18 + #endif
19 +                                        /* manual cache flushing frequency */
20 + #ifndef RTFLUSH
21 + #if MAXQTIME
22 + #define RTFLUSH         (300/MAXQTIME*totqlen)  /* <= 5 minutes */
23 + #else
24 + #define RTFLUSH         (50*totqlen)            /* just guess */
25 + #endif
26 + #endif
27                          /* the following must be consistent with rholo.h */
28   int     NVARS = NRHVARS;                /* total number of variables */
29  
# Line 23 | Line 33 | char   *progname;              /* our program name */
33   char    *hdkfile;               /* holodeck file name */
34   char    froot[256];             /* root file name */
35  
26 int     nowarn = 0;             /* turn warnings off? */
27
36   int     ncprocs = 0;            /* desired number of compute processes */
37  
38   char    *outdev = NULL;         /* output device name */
39  
40   int     readinp = 0;            /* read commands from stdin */
41  
42 < int     force = 0;              /* allow overwrite of holodeck */
42 > int     force = 0;              /* allow overwrite of holodeck (-1 == read-only) */
43  
44   time_t  starttime;              /* time we got started */
45   time_t  endtime;                /* time we should end by */
46   time_t  reporttime;             /* time for next report */
47  
48 < long    maxdisk;                /* maximum file space (bytes) */
48 > off_t   maxdisk;                /* maximum file space (bytes) */
49  
50   int     rtargc = 1;             /* rtrace command */
51   char    *rtargv[128] = {"rtrace", NULL};
# Line 48 | Line 56 | long   nraysdone = 0L;         /* number of rays done */
56   long    npacksdone = 0L;        /* number of packets done */
57  
58   PACKET  *freepacks;             /* available packets */
59 + int     totqlen;                /* maximum queue length (number of packets) */
60  
61   char  *sigerr[NSIG];            /* signal error messages */
62  
63 + extern int      nowarn;         /* turn warnings off? */
64 +
65   extern time_t   time();
66  
67  
# Line 58 | Line 69 | main(argc, argv)
69   int     argc;
70   char    *argv[];
71   {
61        HDGRID  hdg;
72          int     i;
73 <                                                /* mark start time */
64 <        starttime = time(NULL);
73 >
74          initurand(16384);                       /* initialize urand */
75          progname = argv[0];                     /* get arguments */
76          for (i = 1; i < argc && argv[i][0] == '-'; i++)
# Line 70 | Line 79 | char   *argv[];
79                          nowarn++;
80                          break;
81                  case 'f':                       /* force overwrite */
82 <                        force++;
82 >                        force = 1;
83                          break;
84 +                case 'r':                       /* read-only mode */
85 +                        force = -1;
86 +                        break;
87                  case 'i':                       /* read input from stdin */
88                          readinp++;
89                          break;
# Line 89 | Line 101 | char   *argv[];
101                          goto userr;
102                  }
103                                                  /* get root file name */
104 +        if (i >= argc)
105 +                goto userr;
106          rootname(froot, hdkfile=argv[i++]);
107                                                  /* load variables? */
108          if (i < argc)
# Line 105 | Line 119 | char   *argv[];
119                  }
120                                                  /* check settings */
121          checkvalues();
122 <                                                /* load RIF if any */
122 >                                                /* load rad input file */
123          getradfile();
124  
125          if (hdlist[0] == NULL) {                /* create new holodeck */
126 +                HDGRID  hdg[HDMAX];
127                                                          /* set defaults */
128 <                setdefaults(&hdg);
128 >                setdefaults(hdg);
129 >                                                        /* check read-only */
130 >                if (force < 0)
131 >                        error(USER, "cannot create read-only holodeck");
132                                                          /* holodeck exists? */
133                  if (!force && access(hdkfile, R_OK|W_OK) == 0)
134                          error(USER,
135                                  "holodeck file exists -- use -f to overwrite");
136                                                          /* create holodeck */
137 <                creatholo(&hdg);
137 >                creatholo(hdg);
138          } else                                  /* else just set defaults */
139                  setdefaults(NULL);
140                                                  /* initialize */
# Line 128 | Line 146 | char   *argv[];
146          quit(0);
147   userr:
148          fprintf(stderr,
149 < "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
149 > "Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
150                          progname);
151          quit(1);
152   }
153  
154  
155 + void
156   onsig(signo)                            /* fatal signal */
157   int  signo;
158   {
159          static int  gotsig = 0;
160  
161 <        if (gotsig++)                   /* two signals and we're gone! */
161 >        if (gotsig > 1)                 /* we're going as fast as we can! */
162 >                return;
163 >        if (gotsig++) {                 /* two signals and we split */
164 >                hdsync(NULL, 0);        /* don't leave w/o saying goodbye */
165                  _exit(signo);
166 <
167 <        alarm(60);                      /* allow 60 seconds to clean up */
146 <        signal(SIGALRM, SIG_DFL);       /* make certain we do die */
166 >        }
167 >        alarm(300);                     /* allow 5 minutes to clean up */
168          eputs("signal - ");
169          eputs(sigerr[signo]);
170          eputs("\n");
# Line 170 | Line 191 | int    fd, mod;
191          if (fstat(fd, &stbuf) < 0)
192                  error(SYSTEM, "cannot stat open holodeck file");
193          mod &= stbuf.st_mode;           /* always more restrictive */
194 <        if (mod == stbuf.st_mode)
194 >        if (mod == (stbuf.st_mode & 0777))
195                  return(-1);             /* already set */
196                                          /* else change it */
197          if (fchmod(fd, mod) < 0) {
# Line 194 | Line 215 | initrholo()                    /* get our holodeck running */
215                  init_global();
216                                                  /* record disk space limit */
217          if (!vdef(DISKSPACE))
218 <                maxdisk = 0;
218 >                maxdisk = (1L<<(sizeof(off_t)*8-2)) - 1024;
219          else
220                  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;
221                                                  /* set up memory cache */
222          if (outdev == NULL)
223 <                hdcachesize = 0;        /* manual flushing */
223 >                hdcachesize = 0;                /* manual flushing */
224          else if (vdef(CACHE))
225                  hdcachesize = 1024.*1024.*vflt(CACHE);
226                                                  /* open report file */
# Line 213 | Line 229 | initrholo()                    /* get our holodeck running */
229                  if (*s && freopen(s, "a", stderr) == NULL)
230                          quit(2);
231          }
232 +                                                /* mark the starting time */
233 +        starttime = time(NULL);
234 +                                                /* compute end time */
235 +        if (!vdef(TIME) || vflt(TIME) <= FTINY)
236 +                endtime = 0;
237 +        else
238 +                endtime = starttime + vflt(TIME)*3600. + .5;
239                                                  /* start rtrace */
240          if (ncprocs > 0) {
241 <                i = start_rtrace();
241 >                totqlen = i = start_rtrace();
242                  if (i < 1)
243                          error(USER, "cannot start rtrace process(es)");
244                  if (vdef(REPORT)) {             /* make first report */
# Line 252 | Line 275 | initrholo()                    /* get our holodeck running */
275          sigdie(SIGXCPU, "CPU limit exceeded");
276          sigdie(SIGXFSZ, "File size exceeded");
277   #endif
278 <                                                /* protect holodeck file */
279 <        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
278 >                                        /* protect holodeck file */
279 >        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0&force>=0 ? 0 : 0444);
280          return;
281   memerr:
282          error(SYSTEM, "out of memory in initrholo");
# Line 262 | Line 285 | memerr:
285  
286   rholo()                         /* holodeck main loop */
287   {
288 <        static int      idle = 1;
288 >        static long     nextfragwarn = 100*(1L<<20);
289 >        static int      idle = 0;
290          PACKET  *pl = NULL, *plend;
291 +        off_t   fsiz;
292 +        int     pksiz;
293          register PACKET *p;
294          time_t  t;
295 <        long    l;
296 <
271 <        if (outdev != NULL)             /* check display */
272 <                if (!disp_check(idle))
273 <                        return(0);
274 <                                        /* display only? */
275 <        if (nprocs <= 0) {
295 >                                        /* check display */
296 >        if (nprocs <= 0)
297                  idle = 1;
298 <                return(outdev != NULL);
299 <        }
300 <                                        /* check file size */
301 <        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
298 >        if (outdev != NULL) {
299 >                if (!disp_check(idle))
300 >                        return(0);      /* quit request */
301 >                if (nprocs <= 0)
302 >                        return(1);
303 >        } else if (idle)
304 >                return(0);              /* all done */
305 >        fsiz = hdfilen(hdlist[0]->fd);  /* check file size */
306 >        if (maxdisk > 0 && fsiz >= maxdisk) {
307                  error(WARNING, "file limit exceeded");
308                  done_rtrace();
309                  return(1);      /* comes back */
310          }
311 <                                        /* check time */
312 <        if (endtime > 0 || reporttime > 0)
313 <                t = time(NULL);
311 > #if FRAGWARN
312 >        if (fsiz >= nextfragwarn &&
313 >                (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) {
314 >                sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
315 >                                100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz);
316 >                error(WARNING, errmsg);
317 >                nextfragwarn = fsiz + (fsiz>>2);        /* decent interval */
318 >        }
319 > #endif
320 >        t = time(NULL);                 /* check time */
321          if (endtime > 0 && t >= endtime) {
322                  error(WARNING, "time limit exceeded");
323                  done_rtrace();
# Line 292 | Line 325 | rholo()                                /* holodeck main loop */
325          }
326          if (reporttime > 0 && t >= reporttime)
327                  report(t);
328 +                                        /* figure out good packet size */
329 +        pksiz = RPACKSIZ;
330 + #if MAXQTIME
331 +        if (!chunkycmp) {
332 +                pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
333 +                if (pksiz < 1) pksiz = 1;
334 +                else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ;
335 +        }
336 + #endif
337          idle = 0;                       /* get packets to process */
338          while (freepacks != NULL) {
339                  p = freepacks; freepacks = p->next; p->next = NULL;
340 <                if (!next_packet(p)) {
340 >                if (!next_packet(p, pksiz)) {
341                          p->next = freepacks; freepacks = p;
342                          idle = 1;
343                          break;
# Line 315 | Line 357 | register HDGRID        *gp;
357   {
358          extern char     *atos();
359          register int    i;
360 <        double  len[3], maxlen, d;
360 >        int     n;
361 >        double  len[3], d;
362          char    buf[64];
363  
364          if (!vdef(SECTION)) {
365                  sprintf(errmsg, "%s must be defined", vnam(SECTION));
366                  error(USER, errmsg);
367          }
325        if (vdef(SECTION) > 1) {
326                sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
327                error(WARNING, errmsg);
328        }
368          if (!vdef(OCTREE)) {
369                  if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
370                          error(SYSTEM, "out of memory");
# Line 343 | Line 382 | register HDGRID        *gp;
382          if (gp == NULL)         /* already initialized? */
383                  return;
384                                  /* set grid parameters */
385 <        gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
386 <        if (sscanf(vval(SECTION),
385 >        for (n = 0; n < vdef(SECTION); n++, gp++) {
386 >                gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
387 >                if (sscanf(nvalue(SECTION, n),
388                  "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
389 <                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
390 <                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
391 <                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
392 <                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
393 <                        &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
394 <                badvalue(SECTION);
395 <        maxlen = 0.;
396 <        for (i = 0; i < 3; i++)
397 <                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
398 <                        maxlen = len[i];
399 <        if (!vdef(GRID)) {
400 <                sprintf(buf, "%.4f", maxlen/8.);
401 <                vval(GRID) = savqstr(buf);
402 <                vdef(GRID)++;
389 >                                &gp->orig[0], &gp->orig[1], &gp->orig[2],
390 >                                &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
391 >                                &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
392 >                                &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
393 >                                &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
394 >                        badvalue(SECTION);
395 >                for (i = 0; i < 3; i++)
396 >                        len[i] = VLEN(gp->xv[i]);
397 >                if (!vdef(GRID)) {
398 >                        d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
399 >                                        len[2]*len[2] + 4*len[1]*len[2])
400 >                                + len[1]*len[1]*len[2]*(len[2] + 4*len[0])
401 >                                + 4*len[0]*len[1]*len[2]*len[2] );
402 >                        d = sqrt(sqrt(d));
403 >                } else if ((d = vflt(GRID)) <= FTINY)
404 >                        badvalue(GRID);
405 >                for (i = 0; i < 3; i++)
406 >                        if (gp->grid[i] <= 0)
407 >                                gp->grid[i] = len[i]/d + (1.-FTINY);
408          }
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);
409   }
410  
411  
# Line 373 | Line 413 | creatholo(gp)                  /* create a holodeck output file */
413   HDGRID  *gp;
414   {
415          extern char     VersionID[];
416 <        long    endloc = 0;
416 >        int4    lastloc, nextloc;
417 >        int     n;
418          int     fd;
419          FILE    *fp;
420                                          /* open & truncate file */
# Line 387 | Line 428 | HDGRID *gp;
428          printvars(fp);
429          fputformat(HOLOFMT, fp);
430          fputc('\n', fp);
431 <        putw(HOLOMAGIC, fp);            /* put magic number & terminus */
391 <        fwrite(&endloc, sizeof(long), 1, fp);
431 >        putw(HOLOMAGIC, fp);            /* put magic number */
432          fd = dup(fileno(fp));
433          fclose(fp);                     /* flush and close stdio stream */
434 <        hdinit(fd, gp);                 /* allocate and initialize index */
434 >        lastloc = lseek(fd, (off_t)0L, 2);
435 >        for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */
436 >                nextloc = 0L;
437 >                write(fd, (char *)&nextloc, sizeof(nextloc));
438 >                hdinit(fd, gp);                 /* writes beam index */
439 >                if (!n)
440 >                        break;
441 >                nextloc = hdfilen(fd);          /* write section pointer */
442 >                if (lseek(fd, (off_t)lastloc, 0) < 0)
443 >                        error(SYSTEM,
444 >                                "cannot seek on holodeck file in creatholo");
445 >                write(fd, (char *)&nextloc, sizeof(nextloc));
446 >                lseek(fd, (off_t)(lastloc=nextloc), 0);
447 >        }
448   }
449  
450  
451 + int
452   headline(s)                     /* process information header line */
453   char    *s;
454   {
# Line 408 | Line 462 | char   *s;
462                                          HOLOFMT, hdkfile, FMTSTR, fmt);
463                          error(USER, errmsg);
464                  }
465 <                return;
465 >                return(0);
466          }
467          for (cp = s; *cp; cp++)         /* take off any comments */
468                  if (*cp == '#') {
# Line 416 | Line 470 | char   *s;
470                          break;
471                  }
472          setvariable(s, matchvar);       /* don't flag errors */
473 +        return(0);
474   }
475  
476  
477   loadholo()                      /* start loading a holodeck from fname */
478   {
424        extern long     ftell();
479          FILE    *fp;
480          int     fd;
481 <        long    fpos;
482 <                                        /* open holodeck file */
483 <        if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
484 <                sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
485 <                                ncprocs>0 ? "appending" : "reading");
486 <                error(SYSTEM, errmsg);
481 >        int     n;
482 >        int4    nextloc;
483 >        
484 >        if (ncprocs > 0 & force >= 0)
485 >                fp = fopen(hdkfile, "r+");
486 >        else
487 >                fp = NULL;
488 >        if (fp == NULL) {
489 >                if ((fp = fopen(hdkfile, "r")) == NULL) {
490 >                        sprintf(errmsg, "cannot open \"%s\"", hdkfile);
491 >                        error(SYSTEM, errmsg);
492 >                }
493 >                if (ncprocs > 0) {
494 >                        sprintf(errmsg,
495 >                        "\"%s\" opened read-only; new rays will be discarded",
496 >                                        hdkfile);
497 >                        error(WARNING, errmsg);
498 >                        force = -1;
499 >                }
500          }
501                                          /* load variables from header */
502          getheader(fp, headline, NULL);
# Line 439 | Line 506 | loadholo()                     /* start loading a holodeck from fname */
506                                  hdkfile);
507                  error(USER, errmsg);
508          }
509 <        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 */
509 >        nextloc = ftell(fp);                    /* get stdio position */
510          fd = dup(fileno(fp));
511          fclose(fp);                             /* done with stdio */
512 <        lseek(fd, fpos, 0);                     /* align system file pointer */
513 <        hdinit(fd, NULL);                       /* allocate and load index */
512 >        for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
513 >                lseek(fd, (off_t)nextloc, 0);
514 >                read(fd, (char *)&nextloc, sizeof(nextloc));
515 >                hdinit(fd, NULL);
516 >        }
517 >        if (n != vdef(SECTION)) {
518 >                sprintf(errmsg, "number of sections does not match %s setting",
519 >                                vnam(SECTION));
520 >                error(WARNING, errmsg);
521 >        }
522   }
523  
524  
# Line 459 | Line 531 | PACKET *pl;
531          while (pl != NULL) {
532                  p = pl; pl = p->next; p->next = NULL;
533                  if (p->nr > 0) {                /* add to holodeck */
534 <                        bcopy((char *)p->ra,
535 <                                (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
534 >                        bcopy((void *)p->ra,
535 >                                (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
536                                  p->nr*sizeof(RAYVAL));
537                          if (outdev != NULL)     /* display it */
538                                  disp_packet((PACKHEAD *)p);
539 <                        if (hdcachesize <= 0)   /* manual flushing */
540 <                                n2flush += p->nr;
539 >                        if (hdcachesize <= 0)
540 >                                n2flush++;
541                          nraysdone += p->nr;
542                          npacksdone++;
543 +                        p->nr = 0;
544                  }
545 <                p->nr = 0;                      /* push onto free list */
473 <                p->next = freepacks;
545 >                p->next = freepacks;            /* push onto free list */
546                  freepacks = p;
547          }
548 <        if (n2flush > 512*RPACKSIZ*nprocs) {
549 <                hdflush(NULL);                  /* flush holodeck buffers */
548 >        if (n2flush >= RTFLUSH) {
549 >                if (outdev != NULL)
550 >                        hdsync(NULL, 1);
551 >                else
552 >                        hdflush(NULL);
553                  n2flush = 0;
554          }
555   }
# Line 503 | Line 578 | int    vc;
578   }
579  
580  
581 + void
582   eputs(s)                        /* put error message to stderr */
583   register char  *s;
584   {
# Line 522 | Line 598 | register char  *s;
598   }
599  
600  
601 < wputs(s)                        /* put warning string to stderr */
526 < char    *s;
527 < {
528 <        if (!nowarn)
529 <                eputs(s);
530 < }
531 <
532 <
601 > void
602   quit(ec)                        /* exit program gracefully */
603   int     ec;
604   {
# Line 538 | Line 607 | int    ec;
607          if (hdlist[0] != NULL) {        /* close holodeck */
608                  if (nprocs > 0)
609                          status = done_rtrace();         /* calls hdsync() */
610 <                if (ncprocs > 0 && vdef(REPORT)) {
611 <                        long    fsiz, fuse;
610 >                if (ncprocs > 0 & force >= 0 && vdef(REPORT)) {
611 >                        off_t   fsiz, fuse;
612                          fsiz = hdfilen(hdlist[0]->fd);
613                          fuse = hdfiluse(hdlist[0]->fd, 1);
614                          fprintf(stderr,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines