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.23 by gregl, Fri Dec 12 18:33:50 1997 UTC vs.
Revision 3.44 by gwlarson, Wed Dec 30 08:02:37 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1997 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ SGI";
# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ SGI";
14   #include <sys/types.h>
15   #include <sys/stat.h>
16  
17 + #ifndef FRAGWARN
18 + #define FRAGWARN        20              /* fragmentation for warning (%) */
19 + #endif
20 + #ifndef MAXQTIME
21 + #define MAXQTIME        5               /* target maximum seconds in queue */
22 + #endif
23                          /* the following must be consistent with rholo.h */
24   int     NVARS = NRHVARS;                /* total number of variables */
25  
# Line 23 | Line 29 | char   *progname;              /* our program name */
29   char    *hdkfile;               /* holodeck file name */
30   char    froot[256];             /* root file name */
31  
26 int     nowarn = 0;             /* turn warnings off? */
27
32   int     ncprocs = 0;            /* desired number of compute processes */
33  
34   char    *outdev = NULL;         /* output device name */
# Line 48 | Line 52 | long   nraysdone = 0L;         /* number of rays done */
52   long    npacksdone = 0L;        /* number of packets done */
53  
54   PACKET  *freepacks;             /* available packets */
55 + int     totqlen;                /* maximum queue length when full */
56  
57   char  *sigerr[NSIG];            /* signal error messages */
58  
59 + extern int      nowarn;         /* turn warnings off? */
60 +
61   extern time_t   time();
62  
63  
# Line 58 | Line 65 | main(argc, argv)
65   int     argc;
66   char    *argv[];
67   {
61        HDGRID  hdg;
68          int     i;
69 <                                                /* mark start time */
64 <        starttime = time(NULL);
69 >
70          initurand(16384);                       /* initialize urand */
71          progname = argv[0];                     /* get arguments */
72          for (i = 1; i < argc && argv[i][0] == '-'; i++)
# Line 88 | Line 93 | char   *argv[];
93                  default:
94                          goto userr;
95                  }
91                                                /* do we have a job? */
92        if (outdev == NULL && ncprocs <= 0)
93                goto userr;
96                                                  /* get root file name */
97 +        if (i >= argc)
98 +                goto userr;
99          rootname(froot, hdkfile=argv[i++]);
100 <                                                /* load... */
101 <        if (i < argc) {                         /* variables */
102 <                loadvars(argv[i++]);
103 <                                                        /* cmdline settings */
104 <                for ( ; i < argc; i++)
105 <                        if (setvariable(argv[i], matchvar) < 0) {
106 <                                sprintf(errmsg, "unknown variable: %s",
107 <                                                argv[i]);
108 <                                error(USER, errmsg);
109 <                        }
110 <                                                        /* check settings */
111 <                checkvalues();
112 <                                                        /* load RIF if any */
113 <                getradfile();
100 >                                                /* load variables? */
101 >        if (i < argc)
102 >                if (argv[i][0] != '-' && argv[i][0] != '+')
103 >                        loadvars(argv[i]);      /* load variables from file */
104 >
105 >        if (i >= argc || argv[i][0] == '+')
106 >                loadholo();                     /* load existing holodeck */
107 >
108 >        while (++i < argc)                      /* get command line settings */
109 >                if (setvariable(argv[i], matchvar) < 0) {
110 >                        sprintf(errmsg, "unknown variable: %s", argv[i]);
111 >                        error(USER, errmsg);
112 >                }
113 >                                                /* check settings */
114 >        checkvalues();
115 >                                                /* load rad input file */
116 >        getradfile();
117 >
118 >        if (hdlist[0] == NULL) {                /* create new holodeck */
119 >                HDGRID  hdg[HDMAX];
120                                                          /* set defaults */
121 <                setdefaults(&hdg);
121 >                setdefaults(hdg);
122                                                          /* holodeck exists? */
123                  if (!force && access(hdkfile, R_OK|W_OK) == 0)
124                          error(USER,
125                                  "holodeck file exists -- use -f to overwrite");
126                                                          /* create holodeck */
127 <                creatholo(&hdg);
128 <        } else {                                /* else load holodeck */
119 <                loadholo();
120 <                                                        /* check settings */
121 <                checkvalues();
122 <                                                        /* load RIF if any */
123 <                getradfile();
124 <                                                        /* set defaults */
127 >                creatholo(hdg);
128 >        } else                                  /* else just set defaults */
129                  setdefaults(NULL);
126        }
130                                                  /* initialize */
131          initrholo();
132                                                  /* main loop */
# Line 133 | Line 136 | char   *argv[];
136          quit(0);
137   userr:
138          fprintf(stderr,
139 < "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n",
139 > "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
140                          progname);
141          quit(1);
142   }
# Line 147 | Line 150 | int  signo;
150          if (gotsig++)                   /* two signals and we're gone! */
151                  _exit(signo);
152  
153 <        alarm(30);                      /* allow 30 seconds to clean up */
153 >        alarm(60);                      /* allow 60 seconds to clean up */
154          signal(SIGALRM, SIG_DFL);       /* make certain we do die */
155          eputs("signal - ");
156          eputs(sigerr[signo]);
# Line 175 | Line 178 | int    fd, mod;
178          if (fstat(fd, &stbuf) < 0)
179                  error(SYSTEM, "cannot stat open holodeck file");
180          mod &= stbuf.st_mode;           /* always more restrictive */
181 <        if (mod == stbuf.st_mode)
181 >        if (mod == (stbuf.st_mode & 0777))
182                  return(-1);             /* already set */
183                                          /* else change it */
184          if (fchmod(fd, mod) < 0) {
# Line 202 | Line 205 | initrholo()                    /* get our holodeck running */
205                  maxdisk = 0;
206          else
207                  maxdisk = 1024.*1024.*vflt(DISKSPACE);
205                                                /* record end time */
206        if (!vdef(TIME) || vflt(TIME) <= FTINY)
207                endtime = 0;
208        else
209                endtime = starttime + vflt(TIME)*3600. + .5;
208                                                  /* set up memory cache */
209          if (outdev == NULL)
210                  hdcachesize = 0;        /* manual flushing */
# Line 218 | Line 216 | initrholo()                    /* get our holodeck running */
216                  if (*s && freopen(s, "a", stderr) == NULL)
217                          quit(2);
218          }
219 +                                                /* mark the starting time */
220 +        starttime = time(NULL);
221 +                                                /* compute end time */
222 +        if (!vdef(TIME) || vflt(TIME) <= FTINY)
223 +                endtime = 0;
224 +        else
225 +                endtime = starttime + vflt(TIME)*3600. + .5;
226                                                  /* start rtrace */
227          if (ncprocs > 0) {
228 <                i = start_rtrace();
228 >                totqlen = i = start_rtrace();
229                  if (i < 1)
230                          error(USER, "cannot start rtrace process(es)");
231                  if (vdef(REPORT)) {             /* make first report */
# Line 233 | Line 238 | initrholo()                    /* get our holodeck running */
238                          goto memerr;
239                  freepacks[--i].nr = 0;
240                  freepacks[i].next = NULL;
241 <                if (!vbool(OBSTRUCTIONS)) {
241 >                if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
242                          freepacks[i].offset = (float *)bmalloc(
243                                          RPACKSIZ*sizeof(float)*(i+1) );
244                          if (freepacks[i].offset == NULL)
# Line 267 | Line 272 | memerr:
272  
273   rholo()                         /* holodeck main loop */
274   {
275 <        static int      idle = 1;
275 >        static long     nextfragwarn = 100*(1L<<20);
276 >        static int      idle = 0;
277          PACKET  *pl = NULL, *plend;
278 +        long    fsiz;
279 +        int     pksiz;
280          register PACKET *p;
281          time_t  t;
282 <        long    l;
275 <
276 <        if (outdev != NULL)             /* check display */
277 <                if (!disp_check(idle))
278 <                        return(0);
279 <                                        /* display only? */
282 >                                        /* check display */
283          if (nprocs <= 0)
284 <                return(outdev != NULL);
285 <                                        /* check file size */
286 <        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
284 >                idle = 1;
285 >        if (outdev != NULL) {
286 >                if (!disp_check(idle))
287 >                        return(0);      /* quit request */
288 >                if (nprocs <= 0)
289 >                        return(1);
290 >        } else if (idle)
291 >                return(0);              /* all done */
292 >        fsiz = hdfilen(hdlist[0]->fd);  /* check file size */
293 >        if (maxdisk > 0 && fsiz >= maxdisk) {
294                  error(WARNING, "file limit exceeded");
295                  done_rtrace();
286                idle = 1;
296                  return(1);      /* comes back */
297          }
298 <                                        /* check time */
299 <        if (endtime > 0 || reporttime > 0)
300 <                t = time(NULL);
298 > #if FRAGWARN
299 >        if (fsiz >= nextfragwarn &&
300 >                (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) {
301 >                sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
302 >                                100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz);
303 >                error(WARNING, errmsg);
304 >                nextfragwarn = fsiz + (fsiz>>2);        /* decent interval */
305 >        }
306 > #endif
307 >        t = time(NULL);                 /* check time */
308          if (endtime > 0 && t >= endtime) {
309                  error(WARNING, "time limit exceeded");
310                  done_rtrace();
295                idle = 1;
311                  return(1);      /* comes back */
312          }
313          if (reporttime > 0 && t >= reporttime)
314                  report(t);
315 <                                        /* get packets to process */
315 >                                        /* figure out good packet size */
316 > #if MAXQTIME
317 >        pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
318 >        if (pksiz < 1)
319 >                pksiz = 1;
320 >        else if (pksiz > RPACKSIZ)
321 > #endif
322 >                pksiz = RPACKSIZ;
323 >        idle = 0;                       /* get packets to process */
324          while (freepacks != NULL) {
325                  p = freepacks; freepacks = p->next; p->next = NULL;
326 <                if (!next_packet(p)) {
326 >                if (!next_packet(p, pksiz)) {
327                          p->next = freepacks; freepacks = p;
328                          idle = 1;
329                          break;
# Line 320 | Line 343 | register HDGRID        *gp;
343   {
344          extern char     *atos();
345          register int    i;
346 <        double  len[3], maxlen, d;
346 >        int     n;
347 >        double  len[3], d;
348          char    buf[64];
349  
350          if (!vdef(SECTION)) {
351                  sprintf(errmsg, "%s must be defined", vnam(SECTION));
352                  error(USER, errmsg);
353          }
330        if (vdef(SECTION) > 1) {
331                sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
332                error(WARNING, errmsg);
333        }
354          if (!vdef(OCTREE)) {
355                  if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
356                          error(SYSTEM, "out of memory");
357                  sprintf(vval(OCTREE), "%s.oct", froot);
358                  vdef(OCTREE)++;
359          }
340        if (!vdef(OBSTRUCTIONS)) {
341                vval(OBSTRUCTIONS) = "T";
342                vdef(OBSTRUCTIONS)++;
343        }
360          if (!vdef(VDIST)) {
361                  vval(VDIST) = "F";
362                  vdef(VDIST)++;
363          }
348        if (!vdef(OCCUPANCY)) {
349                vval(OCCUPANCY) = "U";
350                vdef(OCCUPANCY)++;
351        }
364                                  /* append rendering options */
365          if (vdef(RENDER))
366                  rtargc += wordstring(rtargv+rtargc, vval(RENDER));
# Line 356 | Line 368 | register HDGRID        *gp;
368          if (gp == NULL)         /* already initialized? */
369                  return;
370                                  /* set grid parameters */
371 <        gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
372 <        if (sscanf(vval(SECTION),
371 >        for (n = 0; n < vdef(SECTION); n++, gp++) {
372 >                gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
373 >                if (sscanf(nvalue(SECTION, n),
374                  "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
375 <                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
376 <                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
377 <                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
378 <                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
379 <                        &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
380 <                badvalue(SECTION);
381 <        maxlen = 0.;
382 <        for (i = 0; i < 3; i++)
383 <                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
384 <                        maxlen = len[i];
385 <        if (!vdef(GRID)) {
386 <                sprintf(buf, "%.4f", maxlen/8.);
387 <                vval(GRID) = savqstr(buf);
388 <                vdef(GRID)++;
375 >                                &gp->orig[0], &gp->orig[1], &gp->orig[2],
376 >                                &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
377 >                                &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
378 >                                &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
379 >                                &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
380 >                        badvalue(SECTION);
381 >                for (i = 0; i < 3; i++)
382 >                        len[i] = VLEN(gp->xv[i]);
383 >                if (!vdef(GRID)) {
384 >                        d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
385 >                                        len[2]*len[2] + 4*len[1]*len[2])
386 >                                + len[1]*len[1]*len[2]*(len[2] + 4*len[0])
387 >                                + 4*len[0]*len[1]*len[2]*len[2] );
388 >                        d = sqrt(sqrt(d));
389 >                } else if ((d = vflt(GRID)) <= FTINY)
390 >                        badvalue(GRID);
391 >                for (i = 0; i < 3; i++)
392 >                        if (gp->grid[i] <= 0)
393 >                                gp->grid[i] = len[i]/d + (1.-FTINY);
394          }
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);
395   }
396  
397  
# Line 386 | Line 399 | creatholo(gp)                  /* create a holodeck output file */
399   HDGRID  *gp;
400   {
401          extern char     VersionID[];
402 <        long    endloc = 0;
402 >        int4    lastloc, nextloc;
403 >        int     n;
404          int     fd;
405          FILE    *fp;
406                                          /* open & truncate file */
# Line 400 | Line 414 | HDGRID *gp;
414          printvars(fp);
415          fputformat(HOLOFMT, fp);
416          fputc('\n', fp);
417 <        putw(HOLOMAGIC, fp);            /* put magic number & terminus */
404 <        fwrite(&endloc, sizeof(long), 1, fp);
417 >        putw(HOLOMAGIC, fp);            /* put magic number */
418          fd = dup(fileno(fp));
419          fclose(fp);                     /* flush and close stdio stream */
420 <        hdinit(fd, gp);                 /* allocate and initialize index */
420 >        lastloc = lseek(fd, 0L, 2);
421 >        for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */
422 >                nextloc = 0L;
423 >                write(fd, (char *)&nextloc, sizeof(nextloc));
424 >                hdinit(fd, gp);                 /* writes beam index */
425 >                if (!n)
426 >                        break;
427 >                nextloc = hdfilen(fd);          /* write section pointer */
428 >                if (lseek(fd, (long)lastloc, 0) < 0)
429 >                        error(SYSTEM,
430 >                                "cannot seek on holodeck file in creatholo");
431 >                write(fd, (char *)&nextloc, sizeof(nextloc));
432 >                lseek(fd, (long)(lastloc=nextloc), 0);
433 >        }
434   }
435  
436  
437 + int
438   headline(s)                     /* process information header line */
439   char    *s;
440   {
# Line 421 | Line 448 | char   *s;
448                                          HOLOFMT, hdkfile, FMTSTR, fmt);
449                          error(USER, errmsg);
450                  }
451 <                return;
451 >                return(0);
452          }
453          for (cp = s; *cp; cp++)         /* take off any comments */
454                  if (*cp == '#') {
# Line 429 | Line 456 | char   *s;
456                          break;
457                  }
458          setvariable(s, matchvar);       /* don't flag errors */
459 +        return(0);
460   }
461  
462  
# Line 437 | Line 465 | loadholo()                     /* start loading a holodeck from fname */
465          extern long     ftell();
466          FILE    *fp;
467          int     fd;
468 <        long    fpos;
468 >        int     n;
469 >        int4    nextloc;
470                                          /* open holodeck file */
471          if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
472 <                sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
473 <                                ncprocs>0 ? "appending" : "reading");
472 >                sprintf(errmsg, "cannot %s \"%s\"",
473 >                                ncprocs>0 ? "append" : "read", hdkfile);
474                  error(SYSTEM, errmsg);
475          }
476                                          /* load variables from header */
# Line 452 | Line 481 | loadholo()                     /* start loading a holodeck from fname */
481                                  hdkfile);
482                  error(USER, errmsg);
483          }
484 <        fread(&fpos, sizeof(long), 1, fp);
456 <        if (fpos != 0)
457 <                error(WARNING, "ignoring multiple sections in holodeck file");
458 <        fpos = ftell(fp);                       /* get stdio position */
484 >        nextloc = ftell(fp);                    /* get stdio position */
485          fd = dup(fileno(fp));
486          fclose(fp);                             /* done with stdio */
487 <        lseek(fd, fpos, 0);                     /* align system file pointer */
488 <        hdinit(fd, NULL);                       /* allocate and load index */
487 >        for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
488 >                lseek(fd, (long)nextloc, 0);
489 >                read(fd, (char *)&nextloc, sizeof(nextloc));
490 >                hdinit(fd, NULL);
491 >        }
492 >        if (n != vdef(SECTION)) {
493 >                sprintf(errmsg, "number of sections does not match %s setting",
494 >                                vnam(SECTION));
495 >                error(WARNING, errmsg);
496 >        }
497   }
498  
499  
# Line 478 | Line 512 | PACKET *pl;
512                          if (outdev != NULL)     /* display it */
513                                  disp_packet((PACKHEAD *)p);
514                          if (hdcachesize <= 0)   /* manual flushing */
515 <                                n2flush += p->nr;
515 >                                n2flush++;
516                          nraysdone += p->nr;
517                          npacksdone++;
518 +                        p->nr = 0;
519                  }
520 <                p->nr = 0;                      /* push onto free list */
486 <                p->next = freepacks;
520 >                p->next = freepacks;            /* push onto free list */
521                  freepacks = p;
522          }
523 <        if (n2flush > 512*RPACKSIZ*nprocs) {
524 <                hdflush(NULL);                  /* flush holodeck buffers */
523 > #if MAXQTIME
524 >        if (n2flush > 300/MAXQTIME*totqlen) {
525 > #else
526 >        if (n2flush > 50*totqlen) {
527 > #endif
528 >                if (outdev == NULL)
529 >                        hdflush(NULL);          /* flush holodeck buffers */
530 >                else
531 >                        hdsync(NULL, 1);        /* sync holodeck file */
532                  n2flush = 0;
533          }
534   }
# Line 535 | Line 576 | register char  *s;
576   }
577  
578  
538 wputs(s)                        /* put warning string to stderr */
539 char    *s;
540 {
541        if (!nowarn)
542                eputs(s);
543 }
544
545
579   quit(ec)                        /* exit program gracefully */
580   int     ec;
581   {
582          int     status = 0;
583  
584 <        if (hdlist[0] != NULL) {        /* flush holodeck */
584 >        if (hdlist[0] != NULL) {        /* close holodeck */
585                  if (nprocs > 0)
586 <                        status = done_rtrace();
554 <                hdflush(NULL);
586 >                        status = done_rtrace();         /* calls hdsync() */
587                  if (ncprocs > 0 && vdef(REPORT)) {
588                          long    fsiz, fuse;
589                          fsiz = hdfilen(hdlist[0]->fd);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines