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.19 by gregl, Thu Dec 11 12:09:33 1997 UTC vs.
Revision 3.37 by gregl, Fri Jan 16 11:45:43 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 10 | Line 10 | static char SCCSid[] = "$SunId$ SGI";
10  
11   #include "rholo.h"
12   #include "random.h"
13 #include "paths.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  
# Line 22 | Line 24 | VARIABLE       vv[] = RHVINIT;         /* variable-value pairs */
24  
25   char    *progname;              /* our program name */
26   char    *hdkfile;               /* holodeck file name */
27 < char    froot[MAXPATH];         /* root file name */
27 > char    froot[256];             /* root file name */
28  
29   int     nowarn = 0;             /* turn warnings off? */
30  
# Line 32 | Line 34 | 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 */
# Line 47 | Line 51 | 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  
# Line 57 | Line 62 | main(argc, argv)
62   int     argc;
63   char    *argv[];
64   {
60        HDGRID  hdg;
65          int     i;
66 <        int     force = 0;
63 <                                                /* mark start time */
64 <        starttime = time(NULL);
66 >
67          initurand(16384);                       /* initialize urand */
68          progname = argv[0];                     /* get arguments */
69          for (i = 1; i < argc && argv[i][0] == '-'; i++)
# Line 88 | Line 90 | char   *argv[];
90                  default:
91                          goto userr;
92                  }
91                                                /* do we have a job? */
92        if (outdev == NULL && ncprocs <= 0)
93                goto userr;
93                                                  /* get root file name */
94 +        if (i >= argc)
95 +                goto userr;
96          rootname(froot, hdkfile=argv[i++]);
97 <                                                /* load... */
98 <        if (i < argc) {                         /* variables */
99 <                loadvars(argv[i++]);
100 <                                                        /* cmdline settings */
101 <                for ( ; i < argc; i++)
102 <                        if (setvariable(argv[i], matchvar) < 0) {
103 <                                sprintf(errmsg, "unknown variable: %s",
104 <                                                argv[i]);
105 <                                error(USER, errmsg);
106 <                        }
107 <                                                        /* check settings */
108 <                checkvalues();
109 <                                                        /* load RIF if any */
110 <                if (vdef(RIF))
111 <                        getradfile(vval(RIF));
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);
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 load holodeck */
120 <                loadholo();
121 <                                                        /* check settings */
122 <                checkvalues();
123 <                                                        /* load RIF if any */
124 <                if (vdef(RIF))
125 <                        getradfile(vval(RIF));
126 <                                                        /* set defaults */
124 >                creatholo(hdg);
125 >        } else                                  /* else just set defaults */
126                  setdefaults(NULL);
128        }
127                                                  /* initialize */
128          initrholo();
129                                                  /* main loop */
# Line 135 | Line 133 | char   *argv[];
133          quit(0);
134   userr:
135          fprintf(stderr,
136 < "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n",
136 > "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
137                          progname);
138          quit(1);
139   }
# Line 149 | Line 147 | int  signo;
147          if (gotsig++)                   /* two signals and we're gone! */
148                  _exit(signo);
149  
150 <        alarm(30);                      /* allow 30 seconds to clean up */
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]);
# Line 177 | Line 175 | int    fd, mod;
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)
178 >        if (mod == (stbuf.st_mode & 0777))
179                  return(-1);             /* already set */
180                                          /* else change it */
181          if (fchmod(fd, mod) < 0) {
# Line 204 | Line 202 | initrholo()                    /* get our holodeck running */
202                  maxdisk = 0;
203          else
204                  maxdisk = 1024.*1024.*vflt(DISKSPACE);
207                                                /* record end time */
208        if (!vdef(TIME) || vflt(TIME) <= FTINY)
209                endtime = 0;
210        else
211                endtime = starttime + vflt(TIME)*3600.;
205                                                  /* set up memory cache */
206          if (outdev == NULL)
207                  hdcachesize = 0;        /* manual flushing */
# Line 220 | Line 213 | initrholo()                    /* get our holodeck running */
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");
227 >                        error(USER, "cannot start rtrace process(es)");
228                  if (vdef(REPORT)) {             /* make first report */
229                          printargs(rtargc, rtargv, stderr);
230                          report(0);
# Line 233 | Line 233 | initrholo()                    /* get our holodeck running */
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 (!vbool(OBSTRUCTIONS)) {
240 >                if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
241                          freepacks[i].offset = (float *)bmalloc(
242                                          RPACKSIZ*sizeof(float)*(i+1) );
243                          if (freepacks[i].offset == NULL)
# Line 269 | Line 271 | memerr:
271  
272   rholo()                         /* holodeck main loop */
273   {
274 <        static int      idle = 1;
274 >        static int      idle = 0;
275          PACKET  *pl = NULL, *plend;
276 +        int     pksiz;
277          register PACKET *p;
278          time_t  t;
279 <        long    l;
277 <
278 <        if (outdev != NULL)             /* check display */
279 <                if (!disp_check(idle))
280 <                        return(0);
281 <                                        /* display only? */
279 >                                        /* check display */
280          if (nprocs <= 0)
281 <                return(1);
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 <                return(0);
292 >                done_rtrace();
293 >                return(1);      /* comes back */
294          }
295 <                                        /* check time */
290 <        if (endtime > 0 || reporttime > 0)
291 <                t = time(NULL);
295 >        t = time(NULL);                 /* check time */
296          if (endtime > 0 && t >= endtime) {
297                  error(WARNING, "time limit exceeded");
298 <                return(0);
298 >                done_rtrace();
299 >                return(1);      /* comes back */
300          }
301          if (reporttime > 0 && t >= reporttime)
302                  report(t);
303 <                                        /* get packets to process */
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)) {
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 <        idle = pl == NULL && freepacks != NULL;
310 <                                        /* are we out of stuff to do? */
311 <        if (idle && outdev == NULL)
312 <                return(0);
313 <                                        /* else process packets */
323 >                                        /* process packets */
324          done_packets(do_packets(pl));
325          return(1);                      /* and continue */
326   }
327  
328  
319 report(t)                       /* report progress so far */
320 time_t  t;
321 {
322        if (t == 0)
323                t = time(NULL);
324        fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
325                        progname, npacksdone, nraysdone, (t-starttime)/3600.);
326        fflush(stderr);
327        if (vdef(REPORT))
328                reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
329 }
330
331
329   setdefaults(gp)                 /* set default values */
330   register HDGRID *gp;
331   {
332          extern char     *atos();
333          register int    i;
334 <        double  len[3], maxlen, d;
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          }
344        if (vdef(SECTION) > 1) {
345                sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
346                error(WARNING, errmsg);
347        }
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          }
354        if (!vdef(OBSTRUCTIONS)) {
355                vval(OBSTRUCTIONS) = "T";
356                vdef(OBSTRUCTIONS)++;
357        }
348          if (!vdef(VDIST)) {
349                  vval(VDIST) = "F";
350                  vdef(VDIST)++;
351          }
362        if (!vdef(OCCUPANCY)) {
363                vval(OCCUPANCY) = "U";
364                vdef(OCCUPANCY)++;
365        }
352                                  /* append rendering options */
353          if (vdef(RENDER))
354                  rtargc += wordstring(rtargv+rtargc, vval(RENDER));
# Line 370 | Line 356 | register HDGRID        *gp;
356          if (gp == NULL)         /* already initialized? */
357                  return;
358                                  /* set grid parameters */
359 <        gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
360 <        if (sscanf(vval(SECTION),
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 <        maxlen = 0.;
370 <        for (i = 0; i < 3; i++)
371 <                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
372 <                        maxlen = len[i];
373 <        if (!vdef(GRID)) {
374 <                sprintf(buf, "%.4f", maxlen/8.);
375 <                vval(GRID) = savqstr(buf);
376 <                vdef(GRID)++;
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          }
391        if ((d = vflt(GRID)) <= FTINY)
392                badvalue(GRID);
393        for (i = 0; i < 3; i++)
394                if (gp->grid[i] <= 0)
395                        gp->grid[i] = len[i]/d + (1.-FTINY);
383   }
384  
385  
# Line 400 | Line 387 | creatholo(gp)                  /* create a holodeck output file */
387   HDGRID  *gp;
388   {
389          extern char     VersionID[];
390 <        long    endloc = 0;
390 >        int4    lastloc, nextloc;
391 >        int     n;
392          int     fd;
393          FILE    *fp;
394                                          /* open & truncate file */
# Line 414 | Line 402 | HDGRID *gp;
402          printvars(fp);
403          fputformat(HOLOFMT, fp);
404          fputc('\n', fp);
405 <        putw(HOLOMAGIC, fp);            /* put magic number & terminus */
418 <        fwrite(&endloc, sizeof(long), 1, fp);
405 >        putw(HOLOMAGIC, fp);            /* put magic number */
406          fd = dup(fileno(fp));
407          fclose(fp);                     /* flush and close stdio stream */
408 <        hdinit(fd, gp);                 /* allocate and initialize index */
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  
# Line 451 | Line 451 | loadholo()                     /* start loading a holodeck from fname */
451          extern long     ftell();
452          FILE    *fp;
453          int     fd;
454 <        long    fpos;
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,
# Line 466 | Line 467 | loadholo()                     /* start loading a holodeck from fname */
467                                  hdkfile);
468                  error(USER, errmsg);
469          }
470 <        fread(&fpos, sizeof(long), 1, fp);
470 <        if (fpos != 0)
471 <                error(WARNING, "ignoring multiple sections in holodeck file");
472 <        fpos = ftell(fp);                       /* get stdio position */
470 >        nextloc = ftell(fp);                    /* get stdio position */
471          fd = dup(fileno(fp));
472          fclose(fp);                             /* done with stdio */
473 <        lseek(fd, fpos, 0);                     /* align system file pointer */
474 <        hdinit(fd, NULL);                       /* allocate and load index */
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  
# Line 492 | Line 498 | PACKET *pl;
498                          if (outdev != NULL)     /* display it */
499                                  disp_packet((PACKHEAD *)p);
500                          if (hdcachesize <= 0)   /* manual flushing */
501 <                                n2flush += p->nr;
501 >                                n2flush++;
502                          nraysdone += p->nr;
503                          npacksdone++;
504 +                        p->nr = 0;
505                  }
506 <                p->nr = 0;                      /* push onto free list */
500 <                p->next = freepacks;
506 >                p->next = freepacks;            /* push onto free list */
507                  freepacks = p;
508          }
509 <        if (n2flush > 512*RPACKSIZ*nprocs) {
509 >        if (n2flush > 300/MAXQTIME*avgqlen*nprocs) {
510                  hdflush(NULL);                  /* flush holodeck buffers */
511                  n2flush = 0;
512          }
513   }
514  
515  
510 getradfile(rfargs)              /* run rad and get needed variables */
511 char    *rfargs;
512 {
513        static short    mvar[] = {OCTREE,-1};
514        static char     tf1[] = TEMPLATE;
515        char    tf2[64];
516        char    combuf[256];
517        char    *pippt;
518        register int    i;
519        register char   *cp;
520                                        /* create rad command */
521        mktemp(tf1);
522        sprintf(tf2, "%s.rif", tf1);
523        sprintf(combuf,
524                "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
525                        rfargs, tf1);
526        cp = combuf;
527        while (*cp){
528                if (*cp == '|') pippt = cp;
529                cp++;
530        }                               /* match unset variables */
531        for (i = 0; mvar[i] >= 0; i++)
532                if (!vdef(mvar[i])) {
533                        *cp++ = '|';
534                        strcpy(cp, vnam(mvar[i]));
535                        while (*cp) cp++;
536                        pippt = NULL;
537                }
538        if (pippt != NULL)
539                strcpy(pippt, "> /dev/null");   /* nothing to match */
540        else
541                sprintf(cp, ")[ \t]*=' > %s", tf2);
542        if (system(combuf)) {
543                error(SYSTEM, "cannot execute rad command");
544                unlink(tf2);                    /* clean up */
545                unlink(tf1);
546                quit(1);
547        }
548        if (pippt == NULL) {
549                loadvars(tf2);                  /* load variables */
550                unlink(tf2);
551        }
552        rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
553        unlink(tf1);                    /* clean up */
554 }
555
556
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 (ISDIRSEP(*rn))
522 >                if (*rn == '/')
523                          dp = rn;
524                  else if (*rn == '.')
525                          tp = rn;
# Line 609 | Line 568 | int    ec;
568   {
569          int     status = 0;
570  
571 <        if (hdlist[0] != NULL) {        /* flush holodeck */
572 <                if (nprocs > 0) {
573 <                        done_packets(flush_queue());
615 <                        status = end_rtrace();  /* close rtrace */
616 <                }
617 <                hdflush(NULL);
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;
620                        report(0);
576                          fsiz = hdfilen(hdlist[0]->fd);
577                          fuse = hdfiluse(hdlist[0]->fd, 1);
578                          fprintf(stderr,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines