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.12 by gregl, Thu Nov 20 11:39:24 1997 UTC vs.
Revision 3.28 by gregl, Mon Dec 15 20:43:01 1997 UTC

# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ SGI";
9   */
10  
11   #include "rholo.h"
12 < #include "paths.h"
12 > #include "random.h"
13   #include <signal.h>
14   #include <sys/types.h>
15   #include <sys/stat.h>
# Line 21 | Line 21 | VARIABLE       vv[] = RHVINIT;         /* variable-value pairs */
21  
22   char    *progname;              /* our program name */
23   char    *hdkfile;               /* holodeck file name */
24 < char    froot[MAXPATH];         /* root file name */
24 > char    froot[256];             /* root file name */
25  
26   int     nowarn = 0;             /* turn warnings off? */
27  
28 double  expval = 1.;            /* global exposure value */
29
28   int     ncprocs = 0;            /* desired number of compute processes */
29  
30   char    *outdev = NULL;         /* output device name */
31  
32 + int     readinp = 0;            /* read commands from stdin */
33 +
34 + int     force = 0;              /* allow overwrite of holodeck */
35 +
36   time_t  starttime;              /* time we got started */
37   time_t  endtime;                /* time we should end by */
38   time_t  reporttime;             /* time for next report */
# Line 58 | Line 60 | char   *argv[];
60   {
61          HDGRID  hdg;
62          int     i;
61        int     force = 0;
63                                                  /* mark start time */
64          starttime = time(NULL);
65 +        initurand(16384);                       /* initialize urand */
66          progname = argv[0];                     /* get arguments */
67          for (i = 1; i < argc && argv[i][0] == '-'; i++)
68                  switch (argv[i][1]) {
# Line 70 | Line 72 | char   *argv[];
72                  case 'f':                       /* force overwrite */
73                          force++;
74                          break;
75 +                case 'i':                       /* read input from stdin */
76 +                        readinp++;
77 +                        break;
78                  case 'n':                       /* compute processes */
79                          if (i >= argc-2)
80                                  goto userr;
# Line 83 | Line 88 | char   *argv[];
88                  default:
89                          goto userr;
90                  }
86                                                /* do we have a job? */
87        if (outdev == NULL && ncprocs <= 0)
88                goto userr;
91                                                  /* get root file name */
92          rootname(froot, hdkfile=argv[i++]);
93 <                                                /* load... */
94 <        if (i < argc) {                         /* variables */
95 <                loadvars(argv[i++]);
96 <                                                        /* cmdline settings */
97 <                for ( ; i < argc; i++)
98 <                        if (setvariable(argv[i], matchvar) < 0) {
99 <                                sprintf(errmsg, "unknown variable: %s",
100 <                                                argv[i]);
101 <                                error(USER, errmsg);
102 <                        }
103 <                                                        /* check settings */
104 <                checkvalues();
105 <                                                        /* load RIF if any */
106 <                if (vdef(RIF))
107 <                        getradfile(vval(RIF));
93 >                                                /* load variables? */
94 >        if (i < argc)
95 >                if (argv[i][0] != '-' && argv[i][0] != '+')
96 >                        loadvars(argv[i]);      /* load variables from file */
97 >
98 >        if (i >= argc || argv[i][0] == '+')
99 >                loadholo();                     /* load existing holodeck */
100 >
101 >        while (++i < argc)                      /* get command line settings */
102 >                if (setvariable(argv[i], matchvar) < 0) {
103 >                        sprintf(errmsg, "unknown variable: %s", argv[i]);
104 >                        error(USER, errmsg);
105 >                }
106 >                                                /* check settings */
107 >        checkvalues();
108 >                                                /* load RIF if any */
109 >        getradfile();
110 >
111 >        if (hdlist[0] == NULL) {                /* create new holodeck */
112                                                          /* set defaults */
113                  setdefaults(&hdg);
114                                                          /* holodeck exists? */
# Line 111 | Line 117 | char   *argv[];
117                                  "holodeck file exists -- use -f to overwrite");
118                                                          /* create holodeck */
119                  creatholo(&hdg);
120 <        } else {                                /* else load holodeck */
121 <                loadholo();
116 <                if (vdef(RIF))                          /* load RIF if any */
117 <                        getradfile(vval(RIF));
118 <        }
120 >        } else                                  /* else just set defaults */
121 >                setdefaults(NULL);
122                                                  /* initialize */
123          initrholo();
124 <                                                /* run */
124 >                                                /* main loop */
125          while (rholo())
126                  ;
127                                                  /* done */
128          quit(0);
129   userr:
130          fprintf(stderr,
131 < "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n",
131 > "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
132                          progname);
133          quit(1);
134   }
# Line 139 | Line 142 | int  signo;
142          if (gotsig++)                   /* two signals and we're gone! */
143                  _exit(signo);
144  
145 <        alarm(30);                      /* allow 30 seconds to clean up */
145 >        alarm(60);                      /* allow 60 seconds to clean up */
146          signal(SIGALRM, SIG_DFL);       /* make certain we do die */
147          eputs("signal - ");
148          eputs(sigerr[signo]);
# Line 182 | Line 185 | initrholo()                    /* get our holodeck running */
185   {
186          extern int      global_packet();
187          register int    i;
188 +                                                /* close holodeck on exec() */
189 +        fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
190  
191          if (outdev != NULL)                     /* open output device */
192                  disp_open(outdev);
# Line 196 | Line 201 | initrholo()                    /* get our holodeck running */
201          if (!vdef(TIME) || vflt(TIME) <= FTINY)
202                  endtime = 0;
203          else
204 <                endtime = starttime + vflt(TIME)*3600.;
204 >                endtime = starttime + vflt(TIME)*3600. + .5;
205                                                  /* set up memory cache */
206          if (outdev == NULL)
207                  hdcachesize = 0;        /* manual flushing */
# Line 212 | Line 217 | initrholo()                    /* get our holodeck running */
217          if (ncprocs > 0) {
218                  i = start_rtrace();
219                  if (i < 1)
220 <                        error(USER, "cannot start rtrace process");
220 >                        error(USER, "cannot start rtrace process(es)");
221                  if (vdef(REPORT)) {             /* make first report */
222                          printargs(rtargc, rtargv, stderr);
223                          report(0);
# Line 223 | Line 228 | initrholo()                    /* get our holodeck running */
228                          goto memerr;
229                  freepacks[--i].nr = 0;
230                  freepacks[i].next = NULL;
231 <                if (!vbool(OBSTRUCTIONS)) {
231 >                if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
232                          freepacks[i].offset = (float *)bmalloc(
233                                          RPACKSIZ*sizeof(float)*(i+1) );
234                          if (freepacks[i].offset == NULL)
# Line 267 | Line 272 | rholo()                                /* holodeck main loop */
272                  if (!disp_check(idle))
273                          return(0);
274                                          /* display only? */
275 <        if (ncprocs <= 0)
276 <                return(1);
275 >        if (nprocs <= 0) {
276 >                idle = 1;
277 >                return(outdev != NULL);
278 >        }
279                                          /* check file size */
280          if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
281                  error(WARNING, "file limit exceeded");
282 <                return(0);
282 >                done_rtrace();
283 >                return(1);      /* comes back */
284          }
285                                          /* check time */
286          if (endtime > 0 || reporttime > 0)
287                  t = time(NULL);
288          if (endtime > 0 && t >= endtime) {
289                  error(WARNING, "time limit exceeded");
290 <                return(0);
290 >                done_rtrace();
291 >                return(1);      /* comes back */
292          }
293          if (reporttime > 0 && t >= reporttime)
294                  report(t);
295 <                                        /* get packets to process */
295 >        idle = 0;                       /* get packets to process */
296          while (freepacks != NULL) {
297                  p = freepacks; freepacks = p->next; p->next = NULL;
298                  if (!next_packet(p)) {
299                          p->next = freepacks; freepacks = p;
300 +                        idle = 1;
301                          break;
302                  }
303                  if (pl == NULL) pl = p;
304                  else plend->next = p;
305                  plend = p;
306          }
307 <        idle = pl == NULL && freepacks != NULL;
298 <                                        /* are we out of stuff to do? */
299 <        if (idle && outdev == NULL)
300 <                return(0);
301 <                                        /* else process packets */
307 >                                        /* process packets */
308          done_packets(do_packets(pl));
309          return(1);                      /* and continue */
310   }
311  
312  
307 report(t)                       /* report progress so far */
308 time_t  t;
309 {
310        if (t == 0)
311                t = time(NULL);
312        fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
313                        progname, npacksdone, nraysdone, (t-starttime)/3600.);
314        fflush(stderr);
315        if (vdef(REPORT))
316                reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
317 }
318
319
313   setdefaults(gp)                 /* set default values */
314   register HDGRID *gp;
315   {
# Line 333 | Line 326 | register HDGRID        *gp;
326                  sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
327                  error(WARNING, errmsg);
328          }
329 +        if (!vdef(OCTREE)) {
330 +                if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
331 +                        error(SYSTEM, "out of memory");
332 +                sprintf(vval(OCTREE), "%s.oct", froot);
333 +                vdef(OCTREE)++;
334 +        }
335 +        if (!vdef(VDIST)) {
336 +                vval(VDIST) = "F";
337 +                vdef(VDIST)++;
338 +        }
339 +                                /* append rendering options */
340 +        if (vdef(RENDER))
341 +                rtargc += wordstring(rtargv+rtargc, vval(RENDER));
342 +        
343 +        if (gp == NULL)         /* already initialized? */
344 +                return;
345 +                                /* set grid parameters */
346 +        gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
347          if (sscanf(vval(SECTION),
348 <                        "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
348 >                "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
349                          &gp->orig[0], &gp->orig[1], &gp->orig[2],
350                          &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
351                          &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
352 <                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
352 >                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
353 >                        &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
354                  badvalue(SECTION);
355          maxlen = 0.;
356          for (i = 0; i < 3; i++)
# Line 352 | Line 364 | register HDGRID        *gp;
364          if ((d = vflt(GRID)) <= FTINY)
365                  badvalue(GRID);
366          for (i = 0; i < 3; i++)
367 <                gp->grid[i] = len[i]/d + (1.-FTINY);
368 <        if (!vdef(EXPOSURE)) {
357 <                sprintf(errmsg, "%s must be defined", vnam(EXPOSURE));
358 <                error(USER, errmsg);
359 <        }
360 <        expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ?
361 <                        pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE);
362 <        if (!vdef(OCTREE)) {
363 <                if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
364 <                        error(SYSTEM, "out of memory");
365 <                sprintf(vval(OCTREE), "%s.oct", froot);
366 <                vdef(OCTREE)++;
367 <        }
368 <        if (!vdef(OBSTRUCTIONS)) {
369 <                vval(OBSTRUCTIONS) = "T";
370 <                vdef(OBSTRUCTIONS)++;
371 <        }
372 <        if (!vdef(OCCUPANCY)) {
373 <                vval(OCCUPANCY) = "U";
374 <                vdef(OCCUPANCY)++;
375 <        }
376 <                                /* append rendering options */
377 <        if (vdef(RENDER))
378 <                rtargc += wordstring(rtargv+rtargc, vval(RENDER));
367 >                if (gp->grid[i] <= 0)
368 >                        gp->grid[i] = len[i]/d + (1.-FTINY);
369   }
370  
371  
# Line 463 | Line 453 | loadholo()                     /* start loading a holodeck from fname */
453   done_packets(pl)                /* handle finished packets */
454   PACKET  *pl;
455   {
456 <        static int      nunflushed = 0;
456 >        static int      n2flush = 0;
457          register PACKET *p;
458  
459          while (pl != NULL) {
# Line 474 | Line 464 | PACKET *pl;
464                                  p->nr*sizeof(RAYVAL));
465                          if (outdev != NULL)     /* display it */
466                                  disp_packet((PACKHEAD *)p);
467 <                        else
468 <                                nunflushed += p->nr;
467 >                        if (hdcachesize <= 0)   /* manual flushing */
468 >                                n2flush += p->nr;
469                          nraysdone += p->nr;
470                          npacksdone++;
471                  }
# Line 483 | Line 473 | PACKET *pl;
473                  p->next = freepacks;
474                  freepacks = p;
475          }
476 <        if (nunflushed >= 256*RPACKSIZ) {
476 >        if (n2flush > 512*RPACKSIZ*nprocs) {
477                  hdflush(NULL);                  /* flush holodeck buffers */
478 <                nunflushed = 0;
478 >                n2flush = 0;
479          }
480   }
481  
482  
493 getradfile(rfargs)              /* run rad and get needed variables */
494 char    *rfargs;
495 {
496        static short    mvar[] = {OCTREE,EXPOSURE,-1};
497        static char     tf1[] = TEMPLATE;
498        char    tf2[64];
499        char    combuf[256];
500        char    *pippt;
501        register int    i;
502        register char   *cp;
503                                        /* create rad command */
504        mktemp(tf1);
505        sprintf(tf2, "%s.rif", tf1);
506        sprintf(combuf,
507                "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
508                        rfargs, tf1);
509        cp = combuf;
510        while (*cp){
511                if (*cp == '|') pippt = cp;
512                cp++;
513        }                               /* match unset variables */
514        for (i = 0; mvar[i] >= 0; i++)
515                if (!vdef(mvar[i])) {
516                        *cp++ = '|';
517                        strcpy(cp, vnam(mvar[i]));
518                        while (*cp) cp++;
519                        pippt = NULL;
520                }
521        if (pippt != NULL)
522                strcpy(pippt, "> /dev/null");   /* nothing to match */
523        else
524                sprintf(cp, ")[ \t]*=' > %s", tf2);
525        if (system(combuf)) {
526                error(SYSTEM, "cannot execute rad command");
527                unlink(tf2);                    /* clean up */
528                unlink(tf1);
529                quit(1);
530        }
531        if (pippt == NULL) {
532                loadvars(tf2);                  /* load variables */
533                unlink(tf2);
534        }
535        rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
536        unlink(tf1);                    /* clean up */
537 }
538
539
483   rootname(rn, fn)                /* remove tail from end of fn */
484   register char   *rn, *fn;
485   {
486          char    *tp, *dp;
487  
488          for (tp = NULL, dp = rn; *rn = *fn++; rn++)
489 <                if (ISDIRSEP(*rn))
489 >                if (*rn == '/')
490                          dp = rn;
491                  else if (*rn == '.')
492                          tp = rn;
# Line 592 | Line 535 | int    ec;
535   {
536          int     status = 0;
537  
538 <        if (hdlist[0] != NULL) {        /* flush holodeck */
539 <                if (ncprocs > 0) {
540 <                        done_packets(flush_queue());
541 <                        status = end_rtrace();  /* close rtrace */
542 <                        hdflush(NULL);
543 <                        if (vdef(REPORT)) {
544 <                                long    fsiz, fuse;
545 <                                report(0);
603 <                                fsiz = hdfilen(hdlist[0]->fd);
604 <                                fuse = hdfiluse(hdlist[0]->fd, 1);
605 <                                fprintf(stderr,
538 >        if (hdlist[0] != NULL) {        /* close holodeck */
539 >                if (nprocs > 0)
540 >                        status = done_rtrace();         /* calls hdsync() */
541 >                if (ncprocs > 0 && vdef(REPORT)) {
542 >                        long    fsiz, fuse;
543 >                        fsiz = hdfilen(hdlist[0]->fd);
544 >                        fuse = hdfiluse(hdlist[0]->fd, 1);
545 >                        fprintf(stderr,
546                          "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
547 <                                                hdkfile, fsiz/(1024.*1024.),
548 <                                                100.*(fsiz-fuse)/fsiz);
549 <                        }
610 <                } else
611 <                        hdflush(NULL);
547 >                                        hdkfile, fsiz/(1024.*1024.),
548 >                                        100.*(fsiz-fuse)/fsiz);
549 >                }
550          }
551          if (orig_mode >= 0)             /* reset holodeck access mode */
552                  fchmod(hdlist[0]->fd, orig_mode);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines