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.33 by gregl, Wed Dec 31 20:45:31 1997 UTC

# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ SGI";
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 48 | 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 58 | Line 62 | main(argc, argv)
62   int     argc;
63   char    *argv[];
64   {
61        HDGRID  hdg;
65          int     i;
66 <                                                /* 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 89 | Line 91 | char   *argv[];
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)
# Line 109 | Line 113 | char   *argv[];
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);
124 >                creatholo(hdg);
125          } else                                  /* else just set defaults */
126                  setdefaults(NULL);
127                                                  /* initialize */
# Line 197 | Line 202 | initrholo()                    /* get our holodeck running */
202                  maxdisk = 0;
203          else
204                  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 */
# Line 213 | 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();
# Line 226 | 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 (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
# Line 262 | 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;
280 <
271 <        if (outdev != NULL)             /* check display */
272 <                if (!disp_check(idle))
273 <                        return(0);
274 <                                        /* display only? */
275 <        if (nprocs <= 0) {
279 >                                        /* check display */
280 >        if (nprocs <= 0)
281                  idle = 1;
282 <                return(outdev != NULL);
283 <        }
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 <                                        /* check time */
286 <        if (endtime > 0 || reporttime > 0)
287 <                t = time(NULL);
295 >        t = time(NULL);                 /* check time */
296          if (endtime > 0 && t >= endtime) {
297                  error(WARNING, "time limit exceeded");
298                  done_rtrace();
# Line 292 | Line 300 | rholo()                                /* holodeck main loop */
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)) {
314 >                if (!next_packet(p, pksiz)) {
315                          p->next = freepacks; freepacks = p;
316                          idle = 1;
317                          break;
# Line 315 | Line 331 | register HDGRID        *gp;
331   {
332          extern char     *atos();
333          register int    i;
334 +        int     n;
335          double  len[3], maxlen, d;
336          char    buf[64];
337  
# Line 322 | Line 339 | register HDGRID        *gp;
339                  sprintf(errmsg, "%s must be defined", vnam(SECTION));
340                  error(USER, errmsg);
341          }
325        if (vdef(SECTION) > 1) {
326                sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
327                error(WARNING, errmsg);
328        }
342          if (!vdef(OCTREE)) {
343                  if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
344                          error(SYSTEM, "out of memory");
# Line 343 | 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 >                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 >                        d = 0.125*maxlen;
375 >                else if ((d = vflt(GRID)) <= FTINY)
376 >                        badvalue(GRID);
377 >                for (i = 0; i < 3; i++)
378 >                        if (gp->grid[i] <= 0)
379 >                                gp->grid[i] = len[i]/d + (1.-FTINY);
380          }
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);
381   }
382  
383  
# Line 373 | Line 385 | creatholo(gp)                  /* create a holodeck output file */
385   HDGRID  *gp;
386   {
387          extern char     VersionID[];
388 <        long    endloc = 0;
388 >        int4    lastloc, nextloc;
389 >        int     n;
390          int     fd;
391          FILE    *fp;
392                                          /* open & truncate file */
# Line 387 | Line 400 | HDGRID *gp;
400          printvars(fp);
401          fputformat(HOLOFMT, fp);
402          fputc('\n', fp);
403 <        putw(HOLOMAGIC, fp);            /* put magic number & terminus */
391 <        fwrite(&endloc, sizeof(long), 1, fp);
403 >        putw(HOLOMAGIC, fp);            /* put magic number */
404          fd = dup(fileno(fp));
405          fclose(fp);                     /* flush and close stdio stream */
406 <        hdinit(fd, gp);                 /* allocate and initialize index */
406 >        lastloc = lseek(fd, 0L, 2);
407 >        for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */
408 >                nextloc = 0L;
409 >                write(fd, (char *)&nextloc, sizeof(nextloc));
410 >                hdinit(fd, gp);                 /* writes beam index */
411 >                if (!n)
412 >                        break;
413 >                nextloc = hdfilen(fd);          /* write section pointer */
414 >                if (lseek(fd, (long)lastloc, 0) < 0)
415 >                        error(SYSTEM,
416 >                                "cannot seek on holodeck file in creatholo");
417 >                write(fd, (char *)&nextloc, sizeof(nextloc));
418 >                lseek(fd, (long)(lastloc=nextloc), 0);
419 >        }
420   }
421  
422  
# Line 424 | Line 449 | loadholo()                     /* start loading a holodeck from fname */
449          extern long     ftell();
450          FILE    *fp;
451          int     fd;
452 <        long    fpos;
452 >        int     n;
453 >        int4    nextloc;
454                                          /* open holodeck file */
455          if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
456                  sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
# Line 439 | Line 465 | loadholo()                     /* start loading a holodeck from fname */
465                                  hdkfile);
466                  error(USER, errmsg);
467          }
468 <        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 */
468 >        nextloc = ftell(fp);                    /* get stdio position */
469          fd = dup(fileno(fp));
470          fclose(fp);                             /* done with stdio */
471 <        lseek(fd, fpos, 0);                     /* align system file pointer */
472 <        hdinit(fd, NULL);                       /* allocate and load index */
471 >        for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
472 >                lseek(fd, (long)nextloc, 0);
473 >                read(fd, (char *)&nextloc, sizeof(nextloc));
474 >                hdinit(fd, NULL);
475 >        }
476 >        if (n != vdef(SECTION)) {
477 >                sprintf(errmsg, "number of sections does not match %s setting",
478 >                                vnam(SECTION));
479 >                error(WARNING, errmsg);
480 >        }
481   }
482  
483  
# Line 465 | Line 496 | PACKET *pl;
496                          if (outdev != NULL)     /* display it */
497                                  disp_packet((PACKHEAD *)p);
498                          if (hdcachesize <= 0)   /* manual flushing */
499 <                                n2flush += p->nr;
499 >                                n2flush++;
500                          nraysdone += p->nr;
501                          npacksdone++;
502 +                        p->nr = 0;
503                  }
504 <                p->nr = 0;                      /* push onto free list */
473 <                p->next = freepacks;
504 >                p->next = freepacks;            /* push onto free list */
505                  freepacks = p;
506          }
507 <        if (n2flush > 512*RPACKSIZ*nprocs) {
507 >        if (n2flush > 300/MAXQTIME*avgqlen*nprocs) {
508                  hdflush(NULL);                  /* flush holodeck buffers */
509                  n2flush = 0;
510          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines